1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /**
6  * Licensed to the Apache Software Foundation (ASF) under one
7  * or more contributor license agreements. See the NOTICE file
8  * distributed with this work for additional information
9  * regarding copyright ownership. The ASF licenses this file
10  * to you under the Apache License, Version 2.0 (the
11  * "License"); you may not use this file except in compliance
12  * with the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing,
17  * software distributed under the License is distributed on an
18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19  * KIND, either express or implied. See the License for the
20  * specific language governing permissions and limitations
21  * under the License.
22  */
23 package com.sun.org.apache.xml.internal.security.utils;
24 
25 import javax.xml.XMLConstants;
26 import javax.xml.transform.TransformerException;
27 import javax.xml.xpath.XPath;
28 import javax.xml.xpath.XPathConstants;
29 import javax.xml.xpath.XPathExpression;
30 import javax.xml.xpath.XPathExpressionException;
31 import javax.xml.xpath.XPathFactory;
32 import javax.xml.xpath.XPathFactoryConfigurationException;
33 
34 import org.w3c.dom.Node;
35 import org.w3c.dom.NodeList;
36 
37 /**
38  * An implementation for XPath evaluation that uses the JDK API.
39  */
40 public class JDKXPathAPI implements XPathAPI {
41 
42     private XPathFactory xpf;
43 
44     private String xpathStr;
45 
46     private XPathExpression xpathExpression;
47 
48     /**
49      *  Use an XPath string to select a nodelist.
50      *  XPath namespace prefixes are resolved from the namespaceNode.
51      *
52      *  @param contextNode The node to start searching from.
53      *  @param xpathnode
54      *  @param str
55      *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
56      *  @return A NodeIterator, should never be null.
57      *
58      * @throws TransformerException
59      */
selectNodeList( Node contextNode, Node xpathnode, String str, Node namespaceNode )60     public NodeList selectNodeList(
61         Node contextNode, Node xpathnode, String str, Node namespaceNode
62     ) throws TransformerException {
63         if (!str.equals(xpathStr) || xpathExpression == null) {
64             if (xpf == null) {
65                 xpf = XPathFactory.newInstance();
66                 try {
67                     xpf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
68                 } catch (XPathFactoryConfigurationException ex) {
69                     throw new TransformerException(ex);
70                 }
71             }
72             XPath xpath = xpf.newXPath();
73             xpath.setNamespaceContext(new DOMNamespaceContext(namespaceNode));
74             xpathStr = str;
75             try {
76                 xpathExpression = xpath.compile(xpathStr);
77             } catch (XPathExpressionException ex) {
78                 throw new TransformerException(ex);
79             }
80         }
81         try {
82             return (NodeList)xpathExpression.evaluate(contextNode, XPathConstants.NODESET);
83         } catch (XPathExpressionException ex) {
84             throw new TransformerException(ex);
85         }
86     }
87 
88     /**
89      * Evaluate an XPath string and return true if the output is to be included or not.
90      *  @param contextNode The node to start searching from.
91      *  @param xpathnode The XPath node
92      *  @param str The XPath expression
93      *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
94      */
evaluate(Node contextNode, Node xpathnode, String str, Node namespaceNode)95     public boolean evaluate(Node contextNode, Node xpathnode, String str, Node namespaceNode)
96         throws TransformerException {
97         if (!str.equals(xpathStr) || xpathExpression == null) {
98             if (xpf == null) {
99                 xpf = XPathFactory.newInstance();
100                 try {
101                     xpf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
102                 } catch (XPathFactoryConfigurationException ex) {
103                     throw new TransformerException(ex);
104                 }
105             }
106             XPath xpath = xpf.newXPath();
107             xpath.setNamespaceContext(new DOMNamespaceContext(namespaceNode));
108             xpathStr = str;
109             try {
110                 xpathExpression = xpath.compile(xpathStr);
111             } catch (XPathExpressionException ex) {
112                 throw new TransformerException(ex);
113             }
114         }
115         try {
116             return (Boolean)xpathExpression.evaluate(contextNode, XPathConstants.BOOLEAN);
117         } catch (XPathExpressionException ex) {
118             throw new TransformerException(ex);
119         }
120     }
121 
122     /**
123      * Clear any context information from this object
124      */
clear()125     public void clear() {
126         xpathStr = null;
127         xpathExpression = null;
128         xpf = null;
129     }
130 
131 }
132