INT-4063: Extend Types for DefXmlPayloadConverter

JIRA: https://jira.spring.io/browse/INT-4063

Since `DefaultXmlPayloadConverter` is used from many out-of-the-box components by default,
make it more flexible with the supported input types which can be converted into `Document`

Improve `xml.adoc` about this types and also fix a lot of typos there as well

Address PR comments
This commit is contained in:
Artem Bilan
2016-07-01 19:14:04 -04:00
committed by Gary Russell
parent 5716315e88
commit f74ddc2aa6
4 changed files with 158 additions and 84 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -16,7 +16,9 @@
package org.springframework.integration.xml;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
@@ -24,6 +26,7 @@ import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
@@ -57,40 +60,54 @@ 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;
try {
if (object instanceof Document) {
return (Document) object;
}
else {
return nodeToDocument(node);
else if (object instanceof Node) {
return nodeToDocument((Node) object);
}
}
if (object instanceof File) {
try {
else if (object instanceof DOMSource) {
Node node = ((DOMSource) object).getNode();
if (node instanceof Document) {
return (Document) node;
}
else {
return nodeToDocument(node);
}
}
else if (object instanceof Source) {
InputSource inputSource = sourceToInputSource((Source) object);
return getDocumentBuilder().parse(inputSource);
}
else if (object instanceof File) {
return getDocumentBuilder().parse((File) object);
}
catch (Exception e) {
throw new MessagingException("failed to parse File payload '" + object + "'", e);
}
}
if (object instanceof String) {
try {
else if (object instanceof String) {
return getDocumentBuilder().parse(new InputSource(new StringReader((String) object)));
}
catch (Exception e) {
throw new MessagingException("failed to parse String payload '" + object + "'", e);
else if (object instanceof InputStream) {
return getDocumentBuilder().parse((InputStream) object);
}
else if (object instanceof byte[]) {
return getDocumentBuilder().parse(new ByteArrayInputStream((byte[]) object));
}
}
catch (Exception e) {
throw new MessagingException("failed to parse " + object.getClass() + " payload '" + object + "'", e);
}
throw new MessagingException("unsupported payload type [" + object.getClass().getName() + "]");
}
private static InputSource sourceToInputSource(Source source) {
InputSource inputSource = SAXSource.sourceToInputSource(source);
if (inputSource == null) {
inputSource = new InputSource(source.getSystemId());
}
return inputSource;
}
protected Document nodeToDocument(Node node) {
Document document = getDocumentBuilder().newDocument();
document.appendChild(document.importNode(node, true));

View File

@@ -19,12 +19,16 @@ package org.springframework.integration.xml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import org.custommonkey.xmlunit.XMLAssert;
import org.junit.Assert;
@@ -35,6 +39,7 @@ import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.messaging.MessagingException;
import org.springframework.xml.transform.StringSource;
@@ -46,17 +51,17 @@ import org.springframework.xml.transform.StringSource;
*/
public class DefaultXmlPayloadConverterTests {
DefaultXmlPayloadConverter converter;
private static final String TEST_DOCUMENT_AS_STRING = "<test>hello</test>";
Document testDocument;
private DefaultXmlPayloadConverter converter;
String testDocumentAsString = "<test>hello</test>";
private Document testDocument;
@Before
public void setUp() throws Exception {
converter = new DefaultXmlPayloadConverter();
testDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
new InputSource(new StringReader(testDocumentAsString)));
new InputSource(new StringReader(TEST_DOCUMENT_AS_STRING)));
}
@Test
@@ -99,7 +104,7 @@ public class DefaultXmlPayloadConverterTests {
@Test
public void testGetSourcePassingString() throws Exception {
Source source = converter.convertToSource(testDocumentAsString);
Source source = converter.convertToSource(TEST_DOCUMENT_AS_STRING);
assertEquals(StringSource.class, source.getClass());
}
@@ -143,4 +148,59 @@ public class DefaultXmlPayloadConverterTests {
assertEquals("hello", childNodes.item(0).getTextContent());
}
@Test
public void testConvertBytesToDocument() throws Exception {
Document doc = converter.convertToDocument("<test>hello</test>".getBytes());
XMLAssert.assertXMLEqual(testDocument, doc);
}
@Test
public void testConvertFileToDocument() throws Exception {
File file = new ClassPathResource("org/springframework/integration/xml/customSource.data").getFile();
Document doc = converter.convertToDocument(file);
XMLAssert.assertXMLEqual(testDocument, doc);
}
@Test
public void testConvertInputStreamToDocument() throws Exception {
InputStream inputStream = new ClassPathResource("org/springframework/integration/xml/customSource.data")
.getInputStream();
Document doc = converter.convertToDocument(inputStream);
XMLAssert.assertXMLEqual(testDocument, doc);
}
@Test
public void testConvertStreamSourceToDocument() throws Exception {
ClassPathResource resource = new ClassPathResource("org/springframework/integration/xml/customSource.data");
StreamSource source = new StreamSource(resource.getInputStream());
Document doc = converter.convertToDocument(source);
XMLAssert.assertXMLEqual(testDocument, doc);
}
@Test
public void testConvertCustomSourceToDocument() throws Exception {
Document doc = converter.convertToDocument(new MySource());
XMLAssert.assertXMLEqual(testDocument, doc);
}
private static class MySource implements Source {
@Override
public void setSystemId(String systemId) {
}
@Override
public String getSystemId() {
try {
return new ClassPathResource("org/springframework/integration/xml/customSource.data")
.getFile()
.getPath();
}
catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
}

View File

@@ -0,0 +1 @@
<test>hello</test>