Added DomUtils

This commit is contained in:
Arjen Poutsma
2007-04-11 12:46:39 +00:00
parent 84fce85a69
commit f07ea88b35
4 changed files with 148 additions and 3 deletions

View 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();
}
}

View File

@@ -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>

View File

@@ -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();