Skip to content

Commit

Permalink
Added DomUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
poutsma committed Apr 11, 2007
1 parent 84fce85 commit f07ea88
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 3 deletions.
65 changes: 65 additions & 0 deletions xml/src/main/java/org/springframework/xml/dom/DomUtils.java
Original file line number Diff line number Diff line change
@@ -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();
}


}
5 changes: 5 additions & 0 deletions xml/src/main/java/org/springframework/xml/dom/package.html
Original file line number Diff line number Diff line change
@@ -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>
6 changes: 3 additions & 3 deletions xml/src/main/java/org/springframework/xml/sax/SaxUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.xml.sax.InputSource;

/**
* Convenient utility methods for dealing with SAX.
*
* @author Arjen Poutsma
*/
public abstract class SaxUtils {
Expand All @@ -42,9 +44,7 @@ public static InputSource createInputSource(Resource resource) throws IOExceptio
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();
Expand Down
75 changes: 75 additions & 0 deletions xml/src/test/java/org/springframework/xml/dom/DomUtilsTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit f07ea88

Please sign in to comment.