Added getDocument
This commit is contained in:
@@ -23,9 +23,13 @@ import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stax.StAXResult;
|
||||
import javax.xml.transform.stax.StAXSource;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.xml.JaxpVersion;
|
||||
|
||||
@@ -186,6 +190,22 @@ public abstract class TraxUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link Document} of the given {@link DOMSource}.
|
||||
*
|
||||
* @param source the DOM source
|
||||
* @return the document
|
||||
*/
|
||||
public static Document getDocument(DOMSource source) {
|
||||
Node node = source.getNode();
|
||||
if (node instanceof Document) {
|
||||
return (Document) node;
|
||||
}
|
||||
else {
|
||||
return node.getOwnerDocument();
|
||||
}
|
||||
}
|
||||
|
||||
/** Inner class to avoid a static JAXP 1.4 dependency. */
|
||||
private static class Jaxp14StaxHandler {
|
||||
|
||||
|
||||
@@ -18,6 +18,8 @@ package org.springframework.xml.transform;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
@@ -37,6 +39,8 @@ import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLTestCase;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
public class TraxUtilsTest extends XMLTestCase {
|
||||
|
||||
@@ -189,4 +193,15 @@ public class TraxUtilsTest extends XMLTestCase {
|
||||
|
||||
assertEquals("Invalid XMLEventWriter", eventWriter, TraxUtils.getXMLEventWriter(result));
|
||||
}
|
||||
|
||||
public void testGetDocument() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document document = documentBuilder.newDocument();
|
||||
assertSame("Invalid document", document, TraxUtils.getDocument(new DOMSource(document)));
|
||||
Element element = document.createElement("element");
|
||||
document.appendChild(element);
|
||||
assertSame("Invalid document", document, TraxUtils.getDocument(new DOMSource(element)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user