@Before public void init() throws Exception { marshaller.setPackagesToScan(ClassUtils.getPackageName(GetCountryRequest.class)); marshaller.afterPropertiesSet(); }
@Test(expected = XmlMappingException.class) public void marshalInvalidClass() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(FlightType.class); marshaller.afterPropertiesSet(); Result result = new StreamResult(new StringWriter()); Flights flights = new Flights(); marshaller.marshal(flights, result); }
@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(); }
@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()); }
@Override protected Jaxb2Marshaller createMarshaller() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath(CONTEXT_PATH); marshaller.afterPropertiesSet(); return marshaller; }
@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; }
@Bean public Jaxb2Marshaller jaxb2MarshallerImage() { Jaxb2Marshaller bean = new Jaxb2Marshaller(); bean.setContextPath("ch.sahits.game.graphic.data.image"); return bean; } @Bean
@Before public void setUp() throws Exception { File directory = new File("build/data"); directory.mkdirs(); resource = new FileSystemResource(File.createTempFile("StaxEventWriterOutputSourceTests", ".xml", directory)); writer = createItemWriter(); executionContext = new ExecutionContext(); jaxbMarshaller = new Jaxb2Marshaller(); jaxbMarshaller.setClassesToBeBound(JAXBItem.class); }
@Override public void setConfiguration(XmlStringToObjectConfiguration configuration) { this.configuration = configuration; marshaller = new Jaxb2Marshaller(); if (StringUtils.isNotEmpty(configuration.getContextPath())){ marshaller.setContextPath(configuration.getContextPath()); } else if (ArrayUtils.isNotEmpty(configuration.getContextPaths())){ marshaller.setContextPaths(configuration.getContextPaths()); } else if (ArrayUtils.isNotEmpty(configuration.getClassesToBeBound())){ marshaller.setClassesToBeBound(configuration.getClassesToBeBound()); } marshaller.setUnmarshallerProperties(configuration.getUnmarshallerProperties()); marshaller.setMarshallerProperties(configuration.getMarshallerProperties()); if (configuration.getValidationEventHandler() != null) { marshaller.setValidationEventHandler(configuration.getValidationEventHandler()); } try { marshaller.afterPropertiesSet(); } catch (Exception e) { throw new ConfigurationException(e.getMessage()); } }
@Before public void createMarshaller() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(MyBean.class); marshaller.afterPropertiesSet(); this.converter = new MarshallingMessageConverter(marshaller); }
@Test public void lazyInit() throws Exception { marshaller = new Jaxb2Marshaller(); marshaller.setContextPath(CONTEXT_PATH); marshaller.setLazyInit(true); marshaller.afterPropertiesSet(); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); marshaller.marshal(flights, result); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING).withDifferenceEvaluator(ev)); }
/** * Enable pretty printing of returned XML */ @Bean public Jaxb2Marshaller marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); Map<String, Boolean> map = new HashMap<>(); map.put(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setMarshallerProperties(map); marshaller.setPackagesToScan("org.nzbhydra"); return marshaller; }
Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setPackagesToScan("org.finra.herd.model.api.xml"); marshaller.setSchemas(resourceResolver.getResources("classpath:herd.xsd")); marshaller.setMarshallerProperties(marshallerProperties);
protected Unmarshaller getUnmarshaller() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(new Class<?>[] { QualifiedTrade.class }); marshaller.setSchema(new ClassPathResource("trade.xsd", Trade.class)); marshaller.afterPropertiesSet(); return marshaller; }
@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)); }
@Test public void marshalAttachments() throws Exception { unmarshaller = new Jaxb2Marshaller(); unmarshaller.setClassesToBeBound(BinaryObject.class); unmarshaller.setMtomEnabled(true); unmarshaller.afterPropertiesSet(); MimeContainer mimeContainer = mock(MimeContainer.class); Resource logo = new ClassPathResource("spring-ws.png", getClass()); DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile())); given(mimeContainer.isXopPackage()).willReturn(true); given(mimeContainer.getAttachment("<6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws>")).willReturn(dataHandler); given(mimeContainer.getAttachment("<99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws>")).willReturn(dataHandler); given(mimeContainer.getAttachment("696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png")).willReturn(dataHandler); String content = "<binaryObject xmlns='http://springframework.org/spring-ws'>" + "<bytes>" + "<xop:Include href='cid:6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" + "</bytes>" + "<dataHandler>" + "<xop:Include href='cid:99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" + "</dataHandler>" + "<swaDataHandler>696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png</swaDataHandler>" + "</binaryObject>"; StringReader reader = new StringReader(content); Object result = unmarshaller.unmarshal(new StreamSource(reader), mimeContainer); assertTrue("Result is not a BinaryObject", result instanceof BinaryObject); BinaryObject object = (BinaryObject) result; assertNotNull("bytes property not set", object.getBytes()); assertTrue("bytes property not set", object.getBytes().length > 0); assertNotNull("datahandler property not set", object.getSwaDataHandler()); }
final Map<String, Object> jaxbContextProps = new HashMap<String, Object>(); jaxbContextProps.put(JAXBContextProperties.OXM_METADATA_SOURCE, "binding.json"); jaxbContextProps.put(JAXBContextProperties.MEDIA_TYPE, "application/xml"); final Map<String, Object> marshallerProps = new HashMap<String, Object>(); marshallerProps.put(Marshaller.JAXB_FORMATTED_OUTPUT, true); final Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setLazyInit(true); marshaller.setClassesToBeBound(Pojo.class); marshaller.setJaxbContextProperties(jaxbContextProps); marshaller.setMarshallerProperties(marshallerProps); Pojo pojo = new Pojo(); pojo.set("message", "This is some message"); pojo.set("shouldValidate", "Yes"); pojo.set("buffer", "1000"); StringWriter writer = new StringWriter(); StreamResult streamResult = new StreamResult(writer); marshaller.marshal(pojo, streamResult); //System.out.println(writer.toString());
Jaxb2QueryResponseDataParser(final ResponseDataParser<R> responseDataMarshaller, final Class<R> responseDataType, final Class<C> contentType) { this.responseDataMarshaller = responseDataMarshaller; this.responseDataType = responseDataType; this.contentType = contentType; marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(contentType); marshaller.setMappedClass(contentType); }
resp.getEvents().add(e); Jaxb2Marshaller m = new Jaxb2Marshaller(); m.setContextPaths( "net.exogeni.orca.manage.beans", "net.exogeni.orca.manage.proxies.soap.beans.actor", Map<String, Object> mp = new HashMap<String, Object>(); mp.put(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.setMarshallerProperties(mp); m.marshal(resp, r); DrainEventsResponse ur = (DrainEventsResponse)m.unmarshal(s);
@Test // SPR-10714 public void marshalAWrappedObjectHoldingAnXmlElementDeclElement() throws Exception { marshaller = new Jaxb2Marshaller(); marshaller.setPackagesToScan("org.springframework.oxm.jaxb"); marshaller.afterPropertiesSet(); Airplane airplane = new Airplane(); airplane.setName("test"); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); marshaller.marshal(airplane, result); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat("Marshalling should use root Element", writer.toString(), isSimilarTo("<airplane><name>test</name></airplane>").withDifferenceEvaluator(ev)); }