INT-3509 Add Convert Node & DOMSource to Document
JIRA: https://jira.spring.io/browse/INT-3509 **Cherry-pick to 3.0.x and 4.0.x** Polishing - Javadoc - Refactor duplicated code
This commit is contained in:
committed by
Gary Russell
parent
af2ec0567a
commit
de1d8b9806
@@ -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) {
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user