2007-12-23 19:28:04 +00:00
|
|
|
|
|
|
|
package net.sourceforge.tuned;
|
|
|
|
|
|
|
|
|
2009-01-04 18:28:28 +00:00
|
|
|
import java.util.AbstractList;
|
2007-12-23 19:28:04 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.xml.xpath.XPathConstants;
|
2008-03-02 17:02:37 +00:00
|
|
|
import javax.xml.xpath.XPathExpression;
|
|
|
|
import javax.xml.xpath.XPathExpressionException;
|
2007-12-23 19:28:04 +00:00
|
|
|
import javax.xml.xpath.XPathFactory;
|
|
|
|
|
|
|
|
import org.w3c.dom.Node;
|
|
|
|
import org.w3c.dom.NodeList;
|
|
|
|
|
|
|
|
|
2009-02-06 17:57:18 +00:00
|
|
|
public final class XPathUtilities {
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2008-02-07 22:05:59 +00:00
|
|
|
public static Node selectNode(String xpath, Object node) {
|
2007-12-23 19:28:04 +00:00
|
|
|
try {
|
2008-03-02 17:02:37 +00:00
|
|
|
return (Node) getXPath(xpath).evaluate(node, XPathConstants.NODE);
|
2007-12-23 19:28:04 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-04 18:28:28 +00:00
|
|
|
public static List<Node> selectNodes(String xpath, Object node) {
|
2008-03-02 17:02:37 +00:00
|
|
|
try {
|
2009-01-04 18:28:28 +00:00
|
|
|
return new NodeListDecorator((NodeList) getXPath(xpath).evaluate(node, XPathConstants.NODESET));
|
2008-03-02 17:02:37 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2008-02-09 17:53:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-04 18:28:28 +00:00
|
|
|
public static String selectString(String xpath, Object node) {
|
2007-12-23 19:28:04 +00:00
|
|
|
try {
|
2009-01-04 18:28:28 +00:00
|
|
|
return ((String) getXPath(xpath).evaluate(node, XPathConstants.STRING)).trim();
|
2007-12-23 19:28:04 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-04 18:28:28 +00:00
|
|
|
/**
|
|
|
|
* @param nodeName search for nodes with this name
|
|
|
|
* @param parentNode search in the child nodes of this nodes
|
|
|
|
* @return text content of the child node or null if no child with the given name was found
|
|
|
|
*/
|
|
|
|
public static Node getChild(String nodeName, Node parentNode) {
|
|
|
|
for (Node child : new NodeListDecorator(parentNode.getChildNodes())) {
|
|
|
|
if (nodeName.equals(child.getNodeName()))
|
|
|
|
return child;
|
2007-12-23 19:28:04 +00:00
|
|
|
}
|
2009-01-04 18:28:28 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get text content of the first child node matching the given node name. Use this method
|
|
|
|
* instead of {@link #selectString(String, Object)} whenever xpath support is not required,
|
|
|
|
* because it is much faster, especially for large documents.
|
|
|
|
*
|
|
|
|
* @param nodeName search for nodes with this name
|
|
|
|
* @param parentNode search in the child nodes of this nodes
|
|
|
|
* @return text content of the child node or null if no child with the given name was found
|
|
|
|
*/
|
|
|
|
public static String getTextContent(String nodeName, Node parentNode) {
|
|
|
|
Node child = getChild(nodeName, parentNode);
|
|
|
|
|
|
|
|
if (child == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return child.getTextContent();
|
2007-12-23 19:28:04 +00:00
|
|
|
}
|
|
|
|
|
2008-04-23 21:47:47 +00:00
|
|
|
|
2008-06-21 23:31:19 +00:00
|
|
|
public static int selectInteger(String xpath, Object node) {
|
|
|
|
return Integer.parseInt(selectString(xpath, node));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-07 23:38:17 +00:00
|
|
|
public static boolean exists(String xpath, Object node) {
|
|
|
|
return selectNode(xpath, node) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-23 21:47:47 +00:00
|
|
|
private static XPathExpression getXPath(String xpath) throws XPathExpressionException {
|
|
|
|
return XPathFactory.newInstance().newXPath().compile(xpath);
|
2008-03-02 17:02:37 +00:00
|
|
|
}
|
2008-04-23 21:47:47 +00:00
|
|
|
|
2008-10-10 19:20:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dummy constructor to prevent instantiation.
|
|
|
|
*/
|
2009-02-06 17:57:18 +00:00
|
|
|
private XPathUtilities() {
|
2008-10-10 19:20:37 +00:00
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
2009-01-04 18:28:28 +00:00
|
|
|
|
|
|
|
protected static class NodeListDecorator extends AbstractList<Node> {
|
|
|
|
|
|
|
|
private final NodeList nodes;
|
|
|
|
|
|
|
|
|
|
|
|
public NodeListDecorator(NodeList nodes) {
|
|
|
|
this.nodes = nodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Node get(int index) {
|
|
|
|
return nodes.item(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int size() {
|
|
|
|
return nodes.getLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-12-23 19:28:04 +00:00
|
|
|
}
|