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: ExsltDynamic.java,v 1.1.2.1 2005/08/01 02:08:51 jeffsuttor Exp $
|
|
22 |
*/
|
|
23 |
package com.sun.org.apache.xalan.internal.lib;
|
|
24 |
|
|
25 |
import javax.xml.parsers.DocumentBuilder;
|
|
26 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
27 |
import javax.xml.transform.TransformerException;
|
|
28 |
|
|
29 |
import com.sun.org.apache.xalan.internal.extensions.ExpressionContext;
|
|
30 |
import com.sun.org.apache.xalan.internal.res.XSLMessages;
|
|
31 |
import com.sun.org.apache.xalan.internal.res.XSLTErrorResources;
|
|
32 |
import com.sun.org.apache.xpath.internal.NodeSet;
|
|
33 |
import com.sun.org.apache.xpath.internal.NodeSetDTM;
|
|
34 |
import com.sun.org.apache.xpath.internal.XPath;
|
|
35 |
import com.sun.org.apache.xpath.internal.XPathContext;
|
|
36 |
import com.sun.org.apache.xpath.internal.objects.XBoolean;
|
|
37 |
import com.sun.org.apache.xpath.internal.objects.XNodeSet;
|
|
38 |
import com.sun.org.apache.xpath.internal.objects.XNumber;
|
|
39 |
import com.sun.org.apache.xpath.internal.objects.XObject;
|
|
40 |
|
|
41 |
import org.w3c.dom.Document;
|
|
42 |
import org.w3c.dom.Element;
|
|
43 |
import org.w3c.dom.Node;
|
|
44 |
import org.w3c.dom.NodeList;
|
|
45 |
import org.w3c.dom.Text;
|
|
46 |
|
|
47 |
import org.xml.sax.SAXNotSupportedException;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* This class contains EXSLT dynamic extension functions.
|
|
51 |
*
|
|
52 |
* It is accessed by specifying a namespace URI as follows:
|
|
53 |
* <pre>
|
|
54 |
* xmlns:dyn="http://exslt.org/dynamic"
|
|
55 |
* </pre>
|
|
56 |
* The documentation for each function has been copied from the relevant
|
|
57 |
* EXSLT Implementer page.
|
|
58 |
*
|
|
59 |
* @see <a href="http://www.exslt.org/">EXSLT</a>
|
|
60 |
|
|
61 |
* @xsl.usage general
|
|
62 |
*/
|
|
63 |
public class ExsltDynamic extends ExsltBase
|
|
64 |
{
|
|
65 |
|
|
66 |
public static final String EXSL_URI = "http://exslt.org/common";
|
|
67 |
|
|
68 |
/**
|
|
69 |
* The dyn:max function calculates the maximum value for the nodes passed as
|
|
70 |
* the first argument, where the value of each node is calculated dynamically
|
|
71 |
* using an XPath expression passed as a string as the second argument.
|
|
72 |
* <p>
|
|
73 |
* The expressions are evaluated relative to the nodes passed as the first argument.
|
|
74 |
* In other words, the value for each node is calculated by evaluating the XPath
|
|
75 |
* expression with all context information being the same as that for the call to
|
|
76 |
* the dyn:max function itself, except for the following:
|
|
77 |
* <p>
|
|
78 |
* <ul>
|
|
79 |
* <li>the context node is the node whose value is being calculated.</li>
|
|
80 |
* <li>the context position is the position of the node within the node set passed as
|
|
81 |
* the first argument to the dyn:max function, arranged in document order.</li>
|
|
82 |
* <li>the context size is the number of nodes passed as the first argument to the
|
|
83 |
* dyn:max function.</li>
|
|
84 |
* </ul>
|
|
85 |
* <p>
|
|
86 |
* The dyn:max function returns the maximum of these values, calculated in exactly
|
|
87 |
* the same way as for math:max.
|
|
88 |
* <p>
|
|
89 |
* If the expression string passed as the second argument is an invalid XPath
|
|
90 |
* expression (including an empty string), this function returns NaN.
|
|
91 |
* <p>
|
|
92 |
* This function must take a second argument. To calculate the maximum of a set of
|
|
93 |
* nodes based on their string values, you should use the math:max function.
|
|
94 |
*
|
|
95 |
* @param myContext The ExpressionContext passed by the extension processor
|
|
96 |
* @param nl The node set
|
|
97 |
* @param expr The expression string
|
|
98 |
*
|
|
99 |
* @return The maximum evaluation value
|
|
100 |
*/
|
|
101 |
public static double max(ExpressionContext myContext, NodeList nl, String expr)
|
|
102 |
throws SAXNotSupportedException
|
|
103 |
{
|
|
104 |
|
|
105 |
XPathContext xctxt = null;
|
|
106 |
if (myContext instanceof XPathContext.XPathExpressionContext)
|
|
107 |
xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
|
|
108 |
else
|
|
109 |
throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext }));
|
|
110 |
|
|
111 |
if (expr == null || expr.length() == 0)
|
|
112 |
return Double.NaN;
|
|
113 |
|
|
114 |
NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt);
|
|
115 |
xctxt.pushContextNodeList(contextNodes);
|
|
116 |
|
|
117 |
double maxValue = - Double.MAX_VALUE;
|
|
118 |
for (int i = 0; i < contextNodes.getLength(); i++)
|
|
119 |
{
|
|
120 |
int contextNode = contextNodes.item(i);
|
|
121 |
xctxt.pushCurrentNode(contextNode);
|
|
122 |
|
|
123 |
double result = 0;
|
|
124 |
try
|
|
125 |
{
|
|
126 |
XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(),
|
|
127 |
xctxt.getNamespaceContext(),
|
|
128 |
XPath.SELECT);
|
|
129 |
result = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext()).num();
|
|
130 |
}
|
|
131 |
catch (TransformerException e)
|
|
132 |
{
|
|
133 |
xctxt.popCurrentNode();
|
|
134 |
xctxt.popContextNodeList();
|
|
135 |
return Double.NaN;
|
|
136 |
}
|
|
137 |
|
|
138 |
xctxt.popCurrentNode();
|
|
139 |
|
|
140 |
if (result > maxValue)
|
|
141 |
maxValue = result;
|
|
142 |
}
|
|
143 |
|
|
144 |
xctxt.popContextNodeList();
|
|
145 |
return maxValue;
|
|
146 |
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* The dyn:min function calculates the minimum value for the nodes passed as the
|
|
151 |
* first argument, where the value of each node is calculated dynamically using
|
|
152 |
* an XPath expression passed as a string as the second argument.
|
|
153 |
* <p>
|
|
154 |
* The expressions are evaluated relative to the nodes passed as the first argument.
|
|
155 |
* In other words, the value for each node is calculated by evaluating the XPath
|
|
156 |
* expression with all context information being the same as that for the call to
|
|
157 |
* the dyn:min function itself, except for the following:
|
|
158 |
* <p>
|
|
159 |
* <ul>
|
|
160 |
* <li>the context node is the node whose value is being calculated.</li>
|
|
161 |
* <li>the context position is the position of the node within the node set passed
|
|
162 |
* as the first argument to the dyn:min function, arranged in document order.</li>
|
|
163 |
* <li>the context size is the number of nodes passed as the first argument to the
|
|
164 |
* dyn:min function.</li>
|
|
165 |
* </ul>
|
|
166 |
* <p>
|
|
167 |
* The dyn:min function returns the minimum of these values, calculated in exactly
|
|
168 |
* the same way as for math:min.
|
|
169 |
* <p>
|
|
170 |
* If the expression string passed as the second argument is an invalid XPath expression
|
|
171 |
* (including an empty string), this function returns NaN.
|
|
172 |
* <p>
|
|
173 |
* This function must take a second argument. To calculate the minimum of a set of
|
|
174 |
* nodes based on their string values, you should use the math:min function.
|
|
175 |
*
|
|
176 |
* @param myContext The ExpressionContext passed by the extension processor
|
|
177 |
* @param nl The node set
|
|
178 |
* @param expr The expression string
|
|
179 |
*
|
|
180 |
* @return The minimum evaluation value
|
|
181 |
*/
|
|
182 |
public static double min(ExpressionContext myContext, NodeList nl, String expr)
|
|
183 |
throws SAXNotSupportedException
|
|
184 |
{
|
|
185 |
|
|
186 |
XPathContext xctxt = null;
|
|
187 |
if (myContext instanceof XPathContext.XPathExpressionContext)
|
|
188 |
xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
|
|
189 |
else
|
|
190 |
throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext }));
|
|
191 |
|
|
192 |
if (expr == null || expr.length() == 0)
|
|
193 |
return Double.NaN;
|
|
194 |
|
|
195 |
NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt);
|
|
196 |
xctxt.pushContextNodeList(contextNodes);
|
|
197 |
|
|
198 |
double minValue = Double.MAX_VALUE;
|
|
199 |
for (int i = 0; i < nl.getLength(); i++)
|
|
200 |
{
|
|
201 |
int contextNode = contextNodes.item(i);
|
|
202 |
xctxt.pushCurrentNode(contextNode);
|
|
203 |
|
|
204 |
double result = 0;
|
|
205 |
try
|
|
206 |
{
|
|
207 |
XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(),
|
|
208 |
xctxt.getNamespaceContext(),
|
|
209 |
XPath.SELECT);
|
|
210 |
result = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext()).num();
|
|
211 |
}
|
|
212 |
catch (TransformerException e)
|
|
213 |
{
|
|
214 |
xctxt.popCurrentNode();
|
|
215 |
xctxt.popContextNodeList();
|
|
216 |
return Double.NaN;
|
|
217 |
}
|
|
218 |
|
|
219 |
xctxt.popCurrentNode();
|
|
220 |
|
|
221 |
if (result < minValue)
|
|
222 |
minValue = result;
|
|
223 |
}
|
|
224 |
|
|
225 |
xctxt.popContextNodeList();
|
|
226 |
return minValue;
|
|
227 |
|
|
228 |
}
|
|
229 |
|
|
230 |
/**
|
|
231 |
* The dyn:sum function calculates the sum for the nodes passed as the first argument,
|
|
232 |
* where the value of each node is calculated dynamically using an XPath expression
|
|
233 |
* passed as a string as the second argument.
|
|
234 |
* <p>
|
|
235 |
* The expressions are evaluated relative to the nodes passed as the first argument.
|
|
236 |
* In other words, the value for each node is calculated by evaluating the XPath
|
|
237 |
* expression with all context information being the same as that for the call to
|
|
238 |
* the dyn:sum function itself, except for the following:
|
|
239 |
* <p>
|
|
240 |
* <ul>
|
|
241 |
* <li>the context node is the node whose value is being calculated.</li>
|
|
242 |
* <li>the context position is the position of the node within the node set passed as
|
|
243 |
* the first argument to the dyn:sum function, arranged in document order.</li>
|
|
244 |
* <li>the context size is the number of nodes passed as the first argument to the
|
|
245 |
* dyn:sum function.</li>
|
|
246 |
* </ul>
|
|
247 |
* <p>
|
|
248 |
* The dyn:sum function returns the sumimum of these values, calculated in exactly
|
|
249 |
* the same way as for sum.
|
|
250 |
* <p>
|
|
251 |
* If the expression string passed as the second argument is an invalid XPath
|
|
252 |
* expression (including an empty string), this function returns NaN.
|
|
253 |
* <p>
|
|
254 |
* This function must take a second argument. To calculate the sumimum of a set of
|
|
255 |
* nodes based on their string values, you should use the sum function.
|
|
256 |
*
|
|
257 |
* @param myContext The ExpressionContext passed by the extension processor
|
|
258 |
* @param nl The node set
|
|
259 |
* @param expr The expression string
|
|
260 |
*
|
|
261 |
* @return The sum of the evaluation value on each node
|
|
262 |
*/
|
|
263 |
public static double sum(ExpressionContext myContext, NodeList nl, String expr)
|
|
264 |
throws SAXNotSupportedException
|
|
265 |
{
|
|
266 |
XPathContext xctxt = null;
|
|
267 |
if (myContext instanceof XPathContext.XPathExpressionContext)
|
|
268 |
xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
|
|
269 |
else
|
|
270 |
throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext }));
|
|
271 |
|
|
272 |
if (expr == null || expr.length() == 0)
|
|
273 |
return Double.NaN;
|
|
274 |
|
|
275 |
NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt);
|
|
276 |
xctxt.pushContextNodeList(contextNodes);
|
|
277 |
|
|
278 |
double sum = 0;
|
|
279 |
for (int i = 0; i < nl.getLength(); i++)
|
|
280 |
{
|
|
281 |
int contextNode = contextNodes.item(i);
|
|
282 |
xctxt.pushCurrentNode(contextNode);
|
|
283 |
|
|
284 |
double result = 0;
|
|
285 |
try
|
|
286 |
{
|
|
287 |
XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(),
|
|
288 |
xctxt.getNamespaceContext(),
|
|
289 |
XPath.SELECT);
|
|
290 |
result = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext()).num();
|
|
291 |
}
|
|
292 |
catch (TransformerException e)
|
|
293 |
{
|
|
294 |
xctxt.popCurrentNode();
|
|
295 |
xctxt.popContextNodeList();
|
|
296 |
return Double.NaN;
|
|
297 |
}
|
|
298 |
|
|
299 |
xctxt.popCurrentNode();
|
|
300 |
|
|
301 |
sum = sum + result;
|
|
302 |
|
|
303 |
}
|
|
304 |
|
|
305 |
xctxt.popContextNodeList();
|
|
306 |
return sum;
|
|
307 |
}
|
|
308 |
|
|
309 |
/**
|
|
310 |
* The dyn:map function evaluates the expression passed as the second argument for
|
|
311 |
* each of the nodes passed as the first argument, and returns a node set of those values.
|
|
312 |
* <p>
|
|
313 |
* The expressions are evaluated relative to the nodes passed as the first argument.
|
|
314 |
* In other words, the value for each node is calculated by evaluating the XPath
|
|
315 |
* expression with all context information being the same as that for the call to
|
|
316 |
* the dyn:map function itself, except for the following:
|
|
317 |
* <p>
|
|
318 |
* <ul>
|
|
319 |
* <li>The context node is the node whose value is being calculated.</li>
|
|
320 |
* <li>the context position is the position of the node within the node set passed
|
|
321 |
* as the first argument to the dyn:map function, arranged in document order.</li>
|
|
322 |
* <li>the context size is the number of nodes passed as the first argument to the
|
|
323 |
* dyn:map function.</li>
|
|
324 |
* </ul>
|
|
325 |
* <p>
|
|
326 |
* If the expression string passed as the second argument is an invalid XPath
|
|
327 |
* expression (including an empty string), this function returns an empty node set.
|
|
328 |
* <p>
|
|
329 |
* If the XPath expression evaluates as a node set, the dyn:map function returns
|
|
330 |
* the union of the node sets returned by evaluating the expression for each of the
|
|
331 |
* nodes in the first argument. Note that this may mean that the node set resulting
|
|
332 |
* from the call to the dyn:map function contains a different number of nodes from
|
|
333 |
* the number in the node set passed as the first argument to the function.
|
|
334 |
* <p>
|
|
335 |
* If the XPath expression evaluates as a number, the dyn:map function returns a
|
|
336 |
* node set containing one exsl:number element (namespace http://exslt.org/common)
|
|
337 |
* for each node in the node set passed as the first argument to the dyn:map function,
|
|
338 |
* in document order. The string value of each exsl:number element is the same as
|
|
339 |
* the result of converting the number resulting from evaluating the expression to
|
|
340 |
* a string as with the number function, with the exception that Infinity results
|
|
341 |
* in an exsl:number holding the highest number the implementation can store, and
|
|
342 |
* -Infinity results in an exsl:number holding the lowest number the implementation
|
|
343 |
* can store.
|
|
344 |
* <p>
|
|
345 |
* If the XPath expression evaluates as a boolean, the dyn:map function returns a
|
|
346 |
* node set containing one exsl:boolean element (namespace http://exslt.org/common)
|
|
347 |
* for each node in the node set passed as the first argument to the dyn:map function,
|
|
348 |
* in document order. The string value of each exsl:boolean element is 'true' if the
|
|
349 |
* expression evaluates as true for the node, and '' if the expression evaluates as
|
|
350 |
* false.
|
|
351 |
* <p>
|
|
352 |
* Otherwise, the dyn:map function returns a node set containing one exsl:string
|
|
353 |
* element (namespace http://exslt.org/common) for each node in the node set passed
|
|
354 |
* as the first argument to the dyn:map function, in document order. The string
|
|
355 |
* value of each exsl:string element is the same as the result of converting the
|
|
356 |
* result of evaluating the expression for the relevant node to a string as with
|
|
357 |
* the string function.
|
|
358 |
*
|
|
359 |
* @param myContext The ExpressionContext passed by the extension processor
|
|
360 |
* @param nl The node set
|
|
361 |
* @param expr The expression string
|
|
362 |
*
|
|
363 |
* @return The node set after evaluation
|
|
364 |
*/
|
|
365 |
public static NodeList map(ExpressionContext myContext, NodeList nl, String expr)
|
|
366 |
throws SAXNotSupportedException
|
|
367 |
{
|
|
368 |
XPathContext xctxt = null;
|
|
369 |
Document lDoc = null;
|
|
370 |
|
|
371 |
if (myContext instanceof XPathContext.XPathExpressionContext)
|
|
372 |
xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
|
|
373 |
else
|
|
374 |
throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext }));
|
|
375 |
|
|
376 |
if (expr == null || expr.length() == 0)
|
|
377 |
return new NodeSet();
|
|
378 |
|
|
379 |
NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt);
|
|
380 |
xctxt.pushContextNodeList(contextNodes);
|
|
381 |
|
|
382 |
NodeSet resultSet = new NodeSet();
|
|
383 |
resultSet.setShouldCacheNodes(true);
|
|
384 |
|
|
385 |
for (int i = 0; i < nl.getLength(); i++)
|
|
386 |
{
|
|
387 |
int contextNode = contextNodes.item(i);
|
|
388 |
xctxt.pushCurrentNode(contextNode);
|
|
389 |
|
|
390 |
XObject object = null;
|
|
391 |
try
|
|
392 |
{
|
|
393 |
XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(),
|
|
394 |
xctxt.getNamespaceContext(),
|
|
395 |
XPath.SELECT);
|
|
396 |
object = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext());
|
|
397 |
|
|
398 |
if (object instanceof XNodeSet)
|
|
399 |
{
|
|
400 |
NodeList nodelist = null;
|
|
401 |
nodelist = ((XNodeSet)object).nodelist();
|
|
402 |
|
|
403 |
for (int k = 0; k < nodelist.getLength(); k++)
|
|
404 |
{
|
|
405 |
Node n = nodelist.item(k);
|
|
406 |
if (!resultSet.contains(n))
|
|
407 |
resultSet.addNode(n);
|
|
408 |
}
|
|
409 |
}
|
|
410 |
else
|
|
411 |
{
|
|
412 |
if (lDoc == null)
|
|
413 |
{
|
|
414 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
|
415 |
dbf.setNamespaceAware(true);
|
|
416 |
DocumentBuilder db = dbf.newDocumentBuilder();
|
|
417 |
lDoc = db.newDocument();
|
|
418 |
}
|
|
419 |
|
|
420 |
Element element = null;
|
|
421 |
if (object instanceof XNumber)
|
|
422 |
element = lDoc.createElementNS(EXSL_URI, "exsl:number");
|
|
423 |
else if (object instanceof XBoolean)
|
|
424 |
element = lDoc.createElementNS(EXSL_URI, "exsl:boolean");
|
|
425 |
else
|
|
426 |
element = lDoc.createElementNS(EXSL_URI, "exsl:string");
|
|
427 |
|
|
428 |
Text textNode = lDoc.createTextNode(object.str());
|
|
429 |
element.appendChild(textNode);
|
|
430 |
resultSet.addNode(element);
|
|
431 |
}
|
|
432 |
}
|
|
433 |
catch (Exception e)
|
|
434 |
{
|
|
435 |
xctxt.popCurrentNode();
|
|
436 |
xctxt.popContextNodeList();
|
|
437 |
return new NodeSet();
|
|
438 |
}
|
|
439 |
|
|
440 |
xctxt.popCurrentNode();
|
|
441 |
|
|
442 |
}
|
|
443 |
|
|
444 |
xctxt.popContextNodeList();
|
|
445 |
return resultSet;
|
|
446 |
}
|
|
447 |
|
|
448 |
/**
|
|
449 |
* The dyn:evaluate function evaluates a string as an XPath expression and returns
|
|
450 |
* the resulting value, which might be a boolean, number, string, node set, result
|
|
451 |
* tree fragment or external object. The sole argument is the string to be evaluated.
|
|
452 |
* <p>
|
|
453 |
* If the expression string passed as the second argument is an invalid XPath
|
|
454 |
* expression (including an empty string), this function returns an empty node set.
|
|
455 |
* <p>
|
|
456 |
* You should only use this function if the expression must be constructed dynamically,
|
|
457 |
* otherwise it is much more efficient to use the expression literally.
|
|
458 |
*
|
|
459 |
* @param myContext The ExpressionContext passed by the extension processor
|
|
460 |
* @param xpathExpr The XPath expression string
|
|
461 |
*
|
|
462 |
* @return The evaluation result
|
|
463 |
*/
|
|
464 |
public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
|
|
465 |
throws SAXNotSupportedException
|
|
466 |
{
|
|
467 |
if (myContext instanceof XPathContext.XPathExpressionContext)
|
|
468 |
{
|
|
469 |
XPathContext xctxt = null;
|
|
470 |
try
|
|
471 |
{
|
|
472 |
xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
|
|
473 |
XPath dynamicXPath = new XPath(xpathExpr, xctxt.getSAXLocator(),
|
|
474 |
xctxt.getNamespaceContext(),
|
|
475 |
XPath.SELECT);
|
|
476 |
|
|
477 |
return dynamicXPath.execute(xctxt, myContext.getContextNode(),
|
|
478 |
xctxt.getNamespaceContext());
|
|
479 |
}
|
|
480 |
catch (TransformerException e)
|
|
481 |
{
|
|
482 |
return new XNodeSet(xctxt.getDTMManager());
|
|
483 |
}
|
|
484 |
}
|
|
485 |
else
|
|
486 |
throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext })); //"Invalid context passed to evaluate "
|
|
487 |
}
|
|
488 |
|
|
489 |
/**
|
|
490 |
* The dyn:closure function creates a node set resulting from transitive closure of
|
|
491 |
* evaluating the expression passed as the second argument on each of the nodes passed
|
|
492 |
* as the first argument, then on the node set resulting from that and so on until no
|
|
493 |
* more nodes are found. For example:
|
|
494 |
* <pre>
|
|
495 |
* dyn:closure(., '*')
|
|
496 |
* </pre>
|
|
497 |
* returns all the descendant elements of the node (its element children, their
|
|
498 |
* children, their children's children and so on).
|
|
499 |
* <p>
|
|
500 |
* The expression is thus evaluated several times, each with a different node set
|
|
501 |
* acting as the context of the expression. The first time the expression is
|
|
502 |
* evaluated, the context node set is the first argument passed to the dyn:closure
|
|
503 |
* function. In other words, the node set for each node is calculated by evaluating
|
|
504 |
* the XPath expression with all context information being the same as that for
|
|
505 |
* the call to the dyn:closure function itself, except for the following:
|
|
506 |
* <p>
|
|
507 |
* <ul>
|
|
508 |
* <li>the context node is the node whose value is being calculated.</li>
|
|
509 |
* <li>the context position is the position of the node within the node set passed
|
|
510 |
* as the first argument to the dyn:closure function, arranged in document order.</li>
|
|
511 |
* <li>the context size is the number of nodes passed as the first argument to the
|
|
512 |
* dyn:closure function.</li>
|
|
513 |
* <li>the current node is the node whose value is being calculated.</li>
|
|
514 |
* </ul>
|
|
515 |
* <p>
|
|
516 |
* The result for a particular iteration is the union of the node sets resulting
|
|
517 |
* from evaluting the expression for each of the nodes in the source node set for
|
|
518 |
* that iteration. This result is then used as the source node set for the next
|
|
519 |
* iteration, and so on. The result of the function as a whole is the union of
|
|
520 |
* the node sets generated by each iteration.
|
|
521 |
* <p>
|
|
522 |
* If the expression string passed as the second argument is an invalid XPath
|
|
523 |
* expression (including an empty string) or an expression that does not return a
|
|
524 |
* node set, this function returns an empty node set.
|
|
525 |
*
|
|
526 |
* @param myContext The ExpressionContext passed by the extension processor
|
|
527 |
* @param nl The node set
|
|
528 |
* @param expr The expression string
|
|
529 |
*
|
|
530 |
* @return The node set after evaluation
|
|
531 |
*/
|
|
532 |
public static NodeList closure(ExpressionContext myContext, NodeList nl, String expr)
|
|
533 |
throws SAXNotSupportedException
|
|
534 |
{
|
|
535 |
XPathContext xctxt = null;
|
|
536 |
if (myContext instanceof XPathContext.XPathExpressionContext)
|
|
537 |
xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
|
|
538 |
else
|
|
539 |
throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext }));
|
|
540 |
|
|
541 |
if (expr == null || expr.length() == 0)
|
|
542 |
return new NodeSet();
|
|
543 |
|
|
544 |
NodeSet closureSet = new NodeSet();
|
|
545 |
closureSet.setShouldCacheNodes(true);
|
|
546 |
|
|
547 |
NodeList iterationList = nl;
|
|
548 |
do
|
|
549 |
{
|
|
550 |
|
|
551 |
NodeSet iterationSet = new NodeSet();
|
|
552 |
|
|
553 |
NodeSetDTM contextNodes = new NodeSetDTM(iterationList, xctxt);
|
|
554 |
xctxt.pushContextNodeList(contextNodes);
|
|
555 |
|
|
556 |
for (int i = 0; i < iterationList.getLength(); i++)
|
|
557 |
{
|
|
558 |
int contextNode = contextNodes.item(i);
|
|
559 |
xctxt.pushCurrentNode(contextNode);
|
|
560 |
|
|
561 |
XObject object = null;
|
|
562 |
try
|
|
563 |
{
|
|
564 |
XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(),
|
|
565 |
xctxt.getNamespaceContext(),
|
|
566 |
XPath.SELECT);
|
|
567 |
object = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext());
|
|
568 |
|
|
569 |
if (object instanceof XNodeSet)
|
|
570 |
{
|
|
571 |
NodeList nodelist = null;
|
|
572 |
nodelist = ((XNodeSet)object).nodelist();
|
|
573 |
|
|
574 |
for (int k = 0; k < nodelist.getLength(); k++)
|
|
575 |
{
|
|
576 |
Node n = nodelist.item(k);
|
|
577 |
if (!iterationSet.contains(n))
|
|
578 |
iterationSet.addNode(n);
|
|
579 |
}
|
|
580 |
}
|
|
581 |
else
|
|
582 |
{
|
|
583 |
xctxt.popCurrentNode();
|
|
584 |
xctxt.popContextNodeList();
|
|
585 |
return new NodeSet();
|
|
586 |
}
|
|
587 |
}
|
|
588 |
catch (TransformerException e)
|
|
589 |
{
|
|
590 |
xctxt.popCurrentNode();
|
|
591 |
xctxt.popContextNodeList();
|
|
592 |
return new NodeSet();
|
|
593 |
}
|
|
594 |
|
|
595 |
xctxt.popCurrentNode();
|
|
596 |
|
|
597 |
}
|
|
598 |
|
|
599 |
xctxt.popContextNodeList();
|
|
600 |
|
|
601 |
iterationList = iterationSet;
|
|
602 |
|
|
603 |
for (int i = 0; i < iterationList.getLength(); i++)
|
|
604 |
{
|
|
605 |
Node n = iterationList.item(i);
|
|
606 |
if (!closureSet.contains(n))
|
|
607 |
closureSet.addNode(n);
|
|
608 |
}
|
|
609 |
|
|
610 |
} while(iterationList.getLength() > 0);
|
|
611 |
|
|
612 |
return closureSet;
|
|
613 |
|
|
614 |
}
|
|
615 |
|
|
616 |
}
|