public void marshal(Object zip, Result result) throws IOException, XmlMappingException { String xmlString = "<weat:GetCityWeatherByZIP xmlns:weat=\"http://ws.cdyne.com/WeatherWS/\">" + " <weat:ZIP>" + zip + "</weat:ZIP>" + "</weat:GetCityWeatherByZIP>"; try { Transformer transformer = transformerFactory.newTransformer(); transformer.transform(new StringSource(xmlString), result); } catch (Exception e) { e.printStackTrace(); } }
/** * Prettifies XML data, e.g. add indentation. * * @param result The XML to prettify * @return Prettified XML * @throws Exception */ public static String formatXml(String result) throws Exception{ Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(2)); StreamResult streamResult = new StreamResult(new StringWriter()); Source source = new StringSource(result); transformer.transform(source, streamResult); String xmlString = streamResult.getWriter().toString(); return xmlString; }
@Test public void testWebServiceRequestAndResponse() { StringResult result = new StringResult(); Source payload = new StringSource( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<echoRequest xmlns=\"http://www.springframework.org/spring-ws/samples/echo\">hello</echoRequest>"); template.sendSourceAndReceiveToResult(WS_URI, payload, result); logger.info("RESULT: " + result.toString()); assertThat(result.toString(), equalTo( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<echoResponse xmlns=\"http://www.springframework.org/spring-ws/samples/echo\">hello</echoResponse>")); } }
private StringSource createStringSourceForFile(File file) { try { String content = FileCopyUtils.copyToString(new FileReader(file)); return new StringSource(content); } catch (Exception e) { throw new MessagingException("failed to create StringSource from file", e); } }
private Templates getXslTemplates() throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" + " <xsl:template match=\"order\">" + " <bob>test</bob>" + " </xsl:template>" + "</xsl:stylesheet>"; return transformerFactory.newTemplates(new StringSource(xsl)); }
private String transformString(String stringPayload, Transformer transformer) throws TransformerException { StringResult result = new StringResult(); Source source; if (this.alwaysUseSourceFactory) { source = this.sourceFactory.createSource(stringPayload); } else { source = new StringSource(stringPayload); } transformer.transform(source, result); return result.toString(); }
public void marshal(Object graph, Result result) throws XmlMappingException, IOException { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); StringSource stringSource = new StringSource("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><root>" + graph.toString() + "</root>"); transformer.transform(stringSource, result); } catch (Exception e) { throw new RuntimeException(e); } }
private StringSource createStringSourceForDocument(Document document) { try { StringResult result = new StringResult(); Transformer transformer = getTransformer(); transformer.transform(new DOMSource(document), result); return new StringSource(result.toString()); } catch (Exception e) { throw new MessagingException("failed to create StringSource from document", e); } }
public Source createSource(Object payload) { Source source = null; if (payload instanceof String) { source = new StringSource((String) payload); } else if (payload instanceof Document) { source = createStringSourceForDocument((Document) payload); } else if (payload instanceof File) { source = createStringSourceForFile((File) payload); } if (source == null) { throw new MessagingException("Failed to create Source for payload type [" + payload.getClass().getName() + "]"); } return source; }
private Object transformUsingResultFactory(Object payload, Transformer transformer) throws TransformerException { Source source; if (this.alwaysUseSourceFactory) { source = this.sourceFactory.createSource(payload); } else if (payload instanceof String) { source = new StringSource((String) payload); } else if (payload instanceof Document) { source = new DOMSource((Document) payload); } else if (payload instanceof Source) { source = (Source) payload; } else { source = this.sourceFactory.createSource(payload); } return transformSource(source, payload, transformer); }
public void marshal(Object graph, Result result) throws XmlMappingException, IOException { Transformer transformer; try { transformer = TransformerFactory.newInstance().newTransformer(); StringSource stringSource = new StringSource(graph.toString()); transformer.transform(stringSource, result); } catch (Exception e) { throw new MarshallingFailureException("Stub failed to marshal", e); } }
@Test public void testDefaultUnmarshall() throws Exception { MessageChannel input = (MessageChannel) appContext.getBean("input"); PollableChannel output = (PollableChannel) appContext.getBean("output"); GenericMessage<Object> message = new GenericMessage<Object>(new StringSource( "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>")); input.send(message); Message<?> result = output.receive(0); assertEquals("Wrong payload after unmarshalling", "unmarshalled", result.getPayload()); assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof StringSource); }
@Test public void testPollingUnmarshall() throws Exception { MessageChannel input = (MessageChannel) appContext.getBean("pollableInput"); PollableChannel output = (PollableChannel) appContext.getBean("output"); GenericMessage<Object> message = new GenericMessage<Object>(new StringSource( "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>")); input.send(message); Message<?> result = output.receive(5000); assertEquals("Wrong payload after unmarshalling", "unmarshalled", result.getPayload()); assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof StringSource); }
@Test public void testSourceAsPayload() throws Exception { GenericMessage<?> message = new GenericMessage<>(new StringSource(this.docAsString)); Object transformed = transformer.doTransform(message); assertThat(transformed) .as("Wrong return type for document payload") .isInstanceOf(DOMResult.class); DOMResult result = (DOMResult) transformed; assertThat(XmlTestUtil.docToString((Document) result.getNode())) .as("Document incorrect after transformation") .isXmlEqualTo(this.outputAsString); }
@Test public void testSourceWithResultTransformer() throws Exception { Integer returnValue = 13; XsltPayloadTransformer transformer = new XsltPayloadTransformer(getXslTemplates(), new StubResultTransformer(returnValue)); transformer.setBeanFactory(Mockito.mock(BeanFactory.class)); transformer.afterPropertiesSet(); Object transformed = transformer .doTransform(new GenericMessage<>(new StringSource(docAsString))); assertThat(transformed).isEqualTo(returnValue); }
@Test public void testStringSourceToString() { Unmarshaller unmarshaller = new TestUnmarshaller(false); UnmarshallingTransformer transformer = new UnmarshallingTransformer(unmarshaller); Object transformed = transformer.transformPayload(new StringSource("world")); assertEquals(String.class, transformed.getClass()); assertEquals("hello world", transformed.toString()); }
@Test public void testMessageReturnValue() { Unmarshaller unmarshaller = new TestUnmarshaller(true); UnmarshallingTransformer transformer = new UnmarshallingTransformer(unmarshaller); Object transformed = transformer.transformPayload(new StringSource("foo")); assertEquals(GenericMessage.class, transformed.getClass()); assertEquals("message: foo", ((Message<?>) transformed).getPayload()); }
@Test public void testXsltPayloadWithTransformerFactoryClassName() throws Exception { Integer returnValue = 13; XsltPayloadTransformer transformer = new XsltPayloadTransformer(getXslResourceThatOutputsText(), new StubResultTransformer(returnValue), "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); transformer.setBeanFactory(Mockito.mock(BeanFactory.class)); transformer.afterPropertiesSet(); Object transformed = transformer.doTransform(new GenericMessage<>(new StringSource(this.docAsString))); assertThat(transformed) .as("Wrong value from result conversion") .isEqualTo(returnValue); }
@Test public void testMessageReturnValueFromTopLevel() { Unmarshaller unmarshaller = new TestUnmarshaller(true); UnmarshallingTransformer transformer = new UnmarshallingTransformer(unmarshaller); Message<?> result = transformer.transform(MessageBuilder.withPayload(new StringSource("bar")).build()); assertNotNull(result); assertEquals("message: bar", result.getPayload()); }
@SuppressWarnings("unchecked") @Test public void testUnmarshalling() throws Exception { StringSource source = new StringSource("<person><firstname>bob</firstname></person>"); unmarshallIn.send(new GenericMessage<Source>(source)); GenericMessage<Object> res = (GenericMessage<Object>) unmarshallOut.receive(2000); assertNotNull("No response", res); assertTrue("Not a Person ", res.getPayload() instanceof JaxbAnnotatedPerson); JaxbAnnotatedPerson person = (JaxbAnnotatedPerson) res.getPayload(); assertEquals("Worng firstname", "bob", person.getFirstName()); }