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: XPathAPI.java,v 1.2.4.1 2005/09/10 18:18:23 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 |
* These functions tend to be a little slow, since a number of objects must be
|
|
40 |
* created for each evaluation. A faster way is to precompile the
|
|
41 |
* XPaths using the low-level API, and then just use the XPaths
|
|
42 |
* over and over.
|
|
43 |
*
|
|
44 |
* NOTE: In particular, each call to this method will create a new
|
|
45 |
* XPathContext, a new DTMManager... and thus a new DTM. That's very
|
|
46 |
* safe, since it guarantees that you're always processing against a
|
|
47 |
* fully up-to-date view of your document. But it's also portentially
|
|
48 |
* very expensive, since you're rebuilding the DTM every time. You should
|
|
49 |
* consider using an instance of CachedXPathAPI rather than these static
|
|
50 |
* methods.
|
|
51 |
*
|
|
52 |
* @see <a href="http://www.w3.org/TR/xpath">XPath Specification</a>
|
|
53 |
* */
|
|
54 |
public class XPathAPI
|
|
55 |
{
|
|
56 |
|
|
57 |
/**
|
|
58 |
* Use an XPath string to select a single node. XPath namespace
|
|
59 |
* prefixes are resolved from the context node, which may not
|
|
60 |
* be what you want (see the next method).
|
|
61 |
*
|
|
62 |
* @param contextNode The node to start searching from.
|
|
63 |
* @param str A valid XPath string.
|
|
64 |
* @return The first node found that matches the XPath, or null.
|
|
65 |
*
|
|
66 |
* @throws TransformerException
|
|
67 |
*/
|
|
68 |
public static Node selectSingleNode(Node contextNode, String str)
|
|
69 |
throws TransformerException
|
|
70 |
{
|
|
71 |
return selectSingleNode(contextNode, str, contextNode);
|
|
72 |
}
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Use an XPath string to select a single node.
|
|
76 |
* XPath namespace prefixes are resolved from the namespaceNode.
|
|
77 |
*
|
|
78 |
* @param contextNode The node to start searching from.
|
|
79 |
* @param str A valid XPath string.
|
|
80 |
* @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
|
|
81 |
* @return The first node found that matches the XPath, or null.
|
|
82 |
*
|
|
83 |
* @throws TransformerException
|
|
84 |
*/
|
|
85 |
public static Node selectSingleNode(
|
|
86 |
Node contextNode, String str, Node namespaceNode)
|
|
87 |
throws TransformerException
|
|
88 |
{
|
|
89 |
|
|
90 |
// Have the XObject return its result as a NodeSetDTM.
|
|
91 |
NodeIterator nl = selectNodeIterator(contextNode, str, namespaceNode);
|
|
92 |
|
|
93 |
// Return the first node, or null
|
|
94 |
return nl.nextNode();
|
|
95 |
}
|
|
96 |
|
|
97 |
/**
|
|
98 |
* Use an XPath string to select a nodelist.
|
|
99 |
* XPath namespace prefixes are resolved from the contextNode.
|
|
100 |
*
|
|
101 |
* @param contextNode The node to start searching from.
|
|
102 |
* @param str A valid XPath string.
|
|
103 |
* @return A NodeIterator, should never be null.
|
|
104 |
*
|
|
105 |
* @throws TransformerException
|
|
106 |
*/
|
|
107 |
public static NodeIterator selectNodeIterator(Node contextNode, String str)
|
|
108 |
throws TransformerException
|
|
109 |
{
|
|
110 |
return selectNodeIterator(contextNode, str, contextNode);
|
|
111 |
}
|
|
112 |
|
|
113 |
/**
|
|
114 |
* Use an XPath string to select a nodelist.
|
|
115 |
* XPath namespace prefixes are resolved from the namespaceNode.
|
|
116 |
*
|
|
117 |
* @param contextNode The node to start searching from.
|
|
118 |
* @param str A valid XPath string.
|
|
119 |
* @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
|
|
120 |
* @return A NodeIterator, should never be null.
|
|
121 |
*
|
|
122 |
* @throws TransformerException
|
|
123 |
*/
|
|
124 |
public static NodeIterator selectNodeIterator(
|
|
125 |
Node contextNode, String str, Node namespaceNode)
|
|
126 |
throws TransformerException
|
|
127 |
{
|
|
128 |
|
|
129 |
// Execute the XPath, and have it return the result
|
|
130 |
XObject list = eval(contextNode, str, namespaceNode);
|
|
131 |
|
|
132 |
// Have the XObject return its result as a NodeSetDTM.
|
|
133 |
return list.nodeset();
|
|
134 |
}
|
|
135 |
|
|
136 |
/**
|
|
137 |
* Use an XPath string to select a nodelist.
|
|
138 |
* XPath namespace prefixes are resolved from the contextNode.
|
|
139 |
*
|
|
140 |
* @param contextNode The node to start searching from.
|
|
141 |
* @param str A valid XPath string.
|
|
142 |
* @return A NodeIterator, should never be null.
|
|
143 |
*
|
|
144 |
* @throws TransformerException
|
|
145 |
*/
|
|
146 |
public static NodeList selectNodeList(Node contextNode, String str)
|
|
147 |
throws TransformerException
|
|
148 |
{
|
|
149 |
return selectNodeList(contextNode, str, contextNode);
|
|
150 |
}
|
|
151 |
|
|
152 |
/**
|
|
153 |
* Use an XPath string to select a nodelist.
|
|
154 |
* XPath namespace prefixes are resolved from the namespaceNode.
|
|
155 |
*
|
|
156 |
* @param contextNode The node to start searching from.
|
|
157 |
* @param str A valid XPath string.
|
|
158 |
* @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
|
|
159 |
* @return A NodeIterator, should never be null.
|
|
160 |
*
|
|
161 |
* @throws TransformerException
|
|
162 |
*/
|
|
163 |
public static NodeList selectNodeList(
|
|
164 |
Node contextNode, String str, Node namespaceNode)
|
|
165 |
throws TransformerException
|
|
166 |
{
|
|
167 |
|
|
168 |
// Execute the XPath, and have it return the result
|
|
169 |
XObject list = eval(contextNode, str, namespaceNode);
|
|
170 |
|
|
171 |
// Return a NodeList.
|
|
172 |
return list.nodelist();
|
|
173 |
}
|
|
174 |
|
|
175 |
/**
|
|
176 |
* Evaluate XPath string to an XObject. Using this method,
|
|
177 |
* XPath namespace prefixes will be resolved from the namespaceNode.
|
|
178 |
* @param contextNode The node to start searching from.
|
|
179 |
* @param str A valid XPath string.
|
|
180 |
* @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
|
|
181 |
* @see com.sun.org.apache.xpath.internal.objects.XObject
|
|
182 |
* @see com.sun.org.apache.xpath.internal.objects.XNull
|
|
183 |
* @see com.sun.org.apache.xpath.internal.objects.XBoolean
|
|
184 |
* @see com.sun.org.apache.xpath.internal.objects.XNumber
|
|
185 |
* @see com.sun.org.apache.xpath.internal.objects.XString
|
|
186 |
* @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
|
|
187 |
*
|
|
188 |
* @throws TransformerException
|
|
189 |
*/
|
|
190 |
public static XObject eval(Node contextNode, String str)
|
|
191 |
throws TransformerException
|
|
192 |
{
|
|
193 |
return eval(contextNode, str, contextNode);
|
|
194 |
}
|
|
195 |
|
|
196 |
/**
|
|
197 |
* Evaluate XPath string to an XObject.
|
|
198 |
* XPath namespace prefixes are resolved from the namespaceNode.
|
|
199 |
* The implementation of this is a little slow, since it creates
|
|
200 |
* a number of objects each time it is called. This could be optimized
|
|
201 |
* to keep the same objects around, but then thread-safety issues would arise.
|
|
202 |
*
|
|
203 |
* @param contextNode The node to start searching from.
|
|
204 |
* @param str A valid XPath string.
|
|
205 |
* @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
|
|
206 |
* @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
|
|
207 |
* @see com.sun.org.apache.xpath.internal.objects.XObject
|
|
208 |
* @see com.sun.org.apache.xpath.internal.objects.XNull
|
|
209 |
* @see com.sun.org.apache.xpath.internal.objects.XBoolean
|
|
210 |
* @see com.sun.org.apache.xpath.internal.objects.XNumber
|
|
211 |
* @see com.sun.org.apache.xpath.internal.objects.XString
|
|
212 |
* @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
|
|
213 |
*
|
|
214 |
* @throws TransformerException
|
|
215 |
*/
|
|
216 |
public static XObject eval(Node contextNode, String str, Node namespaceNode)
|
|
217 |
throws TransformerException
|
|
218 |
{
|
|
219 |
|
|
220 |
// Since we don't have a XML Parser involved here, install some default support
|
|
221 |
// for things like namespaces, etc.
|
|
222 |
// (Changed from: XPathContext xpathSupport = new XPathContext();
|
|
223 |
// because XPathContext is weak in a number of areas... perhaps
|
|
224 |
// XPathContext should be done away with.)
|
|
225 |
XPathContext xpathSupport = new XPathContext();
|
|
226 |
|
|
227 |
// Create an object to resolve namespace prefixes.
|
|
228 |
// XPath namespaces are resolved from the input context node's document element
|
|
229 |
// if it is a root node, or else the current context node (for lack of a better
|
|
230 |
// resolution space, given the simplicity of this sample code).
|
|
231 |
PrefixResolverDefault prefixResolver = new PrefixResolverDefault(
|
|
232 |
(namespaceNode.getNodeType() == Node.DOCUMENT_NODE)
|
|
233 |
? ((Document) namespaceNode).getDocumentElement() : namespaceNode);
|
|
234 |
|
|
235 |
// Create the XPath object.
|
|
236 |
XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);
|
|
237 |
|
|
238 |
// Execute the XPath, and have it return the result
|
|
239 |
// return xpath.execute(xpathSupport, contextNode, prefixResolver);
|
|
240 |
int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
|
|
241 |
|
|
242 |
return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
|
|
243 |
}
|
|
244 |
|
|
245 |
/**
|
|
246 |
* Evaluate XPath string to an XObject.
|
|
247 |
* XPath namespace prefixes are resolved from the namespaceNode.
|
|
248 |
* The implementation of this is a little slow, since it creates
|
|
249 |
* a number of objects each time it is called. This could be optimized
|
|
250 |
* to keep the same objects around, but then thread-safety issues would arise.
|
|
251 |
*
|
|
252 |
* @param contextNode The node to start searching from.
|
|
253 |
* @param str A valid XPath string.
|
|
254 |
* @param prefixResolver Will be called if the parser encounters namespace
|
|
255 |
* prefixes, to resolve the prefixes to URLs.
|
|
256 |
* @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
|
|
257 |
* @see com.sun.org.apache.xpath.internal.objects.XObject
|
|
258 |
* @see com.sun.org.apache.xpath.internal.objects.XNull
|
|
259 |
* @see com.sun.org.apache.xpath.internal.objects.XBoolean
|
|
260 |
* @see com.sun.org.apache.xpath.internal.objects.XNumber
|
|
261 |
* @see com.sun.org.apache.xpath.internal.objects.XString
|
|
262 |
* @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
|
|
263 |
*
|
|
264 |
* @throws TransformerException
|
|
265 |
*/
|
|
266 |
public static XObject eval(
|
|
267 |
Node contextNode, String str, PrefixResolver prefixResolver)
|
|
268 |
throws TransformerException
|
|
269 |
{
|
|
270 |
|
|
271 |
// Since we don't have a XML Parser involved here, install some default support
|
|
272 |
// for things like namespaces, etc.
|
|
273 |
// (Changed from: XPathContext xpathSupport = new XPathContext();
|
|
274 |
// because XPathContext is weak in a number of areas... perhaps
|
|
275 |
// XPathContext should be done away with.)
|
|
276 |
// Create the XPath object.
|
|
277 |
XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);
|
|
278 |
|
|
279 |
// Execute the XPath, and have it return the result
|
|
280 |
XPathContext xpathSupport = new XPathContext();
|
|
281 |
int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
|
|
282 |
|
|
283 |
return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
|
|
284 |
}
|
|
285 |
}
|