@Override public void marshal(Object graph, Result result) throws XmlMappingException { marshal(graph, result, null); }
@Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Type returnType = method.getGenericReturnType(); assertTrue("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(9) + ">", marshaller.supports(returnType)); try { // make sure the marshalling does not result in errors Object returnValue = method.invoke(primitives); marshaller.marshal(returnValue, new StreamResult(new ByteArrayOutputStream())); } catch (InvocationTargetException e) { fail(e.getMessage()); } } }, new ReflectionUtils.MethodFilter() {
@Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Type returnType = method.getGenericReturnType(); assertTrue("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(13) + ">", marshaller.supports(returnType)); try { // make sure the marshalling does not result in errors Object returnValue = method.invoke(standardClasses); marshaller.marshal(returnValue, new StreamResult(new ByteArrayOutputStream())); } catch (InvocationTargetException e) { fail(e.getMessage()); } } }, new ReflectionUtils.MethodFilter() {
@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 marshalSAXResult() throws Exception { ContentHandler contentHandler = mock(ContentHandler.class); SAXResult result = new SAXResult(contentHandler); marshaller.marshal(flights, result); InOrder ordered = inOrder(contentHandler); ordered.verify(contentHandler).setDocumentLocator(isA(Locator.class)); ordered.verify(contentHandler).startDocument(); ordered.verify(contentHandler).startPrefixMapping("", "http://samples.springframework.org/flight"); ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flights"), eq("flights"), isA(Attributes.class)); ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flight"), eq("flight"), isA(Attributes.class)); ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("number"), eq("number"), isA(Attributes.class)); ordered.verify(contentHandler).characters(isA(char[].class), eq(0), eq(2)); ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "number", "number"); ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flight", "flight"); ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flights", "flights"); ordered.verify(contentHandler).endPrefixMapping(""); ordered.verify(contentHandler).endDocument(); }
@Test public void marshalAttachments() throws Exception { marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(BinaryObject.class); marshaller.setMtomEnabled(true); marshaller.afterPropertiesSet(); MimeContainer mimeContainer = mock(MimeContainer.class); Resource logo = new ClassPathResource("spring-ws.png", getClass()); DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile())); given(mimeContainer.convertToXopPackage()).willReturn(true); byte[] bytes = FileCopyUtils.copyToByteArray(logo.getInputStream()); BinaryObject object = new BinaryObject(bytes, dataHandler); StringWriter writer = new StringWriter(); marshaller.marshal(object, new StreamResult(writer), mimeContainer); assertTrue("No XML written", writer.toString().length() > 0); verify(mimeContainer, times(3)).addAttachment(isA(String.class), isA(DataHandler.class)); }
protected Marshaller getMarshaller() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(new Class<?>[] { QualifiedTrade.class }); marshaller.afterPropertiesSet(); StringWriter string = new StringWriter(); marshaller.marshal(new QualifiedTrade("FOO", 100, BigDecimal.valueOf(10.), "bar"), new StreamResult(string)); String content = string.toString(); assertTrue("Wrong content: "+content, content.contains("<customer>bar</customer>")); return marshaller; }
@Override protected Marshaller getMarshaller() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(new Class<?>[] { Trade.class }); marshaller.afterPropertiesSet(); StringWriter string = new StringWriter(); marshaller.marshal(new Trade("FOO", 100, BigDecimal.valueOf(10.), "bar"), new StreamResult(string)); String content = string.toString(); assertTrue("Wrong content: "+content, content.contains("<customer>bar</customer>")); return 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)); }
@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)); }
/** * * @throws Exception */ @Test public void testGetEmployeeAsXml() throws Exception{ Map<String, Object> employeeSearchMap = getEmployeeSearchMap("0"); final String fullUrl = "http://localhost:8080/rest-http/services/employee/{id}/search"; EmployeeList employeeList = restTemplate.execute(fullUrl, HttpMethod.GET, new RequestCallback() { @Override public void doWithRequest(ClientHttpRequest request) throws IOException { HttpHeaders headers = getHttpHeadersWithUserCredentials(request); headers.add("Accept", "application/xml"); } }, responseExtractor, employeeSearchMap); logger.info("The employee list size :"+employeeList.getEmployee().size()); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); marshaller.marshal(employeeList, sr); logger.info(sr.getWriter().toString()); assertTrue(employeeList.getEmployee().size() > 0); }
@Override public void serialize(Object o) { marshaller.marshal(o, result); }
@Override public void marshal(final Object graph, final Result result) throws IOException, XmlMappingException { jaxb2Marshaller.marshal(graph, result); }
public void marshal(Object graph, Result result) throws XmlMappingException { marshal(graph, result, null); }
public void writeMatches(final Path path, final IMatchesHolder matchesHolder) throws IOException { LOGGER.debug("About to start writing out match XML."); try (BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(path))) { Result result = new StreamResult(bos); marshaller.marshal(matchesHolder, result); LOGGER.debug("Finished writing out match XML."); } } }
public void writeMatches(final Path path, final IMatchesHolder matchesHolder) throws IOException { LOGGER.debug("About to start writing out match XML."); try (BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(path))) { Result result = new StreamResult(bos); marshaller.marshal(matchesHolder, result); LOGGER.debug("Finished writing out match XML."); } } }
@Override public String generateIdolXmlIndexDocument(final Iterable<T> objects, final Charset charset) { final Documents documents = new Documents(); final List<Object> documentList = documents.getContent(); for (final T object : objects) { documentList.add(object); } @SuppressWarnings("resource") final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); marshaller.marshal(documents, new StreamResult(outputStream)); return new String(outputStream.toByteArray(), charset) + "\n#DREENDDATANOOP\n\n"; } }
/** * Marshals object graph into XML. * * @param obj the object graph * @param sourceClass the input class * @return XML as string * @see XmlConverter */ public static String marshalToXml(Object obj, Class sourceClass) { Jaxb2Marshaller jaxb2 = new Jaxb2Marshaller(); jaxb2.setContextPath(sourceClass.getPackage().getName()); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); jaxb2.marshal(obj, result); return writer.toString(); }
private void createContainerList(final ConfigurationFactoryOaccXml configurationFactory) throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(ServiceDescriptorList.class); marshaller.afterPropertiesSet(); containerListFile = File.createTempFile("containerList", ".xml"); containerListFile.deleteOnExit(); String containerListPath = containerListFile.getAbsolutePath(); configurationFactory.setProperty("serviceConfigurations.item.serviceRegistry.fileName", "file:"+containerListPath); ServiceDescriptorList containerList = createContainerListObject(); marshaller.marshal(containerList, new StreamResult(containerListFile)); } private ServiceDescriptorList createContainerListObject() {
@Override public void write(Object o, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { NewznabResponse newznabResponse = (NewznabResponse) o; if ("json".equalsIgnoreCase(((NewznabResponse) o).getSearchType())) { jacksonConverter.setPrettyPrint(true); jacksonConverter.write(o, MediaType.APPLICATION_JSON_UTF8, outputMessage); } else { outputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); ByteArrayOutputStream bos = new ByteArrayOutputStream(); marshaller.marshal(newznabResponse, new StreamResult(bos)); String result; if ("torznab".equalsIgnoreCase(newznabResponse.getSearchType())) { result = bos.toString().replace("xmlns:newznab=\"http://www.newznab.com/DTD/2010/feeds/attributes/\"", ""); } else { result = bos.toString().replace("xmlns:torznab=\"http://torznab.com/schemas/2015/feed\"", ""); } outputMessage.getBody().write(result.getBytes("UTF-8")); } }