diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java index 8044d5ea0d..7836d9cc87 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java @@ -33,67 +33,72 @@ import org.xml.sax.InputSource; /** * Default implementation of {@link XmlPayloadConverter}. * Supports {@link Document} and {@link String}. - * + * * @author Jonas Partner */ public class DefaultXmlPayloadConverter implements XmlPayloadConverter { - private DocumentBuilderFactory documentBuilderFactory; + private DocumentBuilderFactory documentBuilderFactory; - public DefaultXmlPayloadConverter() { - this.documentBuilderFactory = DocumentBuilderFactory.newInstance(); - this.documentBuilderFactory.setNamespaceAware(true); - } + public DefaultXmlPayloadConverter() { + this.documentBuilderFactory = DocumentBuilderFactory.newInstance(); + this.documentBuilderFactory.setNamespaceAware(true); + } - public DefaultXmlPayloadConverter(DocumentBuilderFactory documentBuilderFactory) { - this.documentBuilderFactory = documentBuilderFactory; - } + public DefaultXmlPayloadConverter(DocumentBuilderFactory documentBuilderFactory) { + this.documentBuilderFactory = documentBuilderFactory; + } - public Document convertToDocument(Object object) { - if (object instanceof Document) { - return (Document) object; - } - if (object instanceof String) { - try { - return getDocumentBuilder().parse(new InputSource(new StringReader((String) object))); - } - catch (Exception e) { - throw new MessagingException("failed to parse String payload '" + object + "'", e); - } - } - throw new MessagingException("unsupported payload type [" + object.getClass().getName() + "]"); - } + public Document convertToDocument(Object object) { + if (object instanceof Document) { + return (Document) object; + } + if (object instanceof String) { + try { + return getDocumentBuilder().parse(new InputSource(new StringReader((String) object))); + } + catch (Exception e) { + throw new MessagingException("failed to parse String payload '" + object + "'", e); + } + } + throw new MessagingException("unsupported payload type [" + object.getClass().getName() + "]"); + } - public Node convertToNode(Object object) { - if (object instanceof Node){ - return (Node) object; - } - return convertToDocument(object); - } - - public Source convertToSource(Object object){ - Source source; - if(object instanceof Source){ - source = (Source)object; - } else if (object instanceof Document){ - source = new DOMSource((Document)object); - } else if (object instanceof String){ - source = new StringSource((String)object); - } else { - throw new MessagingException("unsupported payload type [" + object.getClass().getName() + "]"); - } - return source; - } + public Node convertToNode(Object object) { + Node n = null; + if (object instanceof Node) { + n = (Node) object; + } else if (object instanceof DOMSource) { + n = ((DOMSource) object).getNode(); + } else { + n = convertToDocument(object); + } + return n; + } - protected synchronized DocumentBuilder getDocumentBuilder() { - try { - return this.documentBuilderFactory.newDocumentBuilder(); - } - catch (ParserConfigurationException e) { - throw new MessagingException("failed to create a new DocumentBuilder", e); - } - } + public Source convertToSource(Object object) { + Source source; + if (object instanceof Source) { + source = (Source) object; + } else if (object instanceof Document) { + source = new DOMSource((Document) object); + } else if (object instanceof String) { + source = new StringSource((String) object); + } else { + throw new MessagingException("unsupported payload type [" + object.getClass().getName() + "]"); + } + return source; + } + + protected synchronized DocumentBuilder getDocumentBuilder() { + try { + return this.documentBuilderFactory.newDocumentBuilder(); + } + catch (ParserConfigurationException e) { + throw new MessagingException("failed to create a new DocumentBuilder", e); + } + } } diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java index 499d1a5095..458f7c3056 100644 --- a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java @@ -112,4 +112,12 @@ public class DefaultXmlPayloadConverterTests { converter.convertToSource(12); } + @Test + public void testGetNodePassingDOMSource(){ + Node element = (Node) testDocument.getElementsByTagName("test").item(0); + Node n = converter.convertToNode(new DOMSource(element)); + assertTrue("Wrong node returned", element == n); + } + + }