You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by an...@apache.org on 2006/04/22 23:46:16 UTC
svn commit: r396166 -
/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/xml/dom/DOMUtil.java
Author: antonio
Date: Sat Apr 22 14:46:14 2006
New Revision: 396166
URL: http://svn.apache.org/viewcvs?rev=396166&view=rev
Log:
Merging from branch 2.1.
Modified:
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/xml/dom/DOMUtil.java
Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/xml/dom/DOMUtil.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/xml/dom/DOMUtil.java?rev=396166&r1=396165&r2=396166&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/xml/dom/DOMUtil.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/xml/dom/DOMUtil.java Sat Apr 22 14:46:14 2006
@@ -49,21 +49,25 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
+import java.util.StringTokenizer;
/**
- * This class is a utility class for miscellaneous DOM functions, like
- * getting and setting values of nodes.
- *
+ * This class is a utility class for miscellaneous DOM functions, like getting
+ * and setting values of nodes.
+ *
* @version $Id$
*/
public final class DOMUtil {
+ private static final String XPATH_IS_REQUIRED = "XPath is required.";
+
/**
- * Get the owner of the DOM document belonging to the node.
- * This works even if the node is the document itself.
- *
- * @param node The node.
- * @return The corresponding document.
+ * Get the owner of the DOM document belonging to the node. This works even
+ * if the node is the document itself.
+ *
+ * @param node
+ * The node.
+ * @return The corresponding document.
*/
public static Document getOwnerDocument(Node node) {
if (node.getNodeType() == Node.DOCUMENT_NODE) {
@@ -74,18 +78,19 @@
}
/**
- * Get the value of the node specified by the XPath.
- * This works similar to xsl:value-of. If the node does not exist <CODE>null</CODE>
- * is returned.
- *
- * @param root The node to start the search.
- * @param path XPath search expression.
- * @return The value of the node or <CODE>null</CODE>
+ * Get the value of the node specified by the XPath. This works similar to
+ * <xsl:value-of>. If the node does not exist <CODE>null</CODE> is
+ * returned.
+ *
+ * @param root
+ * The node to start the search.
+ * @param path
+ * XPath search expression.
+ * @return The value of the node or <CODE>null</CODE>
*/
- public static String getValueOfNode(XPathProcessor processor, Node root, String path)
- throws ProcessingException {
+ public static String getValueOfNode(XPathProcessor processor, Node root, String path) throws ProcessingException {
if (path == null) {
- throw new ProcessingException("Not a valid XPath: " + path);
+ throw new ProcessingException(XPATH_IS_REQUIRED);
}
if (root != null) {
path = StringUtils.strip(path, "/");
@@ -94,25 +99,24 @@
return getValueOfNode(node);
}
}
- return null;
+ return null;
}
/**
- * Get the value of the node specified by the XPath.
- * This works similar to xsl:value-of. If the node is not found
- * the <CODE>defaultValue</CODE> is returned.
- *
- * @param root The node to start the search.
- * @param path XPath search expression.
- * @param defaultValue The default value if the node does not exist.
- * @return The value of the node or <CODE>defaultValue</CODE>
- */
- public static String getValueOfNode(
- XPathProcessor processor,
- Node root,
- String path,
- String defaultValue)
- throws ProcessingException {
+ * Get the value of the node specified by the XPath. This works similar to
+ * <xsl:value-of>. If the node is not found the <CODE>defaultValue</CODE>
+ * is returned.
+ *
+ * @param root
+ * The node to start the search.
+ * @param path
+ * XPath search expression.
+ * @param defaultValue
+ * The default value if the node does not exist.
+ * @return The value of the node or <CODE>defaultValue</CODE>
+ */
+ public static String getValueOfNode(XPathProcessor processor, Node root, String path, String defaultValue)
+ throws ProcessingException {
String value = getValueOfNode(processor, root, path);
if (value == null)
value = defaultValue;
@@ -121,18 +125,21 @@
}
/**
- * Get the boolean value of the node specified by the XPath.
- * This works similar to xsl:value-of. If the node exists and has a value
- * this value is converted to a boolean, e.g. "true" or "false" as value
- * will result into the corresponding boolean values.
- *
- * @param root The node to start the search.
- * @param path XPath search expression.
- * @return The boolean value of the node.
- * @throws ProcessingException If the node is not found.
+ * Get the boolean value of the node specified by the XPath. This works
+ * similar to <xsl:value-of>. If the node exists and has a value this
+ * value is converted to a boolean, e.g. "true" or "false" as value will
+ * result into the corresponding boolean values.
+ *
+ * @param root
+ * The node to start the search.
+ * @param path
+ * XPath search expression.
+ * @return The boolean value of the node.
+ * @throws ProcessingException
+ * If the node is not found.
*/
public static boolean getValueOfNodeAsBoolean(XPathProcessor processor, Node root, String path)
- throws ProcessingException {
+ throws ProcessingException {
String value = getValueOfNode(processor, root, path);
if (value == null) {
throw new ProcessingException("No such node: " + path);
@@ -141,22 +148,22 @@
}
/**
- * Get the boolean value of the node specified by the XPath.
- * This works similar to xsl:value-of. If the node exists and has a value
- * this value is converted to a boolean, e.g. "true" or "false" as value
- * will result into the corresponding boolean values.
- * If the node does not exist, the <CODE>defaultValue</CODE> is returned.
- *
- * @param root The node to start the search.
- * @param path XPath search expression.
- * @param defaultValue Default boolean value.
- * @return The value of the node or <CODE>defaultValue</CODE>
- */
- public static boolean getValueOfNodeAsBoolean(XPathProcessor processor,
- Node root,
- String path,
- boolean defaultValue)
- throws ProcessingException {
+ * Get the boolean value of the node specified by the XPath. This works
+ * similar to <xsl:value-of>. If the node exists and has a value this
+ * value is converted to a boolean, e.g. "true" or "false" as value will
+ * result into the corresponding boolean values. If the node does not exist,
+ * the <CODE>defaultValue</CODE> is returned.
+ *
+ * @param root
+ * The node to start the search.
+ * @param path
+ * XPath search expression.
+ * @param defaultValue
+ * Default boolean value.
+ * @return The value of the node or <CODE>defaultValue</CODE>
+ */
+ public static boolean getValueOfNodeAsBoolean(XPathProcessor processor, Node root, String path, boolean defaultValue)
+ throws ProcessingException {
String value = getValueOfNode(processor, root, path);
if (value != null) {
return BooleanUtils.toBoolean(value);
@@ -165,9 +172,9 @@
}
/**
- * Get the value of the DOM node.
- * The value of a node is the content of the first text node.
- * If the node has no text nodes, <code>null</code> is returned.
+ * Get the value of the DOM node. The value of a node is the content of the
+ * first text node. If the node has no text nodes, <code>null</code> is
+ * returned.
*/
public static String getValueOfNode(Node node) {
if (node != null) {
@@ -191,25 +198,23 @@
}
/**
- * Get the value of the node.
- * The value of the node is the content of the first text node.
- * If the node has no text nodes the <CODE>defaultValue</CODE> is
- * returned.
+ * Get the value of the node. The value of the node is the content of the
+ * first text node. If the node has no text nodes the <CODE>defaultValue</CODE>
+ * is returned.
*/
public static String getValueOfNode(Node node, String defaultValue) {
return StringUtils.defaultString(getValueOfNode(node), defaultValue);
}
/**
- * Set the value of the DOM node.
- * All current children of the node are removed and a new text node
- * with the value is appended.
+ * Set the value of the DOM node. All current children of the node are
+ * removed and a new text node with the value is appended.
*/
public static void setValueOfNode(Node node, String value) {
if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
node.setNodeValue(value);
} else {
- while (node.hasChildNodes() == true) {
+ while (node.hasChildNodes()) {
node.removeChild(node.getFirstChild());
}
node.appendChild(node.getOwnerDocument().createTextNode(value));
@@ -218,16 +223,16 @@
/** XML definition for a document */
private static final String XML_DEFINITION = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
+
private static final String XML_ROOT_DEFINITION = XML_DEFINITION + "<root>";
/**
- * Get a document fragment from a <code>Reader</code>.
- * The reader must provide valid XML, but it is allowed that the XML
- * has more than one root node. This xml is parsed by the
- * specified parser instance and a DOM DocumentFragment is created.
+ * Get a document fragment from a <code>Reader</code>. The reader must
+ * provide valid XML, but it is allowed that the XML has more than one root
+ * node. This xml is parsed by the specified parser instance and a DOM
+ * DocumentFragment is created.
*/
- public static DocumentFragment getDocumentFragment(SAXParser parser, Reader stream)
- throws ProcessingException {
+ public static DocumentFragment getDocumentFragment(SAXParser parser, Reader stream) throws ProcessingException {
DocumentFragment frag = null;
Writer writer;
@@ -254,7 +259,7 @@
// now test if xml input start with <?xml
String xml = writer.toString();
String searchString = XML_ROOT_DEFINITION + "<?xml ";
- if (xml.startsWith(searchString) == true) {
+ if (xml.startsWith(searchString)) {
// now remove the surrounding root element
xml = xml.substring(XML_ROOT_DEFINITION.length(), xml.length() - 7);
removeRoot = false;
@@ -284,7 +289,7 @@
frag.appendChild(root);
} else {
Node child;
- while (root.hasChildNodes() == true) {
+ while (root.hasChildNodes()) {
child = root.getFirstChild();
root.removeChild(child);
frag.appendChild(child);
@@ -299,13 +304,11 @@
}
/**
- * Create a parameter object from xml.
- * The xml is flat and consists of elements which all have exactly one text node:
- * <parone>value_one<parone>
- * <partwo>value_two<partwo>
- * A parameter can occur more than once with different values.
- * If <CODE>source</CODE> is not specified a new paramter object is created
- * otherwise the parameters are added to source.
+ * Create a parameter object from xml. The xml is flat and consists of
+ * elements which all have exactly one text node: <parone>value_one<parone>
+ * <partwo>value_two<partwo> A parameter can occur more than once with
+ * different values. If <CODE>source</CODE> is not specified a new
+ * parameter object is created otherwise the parameters are added to source.
*/
public static SourceParameters createParameters(Node fragment, SourceParameters source) {
SourceParameters par = (source == null ? new SourceParameters() : source);
@@ -346,8 +349,8 @@
}
/**
- * Create a string from a DOM document fragment.
- * Only the top level text nodes are chained together to build the text.
+ * Create a string from a DOM document fragment. Only the top level text
+ * nodes are chained together to build the text.
*/
public static String createText(DocumentFragment fragment) {
StringBuffer value = new StringBuffer();
@@ -372,10 +375,9 @@
}
/**
- * Compare all attributes of two elements.
- * This method returns true only if both nodes have the same number of
- * attributes and the same attributes with equal values.
- * Namespace definition nodes are ignored
+ * Compare all attributes of two elements. This method returns true only if
+ * both nodes have the same number of attributes and the same attributes
+ * with equal values. Namespace definition nodes are ignored
*/
public static boolean compareAttributes(Element first, Element second) {
NamedNodeMap attr1 = first.getAttributes();
@@ -389,14 +391,14 @@
if (attr1Len > 0) {
int l = attr1.getLength();
for (int i = 0; i < l; i++) {
- if (attr1.item(i).getNodeName().startsWith("xmlns:") == true)
+ if (attr1.item(i).getNodeName().startsWith("xmlns:"))
attr1Len--;
}
}
if (attr2Len > 0) {
int l = attr2.getLength();
for (int i = 0; i < l; i++) {
- if (attr2.item(i).getNodeName().startsWith("xmlns:") == true)
+ if (attr2.item(i).getNodeName().startsWith("xmlns:"))
attr2Len--;
}
}
@@ -409,13 +411,13 @@
l2 = attr2.getLength();
boolean ok = true;
// each attribute of first must be in second with the same value
- while (i < l && ok == true) {
+ while (i < l && ok) {
value = attr1.item(i).getNodeName();
if (value.startsWith("xmlns:") == false) {
ok = false;
m = 0;
while (m < l2 && ok == false) {
- if (attr2.item(m).getNodeName().equals(value) == true) {
+ if (attr2.item(m).getNodeName().equals(value)) {
// same name, same value?
ok = attr1.item(i).getNodeValue().equals(attr2.item(m).getNodeValue());
}
@@ -429,11 +431,13 @@
}
/**
- * Implementation for <code>String</code> :
- * outputs characters representing the value.
- *
- * @param parent The node getting the value
- * @param text the value
+ * Implementation for <code>String</code> : outputs characters
+ * representing the value.
+ *
+ * @param parent
+ * The node getting the value
+ * @param text
+ * the value
*/
public static void valueOf(Node parent, String text) throws ProcessingException {
if (text != null) {
@@ -442,11 +446,13 @@
}
/**
- * Implementation for <code>XMLizable</code> :
- * outputs the value by calling <code>v.toSax(contentHandler)</code>.
- *
- * @param parent The node getting the value
- * @param v the XML fragment
+ * Implementation for <code>XMLizable</code> : outputs the value by
+ * calling <code>v.toSax(contentHandler)</code>.
+ *
+ * @param parent
+ * The node getting the value
+ * @param v
+ * the XML fragment
*/
public static void valueOf(Node parent, XMLizable v) throws ProcessingException {
if (v != null) {
@@ -460,11 +466,13 @@
}
/**
- * Implementation for <code>org.w3c.dom.Node</code> :
- * converts the Node to a SAX event stream.
- *
- * @param parent The node getting the value
- * @param v the value
+ * Implementation for <code>org.w3c.dom.Node</code> : converts the Node to
+ * a SAX event stream.
+ *
+ * @param parent
+ * The node getting the value
+ * @param v
+ * the value
*/
public static void valueOf(Node parent, Node v) throws ProcessingException {
if (v != null) {
@@ -473,12 +481,14 @@
}
/**
- * Implementation for <code>java.util.Collection</code> :
- * outputs the value by calling {@link #valueOf(Node, Object)} on each element of the
+ * Implementation for <code>java.util.Collection</code> : outputs the
+ * value by calling {@link #valueOf(Node, Object)} on each element of the
* collection.
- *
- * @param parent The node getting the value
- * @param v the XML fragment
+ *
+ * @param parent
+ * The node getting the value
+ * @param v
+ * the XML fragment
*/
public static void valueOf(Node parent, Collection v) throws ProcessingException {
if (v != null) {
@@ -490,13 +500,15 @@
}
/**
- * Implementation for <code>java.util.Map</code> :
- * For each entry an element is created with the childs key and value
- * Outputs the value and the key by calling {@link #valueOf(Node, Object)}
- * on each value and key of the Map.
- *
- * @param parent The node getting the value
- * @param v the Map
+ * Implementation for <code>java.util.Map</code> : For each entry an
+ * element is created with the childs key and value Outputs the value and
+ * the key by calling {@link #valueOf(Node, Object)} on each value and key
+ * of the Map.
+ *
+ * @param parent
+ * The node getting the value
+ * @param v
+ * the Map
*/
public static void valueOf(Node parent, Map v) throws ProcessingException {
if (v != null) {
@@ -522,13 +534,17 @@
/**
* Implementation for <code>Object</code> depending on its class :
* <ul>
- * <li>if it's an array, call {@link #valueOf(Node, Object)} on all its elements,</li>
- * <li>if it's class has a specific {@link #valueOf(Node, Object)} implementation, use it,</li>
+ * <li>if it's an array, call {@link #valueOf(Node, Object)} on all its
+ * elements,</li>
+ * <li>if it's class has a specific {@link #valueOf(Node, Object)}
+ * implementation, use it,</li>
* <li>else, output it's string representation.</li>
* </ul>
- *
- * @param parent The node getting the value
- * @param v the value
+ *
+ * @param parent
+ * The node getting the value
+ * @param v
+ * the value
*/
public static void valueOf(Node parent, Object v) throws ProcessingException {
if (v == null) {
@@ -576,20 +592,22 @@
}
/**
- * Use an XPath string to select a single node. XPath namespace
- * prefixes are resolved from the context node, which may not
- * be what you want (see the next method).
- *
- * @param contextNode The node to start searching from.
- * @param str A valid XPath string.
- * @param processor The XPath processor to use
+ * Use an XPath string to select a single node. XPath namespace prefixes are
+ * resolved from the context node, which may not be what you want (see the
+ * next method).
+ *
+ * @param contextNode
+ * The node to start searching from.
+ * @param str
+ * A valid XPath string.
+ * @param processor
+ * The XPath processor to use
* @return The first node found that matches the XPath, or null.
- *
+ *
* @throws TransformerException
*/
- public static Node getSingleNode(Node contextNode, String str,
- XPathProcessor processor)
- throws TransformerException {
+ public static Node getSingleNode(Node contextNode, String str, XPathProcessor processor)
+ throws TransformerException {
String[] pathComponents = buildPathArray(str);
if (pathComponents == null) {
return processor.selectSingleNode(contextNode, str);
@@ -600,39 +618,42 @@
/**
* Return the <CODE>Node</CODE> from the DOM Node <CODE>rootNode</CODE>
- * using the XPath expression <CODE>path</CODE>.
- * If the node does not exist, it is created and then returned.
- * This is a very simple method for creating new nodes. If the
- * XPath contains selectors ([,,,]) or "*" it is of course not
- * possible to create the new node. So if you use such XPaths
- * the node must exist beforehand.
- * An simple exception is if the expression contains attribute
- * test to values (e.g. [@id = 'du' and @number = 'you'],
- * the attributes with the given values are added. The attributes
- * must be separated with 'and'.
- * Another problem are namespaces: XPath requires sometimes selectors for
- * namespaces, e.g. : /*[namespace-uri()="uri" and local-name()="name"]
- * Creating such a node with a namespace is not possible right now as we use
- * a very simple XPath parser which is not able to parse all kinds of selectors
- * correctly.
- *
- * @param rootNode The node to start the search.
- * @param path XPath expression for searching the node.
- * @param processor The XPath processor to use
- * @return The node specified by the path.
- * @throws ProcessingException If no path is specified or the XPath engine fails.
+ * using the XPath expression <CODE>path</CODE>. If the node does not
+ * exist, it is created and then returned. This is a very simple method for
+ * creating new nodes. If the XPath contains selectors ([,,,]) or "*" it is
+ * of course not possible to create the new node. So if you use such XPaths
+ * the node must exist beforehand. An simple exception is if the expression
+ * contains attribute test to values (e.g. [@id = 'du' and
+ *
+ * @number = 'you'], the attributes with the given values are added. The
+ * attributes must be separated with 'and'. Another problem are
+ * namespaces: XPath requires sometimes selectors for namespaces,
+ * e.g. : /*[namespace-uri()="uri" and local-name()="name"] Creating
+ * such a node with a namespace is not possible right now as we use
+ * a very simple XPath parser which is not able to parse all kinds
+ * of selectors correctly.
+ *
+ * @param rootNode
+ * The node to start the search.
+ * @param path
+ * XPath expression for searching the node.
+ * @param processor
+ * The XPath processor to use
+ * @return The node specified by the path.
+ * @throws ProcessingException
+ * If no path is specified or the XPath engine fails.
*/
public static Node selectSingleNode(Node rootNode, String path, XPathProcessor processor)
- throws ProcessingException {
+ throws ProcessingException {
// Now we have to parse the string
- // First test: path? rootNode?
+ // First test: path? rootNode?
if (path == null) {
- throw new ProcessingException("XPath is required.");
+ throw new ProcessingException(XPATH_IS_REQUIRED);
}
if (rootNode == null)
return rootNode;
- if (path.length() == 0 || path.equals("/") == true)
+ if (path.length() == 0 || path.equals("/"))
return rootNode;
// now the first "quick" test is if the node exists using the
@@ -641,13 +662,9 @@
Node testNode = getSingleNode(rootNode, path, processor);
if (testNode != null)
return testNode;
- } catch (javax.xml.transform.TransformerException local) {
- throw new ProcessingException(
- "Transforming exception during selectSingleNode with path: '"
- + path
- + "'. Exception: "
- + local,
- local);
+ } catch (TransformerException local) {
+ throw new ProcessingException("Transforming exception during selectSingleNode with path: '" + path
+ + "'. Exception: " + local, local);
}
// remove leading "/" oon both ends
@@ -675,17 +692,16 @@
}
// test for attribute spec
- if (nodeName.startsWith("@") == true) {
+ if (nodeName.startsWith("@")) {
isAttribute = true;
}
Node singleNode;
try {
singleNode = getSingleNode(parent, nodeName, processor);
- } catch (javax.xml.transform.TransformerException localException) {
- throw new ProcessingException(
- "XPathUtil.selectSingleNode: " + localException.getMessage(),
- localException);
+ } catch (TransformerException localException) {
+ throw new ProcessingException("XPathUtil.selectSingleNode: " + localException.getMessage(),
+ localException);
}
// create node if necessary
@@ -698,26 +714,19 @@
XPathExp = nodeName.substring(posSelect + 1, nodeName.length() - 1);
nodeName = nodeName.substring(0, posSelect);
}
- if (isAttribute == true) {
+ if (isAttribute) {
try {
- newNode =
- getOwnerDocument(rootNode).createAttributeNS(
- null,
- nodeName.substring(1));
+ newNode = getOwnerDocument(rootNode).createAttributeNS(null, nodeName.substring(1));
((Element) parent).setAttributeNodeNS((org.w3c.dom.Attr) newNode);
parent = newNode;
} catch (DOMException local) {
- throw new ProcessingException(
- "Unable to create new DOM node: '" + nodeName + "'.",
- local);
+ throw new ProcessingException("Unable to create new DOM node: '" + nodeName + "'.", local);
}
} else {
try {
newNode = getOwnerDocument(rootNode).createElementNS(null, nodeName);
} catch (DOMException local) {
- throw new ProcessingException(
- "Unable to create new DOM node: '" + nodeName + "'.",
- local);
+ throw new ProcessingException("Unable to create new DOM node: '" + nodeName + "'.", local);
}
if (XPathExp != null) {
java.util.List attrValuePairs = new java.util.ArrayList(4);
@@ -726,12 +735,11 @@
String attr;
String value;
// scan for attributes
- java.util.StringTokenizer tokenizer =
- new java.util.StringTokenizer(XPathExp, "= ");
- while (tokenizer.hasMoreTokens() == true) {
+ StringTokenizer tokenizer = new StringTokenizer(XPathExp, "= ");
+ while (tokenizer.hasMoreTokens()) {
attr = tokenizer.nextToken();
- if (attr.startsWith("@") == true) {
- if (tokenizer.hasMoreTokens() == true) {
+ if (attr.startsWith("@")) {
+ if (tokenizer.hasMoreTokens()) {
value = tokenizer.nextToken();
if (value.startsWith("'") && value.endsWith("'"))
value = value.substring(1, value.length() - 1);
@@ -746,12 +754,10 @@
noError = false;
}
}
- if (noError == true) {
+ if (noError) {
for (int l = 0; l < attrValuePairs.size(); l = l + 2) {
- ((Element) newNode).setAttributeNS(
- null,
- (String) attrValuePairs.get(l),
- (String) attrValuePairs.get(l + 1));
+ ((Element) newNode).setAttributeNS(null, (String) attrValuePairs.get(l),
+ (String) attrValuePairs.get(l + 1));
}
}
}
@@ -767,19 +773,21 @@
}
/**
- * Get the value of the node specified by the XPath.
- * This works similar to xsl:value-of. If the node does not exist <CODE>null</CODE>
- * is returned.
- *
- * @param root The node to start the search.
- * @param path XPath search expression.
- * @param processor The XPath processor to use
- * @return The value of the node or <CODE>null</CODE>
+ * Get the value of the node specified by the XPath. This works similar to
+ * <xsl:value-of>. If the node does not exist <CODE>null</CODE> is
+ * returned.
+ *
+ * @param root
+ * The node to start the search.
+ * @param path
+ * XPath search expression.
+ * @param processor
+ * The XPath processor to use
+ * @return The value of the node or <CODE>null</CODE>
*/
- public static String getValueOf(Node root, String path,
- XPathProcessor processor) throws ProcessingException {
+ public static String getValueOf(Node root, String path, XPathProcessor processor) throws ProcessingException {
if (path == null) {
- throw new ProcessingException("Not a valid XPath: " + path);
+ throw new ProcessingException(XPATH_IS_REQUIRED);
}
if (root == null)
return null;
@@ -790,10 +798,8 @@
if (node != null) {
return getValueOfNode(node);
}
- } catch (javax.xml.transform.TransformerException localException) {
- throw new ProcessingException(
- "XPathUtil.selectSingleNode: " + localException.getMessage(),
- localException);
+ } catch (TransformerException localException) {
+ throw new ProcessingException("XPathUtil.selectSingleNode: " + localException.getMessage(), localException);
}
return null;
}
@@ -802,16 +808,19 @@
* Get the value of the node specified by the XPath.
* This works similar to xsl:value-of. If the node is not found
* the <CODE>defaultValue</CODE> is returned.
- *
- * @param root The node to start the search.
- * @param path XPath search expression.
- * @param defaultValue The default value if the node does not exist.
- * @param processor The XPath Processor
- * @return The value of the node or <CODE>defaultValue</CODE>
- */
- public static String getValueOf(Node root, String path, String defaultValue,
- XPathProcessor processor)
- throws ProcessingException {
+ *
+ * @param root
+ * The node to start the search.
+ * @param path
+ * XPath search expression.
+ * @param defaultValue
+ * The default value if the node does not exist.
+ * @param processor
+ * The XPath Processor
+ * @return The value of the node or <CODE>defaultValue</CODE>
+ */
+ public static String getValueOf(Node root, String path, String defaultValue, XPathProcessor processor)
+ throws ProcessingException {
String value = getValueOf(root, path, processor);
if (value == null) {
value = defaultValue;
@@ -824,16 +833,19 @@
* This works similar to xsl:value-of. If the node exists and has a value
* this value is converted to a boolean, e.g. "true" or "false" as value
* will result into the corresponding boolean values.
- *
- * @param root The node to start the search.
- * @param path XPath search expression.
- * @param processor The XPath Processor
- * @return The boolean value of the node.
- * @throws ProcessingException If the node is not found.
- */
- public static boolean getValueAsBooleanOf(Node root, String path,
- XPathProcessor processor)
- throws ProcessingException {
+ *
+ * @param root
+ * The node to start the search.
+ * @param path
+ * XPath search expression.
+ * @param processor
+ * The XPath Processor
+ * @return The boolean value of the node.
+ * @throws ProcessingException
+ * If the node is not found.
+ */
+ public static boolean getValueAsBooleanOf(Node root, String path, XPathProcessor processor)
+ throws ProcessingException {
String value = getValueOf(root, path, processor);
if (value == null) {
throw new ProcessingException("No such node: " + path);
@@ -847,16 +859,19 @@
* this value is converted to a boolean, e.g. "true" or "false" as value
* will result into the corresponding boolean values.
* If the node does not exist, the <CODE>defaultValue</CODE> is returned.
- *
- * @param root The node to start the search.
- * @param path XPath search expression.
- * @param defaultValue Default boolean value.
- * @param processor The XPath Processor
- * @return The value of the node or <CODE>defaultValue</CODE>
- */
- public static boolean getValueAsBooleanOf(Node root, String path, boolean defaultValue,
- XPathProcessor processor)
- throws ProcessingException {
+ *
+ * @param root
+ * The node to start the search.
+ * @param path
+ * XPath search expression.
+ * @param defaultValue
+ * Default boolean value.
+ * @param processor
+ * The XPath Processor
+ * @return The value of the node or <CODE>defaultValue</CODE>
+ */
+ public static boolean getValueAsBooleanOf(Node root, String path, boolean defaultValue, XPathProcessor processor)
+ throws ProcessingException {
String value = getValueOf(root, path, processor);
if (value != null) {
return Boolean.valueOf(value).booleanValue();
@@ -882,27 +897,29 @@
/**
* Use an XPath string to select a nodelist.
* XPath namespace prefixes are resolved from the contextNode.
- *
- * @param contextNode The node to start searching from.
- * @param str A valid XPath string.
- * @param processor The XPath Processor
- * @return A NodeIterator, should never be null.
- *
+ *
+ * @param contextNode
+ * The node to start searching from.
+ * @param str
+ * A valid XPath string.
+ * @param processor
+ * The XPath Processor
+ * @return A NodeIterator, should never be null.
+ *
* @throws TransformerException
*/
public static NodeList selectNodeList(Node contextNode, String str, XPathProcessor processor)
- throws TransformerException {
+ throws TransformerException {
String[] pathComponents = buildPathArray(str);
if (pathComponents != null) {
return getNodeListFromPath(contextNode, pathComponents);
}
- return processor.selectNodeList(contextNode, str);
+ return processor.selectNodeList(contextNode, str);
}
/**
- * Build the input for the get...FromPath methods. If the XPath
- * expression cannot be handled by the methods, <code>null</code>
- * is returned.
+ * Build the input for the get...FromPath methods. If the XPath expression
+ * cannot be handled by the methods, <code>null</code> is returned.
*/
public static String[] buildPathArray(String xpath) {
String[] result = null;
@@ -915,25 +932,25 @@
i = 0;
while (i < l && found == false) {
switch (xpath.charAt(i)) {
- case '[' :
- found = true;
- break;
- case '(' :
- found = true;
- break;
- case '*' :
- found = true;
- break;
- case '@' :
- found = true;
- break;
- case ':' :
- found = true;
- break;
- case '/' :
- components++;
- default :
- i++;
+ case '[':
+ found = true;
+ break;
+ case '(':
+ found = true;
+ break;
+ case '*':
+ found = true;
+ break;
+ case '@':
+ found = true;
+ break;
+ case ':':
+ found = true;
+ break;
+ case '/':
+ components++;
+ default:
+ i++;
}
}
if (found == false) {
@@ -960,24 +977,25 @@
}
/**
- * Use a path to select the first occurence of a node. The namespace
- * of a node is ignored!
- * @param contextNode The node starting the search.
- * @param path The path to search the node. The
- * contextNode is searched for a child named path[0],
- * this node is searched for a child named path[1]...
- * @param create If a child with the corresponding name is not found
- * and create is set, this node will be created.
- */
- public static Node getFirstNodeFromPath(
- Node contextNode,
- final String[] path,
- final boolean create) {
+ * Use a path to select the first occurence of a node. The namespace of a
+ * node is ignored!
+ *
+ * @param contextNode
+ * The node starting the search.
+ * @param path
+ * The path to search the node. The contextNode is searched for a
+ * child named path[0], this node is searched for a child named
+ * path[1]...
+ * @param create
+ * If a child with the corresponding name is not found and create
+ * is set, this node will be created.
+ */
+ public static Node getFirstNodeFromPath(Node contextNode, final String[] path, final boolean create) {
if (contextNode == null || path == null || path.length == 0)
return contextNode;
// first test if the node exists
Node item = getFirstNodeFromPath(contextNode, path, 0);
- if (item == null && create == true) {
+ if (item == null && create) {
int i = 0;
NodeList childs;
boolean found;
@@ -990,8 +1008,7 @@
l = childs.getLength();
while (found == false && m < l) {
item = childs.item(m);
- if (item.getNodeType() == Node.ELEMENT_NODE
- && item.getLocalName().equals(path[i]) == true) {
+ if (item.getNodeType() == Node.ELEMENT_NODE && item.getLocalName().equals(path[i])) {
found = true;
contextNode = item;
}
@@ -1013,10 +1030,7 @@
/**
* Private helper method for getFirstNodeFromPath()
*/
- private static Node getFirstNodeFromPath(
- final Node contextNode,
- final String[] path,
- final int startIndex) {
+ private static Node getFirstNodeFromPath(final Node contextNode, final String[] path, final int startIndex) {
int i = 0;
NodeList childs;
boolean found;
@@ -1031,9 +1045,8 @@
while (found == false && i < l) {
item = childs.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE
- && path[startIndex].equals(
- item.getLocalName() != null ? item.getLocalName() : item.getNodeName())
- == true) {
+ && path[startIndex].equals(item.getLocalName() != null ? item.getLocalName() : item
+ .getNodeName())) {
if (startIndex == path.length - 1) {
found = true;
} else {
@@ -1054,12 +1067,15 @@
}
/**
- * Use a path to select all occurences of a node. The namespace
- * of a node is ignored!
- * @param contextNode The node starting the search.
- * @param path The path to search the node. The
- * contextNode is searched for a child named path[0],
- * this node is searched for a child named path[1]...
+ * Use a path to select all occurences of a node. The namespace of a node is
+ * ignored!
+ *
+ * @param contextNode
+ * The node starting the search.
+ * @param path
+ * The path to search the node. The contextNode is searched for a
+ * child named path[0], this node is searched for a child named
+ * path[1]...
*/
public static NodeList getNodeListFromPath(Node contextNode, String[] path) {
if (contextNode == null)
@@ -1073,8 +1089,7 @@
} catch (NullPointerException npe) {
// this NPE is thrown because the parser is not configured
// to use DOM Level 2
- throw new NullPointerException(
- "XMLUtil.getNodeListFromPath() did catch a NullPointerException."
+ throw new NullPointerException("XMLUtil.getNodeListFromPath() did catch a NullPointerException."
+ "This might be due to a missconfigured XML parser which does not use DOM Level 2."
+ "Make sure that you use the XML parser shipped with Cocoon.");
}
@@ -1084,11 +1099,8 @@
/**
* Helper method for getNodeListFromPath()
*/
- private static void getNodesFromPath(
- final NodeListImpl result,
- final Node contextNode,
- final String[] path,
- final int startIndex) {
+ private static void getNodesFromPath(final NodeListImpl result, final Node contextNode, final String[] path,
+ final int startIndex) {
final NodeList childs = contextNode.getChildNodes();
int m, l;
Node item;
@@ -1099,13 +1111,10 @@
while (m < l) {
item = childs.item(m);
if (item.getNodeType() == Node.ELEMENT_NODE) {
- // Work around: org.apache.xerces.dom.ElementImpl doesn't handle getLocalName() correct
- if (path[startIndex]
- .equals(
- item.getLocalName() != null
- ? item.getLocalName()
- : item.getNodeName())
- == true) {
+ // Work around: org.apache.xerces.dom.ElementImpl
+ // doesn't handle getLocalName() correct
+ if (path[startIndex].equals(item.getLocalName() != null ? item.getLocalName() : item
+ .getNodeName())) {
result.addNode(item);
}
}
@@ -1119,13 +1128,10 @@
while (m < l) {
item = childs.item(m);
if (item.getNodeType() == Node.ELEMENT_NODE) {
- // Work around: org.apache.xerces.dom.ElementImpl doesn't handle getLocalName() correct
- if (path[startIndex]
- .equals(
- item.getLocalName() != null
- ? item.getLocalName()
- : item.getNodeName())
- == true) {
+ // Work around: org.apache.xerces.dom.ElementImpl
+ // doesn't handle getLocalName() correct
+ if (path[startIndex].equals(item.getLocalName() != null ? item.getLocalName() : item
+ .getNodeName())) {
getNodesFromPath(result, item, path, startIndex + 1);
}
}