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(); } }
@Override public Object unmarshal(Source source) throws XmlMappingException, IOException { if (source instanceof StringSource) { char[] chars = new char[8]; ((StringSource) source).getReader().read(chars); if (returnMessage) { return new GenericMessage<String>("message: " + new String(chars).trim()); } return "hello " + new String(chars).trim(); } else if (source instanceof StreamSource) { byte[] bytes = new byte[8]; ((StreamSource) source).getInputStream().read(bytes); if (returnMessage) { return new GenericMessage<String>("message: " + new String(bytes).trim()); } return "hello " + new String(bytes).trim(); } return null; }
/** * Converts a Spring-WS {@link org.springframework.xml.transform.StringSource} * to a Camel {@link org.apache.camel.converter.jaxp.StringSource} */ @Converter public static StringSource toStringSourceFromSpring(org.springframework.xml.transform.StringSource springStringSource) { return new StringSource(springStringSource.toString()); }
@Test public void testWithString() throws Exception { String docString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><item>one</item>"; StringSource source = (StringSource) sourceFactory.createSource(docString); BufferedReader reader = new BufferedReader(source.getReader()); String docAsString = reader.readLine(); assertThat(docAsString).isXmlEqualTo(docString); }
/** * 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 testWithDocument() throws Exception { String docString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><item>one</item>"; Document doc = XmlTestUtil.getDocumentForString(docString); StringSource source = (StringSource) sourceFactory.createSource(doc); BufferedReader reader = new BufferedReader(source.getReader()); String docAsString = reader.readLine(); assertThat(docAsString).isXmlEqualTo(docString); }
@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 Source extractSource(Object requestPayload) throws IOException, TransformerException { Source source = null; if (requestPayload instanceof Source) { source = (Source) requestPayload; Object o = SimpleWebServiceOutboundGateway.this.sourceExtractor.extractData(source); Assert.isInstanceOf(Source.class, o); source = (Source) o; } else if (requestPayload instanceof String) { source = new StringSource((String) requestPayload); } else if (requestPayload instanceof Document) { source = new DOMSource((Document) requestPayload); } else if (requestPayload instanceof WebServiceMessage) { source = ((WebServiceMessage) requestPayload).getPayloadSource(); } else { throw new MessagingException("Unsupported payload type '" + requestPayload.getClass() + "'. " + this.getClass().getName() + " only supports 'java.lang.String', '" + Source.class.getName() + "', '" + Document.class.getName() + "' and '" + WebServiceMessage.class.getName() + "'. " + "Consider either using the '" + MarshallingWebServiceOutboundGateway.class.getName() + "' or a Message Transformer."); } return source; }
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); } }
source = new StringSource((String) payload);
@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); }
responseSource = new StringSource((String) replyPayload);