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.StringTokenizer;
|
|
26 |
|
|
27 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
28 |
import javax.xml.parsers.ParserConfigurationException;
|
|
29 |
|
|
30 |
import com.sun.org.apache.xalan.internal.extensions.ExpressionContext;
|
|
31 |
import com.sun.org.apache.xpath.internal.NodeSet;
|
|
32 |
import com.sun.org.apache.xpath.internal.objects.XBoolean;
|
|
33 |
import com.sun.org.apache.xpath.internal.objects.XNumber;
|
|
34 |
import com.sun.org.apache.xpath.internal.objects.XObject;
|
|
35 |
|
|
36 |
import org.w3c.dom.Document;
|
|
37 |
import org.w3c.dom.DocumentFragment;
|
|
38 |
import org.w3c.dom.Node;
|
|
39 |
import org.w3c.dom.NodeList;
|
|
40 |
import org.w3c.dom.Text;
|
|
41 |
import org.w3c.dom.traversal.NodeIterator;
|
|
42 |
|
|
43 |
import org.xml.sax.SAXNotSupportedException;
|
|
44 |
|
|
45 |
/**
|
|
46 |
* This class contains many of the Xalan-supplied extensions.
|
|
47 |
* It is accessed by specifying a namespace URI as follows:
|
|
48 |
* <pre>
|
|
49 |
* xmlns:xalan="http://xml.apache.org/xalan"
|
|
50 |
* </pre>
|
|
51 |
* @xsl.usage general
|
|
52 |
*/
|
|
53 |
public class Extensions
|
|
54 |
{
|
22416
|
55 |
static final String JDK_DEFAULT_DOM = "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl";
|
6
|
56 |
/**
|
|
57 |
* Constructor Extensions
|
|
58 |
*
|
|
59 |
*/
|
|
60 |
private Extensions(){} // Make sure class cannot be instantiated
|
|
61 |
|
|
62 |
/**
|
|
63 |
* This method is an extension that implements as a Xalan extension
|
|
64 |
* the node-set function also found in xt and saxon.
|
|
65 |
* If the argument is a Result Tree Fragment, then <code>nodeset</code>
|
|
66 |
* returns a node-set consisting of a single root node as described in
|
|
67 |
* section 11.1 of the XSLT 1.0 Recommendation. If the argument is a
|
|
68 |
* node-set, <code>nodeset</code> returns a node-set. If the argument
|
|
69 |
* is a string, number, or boolean, then <code>nodeset</code> returns
|
|
70 |
* a node-set consisting of a single root node with a single text node
|
|
71 |
* child that is the result of calling the XPath string() function on the
|
|
72 |
* passed parameter. If the argument is anything else, then a node-set
|
|
73 |
* is returned consisting of a single root node with a single text node
|
|
74 |
* child that is the result of calling the java <code>toString()</code>
|
|
75 |
* method on the passed argument.
|
|
76 |
* Most of the
|
|
77 |
* actual work here is done in <code>MethodResolver</code> and
|
|
78 |
* <code>XRTreeFrag</code>.
|
|
79 |
* @param myProcessor Context passed by the extension processor
|
|
80 |
* @param rtf Argument in the stylesheet to the nodeset extension function
|
|
81 |
*
|
|
82 |
* NEEDSDOC ($objectName$) @return
|
|
83 |
*/
|
|
84 |
public static NodeSet nodeset(ExpressionContext myProcessor, Object rtf)
|
|
85 |
{
|
|
86 |
|
|
87 |
String textNodeValue;
|
|
88 |
|
|
89 |
if (rtf instanceof NodeIterator)
|
|
90 |
{
|
|
91 |
return new NodeSet((NodeIterator) rtf);
|
|
92 |
}
|
|
93 |
else
|
|
94 |
{
|
|
95 |
if (rtf instanceof String)
|
|
96 |
{
|
|
97 |
textNodeValue = (String) rtf;
|
|
98 |
}
|
|
99 |
else if (rtf instanceof Boolean)
|
|
100 |
{
|
|
101 |
textNodeValue = new XBoolean(((Boolean) rtf).booleanValue()).str();
|
|
102 |
}
|
|
103 |
else if (rtf instanceof Double)
|
|
104 |
{
|
|
105 |
textNodeValue = new XNumber(((Double) rtf).doubleValue()).str();
|
|
106 |
}
|
|
107 |
else
|
|
108 |
{
|
|
109 |
textNodeValue = rtf.toString();
|
|
110 |
}
|
|
111 |
|
|
112 |
// This no longer will work right since the DTM.
|
|
113 |
// Document myDoc = myProcessor.getContextNode().getOwnerDocument();
|
22417
|
114 |
Document myDoc = getDocument();
|
6
|
115 |
|
|
116 |
Text textNode = myDoc.createTextNode(textNodeValue);
|
|
117 |
DocumentFragment docFrag = myDoc.createDocumentFragment();
|
|
118 |
|
|
119 |
docFrag.appendChild(textNode);
|
|
120 |
|
22417
|
121 |
return new NodeSet(docFrag);
|
6
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
/**
|
|
126 |
* Returns the intersection of two node-sets.
|
|
127 |
*
|
|
128 |
* @param nl1 NodeList for first node-set
|
|
129 |
* @param nl2 NodeList for second node-set
|
|
130 |
* @return a NodeList containing the nodes in nl1 that are also in nl2
|
|
131 |
*
|
|
132 |
* Note: The usage of this extension function in the xalan namespace
|
|
133 |
* is deprecated. Please use the same function in the EXSLT sets extension
|
|
134 |
* (http://exslt.org/sets).
|
|
135 |
*/
|
|
136 |
public static NodeList intersection(NodeList nl1, NodeList nl2)
|
|
137 |
{
|
|
138 |
return ExsltSets.intersection(nl1, nl2);
|
|
139 |
}
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Returns the difference between two node-sets.
|
|
143 |
*
|
|
144 |
* @param nl1 NodeList for first node-set
|
|
145 |
* @param nl2 NodeList for second node-set
|
|
146 |
* @return a NodeList containing the nodes in nl1 that are not in nl2
|
|
147 |
*
|
|
148 |
* Note: The usage of this extension function in the xalan namespace
|
|
149 |
* is deprecated. Please use the same function in the EXSLT sets extension
|
|
150 |
* (http://exslt.org/sets).
|
|
151 |
*/
|
|
152 |
public static NodeList difference(NodeList nl1, NodeList nl2)
|
|
153 |
{
|
|
154 |
return ExsltSets.difference(nl1, nl2);
|
|
155 |
}
|
|
156 |
|
|
157 |
/**
|
|
158 |
* Returns node-set containing distinct string values.
|
|
159 |
*
|
|
160 |
* @param nl NodeList for node-set
|
|
161 |
* @return a NodeList with nodes from nl containing distinct string values.
|
|
162 |
* In other words, if more than one node in nl contains the same string value,
|
|
163 |
* only include the first such node found.
|
|
164 |
*
|
|
165 |
* Note: The usage of this extension function in the xalan namespace
|
|
166 |
* is deprecated. Please use the same function in the EXSLT sets extension
|
|
167 |
* (http://exslt.org/sets).
|
|
168 |
*/
|
|
169 |
public static NodeList distinct(NodeList nl)
|
|
170 |
{
|
|
171 |
return ExsltSets.distinct(nl);
|
|
172 |
}
|
|
173 |
|
|
174 |
/**
|
|
175 |
* Returns true if both node-sets contain the same set of nodes.
|
|
176 |
*
|
|
177 |
* @param nl1 NodeList for first node-set
|
|
178 |
* @param nl2 NodeList for second node-set
|
|
179 |
* @return true if nl1 and nl2 contain exactly the same set of nodes.
|
|
180 |
*/
|
|
181 |
public static boolean hasSameNodes(NodeList nl1, NodeList nl2)
|
|
182 |
{
|
|
183 |
|
|
184 |
NodeSet ns1 = new NodeSet(nl1);
|
|
185 |
NodeSet ns2 = new NodeSet(nl2);
|
|
186 |
|
|
187 |
if (ns1.getLength() != ns2.getLength())
|
|
188 |
return false;
|
|
189 |
|
|
190 |
for (int i = 0; i < ns1.getLength(); i++)
|
|
191 |
{
|
|
192 |
Node n = ns1.elementAt(i);
|
|
193 |
|
|
194 |
if (!ns2.contains(n))
|
|
195 |
return false;
|
|
196 |
}
|
|
197 |
|
|
198 |
return true;
|
|
199 |
}
|
|
200 |
|
|
201 |
/**
|
|
202 |
* Returns the result of evaluating the argument as a string containing
|
|
203 |
* an XPath expression. Used where the XPath expression is not known until
|
|
204 |
* run-time. The expression is evaluated as if the run-time value of the
|
|
205 |
* argument appeared in place of the evaluate function call at compile time.
|
|
206 |
*
|
|
207 |
* @param myContext an <code>ExpressionContext</code> passed in by the
|
|
208 |
* extension mechanism. This must be an XPathContext.
|
|
209 |
* @param xpathExpr The XPath expression to be evaluated.
|
|
210 |
* @return the XObject resulting from evaluating the XPath
|
|
211 |
*
|
|
212 |
* @throws SAXNotSupportedException
|
|
213 |
*
|
|
214 |
* Note: The usage of this extension function in the xalan namespace
|
|
215 |
* is deprecated. Please use the same function in the EXSLT dynamic extension
|
|
216 |
* (http://exslt.org/dynamic).
|
|
217 |
*/
|
|
218 |
public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
|
|
219 |
throws SAXNotSupportedException
|
|
220 |
{
|
|
221 |
return ExsltDynamic.evaluate(myContext, xpathExpr);
|
|
222 |
}
|
|
223 |
|
|
224 |
/**
|
|
225 |
* Returns a NodeSet containing one text node for each token in the first argument.
|
|
226 |
* Delimiters are specified in the second argument.
|
|
227 |
* Tokens are determined by a call to <code>StringTokenizer</code>.
|
|
228 |
* If the first argument is an empty string or contains only delimiters, the result
|
|
229 |
* will be an empty NodeSet.
|
|
230 |
*
|
|
231 |
* Contributed to XalanJ1 by <a href="mailto:benoit.cerrina@writeme.com">Benoit Cerrina</a>.
|
|
232 |
*
|
|
233 |
* @param toTokenize The string to be split into text tokens.
|
|
234 |
* @param delims The delimiters to use.
|
|
235 |
* @return a NodeSet as described above.
|
|
236 |
*/
|
|
237 |
public static NodeList tokenize(String toTokenize, String delims)
|
|
238 |
{
|
|
239 |
|
22417
|
240 |
Document doc = getDocument();
|
6
|
241 |
|
|
242 |
StringTokenizer lTokenizer = new StringTokenizer(toTokenize, delims);
|
|
243 |
NodeSet resultSet = new NodeSet();
|
|
244 |
|
|
245 |
synchronized (doc)
|
|
246 |
{
|
|
247 |
while (lTokenizer.hasMoreTokens())
|
|
248 |
{
|
|
249 |
resultSet.addNode(doc.createTextNode(lTokenizer.nextToken()));
|
|
250 |
}
|
|
251 |
}
|
|
252 |
|
|
253 |
return resultSet;
|
|
254 |
}
|
|
255 |
|
|
256 |
/**
|
|
257 |
* Returns a NodeSet containing one text node for each token in the first argument.
|
|
258 |
* Delimiters are whitespace. That is, the delimiters that are used are tab (	),
|
|
259 |
* linefeed (
), return (
), and space ( ).
|
|
260 |
* Tokens are determined by a call to <code>StringTokenizer</code>.
|
|
261 |
* If the first argument is an empty string or contains only delimiters, the result
|
|
262 |
* will be an empty NodeSet.
|
|
263 |
*
|
|
264 |
* Contributed to XalanJ1 by <a href="mailto:benoit.cerrina@writeme.com">Benoit Cerrina</a>.
|
|
265 |
*
|
|
266 |
* @param toTokenize The string to be split into text tokens.
|
|
267 |
* @return a NodeSet as described above.
|
|
268 |
*/
|
|
269 |
public static NodeList tokenize(String toTokenize)
|
|
270 |
{
|
|
271 |
return tokenize(toTokenize, " \t\n\r");
|
|
272 |
}
|
|
273 |
|
|
274 |
/**
|
22417
|
275 |
* @return an instance of DOM Document
|
6
|
276 |
*/
|
22417
|
277 |
private static Document getDocument()
|
|
278 |
{
|
|
279 |
try
|
6
|
280 |
{
|
22417
|
281 |
if (System.getSecurityManager() == null) {
|
|
282 |
return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
|
283 |
} else {
|
|
284 |
return DocumentBuilderFactory.newInstance(JDK_DEFAULT_DOM, null).newDocumentBuilder().newDocument();
|
6
|
285 |
}
|
22417
|
286 |
}
|
|
287 |
catch(ParserConfigurationException pce)
|
|
288 |
{
|
|
289 |
throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
|
6
|
290 |
}
|
|
291 |
}
|
|
292 |
}
|