diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/source/DomSourceFactory.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/source/DomSourceFactory.java index cdbda46930..96e583fb97 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/source/DomSourceFactory.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/source/DomSourceFactory.java @@ -18,7 +18,9 @@ package org.springframework.integration.xml.source; import java.io.StringReader; +import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; @@ -69,8 +71,7 @@ public class DomSourceFactory implements SourceFactory { protected DOMSource createDomSourceForString(String s) { try { - Document doc = docBuilderFactory.newDocumentBuilder().parse( - new InputSource(new StringReader(s))); + Document doc = getNewDocumentBuilder().parse(new InputSource(new StringReader(s))); DOMSource source = new DOMSource(doc.getDocumentElement()); return source; } catch (Exception e) { @@ -78,4 +79,10 @@ public class DomSourceFactory implements SourceFactory { } } + protected DocumentBuilder getNewDocumentBuilder() throws ParserConfigurationException{ + synchronized (docBuilderFactory) { + return docBuilderFactory.newDocumentBuilder(); + } + + } } diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/ResultToStringTransformer.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/ResultToStringTransformer.java new file mode 100644 index 0000000000..0b371f5b25 --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/ResultToStringTransformer.java @@ -0,0 +1,90 @@ +/* + * Copyright 2002-2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.xml.transformer; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Result; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.dom.DOMSource; + +import org.springframework.integration.message.MessagingException; +import org.springframework.xml.transform.StringResult; + +/** + * Converts the passed {@link Result} to an instance of {@link String} + * + * Supports {@link StringResult} and {@link DOMResult} + * + * @author Jonas Partner + * + */ +public class ResultToStringTransformer implements ResultTransformer { + + private DocumentBuilderFactory docBuilderFactory; + + private TransformerFactory transformerFactory; + + public ResultToStringTransformer() { + this.docBuilderFactory = DocumentBuilderFactory.newInstance(); + this.transformerFactory = TransformerFactory.newInstance(); + } + + protected Transformer getNewTransformer() + throws TransformerConfigurationException { + synchronized (transformerFactory) { + return transformerFactory.newTransformer(); + } + } + + public Object transformResult(Result res) { + String returnString = null; + if (res instanceof StringResult) { + returnString = ((StringResult) res).toString(); + } else if (res instanceof DOMResult) { + try { + StringResult strRes = new StringResult(); + getNewTransformer().transform( + new DOMSource(((DOMResult) res).getNode()), strRes); + returnString = strRes.toString(); + } catch (TransformerException transE) { + throw new MessagingException( + "Transformation from DOMSOurce failed", transE); + } + } + + if (returnString == null) { + throw new MessagingException("Could not convert Result type " + + res.getClass().getName() + " to string"); + } + + return returnString; + } + + protected DocumentBuilder getNewDocumentBuilder() + throws ParserConfigurationException { + synchronized (docBuilderFactory) { + return docBuilderFactory.newDocumentBuilder(); + } + + } + +} diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XmlPayloadMarshallingTransformer.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XmlPayloadMarshallingTransformer.java index b9159240a1..96747f43b8 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XmlPayloadMarshallingTransformer.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XmlPayloadMarshallingTransformer.java @@ -45,7 +45,8 @@ public class XmlPayloadMarshallingTransformer implements private final ResultTransformer resultTransformer; public XmlPayloadMarshallingTransformer(Marshaller marshaller, - ResultTransformer resultTransformer) throws ParserConfigurationException { + ResultTransformer resultTransformer) + throws ParserConfigurationException { Assert.notNull(marshaller, "a marshaller is required"); this.marshaller = marshaller; this.resultTransformer = resultTransformer; @@ -78,10 +79,9 @@ public class XmlPayloadMarshallingTransformer implements if (transformedPayload == null) { throw new MessagingException("Failed to transform payload"); } - if(resultTransformer != null){ + if (resultTransformer != null) { transformedPayload = resultTransformer.transformResult(result); } - return transformedPayload; } diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/ResultToDocumentTransformerTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/ResultToDocumentTransformerTests.java index 177b8d2625..bae1bbe6d5 100644 --- a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/ResultToDocumentTransformerTests.java +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/ResultToDocumentTransformerTests.java @@ -35,7 +35,7 @@ import org.springframework.xml.transform.StringResult; */ public class ResultToDocumentTransformerTests { - private String doc = "test"; + private String startDoc = "test"; private ResultToDocumentTransformer resToDocTransformer; @@ -48,7 +48,7 @@ public class ResultToDocumentTransformerTests { @Test public void testWithDomResult() throws Exception { - DOMResult result = XmlTestUtil.getDomResultForString(doc); + DOMResult result = XmlTestUtil.getDomResultForString(startDoc); Object transformed = resToDocTransformer.transformResult(result); assertTrue("Wrong transformed type expected Document", transformed instanceof Document); Document doc = (Document) transformed; @@ -57,7 +57,7 @@ public class ResultToDocumentTransformerTests { @Test public void testWithStringResult() throws Exception { - StringResult result = XmlTestUtil.getStringResultForString(doc); + StringResult result = XmlTestUtil.getStringResultForString(startDoc); Object transformed = resToDocTransformer.transformResult(result); assertTrue("Wrong transformed type expected Document", transformed instanceof Document); Document doc = (Document) transformed; diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/ResultToStringTransfomerTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/ResultToStringTransfomerTests.java new file mode 100644 index 0000000000..98bbd39f28 --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/ResultToStringTransfomerTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2002-2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.xml.transformer; + + +import static org.custommonkey.xmlunit.XMLAssert.*; + +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.sax.SAXResult; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.integration.message.MessagingException; +import org.springframework.integration.xml.util.XmlTestUtil; +import org.springframework.xml.transform.StringResult; +import org.w3c.dom.Document; + +public class ResultToStringTransfomerTests { + + ResultToStringTransformer transformer; + + private String doc = "test"; + + @Before + public void setUp(){ + transformer = new ResultToStringTransformer(); + } + @Test + public void testWithDomResult() throws Exception { + DOMResult result = XmlTestUtil.getDomResultForString(doc); + Object transformed = transformer.transformResult(result); + assertTrue("Wrong transformed type expected String", transformed instanceof String); + String transformedString = (String) transformed; + assertXMLEqual("Wrong content", doc, transformedString); + } + + @Test + public void testWithStringResult() throws Exception { + StringResult result = XmlTestUtil.getStringResultForString(doc); + Object transformed = transformer.transformResult(result); + assertTrue("Wrong transformed type expected String", transformed instanceof String); + String transformedString = (String) transformed; + assertXMLEqual("Wrong content", doc, transformedString); + } + + @Test(expected = MessagingException.class) + public void testWithUnsupportedSaxResult() throws Exception { + SAXResult result = new SAXResult(); + transformer.transformResult(result); + } + +}