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: Extensions.java,v 1.2.4.1 2005/09/10 18:53:32 jeffsuttor Exp $
|
|
22 |
*/
|
|
23 |
package com.sun.org.apache.xalan.internal.lib;
|
|
24 |
|
|
25 |
import java.util.Hashtable;
|
|
26 |
import java.util.StringTokenizer;
|
|
27 |
|
|
28 |
import javax.xml.parsers.DocumentBuilder;
|
|
29 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
30 |
import javax.xml.parsers.ParserConfigurationException;
|
|
31 |
|
|
32 |
import com.sun.org.apache.xalan.internal.extensions.ExpressionContext;
|
|
33 |
import com.sun.org.apache.xalan.internal.xslt.EnvironmentCheck;
|
|
34 |
import com.sun.org.apache.xpath.internal.NodeSet;
|
|
35 |
import com.sun.org.apache.xpath.internal.objects.XBoolean;
|
|
36 |
import com.sun.org.apache.xpath.internal.objects.XNumber;
|
|
37 |
import com.sun.org.apache.xpath.internal.objects.XObject;
|
12458
|
38 |
import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
|
6
|
39 |
|
|
40 |
import org.w3c.dom.Document;
|
|
41 |
import org.w3c.dom.DocumentFragment;
|
|
42 |
import org.w3c.dom.Node;
|
|
43 |
import org.w3c.dom.NodeList;
|
|
44 |
import org.w3c.dom.Text;
|
|
45 |
import org.w3c.dom.traversal.NodeIterator;
|
|
46 |
|
|
47 |
import org.xml.sax.SAXNotSupportedException;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* This class contains many of the Xalan-supplied extensions.
|
|
51 |
* It is accessed by specifying a namespace URI as follows:
|
|
52 |
* <pre>
|
|
53 |
* xmlns:xalan="http://xml.apache.org/xalan"
|
|
54 |
* </pre>
|
|
55 |
* @xsl.usage general
|
|
56 |
*/
|
|
57 |
public class Extensions
|
|
58 |
{
|
|
59 |
/**
|
|
60 |
* Constructor Extensions
|
|
61 |
*
|
|
62 |
*/
|
|
63 |
private Extensions(){} // Make sure class cannot be instantiated
|
|
64 |
|
|
65 |
/**
|
|
66 |
* This method is an extension that implements as a Xalan extension
|
|
67 |
* the node-set function also found in xt and saxon.
|
|
68 |
* If the argument is a Result Tree Fragment, then <code>nodeset</code>
|
|
69 |
* returns a node-set consisting of a single root node as described in
|
|
70 |
* section 11.1 of the XSLT 1.0 Recommendation. If the argument is a
|
|
71 |
* node-set, <code>nodeset</code> returns a node-set. If the argument
|
|
72 |
* is a string, number, or boolean, then <code>nodeset</code> returns
|
|
73 |
* a node-set consisting of a single root node with a single text node
|
|
74 |
* child that is the result of calling the XPath string() function on the
|
|
75 |
* passed parameter. If the argument is anything else, then a node-set
|
|
76 |
* is returned consisting of a single root node with a single text node
|
|
77 |
* child that is the result of calling the java <code>toString()</code>
|
|
78 |
* method on the passed argument.
|
|
79 |
* Most of the
|
|
80 |
* actual work here is done in <code>MethodResolver</code> and
|
|
81 |
* <code>XRTreeFrag</code>.
|
|
82 |
* @param myProcessor Context passed by the extension processor
|
|
83 |
* @param rtf Argument in the stylesheet to the nodeset extension function
|
|
84 |
*
|
|
85 |
* NEEDSDOC ($objectName$) @return
|
|
86 |
*/
|
|
87 |
public static NodeSet nodeset(ExpressionContext myProcessor, Object rtf)
|
|
88 |
{
|
|
89 |
|
|
90 |
String textNodeValue;
|
|
91 |
|
|
92 |
if (rtf instanceof NodeIterator)
|
|
93 |
{
|
|
94 |
return new NodeSet((NodeIterator) rtf);
|
|
95 |
}
|
|
96 |
else
|
|
97 |
{
|
|
98 |
if (rtf instanceof String)
|
|
99 |
{
|
|
100 |
textNodeValue = (String) rtf;
|
|
101 |
}
|
|
102 |
else if (rtf instanceof Boolean)
|
|
103 |
{
|
|
104 |
textNodeValue = new XBoolean(((Boolean) rtf).booleanValue()).str();
|
|
105 |
}
|
|
106 |
else if (rtf instanceof Double)
|
|
107 |
{
|
|
108 |
textNodeValue = new XNumber(((Double) rtf).doubleValue()).str();
|
|
109 |
}
|
|
110 |
else
|
|
111 |
{
|
|
112 |
textNodeValue = rtf.toString();
|
|
113 |
}
|
|
114 |
|
|
115 |
// This no longer will work right since the DTM.
|
|
116 |
// Document myDoc = myProcessor.getContextNode().getOwnerDocument();
|
|
117 |
try
|
|
118 |
{
|
|
119 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
|
120 |
DocumentBuilder db = dbf.newDocumentBuilder();
|
|
121 |
Document myDoc = db.newDocument();
|
|
122 |
|
|
123 |
Text textNode = myDoc.createTextNode(textNodeValue);
|
|
124 |
DocumentFragment docFrag = myDoc.createDocumentFragment();
|
|
125 |
|
|
126 |
docFrag.appendChild(textNode);
|
|
127 |
|
|
128 |
return new NodeSet(docFrag);
|
|
129 |
}
|
|
130 |
catch(ParserConfigurationException pce)
|
|
131 |
{
|
|
132 |
throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
|
|
133 |
}
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
/**
|
|
138 |
* Returns the intersection of two node-sets.
|
|
139 |
*
|
|
140 |
* @param nl1 NodeList for first node-set
|
|
141 |
* @param nl2 NodeList for second node-set
|
|
142 |
* @return a NodeList containing the nodes in nl1 that are also in nl2
|
|
143 |
*
|
|
144 |
* Note: The usage of this extension function in the xalan namespace
|
|
145 |
* is deprecated. Please use the same function in the EXSLT sets extension
|
|
146 |
* (http://exslt.org/sets).
|
|
147 |
*/
|
|
148 |
public static NodeList intersection(NodeList nl1, NodeList nl2)
|
|
149 |
{
|
|
150 |
return ExsltSets.intersection(nl1, nl2);
|
|
151 |
}
|
|
152 |
|
|
153 |
/**
|
|
154 |
* Returns the difference between two node-sets.
|
|
155 |
*
|
|
156 |
* @param nl1 NodeList for first node-set
|
|
157 |
* @param nl2 NodeList for second node-set
|
|
158 |
* @return a NodeList containing the nodes in nl1 that are not in nl2
|
|
159 |
*
|
|
160 |
* Note: The usage of this extension function in the xalan namespace
|
|
161 |
* is deprecated. Please use the same function in the EXSLT sets extension
|
|
162 |
* (http://exslt.org/sets).
|
|
163 |
*/
|
|
164 |
public static NodeList difference(NodeList nl1, NodeList nl2)
|
|
165 |
{
|
|
166 |
return ExsltSets.difference(nl1, nl2);
|
|
167 |
}
|
|
168 |
|
|
169 |
/**
|
|
170 |
* Returns node-set containing distinct string values.
|
|
171 |
*
|
|
172 |
* @param nl NodeList for node-set
|
|
173 |
* @return a NodeList with nodes from nl containing distinct string values.
|
|
174 |
* In other words, if more than one node in nl contains the same string value,
|
|
175 |
* only include the first such node found.
|
|
176 |
*
|
|
177 |
* Note: The usage of this extension function in the xalan namespace
|
|
178 |
* is deprecated. Please use the same function in the EXSLT sets extension
|
|
179 |
* (http://exslt.org/sets).
|
|
180 |
*/
|
|
181 |
public static NodeList distinct(NodeList nl)
|
|
182 |
{
|
|
183 |
return ExsltSets.distinct(nl);
|
|
184 |
}
|
|
185 |
|
|
186 |
/**
|
|
187 |
* Returns true if both node-sets contain the same set of nodes.
|
|
188 |
*
|
|
189 |
* @param nl1 NodeList for first node-set
|
|
190 |
* @param nl2 NodeList for second node-set
|
|
191 |
* @return true if nl1 and nl2 contain exactly the same set of nodes.
|
|
192 |
*/
|
|
193 |
public static boolean hasSameNodes(NodeList nl1, NodeList nl2)
|
|
194 |
{
|
|
195 |
|
|
196 |
NodeSet ns1 = new NodeSet(nl1);
|
|
197 |
NodeSet ns2 = new NodeSet(nl2);
|
|
198 |
|
|
199 |
if (ns1.getLength() != ns2.getLength())
|
|
200 |
return false;
|
|
201 |
|
|
202 |
for (int i = 0; i < ns1.getLength(); i++)
|
|
203 |
{
|
|
204 |
Node n = ns1.elementAt(i);
|
|
205 |
|
|
206 |
if (!ns2.contains(n))
|
|
207 |
return false;
|
|
208 |
}
|
|
209 |
|
|
210 |
return true;
|
|
211 |
}
|
|
212 |
|
|
213 |
/**
|
|
214 |
* Returns the result of evaluating the argument as a string containing
|
|
215 |
* an XPath expression. Used where the XPath expression is not known until
|
|
216 |
* run-time. The expression is evaluated as if the run-time value of the
|
|
217 |
* argument appeared in place of the evaluate function call at compile time.
|
|
218 |
*
|
|
219 |
* @param myContext an <code>ExpressionContext</code> passed in by the
|
|
220 |
* extension mechanism. This must be an XPathContext.
|
|
221 |
* @param xpathExpr The XPath expression to be evaluated.
|
|
222 |
* @return the XObject resulting from evaluating the XPath
|
|
223 |
*
|
|
224 |
* @throws SAXNotSupportedException
|
|
225 |
*
|
|
226 |
* Note: The usage of this extension function in the xalan namespace
|
|
227 |
* is deprecated. Please use the same function in the EXSLT dynamic extension
|
|
228 |
* (http://exslt.org/dynamic).
|
|
229 |
*/
|
|
230 |
public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
|
|
231 |
throws SAXNotSupportedException
|
|
232 |
{
|
|
233 |
return ExsltDynamic.evaluate(myContext, xpathExpr);
|
|
234 |
}
|
|
235 |
|
|
236 |
/**
|
|
237 |
* Returns a NodeSet containing one text node for each token in the first argument.
|
|
238 |
* Delimiters are specified in the second argument.
|
|
239 |
* Tokens are determined by a call to <code>StringTokenizer</code>.
|
|
240 |
* If the first argument is an empty string or contains only delimiters, the result
|
|
241 |
* will be an empty NodeSet.
|
|
242 |
*
|
|
243 |
* Contributed to XalanJ1 by <a href="mailto:benoit.cerrina@writeme.com">Benoit Cerrina</a>.
|
|
244 |
*
|
|
245 |
* @param toTokenize The string to be split into text tokens.
|
|
246 |
* @param delims The delimiters to use.
|
|
247 |
* @return a NodeSet as described above.
|
|
248 |
*/
|
|
249 |
public static NodeList tokenize(String toTokenize, String delims)
|
|
250 |
{
|
|
251 |
|
|
252 |
Document doc = DocumentHolder.m_doc;
|
|
253 |
|
|
254 |
|
|
255 |
StringTokenizer lTokenizer = new StringTokenizer(toTokenize, delims);
|
|
256 |
NodeSet resultSet = new NodeSet();
|
|
257 |
|
|
258 |
synchronized (doc)
|
|
259 |
{
|
|
260 |
while (lTokenizer.hasMoreTokens())
|
|
261 |
{
|
|
262 |
resultSet.addNode(doc.createTextNode(lTokenizer.nextToken()));
|
|
263 |
}
|
|
264 |
}
|
|
265 |
|
|
266 |
return resultSet;
|
|
267 |
}
|
|
268 |
|
|
269 |
/**
|
|
270 |
* Returns a NodeSet containing one text node for each token in the first argument.
|
|
271 |
* Delimiters are whitespace. That is, the delimiters that are used are tab (	),
|
|
272 |
* linefeed (
), return (
), and space ( ).
|
|
273 |
* Tokens are determined by a call to <code>StringTokenizer</code>.
|
|
274 |
* If the first argument is an empty string or contains only delimiters, the result
|
|
275 |
* will be an empty NodeSet.
|
|
276 |
*
|
|
277 |
* Contributed to XalanJ1 by <a href="mailto:benoit.cerrina@writeme.com">Benoit Cerrina</a>.
|
|
278 |
*
|
|
279 |
* @param toTokenize The string to be split into text tokens.
|
|
280 |
* @return a NodeSet as described above.
|
|
281 |
*/
|
|
282 |
public static NodeList tokenize(String toTokenize)
|
|
283 |
{
|
|
284 |
return tokenize(toTokenize, " \t\n\r");
|
|
285 |
}
|
|
286 |
|
|
287 |
/**
|
|
288 |
* Return a Node of basic debugging information from the
|
|
289 |
* EnvironmentCheck utility about the Java environment.
|
|
290 |
*
|
|
291 |
* <p>Simply calls the {@link com.sun.org.apache.xalan.internal.xslt.EnvironmentCheck}
|
|
292 |
* utility to grab info about the Java environment and CLASSPATH,
|
|
293 |
* etc., and then returns the resulting Node. Stylesheets can
|
|
294 |
* then maniuplate this data or simply xsl:copy-of the Node. Note
|
|
295 |
* that we first attempt to load the more advanced
|
|
296 |
* org.apache.env.Which utility by reflection; only if that fails
|
|
297 |
* to we still use the internal version. Which is available from
|
|
298 |
* <a href="http://xml.apache.org/commons/">http://xml.apache.org/commons/</a>.</p>
|
|
299 |
*
|
|
300 |
* <p>We throw a WrappedRuntimeException in the unlikely case
|
|
301 |
* that reading information from the environment throws us an
|
|
302 |
* exception. (Is this really the best thing to do?)</p>
|
|
303 |
*
|
|
304 |
* @param myContext an <code>ExpressionContext</code> passed in by the
|
|
305 |
* extension mechanism. This must be an XPathContext.
|
|
306 |
* @return a Node as described above.
|
|
307 |
*/
|
|
308 |
public static Node checkEnvironment(ExpressionContext myContext)
|
|
309 |
{
|
|
310 |
|
|
311 |
Document factoryDocument;
|
|
312 |
try
|
|
313 |
{
|
|
314 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
|
315 |
DocumentBuilder db = dbf.newDocumentBuilder();
|
|
316 |
factoryDocument = db.newDocument();
|
|
317 |
}
|
|
318 |
catch(ParserConfigurationException pce)
|
|
319 |
{
|
|
320 |
throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
|
|
321 |
}
|
|
322 |
|
|
323 |
Node resultNode = null;
|
|
324 |
try
|
|
325 |
{
|
|
326 |
// First use reflection to try to load Which, which is a
|
|
327 |
// better version of EnvironmentCheck
|
|
328 |
resultNode = checkEnvironmentUsingWhich(myContext, factoryDocument);
|
|
329 |
|
|
330 |
if (null != resultNode)
|
|
331 |
return resultNode;
|
|
332 |
|
|
333 |
// If reflection failed, fallback to our internal EnvironmentCheck
|
|
334 |
EnvironmentCheck envChecker = new EnvironmentCheck();
|
|
335 |
Hashtable h = envChecker.getEnvironmentHash();
|
|
336 |
resultNode = factoryDocument.createElement("checkEnvironmentExtension");
|
|
337 |
envChecker.appendEnvironmentReport(resultNode, factoryDocument, h);
|
|
338 |
envChecker = null;
|
|
339 |
}
|
|
340 |
catch(Exception e)
|
|
341 |
{
|
|
342 |
throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(e);
|
|
343 |
}
|
|
344 |
|
|
345 |
return resultNode;
|
|
346 |
}
|
|
347 |
|
|
348 |
/**
|
|
349 |
* Private worker method to attempt to use org.apache.env.Which.
|
|
350 |
*
|
|
351 |
* @param myContext an <code>ExpressionContext</code> passed in by the
|
|
352 |
* extension mechanism. This must be an XPathContext.
|
|
353 |
* @param factoryDocument providing createElement services, etc.
|
|
354 |
* @return a Node with environment info; null if any error
|
|
355 |
*/
|
|
356 |
private static Node checkEnvironmentUsingWhich(ExpressionContext myContext,
|
|
357 |
Document factoryDocument)
|
|
358 |
{
|
|
359 |
final String WHICH_CLASSNAME = "org.apache.env.Which";
|
|
360 |
final String WHICH_METHODNAME = "which";
|
|
361 |
final Class WHICH_METHOD_ARGS[] = { java.util.Hashtable.class,
|
|
362 |
java.lang.String.class,
|
|
363 |
java.lang.String.class };
|
|
364 |
try
|
|
365 |
{
|
|
366 |
// Use reflection to try to find xml-commons utility 'Which'
|
12458
|
367 |
Class clazz = ObjectFactory.findProviderClass(WHICH_CLASSNAME, true);
|
6
|
368 |
if (null == clazz)
|
|
369 |
return null;
|
|
370 |
|
|
371 |
// Fully qualify names since this is the only method they're used in
|
|
372 |
java.lang.reflect.Method method = clazz.getMethod(WHICH_METHODNAME, WHICH_METHOD_ARGS);
|
|
373 |
Hashtable report = new Hashtable();
|
|
374 |
|
|
375 |
// Call the method with our Hashtable, common options, and ignore return value
|
|
376 |
Object[] methodArgs = { report, "XmlCommons;Xalan;Xerces;Crimson;Ant", "" };
|
|
377 |
Object returnValue = method.invoke(null, methodArgs);
|
|
378 |
|
|
379 |
// Create a parent to hold the report and append hash to it
|
|
380 |
Node resultNode = factoryDocument.createElement("checkEnvironmentExtension");
|
|
381 |
com.sun.org.apache.xml.internal.utils.Hashtree2Node.appendHashToNode(report, "whichReport",
|
|
382 |
resultNode, factoryDocument);
|
|
383 |
|
|
384 |
return resultNode;
|
|
385 |
}
|
|
386 |
catch (Throwable t)
|
|
387 |
{
|
|
388 |
// Simply return null; no need to report error
|
|
389 |
return null;
|
|
390 |
}
|
|
391 |
}
|
|
392 |
|
|
393 |
/**
|
|
394 |
* This class is not loaded until first referenced (see Java Language
|
|
395 |
* Specification by Gosling/Joy/Steele, section 12.4.1)
|
|
396 |
*
|
|
397 |
* The static members are created when this class is first referenced, as a
|
|
398 |
* lazy initialization not needing checking against null or any
|
|
399 |
* synchronization.
|
|
400 |
*
|
|
401 |
*/
|
|
402 |
private static class DocumentHolder
|
|
403 |
{
|
|
404 |
// Reuse the Document object to reduce memory usage.
|
|
405 |
private static final Document m_doc;
|
|
406 |
static
|
|
407 |
{
|
|
408 |
try
|
|
409 |
{
|
|
410 |
m_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
|
411 |
}
|
|
412 |
|
|
413 |
catch(ParserConfigurationException pce)
|
|
414 |
{
|
|
415 |
throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
|
|
416 |
}
|
|
417 |
|
|
418 |
}
|
|
419 |
}
|
|
420 |
}
|