Added XPath utilities and updated the javadocs.
[utils] / support / general / src / main / java / org / wamblee / xml / XPathExpression.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.xml;
17
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;
23
24 import org.w3c.dom.Node;
25 import org.w3c.dom.NodeList;
26
27 /**
28  * XPath expression. 
29  * 
30  * @author Erik Brakkee
31  */
32 public class XPathExpression {
33     private String expressionString;
34     private javax.xml.xpath.XPathExpression expression;
35
36     /**
37      * Constructs the expression. Users typically use {@link XPathContext#createExpression(String)} instead
38      * of this constructor.
39      * 
40      * @param aXpath Xpath interface.
41      * @param aExpression Expression.
42      * @throws XMLException In case of problems. 
43      */
44     public XPathExpression(XPath aXpath, String aExpression)
45         throws XMLException {
46         expressionString = aExpression;
47         try {
48             expression = aXpath.compile(aExpression);
49         } catch (XPathExpressionException e) {
50             throw new XMLException("Could not compile xpath expression '" +
51                 aExpression + "'", e);
52         }
53     }
54
55     /**
56      * Evaluates the xpath expression to a string. 
57      * @param aDocument Document
58      * @return Value.
59      * @throws XMLException In case of problems. 
60      */
61     public String stringEval(XMLDocument aDocument) throws XMLException {
62         return (String)evaluateImpl(aDocument, XPathConstants.STRING);
63     }
64     
65     /**
66      * Evaluates the xpath expression to a boolean. 
67      * @param aDocument Document
68      * @return Value.
69      * @throws XMLException In case of problems. 
70      */
71     public boolean booleanEval(XMLDocument aDocument) throws XMLException {
72         return (Boolean)evaluateImpl(aDocument, XPathConstants.BOOLEAN);
73     }
74     
75     /**
76      * Evaluates the xpath expression to a node list. 
77      * @param aDocument Document
78      * @return Value.
79      * @throws XMLException In case of problems. 
80      */
81     public NodeList nodelistEval(XMLDocument aDocument) throws XMLException {
82         return (NodeList)evaluateImpl(aDocument, XPathConstants.NODESET);
83     }
84     
85     /**
86      * Evaluates the xpath expression to a Node. 
87      * @param aDocument Document
88      * @return Value.
89      * @throws XMLException In case of problems. 
90      */
91     public Node nodeEval(XMLDocument aDocument) throws XMLException {
92         return (Node)evaluateImpl(aDocument, XPathConstants.NODE);
93     }
94     
95     /**
96      * Evaluates the xpath expression to a number. 
97      * @param aDocument Document
98      * @return Value.
99      * @throws XMLException In case of problems. 
100      */
101     public Double numberEval(XMLDocument aDocument) throws XMLException {
102         return (Double)evaluateImpl(aDocument, XPathConstants.NUMBER);
103     }
104     
105     /**
106      * @return Low-level xpath expression object.
107      */
108     public javax.xml.xpath.XPathExpression getExpression() {
109         return expression;
110     }
111     
112     /**
113      * @return XPath expression. 
114      */
115     public String getExpressionString() {
116         return expressionString;
117     }
118     
119
120     private Object evaluateImpl(XMLDocument aDocument, QName aResultType) throws XMLException {
121         try {
122             return expression.evaluate(aDocument.getDocument(),
123                 aResultType);
124         } catch (XPathExpressionException e) {
125             throw new XMLException("Problem evaluating expression '" +
126                 expressionString + "' on document '" + aDocument.print(true) +
127                 "'", e);
128         }
129     }
130     
131 }