1 16 19 package org.apache.xalan.lib; 20 21 import java.util.Hashtable ; 22 import java.util.StringTokenizer ; 23 24 import javax.xml.parsers.DocumentBuilder ; 25 import javax.xml.parsers.DocumentBuilderFactory ; 26 import javax.xml.parsers.ParserConfigurationException ; 27 28 import org.apache.xalan.extensions.ExpressionContext; 29 import org.apache.xalan.xslt.EnvironmentCheck; 30 import org.apache.xpath.NodeSet; 31 import org.apache.xpath.objects.XBoolean; 32 import org.apache.xpath.objects.XNumber; 33 import org.apache.xpath.objects.XObject; 34 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.DocumentFragment ; 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.NodeList ; 39 import org.w3c.dom.Text ; 40 import org.w3c.dom.traversal.NodeIterator; 41 42 import org.xml.sax.SAXNotSupportedException ; 43 44 52 public class Extensions 53 { 54 58 private Extensions(){} 60 82 public static NodeSet nodeset(ExpressionContext myProcessor, Object rtf) 83 { 84 85 String textNodeValue; 86 87 if (rtf instanceof NodeIterator) 88 { 89 return new NodeSet((NodeIterator) rtf); 90 } 91 else 92 { 93 if (rtf instanceof String ) 94 { 95 textNodeValue = (String ) rtf; 96 } 97 else if (rtf instanceof Boolean ) 98 { 99 textNodeValue = new XBoolean(((Boolean ) rtf).booleanValue()).str(); 100 } 101 else if (rtf instanceof Double ) 102 { 103 textNodeValue = new XNumber(((Double ) rtf).doubleValue()).str(); 104 } 105 else 106 { 107 textNodeValue = rtf.toString(); 108 } 109 110 try 113 { 114 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 115 DocumentBuilder db = dbf.newDocumentBuilder(); 116 Document myDoc = db.newDocument(); 117 118 Text textNode = myDoc.createTextNode(textNodeValue); 119 DocumentFragment docFrag = myDoc.createDocumentFragment(); 120 121 docFrag.appendChild(textNode); 122 123 return new NodeSet(docFrag); 124 } 125 catch(ParserConfigurationException pce) 126 { 127 throw new org.apache.xml.utils.WrappedRuntimeException(pce); 128 } 129 } 130 } 131 132 143 public static NodeList intersection(NodeList nl1, NodeList nl2) 144 { 145 return ExsltSets.intersection(nl1, nl2); 146 } 147 148 159 public static NodeList difference(NodeList nl1, NodeList nl2) 160 { 161 return ExsltSets.difference(nl1, nl2); 162 } 163 164 176 public static NodeList distinct(NodeList nl) 177 { 178 return ExsltSets.distinct(nl); 179 } 180 181 188 public static boolean hasSameNodes(NodeList nl1, NodeList nl2) 189 { 190 191 NodeSet ns1 = new NodeSet(nl1); 192 NodeSet ns2 = new NodeSet(nl2); 193 194 if (ns1.getLength() != ns2.getLength()) 195 return false; 196 197 for (int i = 0; i < ns1.getLength(); i++) 198 { 199 Node n = ns1.elementAt(i); 200 201 if (!ns2.contains(n)) 202 return false; 203 } 204 205 return true; 206 } 207 208 225 public static XObject evaluate(ExpressionContext myContext, String xpathExpr) 226 throws SAXNotSupportedException 227 { 228 return ExsltDynamic.evaluate(myContext, xpathExpr); 229 } 230 231 246 public static NodeList tokenize(String toTokenize, String delims) 247 { 248 249 Document doc = DocumentHolder.m_doc; 250 251 252 StringTokenizer lTokenizer = new StringTokenizer (toTokenize, delims); 253 NodeSet resultSet = new NodeSet(); 254 255 synchronized (doc) 256 { 257 while (lTokenizer.hasMoreTokens()) 258 { 259 resultSet.addNode(doc.createTextNode(lTokenizer.nextToken())); 260 } 261 } 262 263 return resultSet; 264 } 265 266 281 public static NodeList tokenize(String toTokenize) 282 { 283 return tokenize(toTokenize, " \t\n\r"); 284 } 285 286 307 public static Node checkEnvironment(ExpressionContext myContext) 308 { 309 310 Document factoryDocument; 311 try 312 { 313 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 314 DocumentBuilder db = dbf.newDocumentBuilder(); 315 factoryDocument = db.newDocument(); 316 } 317 catch(ParserConfigurationException pce) 318 { 319 throw new org.apache.xml.utils.WrappedRuntimeException(pce); 320 } 321 322 Node resultNode = null; 323 try 324 { 325 resultNode = checkEnvironmentUsingWhich(myContext, factoryDocument); 328 329 if (null != resultNode) 330 return resultNode; 331 332 EnvironmentCheck envChecker = new EnvironmentCheck(); 334 Hashtable h = envChecker.getEnvironmentHash(); 335 resultNode = factoryDocument.createElement("checkEnvironmentExtension"); 336 envChecker.appendEnvironmentReport(resultNode, factoryDocument, h); 337 envChecker = null; 338 } 339 catch(Exception e) 340 { 341 throw new org.apache.xml.utils.WrappedRuntimeException(e); 342 } 343 344 return resultNode; 345 } 346 347 355 private static Node checkEnvironmentUsingWhich(ExpressionContext myContext, 356 Document factoryDocument) 357 { 358 final String WHICH_CLASSNAME = "org.apache.env.Which"; 359 final String WHICH_METHODNAME = "which"; 360 final Class WHICH_METHOD_ARGS[] = { java.util.Hashtable .class, 361 java.lang.String .class, 362 java.lang.String .class }; 363 try 364 { 365 Class clazz = ObjectFactory.findProviderClass( 367 WHICH_CLASSNAME, ObjectFactory.findClassLoader(), true); 368 if (null == clazz) 369 return null; 370 371 java.lang.reflect.Method method = clazz.getMethod(WHICH_METHODNAME, WHICH_METHOD_ARGS); 373 Hashtable report = new Hashtable (); 374 375 Object [] methodArgs = { report, "XmlCommons;Xalan;Xerces;Crimson;Ant", "" }; 377 Object returnValue = method.invoke(null, methodArgs); 378 379 Node resultNode = factoryDocument.createElement("checkEnvironmentExtension"); 381 org.apache.xml.utils.Hashtree2Node.appendHashToNode(report, "whichReport", 382 resultNode, factoryDocument); 383 384 return resultNode; 385 } 386 catch (Throwable t) 387 { 388 return null; 390 } 391 } 392 393 402 private static class DocumentHolder 403 { 404 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 org.apache.xml.utils.WrappedRuntimeException(pce); 416 } 417 418 } 419 } 420 } 421 | Popular Tags |