Skip to content

Commit

Permalink
INT-4063: Extend Types for DefXmlPayloadConverter
Browse files Browse the repository at this point in the history
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
  • Loading branch information
artembilan authored and garyrussell committed Jul 22, 2016
1 parent 5716315 commit f74ddc2
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,14 +16,17 @@

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;
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;
Expand Down Expand Up @@ -57,40 +60,54 @@ public DefaultXmlPayloadConverter(DocumentBuilderFactory documentBuilderFactory)

@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 {
return getDocumentBuilder().parse((File) object);
else if (object instanceof DOMSource) {
Node node = ((DOMSource) object).getNode();
if (node instanceof Document) {
return (Document) node;
}
else {
return nodeToDocument(node);
}
}
catch (Exception e) {
throw new MessagingException("failed to parse File payload '" + object + "'", e);
else if (object instanceof Source) {
InputSource inputSource = sourceToInputSource((Source) object);
return getDocumentBuilder().parse(inputSource);
}
}
if (object instanceof String) {
try {
else if (object instanceof File) {
return getDocumentBuilder().parse((File) object);
}
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
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;
Expand All @@ -35,6 +39,7 @@
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;

Expand All @@ -46,17 +51,17 @@
*/
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
Expand Down Expand Up @@ -99,7 +104,7 @@ public void testGetSourcePassingDocument() throws Exception {

@Test
public void testGetSourcePassingString() throws Exception {
Source source = converter.convertToSource(testDocumentAsString);
Source source = converter.convertToSource(TEST_DOCUMENT_AS_STRING);
assertEquals(StringSource.class, source.getClass());
}

Expand Down Expand Up @@ -143,4 +148,59 @@ public void testConvertSourceToDocument() throws Exception {
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);
}
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<test>hello</test>
Loading

0 comments on commit f74ddc2

Please sign in to comment.