diff --git a/spring-integration-xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java b/spring-integration-xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java index 3a206b7929..e4372d556b 100644 --- a/spring-integration-xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java +++ b/spring-integration-xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2014 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. @@ -34,13 +34,15 @@ import org.springframework.xml.transform.StringSource; /** * Default implementation of {@link XmlPayloadConverter}. Supports - * {@link Document}, {@link File} and {@link String} payloads. - * + * {@link Document}, {@link File}, {@link String}, {@link Node} and + * {@link DOMSource} payloads. + * * @author Jonas Partner + * @author Artem Bilan */ public class DefaultXmlPayloadConverter implements XmlPayloadConverter { - private DocumentBuilderFactory documentBuilderFactory; + private final DocumentBuilderFactory documentBuilderFactory; public DefaultXmlPayloadConverter() { @@ -53,10 +55,23 @@ public class DefaultXmlPayloadConverter implements XmlPayloadConverter { } + @Override public Document convertToDocument(Object object) { if (object instanceof Document) { return (Document) object; } + if (object instanceof Node) { + return nodeToDocument((Node) object); + } + else if (object instanceof DOMSource) { + Node node = ((DOMSource) object).getNode(); + if (node instanceof Document) { + return (Document) node; + } + else { + return nodeToDocument(node); + } + } if (object instanceof File) { try { return getDocumentBuilder().parse((File) object); @@ -76,6 +91,13 @@ public class DefaultXmlPayloadConverter implements XmlPayloadConverter { throw new MessagingException("unsupported payload type [" + object.getClass().getName() + "]"); } + protected Document nodeToDocument(Node node) { + Document document = getDocumentBuilder().newDocument(); + document.appendChild(document.importNode(node, true)); + return document; + } + + @Override public Node convertToNode(Object object) { Node node = null; if (object instanceof Node) { @@ -90,6 +112,7 @@ public class DefaultXmlPayloadConverter implements XmlPayloadConverter { return node; } + @Override public Source convertToSource(Object object) { Source source = null; if (object instanceof Source) { diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java b/spring-integration-xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java index 14cd11ec8c..349f7ae53c 100644 --- a/spring-integration-xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.xml; import static org.junit.Assert.assertEquals; @@ -25,22 +26,23 @@ import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXSource; -import org.junit.Assert; - import org.custommonkey.xmlunit.XMLAssert; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.springframework.messaging.MessagingException; -import org.springframework.xml.transform.StringSource; import org.w3c.dom.Document; import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import org.xml.sax.InputSource; +import org.springframework.messaging.MessagingException; +import org.springframework.xml.transform.StringSource; + /** * * @author Jonas Partner * @author Gunnar Hillert - * + * @author Artem Bilan */ public class DefaultXmlPayloadConverterTests { @@ -90,7 +92,7 @@ public class DefaultXmlPayloadConverterTests { @Test - public void testGetSourcePassingDocumet() throws Exception{ + public void testGetSourcePassingDocument() throws Exception{ Source source = converter.convertToSource(testDocument); assertEquals(DOMSource.class, source.getClass()); } @@ -120,5 +122,25 @@ public class DefaultXmlPayloadConverterTests { assertTrue("Wrong node returned", element == n); } + @Test + public void testConvertNodeToDocument() { + Node element = testDocument.getElementsByTagName("test").item(0); + Document doc = converter.convertToDocument(element); + NodeList childNodes = doc.getChildNodes(); + assertEquals(1, childNodes.getLength()); + assertEquals("test", childNodes.item(0).getNodeName()); + assertEquals("hello", childNodes.item(0).getTextContent()); + } + + @Test + public void testConvertSourceToDocument() throws Exception{ + Node element = testDocument.getElementsByTagName("test").item(0); + DOMSource domSource = new DOMSource(element); + Document doc = converter.convertToDocument(element); + NodeList childNodes = doc.getChildNodes(); + assertEquals(1, childNodes.getLength()); + assertEquals("test", childNodes.item(0).getNodeName()); + assertEquals("hello", childNodes.item(0).getTextContent()); + } }