2 * Copyright 2005-2011 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.xml;
18 import javax.xml.namespace.NamespaceContext;
19 import javax.xml.namespace.QName;
20 import javax.xml.xpath.XPath;
21 import javax.xml.xpath.XPathConstants;
22 import javax.xml.xpath.XPathExpressionException;
24 import org.w3c.dom.Node;
25 import org.w3c.dom.NodeList;
30 * @author Erik Brakkee
32 public class XPathExpression {
33 private String expressionString;
34 private javax.xml.xpath.XPathExpression expression;
37 * Constructs the expression. Users typically use {@link XPathContext#createExpression(String)} instead
38 * of this constructor.
40 * @param aXpath Xpath interface.
41 * @param aExpression Expression.
42 * @throws XMLException In case of problems.
44 public XPathExpression(XPath aXpath, String aExpression)
46 expressionString = aExpression;
48 expression = aXpath.compile(aExpression);
49 } catch (XPathExpressionException e) {
50 throw new XMLException("Could not compile xpath expression '" +
51 aExpression + "'", e);
56 * Evaluates the xpath expression to a string.
57 * @param aDocument Document
59 * @throws XMLException In case of problems.
61 public String stringEval(XMLDocument aDocument) throws XMLException {
62 return (String)evaluateImpl(aDocument, XPathConstants.STRING);
66 * Evaluates the xpath expression to a boolean.
67 * @param aDocument Document
69 * @throws XMLException In case of problems.
71 public boolean booleanEval(XMLDocument aDocument) throws XMLException {
72 return (Boolean)evaluateImpl(aDocument, XPathConstants.BOOLEAN);
76 * Evaluates the xpath expression to a node list.
77 * @param aDocument Document
79 * @throws XMLException In case of problems.
81 public NodeList nodelistEval(XMLDocument aDocument) throws XMLException {
82 return (NodeList)evaluateImpl(aDocument, XPathConstants.NODESET);
86 * Evaluates the xpath expression to a Node.
87 * @param aDocument Document
89 * @throws XMLException In case of problems.
91 public Node nodeEval(XMLDocument aDocument) throws XMLException {
92 return (Node)evaluateImpl(aDocument, XPathConstants.NODE);
96 * Evaluates the xpath expression to a number.
97 * @param aDocument Document
99 * @throws XMLException In case of problems.
101 public Double numberEval(XMLDocument aDocument) throws XMLException {
102 return (Double)evaluateImpl(aDocument, XPathConstants.NUMBER);
106 * @return Low-level xpath expression object.
108 public javax.xml.xpath.XPathExpression getExpression() {
113 * @return XPath expression.
115 public String getExpressionString() {
116 return expressionString;
120 private Object evaluateImpl(XMLDocument aDocument, QName aResultType) throws XMLException {
122 return expression.evaluate(aDocument.getDocument(),
124 } catch (XPathExpressionException e) {
125 throw new XMLException("Problem evaluating expression '" +
126 expressionString + "' on document '" + aDocument.print(true) +