@Test public void testUnmarshal() { String expectedPayload = "MyPayload"; String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<logEvent xmlns=\"urn:org.soitoolkit.commons.logentry.schema:v1\">" + "<logEntry>" + "<payload>MyPayload</payload>" + "</logEntry>" + "</logEvent>"; JaxbUtil jaxbUtil = new JaxbUtil(LogEvent.class); LogEvent logEvent = (LogEvent)jaxbUtil.unmarshal(xml); String actualPayload = logEvent.getLogEntry().getPayload(); log.info(actualPayload); assertEquals(expectedPayload, actualPayload); }
@SuppressWarnings("rawtypes") public Object unmarshal(Object payload) { if (jaxbContext == null) { logger.error("Trying to unmarshal with a null jaxbContext, returns null. Check your configuration, e.g. jaxb-transformers!"); return null; } try { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); if (unmarshallProps != null) { for (Entry<String, Object> entry : unmarshallProps.entrySet()) { unmarshaller.setProperty(entry.getKey(), entry.getValue()); } } Object jaxbObject = null; if (payload instanceof String) { String src = (String)payload; jaxbObject = unmarshaller.unmarshal(new StringReader(src)); } else if (payload instanceof byte[]) { byte[] src = (byte[])payload; jaxbObject = unmarshaller.unmarshal(new ByteArrayInputStream(src)); } else if (payload instanceof File) { jaxbObject = unmarshaller.unmarshal((File)payload); } else if (payload instanceof InputSource) { jaxbObject = unmarshaller.unmarshal((InputSource)payload); } else if (payload instanceof InputStream) { jaxbObject = unmarshaller.unmarshal((InputStream)payload); } else if (payload instanceof Reader) { jaxbObject = unmarshaller.unmarshal((Reader)payload); } else if (payload instanceof URL) { jaxbObject = unmarshaller.unmarshal((URL)payload); } else if (payload instanceof InputSource) { jaxbObject = unmarshaller.unmarshal((InputSource)payload); } else if (payload instanceof Node) { jaxbObject = unmarshaller.unmarshal((Node)payload); } else if (payload instanceof Source) { jaxbObject = unmarshaller.unmarshal((Source)payload); } else if (payload instanceof XMLEventReader) { jaxbObject = unmarshaller.unmarshal((XMLEventReader)payload); } else if (payload instanceof XMLStreamReader) { jaxbObject = unmarshaller.unmarshal((XMLStreamReader)payload); } else { throw new RuntimeException("Unknown sourcetype of the xml payload: " + payload.getClass().getName()); } if (jaxbObject instanceof JAXBElement) { if (logger.isDebugEnabled()) logger.debug("Found a JAXBElement, returns it value"); jaxbObject = ((JAXBElement)jaxbObject).getValue(); } logger.debug("unmarshalled xml payload of type: {}, returns jaxb object of type {}", payload.getClass().getName(), jaxbObject.getClass().getName()); return jaxbObject; } catch (JAXBException e) { throw new RuntimeException(e); } }
JaxbUtil { @SuppressWarnings("rawtypes") public Object unmarshal(Object payload) { if (jaxbContext == null) { logger.error("Trying to unmarshal with a null jaxbContext, returns null. Check your configuration, e.g. jaxb-transformers!"); return null; } try { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); if (unmarshallProps != null) { for (Entry<String, Object> entry : unmarshallProps.entrySet()) { unmarshaller.setProperty(entry.getKey(), entry.getValue()); } } Object jaxbObject = null; if (payload instanceof String) { String src = (String)payload; jaxbObject = unmarshaller.unmarshal(new StringReader(src)); } else if (payload instanceof byte[]) { byte[] src = (byte[])payload; jaxbObject = unmarshaller.unmarshal(new ByteArrayInputStream(src)); } else if (payload instanceof File) { jaxbObject = unmarshaller.unmarshal((File)payload); } else if (payload instanceof InputSource) { jaxbObject = unmarshaller.unmarshal((InputSource)payload); } else if (payload instanceof InputStream) { jaxbObject = unmarshaller.unmarshal((InputStream)payload); } else if (payload instanceof Reader) { jaxbObject = unmarshaller.unmarshal((Reader)payload); } else if (payload instanceof URL) { jaxbObject = unmarshaller.unmarshal((URL)payload); } else if (payload instanceof InputSource) { jaxbObject = unmarshaller.unmarshal((InputSource)payload); } else if (payload instanceof Node) { jaxbObject = unmarshaller.unmarshal((Node)payload); } else if (payload instanceof Source) { jaxbObject = unmarshaller.unmarshal((Source)payload); } else if (payload instanceof XMLEventReader) { jaxbObject = unmarshaller.unmarshal((XMLEventReader)payload); } else if (payload instanceof XMLStreamReader) { jaxbObject = unmarshaller.unmarshal((XMLStreamReader)payload); } else { throw new RuntimeException("Unknown sourcetype of the xml payload: " + payload.getClass().getName()); } if (jaxbObject instanceof JAXBElement) { if (logger.isDebugEnabled()) logger.debug("Found a JAXBElement, returns it value"); jaxbObject = ((JAXBElement)jaxbObject).getValue(); } logger.debug("unmarshalled xml payload of type: {}, returns jaxb object of type {}", payload.getClass().getName(), jaxbObject.getClass().getName()); return jaxbObject; } catch (JAXBException e) { throw new RuntimeException(e); } } }
JaxbUtil { @SuppressWarnings("rawtypes") public Object unmarshal(Object payload) { if (jaxbContext == null) { logger.error("Trying to unmarshal with a null jaxbContext, returns null. Check your configuration, e.g. jaxb-transformers!"); return null; } try { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); if (unmarshallProps != null) { for (Entry<String, Object> entry : unmarshallProps.entrySet()) { unmarshaller.setProperty(entry.getKey(), entry.getValue()); } } Object jaxbObject = null; if (payload instanceof String) { String src = (String)payload; jaxbObject = unmarshaller.unmarshal(new StringReader(src)); } else if (payload instanceof byte[]) { byte[] src = (byte[])payload; jaxbObject = unmarshaller.unmarshal(new ByteArrayInputStream(src)); } else if (payload instanceof File) { jaxbObject = unmarshaller.unmarshal((File)payload); } else if (payload instanceof InputSource) { jaxbObject = unmarshaller.unmarshal((InputSource)payload); } else if (payload instanceof InputStream) { jaxbObject = unmarshaller.unmarshal((InputStream)payload); } else if (payload instanceof Reader) { jaxbObject = unmarshaller.unmarshal((Reader)payload); } else if (payload instanceof URL) { jaxbObject = unmarshaller.unmarshal((URL)payload); } else if (payload instanceof InputSource) { jaxbObject = unmarshaller.unmarshal((InputSource)payload); } else if (payload instanceof Node) { jaxbObject = unmarshaller.unmarshal((Node)payload); } else if (payload instanceof Source) { jaxbObject = unmarshaller.unmarshal((Source)payload); } else if (payload instanceof XMLEventReader) { jaxbObject = unmarshaller.unmarshal((XMLEventReader)payload); } else if (payload instanceof XMLStreamReader) { jaxbObject = unmarshaller.unmarshal((XMLStreamReader)payload); } else { throw new RuntimeException("Unknown sourcetype of the xml payload: " + payload.getClass().getName()); } if (jaxbObject instanceof JAXBElement) { if (logger.isDebugEnabled()) logger.debug("Found a JAXBElement, returns it value"); jaxbObject = ((JAXBElement)jaxbObject).getValue(); } logger.debug("unmarshalled xml payload of type: {}, returns jaxb object of type {}", payload.getClass().getName(), jaxbObject.getClass().getName()); return jaxbObject; } catch (JAXBException e) { throw new RuntimeException(e); } } @SuppressWarnings("rawtypes")  JaxbUtil(Class... classesToBeBound);  JaxbUtil(String contextPath); }
JaxbUtil { @SuppressWarnings("rawtypes") public Object unmarshal(Object payload) { if (jaxbContext == null) { logger.error("Trying to unmarshal with a null jaxbContext, returns null. Check your configuration, e.g. jaxb-transformers!"); return null; } try { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); if (unmarshallProps != null) { for (Entry<String, Object> entry : unmarshallProps.entrySet()) { unmarshaller.setProperty(entry.getKey(), entry.getValue()); } } Object jaxbObject = null; if (payload instanceof String) { String src = (String)payload; jaxbObject = unmarshaller.unmarshal(new StringReader(src)); } else if (payload instanceof byte[]) { byte[] src = (byte[])payload; jaxbObject = unmarshaller.unmarshal(new ByteArrayInputStream(src)); } else if (payload instanceof File) { jaxbObject = unmarshaller.unmarshal((File)payload); } else if (payload instanceof InputSource) { jaxbObject = unmarshaller.unmarshal((InputSource)payload); } else if (payload instanceof InputStream) { jaxbObject = unmarshaller.unmarshal((InputStream)payload); } else if (payload instanceof Reader) { jaxbObject = unmarshaller.unmarshal((Reader)payload); } else if (payload instanceof URL) { jaxbObject = unmarshaller.unmarshal((URL)payload); } else if (payload instanceof InputSource) { jaxbObject = unmarshaller.unmarshal((InputSource)payload); } else if (payload instanceof Node) { jaxbObject = unmarshaller.unmarshal((Node)payload); } else if (payload instanceof Source) { jaxbObject = unmarshaller.unmarshal((Source)payload); } else if (payload instanceof XMLEventReader) { jaxbObject = unmarshaller.unmarshal((XMLEventReader)payload); } else if (payload instanceof XMLStreamReader) { jaxbObject = unmarshaller.unmarshal((XMLStreamReader)payload); } else { throw new RuntimeException("Unknown sourcetype of the xml payload: " + payload.getClass().getName()); } if (jaxbObject instanceof JAXBElement) { if (logger.isDebugEnabled()) logger.debug("Found a JAXBElement, returns it value"); jaxbObject = ((JAXBElement)jaxbObject).getValue(); } logger.debug("unmarshalled xml payload of type: {}, returns jaxb object of type {}", payload.getClass().getName(), jaxbObject.getClass().getName()); return jaxbObject; } catch (JAXBException e) { throw new RuntimeException(e); } } @SuppressWarnings("rawtypes")  JaxbUtil(Class... classesToBeBound);  JaxbUtil(String contextPath); JAXBContext getContext(); void setContextPath(String contextPath); void addMarshallProperty(String name, Object value); void addUnmarshallProperty(String name, Object value); String marshal(Object jaxbObject); @SuppressWarnings({ "unchecked", "rawtypes" }) String marshal(Object jaxbObject, String namespaceURI, String localPart); @SuppressWarnings("rawtypes") Object unmarshal(Object payload); }
JaxbUtil { @SuppressWarnings("rawtypes") public Object unmarshal(Object payload) { if (jaxbContext == null) { logger.error("Trying to unmarshal with a null jaxbContext, returns null. Check your configuration, e.g. jaxb-transformers!"); return null; } try { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); if (unmarshallProps != null) { for (Entry<String, Object> entry : unmarshallProps.entrySet()) { unmarshaller.setProperty(entry.getKey(), entry.getValue()); } } Object jaxbObject = null; if (payload instanceof String) { String src = (String)payload; jaxbObject = unmarshaller.unmarshal(new StringReader(src)); } else if (payload instanceof byte[]) { byte[] src = (byte[])payload; jaxbObject = unmarshaller.unmarshal(new ByteArrayInputStream(src)); } else if (payload instanceof File) { jaxbObject = unmarshaller.unmarshal((File)payload); } else if (payload instanceof InputSource) { jaxbObject = unmarshaller.unmarshal((InputSource)payload); } else if (payload instanceof InputStream) { jaxbObject = unmarshaller.unmarshal((InputStream)payload); } else if (payload instanceof Reader) { jaxbObject = unmarshaller.unmarshal((Reader)payload); } else if (payload instanceof URL) { jaxbObject = unmarshaller.unmarshal((URL)payload); } else if (payload instanceof InputSource) { jaxbObject = unmarshaller.unmarshal((InputSource)payload); } else if (payload instanceof Node) { jaxbObject = unmarshaller.unmarshal((Node)payload); } else if (payload instanceof Source) { jaxbObject = unmarshaller.unmarshal((Source)payload); } else if (payload instanceof XMLEventReader) { jaxbObject = unmarshaller.unmarshal((XMLEventReader)payload); } else if (payload instanceof XMLStreamReader) { jaxbObject = unmarshaller.unmarshal((XMLStreamReader)payload); } else { throw new RuntimeException("Unknown sourcetype of the xml payload: " + payload.getClass().getName()); } if (jaxbObject instanceof JAXBElement) { if (logger.isDebugEnabled()) logger.debug("Found a JAXBElement, returns it value"); jaxbObject = ((JAXBElement)jaxbObject).getValue(); } logger.debug("unmarshalled xml payload of type: {}, returns jaxb object of type {}", payload.getClass().getName(), jaxbObject.getClass().getName()); return jaxbObject; } catch (JAXBException e) { throw new RuntimeException(e); } } @SuppressWarnings("rawtypes")  JaxbUtil(Class... classesToBeBound);  JaxbUtil(String contextPath); JAXBContext getContext(); void setContextPath(String contextPath); void addMarshallProperty(String name, Object value); void addUnmarshallProperty(String name, Object value); String marshal(Object jaxbObject); @SuppressWarnings({ "unchecked", "rawtypes" }) String marshal(Object jaxbObject, String namespaceURI, String localPart); @SuppressWarnings("rawtypes") Object unmarshal(Object payload);  }