@Override public void marshal(Object graph, Result result) throws XmlMappingException { marshal(graph, result, null); }
@Override public Object unmarshal(Source source) throws XmlMappingException { return unmarshal(source, null); }
/** * Indicates whether external XML entities are processed when unmarshalling. * <p>Default is {@code false}, meaning that external entities are not resolved. * Note that processing of external entities will only be enabled/disabled when the * {@code Source} passed to {@link #unmarshal(Source)} is a {@link SAXSource} or * {@link StreamSource}. It has no effect for {@link DOMSource} or {@link StAXSource} * instances. * <p><strong>Note:</strong> setting this option to {@code true} also * automatically sets {@link #setSupportDtd} to {@code true}. */ public void setProcessExternalEntities(boolean processExternalEntities) { this.processExternalEntities = processExternalEntities; if (processExternalEntities) { setSupportDtd(true); } }
@Override protected Jaxb2Marshaller createMarshaller() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath(CONTEXT_PATH); marshaller.afterPropertiesSet(); return marshaller; }
@Test public void properties() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath(CONTEXT_PATH); marshaller.setMarshallerProperties( Collections.<String, Object>singletonMap(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE)); marshaller.afterPropertiesSet(); }
@Override protected Jaxb2Marshaller createUnmarshaller() throws Exception { Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller(); unmarshaller.setContextPath("org.springframework.oxm.jaxb.test"); unmarshaller.setSchema(new ClassPathResource("org/springframework/oxm/flight.xsd")); unmarshaller.afterPropertiesSet(); return unmarshaller; }
@Test public void supportsClassesToBeBound() throws Exception { marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(Flights.class, FlightType.class); marshaller.afterPropertiesSet(); testSupports(); }
@Test @SuppressWarnings("unchecked") public void unmarshalAnXmlReferingToAWrappedXmlElementDecl() throws Exception { // SPR-10714 unmarshaller = new Jaxb2Marshaller(); unmarshaller.setPackagesToScan(new String[] { "org.springframework.oxm.jaxb" }); unmarshaller.afterPropertiesSet(); Source source = new StreamSource(new StringReader( "<brand-airplane><name>test</name></brand-airplane>")); JAXBElement<Airplane> airplane = (JAXBElement<Airplane>) unmarshaller.unmarshal(source); assertEquals("Unmarshalling via explicit @XmlRegistry tag should return correct type", "test", airplane.getValue().getName()); }
@Test public void supportsXmlRootElement() throws Exception { marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(DummyRootElement.class, DummyType.class); marshaller.afterPropertiesSet(); assertTrue("Jaxb2Marshaller does not support XmlRootElement class", marshaller.supports(DummyRootElement.class)); assertTrue("Jaxb2Marshaller does not support XmlRootElement generic type", marshaller.supports((Type)DummyRootElement.class)); assertFalse("Jaxb2Marshaller supports DummyType class", marshaller.supports(DummyType.class)); assertFalse("Jaxb2Marshaller supports DummyType type", marshaller.supports((Type)DummyType.class)); }
@Before public void createMarshaller() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(MyBean.class); marshaller.afterPropertiesSet(); this.converter = new MarshallingMessageConverter(marshaller); }
@Test public void supportsPackagesToScan() throws Exception { marshaller = new Jaxb2Marshaller(); marshaller.setPackagesToScan(CONTEXT_PATH); marshaller.afterPropertiesSet(); testSupports(); }
@Test(expected = IllegalArgumentException.class) public void noContextPathOrClassesToBeBound() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.afterPropertiesSet(); }
@Test public void unmarshalFile() throws IOException { Resource resource = new ClassPathResource("jaxb2.xml", getClass()); File file = resource.getFile(); Flights f = (Flights) unmarshaller.unmarshal(new StreamSource(file)); testFlights(f); }
@Override public void marshal(Object graph, Result result, @Nullable MimeContainer mimeContainer) throws XmlMappingException { try { Marshaller marshaller = createMarshaller(); if (this.mtomEnabled && mimeContainer != null) { marshaller.setAttachmentMarshaller(new Jaxb2AttachmentMarshaller(mimeContainer)); } if (StaxUtils.isStaxResult(result)) { marshalStaxResult(marshaller, graph, result); } else { marshaller.marshal(graph, result); } } catch (JAXBException ex) { throw convertJaxbException(ex); } }
/** * Return a newly created JAXB marshaller. JAXB marshallers are not necessarily thread safe. */ protected Marshaller createMarshaller() { try { Marshaller marshaller = getJaxbContext().createMarshaller(); initJaxbMarshaller(marshaller); return marshaller; } catch (JAXBException ex) { throw convertJaxbException(ex); } }
/** * Return a newly created JAXB unmarshaller. * Note: JAXB unmarshallers are not necessarily thread-safe. */ protected Unmarshaller createUnmarshaller() { try { Unmarshaller unmarshaller = getJaxbContext().createUnmarshaller(); initJaxbUnmarshaller(unmarshaller); return unmarshaller; } catch (JAXBException ex) { throw convertJaxbException(ex); } }
@Override public String addMtomAttachment(byte[] data, int offset, int length, String mimeType, String elementNamespace, String elementLocalName) { ByteArrayDataSource dataSource = new ByteArrayDataSource(mimeType, data, offset, length); return addMtomAttachment(new DataHandler(dataSource), elementNamespace, elementLocalName); }
@Override public boolean supports(Class<?> clazz) { return (this.supportJaxbElementClass && JAXBElement.class.isAssignableFrom(clazz)) || supportsInternal(clazz, this.checkForXmlRootElement); }
@Test public void supportsContextPath() throws Exception { testSupports(); }
@Test(expected = UncategorizedMappingException.class) public void testInvalidContextPath() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath("ab"); marshaller.afterPropertiesSet(); }