(no commit message)
[utils] / support / general / src / main / java / org / wamblee / xml / XPathContext.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.xpath.XPath;
20 import javax.xml.xpath.XPathFactory;
21
22 /**
23  * Represents a namespace context and is a factory for creating xpath expressions. 
24  * @author Erik Brakkee
25  */
26 public class XPathContext {
27     private XPath xpath; 
28     
29     /**
30      * Constructs the context. 
31      * @param aContext Namespaces.
32      */
33     public XPathContext(NamespaceContext aContext) { 
34         xpath = XPathFactory.newInstance().newXPath();
35         xpath.setNamespaceContext(aContext);
36     }
37     
38     /**
39      * Creation of the XPath context for use with static imports. 
40      * @param aContext Namespace context
41      * @return XPath context.
42      */
43     public static XPathContext xpathcontext(NamespaceContext aContext) { 
44         return new XPathContext(aContext);
45     }
46     
47     /**
48      * Creates the expression. 
49      * @param aExpression XPath expression. 
50      * @return Xpath expression. 
51      * @throws XMLException
52      */
53     public XPathExpression createExpression(String aExpression) throws XMLException { 
54         return new XPathExpression(xpath, aExpression);
55     }
56     
57     /**
58      * To get access to the lower level API. 
59      * @return Lower level API. 
60      */
61     public XPath getXpath() {
62         return xpath;
63     }
64 }