Refactored DomUtils into relevant classes.
This commit is contained in:
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* 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.TransformerFactory;
|
||||
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 transformerFactory a transformer factory, necessary if the given source is not a <code>DOMSource</code>
|
||||
* @return the root element
|
||||
*/
|
||||
public static Element getRootElement(Source source, TransformerFactory transformerFactory)
|
||||
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();
|
||||
}
|
||||
}
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(source, domResult);
|
||||
Document document = (Document) domResult.getNode();
|
||||
return document.getDocumentElement();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -17,19 +17,30 @@
|
||||
package org.springframework.xml.namespace;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.XMLStreamConstants;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
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.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Helper class for using <code>javax.xml.namespace.QName</code>.
|
||||
* Helper class for using {@link QName}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see javax.xml.namespace.QName
|
||||
*/
|
||||
public abstract class QNameUtils {
|
||||
|
||||
/** Indicates whether {@link QName} has a prefix. The first release of the class did not have this. */
|
||||
private static boolean qNameHasPrefix;
|
||||
|
||||
static {
|
||||
@@ -51,7 +62,7 @@ public abstract class QNameUtils {
|
||||
* @param localPart local part of the <code>QName</code>
|
||||
* @param prefix prefix of the <code>QName</code>. May be ignored.
|
||||
* @return the created <code>QName</code>
|
||||
* @see QName#QName(String, String, String)
|
||||
* @see QName#QName(String,String,String)
|
||||
*/
|
||||
public static QName createQName(String namespaceUri, String localPart, String prefix) {
|
||||
if (qNameHasPrefix) {
|
||||
@@ -117,6 +128,44 @@ public abstract class QNameUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the root qualified name of the given source, transforming it if necessary.
|
||||
*
|
||||
* @param source the source to get the root element from
|
||||
* @param transformerFactory a transformer factory, necessary if the given source is not a <code>DOMSource</code>
|
||||
* @return the root element
|
||||
*/
|
||||
public static QName getQNameForSource(Source source, TransformerFactory transformerFactory)
|
||||
throws TransformerException {
|
||||
if (source instanceof DOMSource) {
|
||||
DOMSource domSource = (DOMSource) source;
|
||||
Node node = domSource.getNode();
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
return getQNameForNode(node);
|
||||
}
|
||||
else if (node.getNodeType() == Node.DOCUMENT_NODE) {
|
||||
Document document = (Document) node;
|
||||
return getQNameForNode(document.getDocumentElement());
|
||||
}
|
||||
}
|
||||
else if (source instanceof StaxSource) {
|
||||
StaxSource staxSource = (StaxSource) source;
|
||||
if (staxSource.getXMLStreamReader() != null) {
|
||||
XMLStreamReader streamReader = staxSource.getXMLStreamReader();
|
||||
if (streamReader.getEventType() == XMLStreamConstants.START_ELEMENT ||
|
||||
streamReader.getEventType() == XMLStreamConstants.END_ELEMENT) {
|
||||
return streamReader.getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
// we have no other option than to transform
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(source, domResult);
|
||||
Document document = (Document) domResult.getNode();
|
||||
return getQNameForNode(document.getDocumentElement());
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a <code>QName</code> to a qualified name, as used by DOM and SAX. The returned string has a format of
|
||||
* <code>prefix:localName</code> if the prefix is set, or just <code>localName</code> if not.
|
||||
@@ -185,5 +234,4 @@ public abstract class QNameUtils {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -18,9 +18,14 @@ package org.springframework.xml.xpath;
|
||||
|
||||
import java.util.Properties;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.springframework.xml.transform.TransformerObjectSupport;
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
@@ -61,4 +66,19 @@ public abstract class AbstractXPathTemplate extends TransformerObjectSupport imp
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the root element of the given source.
|
||||
*
|
||||
* @param source the source to get the root element from
|
||||
* @return the root element
|
||||
*/
|
||||
protected Element getRootElement(Source source) throws TransformerException {
|
||||
Transformer transformer = createTransformer();
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(source, domResult);
|
||||
Document document = (Document) domResult.getNode();
|
||||
return document.getDocumentElement();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ import org.jaxen.JaxenException;
|
||||
import org.jaxen.SimpleNamespaceContext;
|
||||
import org.jaxen.XPath;
|
||||
import org.jaxen.dom.DOMXPath;
|
||||
import org.springframework.xml.dom.DomUtils;
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
@@ -43,7 +42,7 @@ public class JaxenXPathTemplate extends AbstractXPathTemplate {
|
||||
public boolean evaluateAsBoolean(String expression, Source context) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
Element element = getRootElement(context);
|
||||
return xpath.booleanValueOf(element);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
@@ -57,7 +56,7 @@ public class JaxenXPathTemplate extends AbstractXPathTemplate {
|
||||
public Node evaluateAsNode(String expression, Source context) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
Element element = getRootElement(context);
|
||||
return (Node) xpath.selectSingleNode(element);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
@@ -71,7 +70,7 @@ public class JaxenXPathTemplate extends AbstractXPathTemplate {
|
||||
public List evaluateAsNodeList(String expression, Source context) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
Element element = getRootElement(context);
|
||||
return xpath.selectNodes(element);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
@@ -85,7 +84,7 @@ public class JaxenXPathTemplate extends AbstractXPathTemplate {
|
||||
public double evaluateAsDouble(String expression, Source context) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
Element element = getRootElement(context);
|
||||
return xpath.numberValueOf(element).doubleValue();
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
@@ -99,7 +98,7 @@ public class JaxenXPathTemplate extends AbstractXPathTemplate {
|
||||
public String evaluateAsString(String expression, Source context) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
Element element = getRootElement(context);
|
||||
return xpath.stringValueOf(element);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
@@ -113,7 +112,7 @@ public class JaxenXPathTemplate extends AbstractXPathTemplate {
|
||||
public Object evaluateAsObject(String expression, Source context, NodeMapper nodeMapper) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
Element element = getRootElement(context);
|
||||
Node node = (Node) xpath.selectSingleNode(element);
|
||||
if (node != null) {
|
||||
try {
|
||||
@@ -139,7 +138,7 @@ public class JaxenXPathTemplate extends AbstractXPathTemplate {
|
||||
public List evaluate(String expression, Source context, NodeMapper nodeMapper) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
Element element = getRootElement(context);
|
||||
List nodes = (List) xpath.selectNodes(element);
|
||||
List results = new ArrayList(nodes.size());
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
|
||||
@@ -29,7 +29,6 @@ import javax.xml.xpath.XPathConstants;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
import javax.xml.xpath.XPathFactoryConfigurationException;
|
||||
|
||||
import org.springframework.xml.dom.DomUtils;
|
||||
import org.springframework.xml.namespace.SimpleNamespaceContext;
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
import org.w3c.dom.DOMException;
|
||||
@@ -128,7 +127,8 @@ public class Jaxp13XPathTemplate extends AbstractXPathTemplate {
|
||||
}
|
||||
try {
|
||||
if (context instanceof StaxSource) {
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
// StaxSource is a subclass of SAXSource, but it has no InputSource, therefore we handle it differently
|
||||
Element element = getRootElement(context);
|
||||
return xpath.evaluate(expression, element, returnType);
|
||||
}
|
||||
else if (context instanceof SAXSource) {
|
||||
|
||||
Reference in New Issue
Block a user