42fa99d436acd95c98864fbde859e808eb87a191
[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     public static XPathContext xpathcontext(NamespaceContext aContext) { 
39         return new XPathContext(aContext);
40     }
41     
42     /**
43      * Creates the expression. 
44      * @param aExpression XPath expression. 
45      * @return Xpath expression. 
46      * @throws XMLException
47      */
48     public XPathExpression createExpression(String aExpression) throws XMLException { 
49         return new XPathExpression(xpath, aExpression);
50     }
51     
52     /**
53      * To get access to the lower level API. 
54      * @return Lower level API. 
55      */
56     public XPath getXpath() {
57         return xpath;
58     }
59 }