Added DomUtils
This commit is contained in:
65
xml/src/main/java/org/springframework/xml/dom/DomUtils.java
Normal file
65
xml/src/main/java/org/springframework/xml/dom/DomUtils.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2007 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.dom;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Convenient utility methods for dealing with DOM.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public abstract class DomUtils {
|
||||
|
||||
/**
|
||||
* Returns the root element of the given source, transforming it if necessary.
|
||||
*
|
||||
* @param source the source to get the root element from
|
||||
* @param transformer a transformer
|
||||
* @return the root element
|
||||
*/
|
||||
public static Element getRootElement(Source source, Transformer transformer) throws TransformerException {
|
||||
if (source instanceof DOMSource) {
|
||||
DOMSource domSource = (DOMSource) source;
|
||||
Node node = domSource.getNode();
|
||||
if (node == null) {
|
||||
return null;
|
||||
}
|
||||
else if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
return (Element) node;
|
||||
}
|
||||
else if (node.getNodeType() == Node.DOCUMENT_NODE) {
|
||||
Document document = (Document) node;
|
||||
return document.getDocumentElement();
|
||||
}
|
||||
}
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(source, domResult);
|
||||
Document document = (Document) domResult.getNode();
|
||||
return document.getDocumentElement();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Provides classes that help with DOM: the Document Object Model. Mostly for internal use by the framework.
|
||||
</body>
|
||||
</html>
|
||||
@@ -22,6 +22,8 @@ import org.springframework.core.io.Resource;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
/**
|
||||
* Convenient utility methods for dealing with SAX.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public abstract class SaxUtils {
|
||||
@@ -42,9 +44,7 @@ public abstract class SaxUtils {
|
||||
return inputSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the URL from the given resource as System ID. Returns <code>null</code> if it cannot be openened.
|
||||
*/
|
||||
/** Retrieves the URL from the given resource as System ID. Returns <code>null</code> if it cannot be openened. */
|
||||
public static String getSystemId(Resource resource) {
|
||||
try {
|
||||
return resource.getURL().toString();
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2007 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.dom;
|
||||
|
||||
import java.io.StringReader;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
public class DomUtilsTest extends TestCase {
|
||||
|
||||
private Transformer transformer;
|
||||
|
||||
private static final String NAMESPACE = "http://springframework.org/spring-ws";
|
||||
|
||||
private static final String LOCAL_NAME = "Root";
|
||||
|
||||
private static final String XML = "<" + LOCAL_NAME + " xmlns='" + NAMESPACE + "'/>";
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
transformer = TransformerFactory.newInstance().newTransformer();
|
||||
}
|
||||
|
||||
public void testGetRootElementDomSource() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document document = documentBuilder.newDocument();
|
||||
Element rootElement = document.createElementNS(NAMESPACE, LOCAL_NAME);
|
||||
document.appendChild(rootElement);
|
||||
|
||||
testSource(new DOMSource(document));
|
||||
}
|
||||
|
||||
public void testGetRootElementSaxSource() throws Exception {
|
||||
InputSource inputSource = new InputSource(new StringReader(XML));
|
||||
testSource(new SAXSource(inputSource));
|
||||
}
|
||||
|
||||
public void testGetRootElementStreamSource() throws Exception {
|
||||
testSource(new StreamSource(new StringReader(XML)));
|
||||
}
|
||||
|
||||
private void testSource(Source source) throws TransformerException {
|
||||
Element result = DomUtils.getRootElement(source, transformer);
|
||||
assertNotNull("No result", result);
|
||||
assertEquals("Invalid namespace", NAMESPACE, result.getNamespaceURI());
|
||||
assertEquals("Invalid local name", LOCAL_NAME, result.getLocalName());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user