6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 1999-2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
/*
|
|
21 |
* $Id: CachedXPathAPI.java,v 1.2.4.1 2005/09/10 03:47:42 jeffsuttor Exp $
|
|
22 |
*/
|
|
23 |
package com.sun.org.apache.xpath.internal;
|
|
24 |
|
|
25 |
import javax.xml.transform.TransformerException;
|
|
26 |
|
|
27 |
import com.sun.org.apache.xml.internal.utils.PrefixResolver;
|
|
28 |
import com.sun.org.apache.xml.internal.utils.PrefixResolverDefault;
|
|
29 |
import com.sun.org.apache.xpath.internal.objects.XObject;
|
|
30 |
|
|
31 |
import org.w3c.dom.Document;
|
|
32 |
import org.w3c.dom.Node;
|
|
33 |
import org.w3c.dom.NodeList;
|
|
34 |
import org.w3c.dom.traversal.NodeIterator;
|
|
35 |
|
|
36 |
/**
|
|
37 |
* The methods in this class are convenience methods into the
|
|
38 |
* low-level XPath API.
|
|
39 |
*
|
|
40 |
* These functions tend to be a little slow, since a number of objects must be
|
|
41 |
* created for each evaluation. A faster way is to precompile the
|
|
42 |
* XPaths using the low-level API, and then just use the XPaths
|
|
43 |
* over and over.
|
|
44 |
*
|
|
45 |
* This is an alternative for the old XPathAPI class, which provided
|
|
46 |
* static methods for the purpose but had the drawback of
|
|
47 |
* instantiating a new XPathContext (and thus building a new DTMManager,
|
|
48 |
* and new DTMs) each time it was called. XPathAPIObject instead retains
|
|
49 |
* its context as long as the object persists, reusing the DTMs. This
|
|
50 |
* does have a downside: if you've changed your source document, you should
|
|
51 |
* obtain a new XPathAPIObject to continue searching it, since trying to use
|
|
52 |
* the old DTMs will probably yield bad results or malfunction outright... and
|
|
53 |
* the cached DTMs may consume memory until this object and its context are
|
|
54 |
* returned to the heap. Essentially, it's the caller's responsibility to
|
|
55 |
* decide when to discard the cache.
|
|
56 |
*
|
|
57 |
* @see <a href="http://www.w3.org/TR/xpath">XPath Specification</a>
|
|
58 |
* */
|
|
59 |
public class CachedXPathAPI
|
|
60 |
{
|
|
61 |
/** XPathContext, and thus the document model system (DTMs), persists through multiple
|
|
62 |
calls to this object. This is set in the constructor.
|
|
63 |
*/
|
|
64 |
protected XPathContext xpathSupport;
|
|
65 |
|
|
66 |
/**
|
|
67 |
* <p>Default constructor. Establishes its own {@link XPathContext}, and hence
|
|
68 |
* its own {@link com.sun.org.apache.xml.internal.dtm.DTMManager}.
|
|
69 |
* Good choice for simple uses.</p>
|
|
70 |
* <p>Note that any particular instance of {@link CachedXPathAPI} must not be
|
|
71 |
* operated upon by multiple threads without synchronization; we do
|
|
72 |
* not currently support multithreaded access to a single
|
|
73 |
* {@link com.sun.org.apache.xml.internal.dtm.DTM}.</p>
|
|
74 |
*/
|
|
75 |
public CachedXPathAPI()
|
|
76 |
{
|
|
77 |
xpathSupport = new XPathContext();
|
|
78 |
}
|
|
79 |
|
|
80 |
/**
|
|
81 |
* <p>This constructor shares its {@link XPathContext} with a pre-existing
|
|
82 |
* {@link CachedXPathAPI}. That allows sharing document models
|
|
83 |
* ({@link com.sun.org.apache.xml.internal.dtm.DTM}) and previously established location
|
|
84 |
* state.</p>
|
|
85 |
* <p>Note that the original {@link CachedXPathAPI} and the new one should
|
|
86 |
* not be operated upon concurrently; we do not support multithreaded access
|
|
87 |
* to a single {@link com.sun.org.apache.xml.internal.dtm.DTM} at this time. Similarly,
|
|
88 |
* any particular instance of {@link CachedXPathAPI} must not be operated
|
|
89 |
* upon by multiple threads without synchronization.</p>
|
|
90 |
* <p>%REVIEW% Should this instead do a clone-and-reset on the XPathSupport object?</p>
|
|
91 |
*
|
|
92 |
*/
|
|
93 |
public CachedXPathAPI(CachedXPathAPI priorXPathAPI)
|
|
94 |
{
|
|
95 |
xpathSupport = priorXPathAPI.xpathSupport;
|
|
96 |
}
|
|
97 |
|
|
98 |
|
|
99 |
/** Returns the XPathSupport object used in this CachedXPathAPI
|
|
100 |
*
|
|
101 |
* %REVIEW% I'm somewhat concerned about the loss of encapsulation
|
|
102 |
* this causes, but the xml-security folks say they need it.
|
|
103 |
* */
|
|
104 |
public XPathContext getXPathContext()
|
|
105 |
{
|
|
106 |
return this.xpathSupport;
|
|
107 |
}
|
|
108 |
|
|
109 |
|
|
110 |
/**
|
|
111 |
* Use an XPath string to select a single node. XPath namespace
|
|
112 |
* prefixes are resolved from the context node, which may not
|
|
113 |
* be what you want (see the next method).
|
|
114 |
*
|
|
115 |
* @param contextNode The node to start searching from.
|
|
116 |
* @param str A valid XPath string.
|
|
117 |
* @return The first node found that matches the XPath, or null.
|
|
118 |
*
|
|
119 |
* @throws TransformerException
|
|
120 |
*/
|
|
121 |
public Node selectSingleNode(Node contextNode, String str)
|
|
122 |
throws TransformerException
|
|
123 |
{
|
|
124 |
return selectSingleNode(contextNode, str, contextNode);
|
|
125 |
}
|
|
126 |
|
|
127 |
/**
|
|
128 |
* Use an XPath string to select a single node.
|
|
129 |
* XPath namespace prefixes are resolved from the namespaceNode.
|
|
130 |
*
|
|
131 |
* @param contextNode The node to start searching from.
|
|
132 |
* @param str A valid XPath string.
|
|
133 |
* @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
|
|
134 |
* @return The first node found that matches the XPath, or null.
|
|
135 |
*
|
|
136 |
* @throws TransformerException
|
|
137 |
*/
|
|
138 |
public Node selectSingleNode(
|
|
139 |
Node contextNode, String str, Node namespaceNode)
|
|
140 |
throws TransformerException
|
|
141 |
{
|
|
142 |
|
|
143 |
// Have the XObject return its result as a NodeSetDTM.
|
|
144 |
NodeIterator nl = selectNodeIterator(contextNode, str, namespaceNode);
|
|
145 |
|
|
146 |
// Return the first node, or null
|
|
147 |
return nl.nextNode();
|
|
148 |
}
|
|
149 |
|
|
150 |
/**
|
|
151 |
* Use an XPath string to select a nodelist.
|
|
152 |
* XPath namespace prefixes are resolved from the contextNode.
|
|
153 |
*
|
|
154 |
* @param contextNode The node to start searching from.
|
|
155 |
* @param str A valid XPath string.
|
|
156 |
* @return A NodeIterator, should never be null.
|
|
157 |
*
|
|
158 |
* @throws TransformerException
|
|
159 |
*/
|
|
160 |
public NodeIterator selectNodeIterator(Node contextNode, String str)
|
|
161 |
throws TransformerException
|
|
162 |
{
|
|
163 |
return selectNodeIterator(contextNode, str, contextNode);
|
|
164 |
}
|
|
165 |
|
|
166 |
/**
|
|
167 |
* Use an XPath string to select a nodelist.
|
|
168 |
* XPath namespace prefixes are resolved from the namespaceNode.
|
|
169 |
*
|
|
170 |
* @param contextNode The node to start searching from.
|
|
171 |
* @param str A valid XPath string.
|
|
172 |
* @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
|
|
173 |
* @return A NodeIterator, should never be null.
|
|
174 |
*
|
|
175 |
* @throws TransformerException
|
|
176 |
*/
|
|
177 |
public NodeIterator selectNodeIterator(
|
|
178 |
Node contextNode, String str, Node namespaceNode)
|
|
179 |
throws TransformerException
|
|
180 |
{
|
|
181 |
|
|
182 |
// Execute the XPath, and have it return the result
|
|
183 |
XObject list = eval(contextNode, str, namespaceNode);
|
|
184 |
|
|
185 |
// Have the XObject return its result as a NodeSetDTM.
|
|
186 |
return list.nodeset();
|
|
187 |
}
|
|
188 |
|
|
189 |
/**
|
|
190 |
* Use an XPath string to select a nodelist.
|
|
191 |
* XPath namespace prefixes are resolved from the contextNode.
|
|
192 |
*
|
|
193 |
* @param contextNode The node to start searching from.
|
|
194 |
* @param str A valid XPath string.
|
|
195 |
* @return A NodeIterator, should never be null.
|
|
196 |
*
|
|
197 |
* @throws TransformerException
|
|
198 |
*/
|
|
199 |
public NodeList selectNodeList(Node contextNode, String str)
|
|
200 |
throws TransformerException
|
|
201 |
{
|
|
202 |
return selectNodeList(contextNode, str, contextNode);
|
|
203 |
}
|
|
204 |
|
|
205 |
/**
|
|
206 |
* Use an XPath string to select a nodelist.
|
|
207 |
* XPath namespace prefixes are resolved from the namespaceNode.
|
|
208 |
*
|
|
209 |
* @param contextNode The node to start searching from.
|
|
210 |
* @param str A valid XPath string.
|
|
211 |
* @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
|
|
212 |
* @return A NodeIterator, should never be null.
|
|
213 |
*
|
|
214 |
* @throws TransformerException
|
|
215 |
*/
|
|
216 |
public NodeList selectNodeList(
|
|
217 |
Node contextNode, String str, Node namespaceNode)
|
|
218 |
throws TransformerException
|
|
219 |
{
|
|
220 |
|
|
221 |
// Execute the XPath, and have it return the result
|
|
222 |
XObject list = eval(contextNode, str, namespaceNode);
|
|
223 |
|
|
224 |
// Return a NodeList.
|
|
225 |
return list.nodelist();
|
|
226 |
}
|
|
227 |
|
|
228 |
/**
|
|
229 |
* Evaluate XPath string to an XObject. Using this method,
|
|
230 |
* XPath namespace prefixes will be resolved from the namespaceNode.
|
|
231 |
* @param contextNode The node to start searching from.
|
|
232 |
* @param str A valid XPath string.
|
|
233 |
* @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
|
|
234 |
* @see com.sun.org.apache.xpath.internal.objects.XObject
|
|
235 |
* @see com.sun.org.apache.xpath.internal.objects.XNull
|
|
236 |
* @see com.sun.org.apache.xpath.internal.objects.XBoolean
|
|
237 |
* @see com.sun.org.apache.xpath.internal.objects.XNumber
|
|
238 |
* @see com.sun.org.apache.xpath.internal.objects.XString
|
|
239 |
* @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
|
|
240 |
*
|
|
241 |
* @throws TransformerException
|
|
242 |
*/
|
|
243 |
public XObject eval(Node contextNode, String str)
|
|
244 |
throws TransformerException
|
|
245 |
{
|
|
246 |
return eval(contextNode, str, contextNode);
|
|
247 |
}
|
|
248 |
|
|
249 |
/**
|
|
250 |
* Evaluate XPath string to an XObject.
|
|
251 |
* XPath namespace prefixes are resolved from the namespaceNode.
|
|
252 |
* The implementation of this is a little slow, since it creates
|
|
253 |
* a number of objects each time it is called. This could be optimized
|
|
254 |
* to keep the same objects around, but then thread-safety issues would arise.
|
|
255 |
*
|
|
256 |
* @param contextNode The node to start searching from.
|
|
257 |
* @param str A valid XPath string.
|
|
258 |
* @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
|
|
259 |
* @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
|
|
260 |
* @see com.sun.org.apache.xpath.internal.objects.XObject
|
|
261 |
* @see com.sun.org.apache.xpath.internal.objects.XNull
|
|
262 |
* @see com.sun.org.apache.xpath.internal.objects.XBoolean
|
|
263 |
* @see com.sun.org.apache.xpath.internal.objects.XNumber
|
|
264 |
* @see com.sun.org.apache.xpath.internal.objects.XString
|
|
265 |
* @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
|
|
266 |
*
|
|
267 |
* @throws TransformerException
|
|
268 |
*/
|
|
269 |
public XObject eval(Node contextNode, String str, Node namespaceNode)
|
|
270 |
throws TransformerException
|
|
271 |
{
|
|
272 |
|
|
273 |
// Since we don't have a XML Parser involved here, install some default support
|
|
274 |
// for things like namespaces, etc.
|
|
275 |
// (Changed from: XPathContext xpathSupport = new XPathContext();
|
|
276 |
// because XPathContext is weak in a number of areas... perhaps
|
|
277 |
// XPathContext should be done away with.)
|
|
278 |
|
|
279 |
// Create an object to resolve namespace prefixes.
|
|
280 |
// XPath namespaces are resolved from the input context node's document element
|
|
281 |
// if it is a root node, or else the current context node (for lack of a better
|
|
282 |
// resolution space, given the simplicity of this sample code).
|
|
283 |
PrefixResolverDefault prefixResolver = new PrefixResolverDefault(
|
|
284 |
(namespaceNode.getNodeType() == Node.DOCUMENT_NODE)
|
|
285 |
? ((Document) namespaceNode).getDocumentElement() : namespaceNode);
|
|
286 |
|
|
287 |
// Create the XPath object.
|
|
288 |
XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);
|
|
289 |
|
|
290 |
// Execute the XPath, and have it return the result
|
|
291 |
// return xpath.execute(xpathSupport, contextNode, prefixResolver);
|
|
292 |
int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
|
|
293 |
|
|
294 |
return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
|
|
295 |
}
|
|
296 |
|
|
297 |
/**
|
|
298 |
* Evaluate XPath string to an XObject.
|
|
299 |
* XPath namespace prefixes are resolved from the namespaceNode.
|
|
300 |
* The implementation of this is a little slow, since it creates
|
|
301 |
* a number of objects each time it is called. This could be optimized
|
|
302 |
* to keep the same objects around, but then thread-safety issues would arise.
|
|
303 |
*
|
|
304 |
* @param contextNode The node to start searching from.
|
|
305 |
* @param str A valid XPath string.
|
|
306 |
* @param prefixResolver Will be called if the parser encounters namespace
|
|
307 |
* prefixes, to resolve the prefixes to URLs.
|
|
308 |
* @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
|
|
309 |
* @see com.sun.org.apache.xpath.internal.objects.XObject
|
|
310 |
* @see com.sun.org.apache.xpath.internal.objects.XNull
|
|
311 |
* @see com.sun.org.apache.xpath.internal.objects.XBoolean
|
|
312 |
* @see com.sun.org.apache.xpath.internal.objects.XNumber
|
|
313 |
* @see com.sun.org.apache.xpath.internal.objects.XString
|
|
314 |
* @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
|
|
315 |
*
|
|
316 |
* @throws TransformerException
|
|
317 |
*/
|
|
318 |
public XObject eval(
|
|
319 |
Node contextNode, String str, PrefixResolver prefixResolver)
|
|
320 |
throws TransformerException
|
|
321 |
{
|
|
322 |
|
|
323 |
// Since we don't have a XML Parser involved here, install some default support
|
|
324 |
// for things like namespaces, etc.
|
|
325 |
// (Changed from: XPathContext xpathSupport = new XPathContext();
|
|
326 |
// because XPathContext is weak in a number of areas... perhaps
|
|
327 |
// XPathContext should be done away with.)
|
|
328 |
// Create the XPath object.
|
|
329 |
XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);
|
|
330 |
|
|
331 |
// Execute the XPath, and have it return the result
|
|
332 |
XPathContext xpathSupport = new XPathContext();
|
|
333 |
int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
|
|
334 |
|
|
335 |
return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
|
|
336 |
}
|
|
337 |
}
|