1 17 package com.sun.org.apache.xml.internal.security.utils; 18 19 20 21 import com.sun.org.apache.xml.internal.dtm.DTMManager; 22 import com.sun.org.apache.xml.internal.security.transforms.implementations.FuncHere; 23 import com.sun.org.apache.xml.internal.security.transforms.implementations.FuncHereContext; 24 import com.sun.org.apache.xml.internal.utils.PrefixResolver; 25 import com.sun.org.apache.xml.internal.utils.PrefixResolverDefault; 26 import com.sun.org.apache.xpath.internal.CachedXPathAPI; 27 import com.sun.org.apache.xpath.internal.Expression; 28 import com.sun.org.apache.xpath.internal.XPath; 29 import com.sun.org.apache.xpath.internal.XPathContext; 30 import com.sun.org.apache.xpath.internal.compiler.FunctionTable; 31 import com.sun.org.apache.xpath.internal.objects.XObject; 32 import org.w3c.dom.*; 33 import org.w3c.dom.traversal.NodeIterator; 34 35 import javax.xml.transform.ErrorListener ; 36 import javax.xml.transform.SourceLocator ; 37 import javax.xml.transform.TransformerException ; 38 import java.lang.reflect.Constructor ; 39 import java.lang.reflect.Method ; 40 import java.lang.reflect.Modifier ; 41 42 46 public class CachedXPathFuncHereAPI { 47 48 static java.util.logging.Logger log = 49 java.util.logging.Logger.getLogger(CachedXPathFuncHereAPI.class.getName()); 50 54 FuncHereContext _funcHereContext = null; 55 56 57 DTMManager _dtmManager = null; 58 59 XPathContext _context = null; 60 61 String xpathStr=null; 62 63 XPath xpath=null; 64 65 static FunctionTable _funcTable = null; 66 67 static { 68 fixupFunctionTable(); 69 } 70 71 76 public FuncHereContext getFuncHereContext() { 77 return this._funcHereContext; 78 } 79 80 84 private CachedXPathFuncHereAPI() {} 85 86 91 public CachedXPathFuncHereAPI(XPathContext existingXPathContext) { 92 this._dtmManager = existingXPathContext.getDTMManager(); 93 this._context=existingXPathContext; 94 } 95 96 101 public CachedXPathFuncHereAPI(CachedXPathAPI previouslyUsed) { 102 this._dtmManager = previouslyUsed.getXPathContext().getDTMManager(); 103 this._context=previouslyUsed.getXPathContext(); 104 } 105 106 117 public Node selectSingleNode(Node contextNode, Node xpathnode) 118 throws TransformerException { 119 return selectSingleNode(contextNode, xpathnode, contextNode); 120 } 121 122 133 public Node selectSingleNode( 134 Node contextNode, Node xpathnode, Node namespaceNode) 135 throws TransformerException { 136 137 NodeIterator nl = selectNodeIterator(contextNode, xpathnode, 139 namespaceNode); 140 141 return nl.nextNode(); 143 } 144 145 155 public NodeIterator selectNodeIterator(Node contextNode, Node xpathnode) 156 throws TransformerException { 157 return selectNodeIterator(contextNode, xpathnode, contextNode); 158 } 159 160 172 public NodeIterator selectNodeIterator( 173 Node contextNode, Node xpathnode, Node namespaceNode) 174 throws TransformerException { 175 176 XObject list = eval(contextNode, xpathnode, getStrFromNode(xpathnode), namespaceNode); 178 179 return list.nodeset(); 181 } 182 183 194 public NodeList selectNodeList(Node contextNode, Node xpathnode) 195 throws TransformerException { 196 return selectNodeList(contextNode, xpathnode, getStrFromNode(xpathnode), contextNode); 197 } 198 199 211 public NodeList selectNodeList( 212 Node contextNode, Node xpathnode, String str, Node namespaceNode) 213 throws TransformerException { 214 215 XObject list = eval(contextNode, xpathnode, str, namespaceNode); 217 218 return list.nodelist(); 220 } 221 222 238 public XObject eval(Node contextNode, Node xpathnode) 239 throws TransformerException { 240 return eval(contextNode, xpathnode, getStrFromNode(xpathnode),contextNode); 241 } 242 243 264 public XObject eval(Node contextNode, Node xpathnode, String str, Node namespaceNode) 265 throws TransformerException { 266 269 if (this._funcHereContext == null) { 275 this._funcHereContext = new FuncHereContext(xpathnode, 276 this._dtmManager); 277 } 278 279 PrefixResolverDefault prefixResolver = 284 new PrefixResolverDefault((namespaceNode.getNodeType() 285 == Node.DOCUMENT_NODE) 286 ? ((Document) namespaceNode) 287 .getDocumentElement() 288 : namespaceNode); 289 290 if (str!=xpathStr) { 291 if (str.indexOf("here()")>0) { 292 _context.reset(); 293 _dtmManager=_context.getDTMManager(); 294 } 295 xpath = createXPath(str, prefixResolver); 296 xpathStr=str; 297 } 298 299 int ctxtNode = this._funcHereContext.getDTMHandleFromNode(contextNode); 302 303 return xpath.execute(this._funcHereContext, ctxtNode, prefixResolver); 304 } 305 306 328 public XObject eval( 329 Node contextNode, Node xpathnode, String str, PrefixResolver prefixResolver) 330 throws TransformerException { 331 332 if (str!=xpathStr) { 340 if (str.indexOf("here()")>0) { 341 _context.reset(); 342 _dtmManager=_context.getDTMManager(); 343 } 344 try { 345 xpath = createXPath(str, prefixResolver); 346 } catch (TransformerException ex) { 347 Throwable th= ex.getCause(); 349 if (th instanceof ClassNotFoundException ) { 350 if (th.getMessage().indexOf("FuncHere")>0) { 351 throw new RuntimeException (I18n.translate("endorsed.jdk1.4.0")+ex); 352 } 353 } 354 throw ex; 355 } 356 xpathStr=str; 357 } 358 359 if (this._funcHereContext == null) { 361 this._funcHereContext = new FuncHereContext(xpathnode, 362 this._dtmManager); 363 } 364 365 int ctxtNode = this._funcHereContext.getDTMHandleFromNode(contextNode); 366 367 return xpath.execute(this._funcHereContext, ctxtNode, prefixResolver); 368 } 369 370 private XPath createXPath(String str, PrefixResolver prefixResolver) throws TransformerException { 371 XPath xpath = null; 372 Class [] classes = new Class []{String .class, SourceLocator .class, PrefixResolver.class, int.class, 373 ErrorListener .class, FunctionTable.class}; 374 Object [] objects = new Object []{str, null, prefixResolver, new Integer (XPath.SELECT), null, _funcTable}; 375 try { 376 Constructor constructor = XPath.class.getConstructor(classes); 377 xpath = (XPath) constructor.newInstance(objects); 378 } catch (Throwable t) { 379 } 380 if (xpath == null) { 381 xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null); 382 } 383 return xpath; 384 } 385 386 392 public static String getStrFromNode(Node xpathnode) { 393 394 if (xpathnode.getNodeType() == Node.TEXT_NODE) { 395 396 StringBuffer sb = new StringBuffer (); 399 400 for (Node currentSibling = xpathnode.getParentNode().getFirstChild(); 401 currentSibling != null; 402 currentSibling = currentSibling.getNextSibling()) { 403 if (currentSibling.getNodeType() == Node.TEXT_NODE) { 404 sb.append(((Text) currentSibling).getData()); 405 } 406 } 407 408 return sb.toString(); 409 } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) { 410 return ((Attr) xpathnode).getNodeValue(); 411 } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { 412 return ((ProcessingInstruction) xpathnode).getNodeValue(); 413 } 414 415 return null; 416 } 417 418 private static void fixupFunctionTable() { 419 boolean installed = false; 420 if (log.isLoggable(java.util.logging.Level.INFO)) log.log(java.util.logging.Level.INFO, "Registering Here function"); 421 424 try { 425 Class []args = {String .class, Expression.class}; 426 Method installFunction = FunctionTable.class.getMethod("installFunction", args); 427 if ((installFunction.getModifiers() & Modifier.STATIC) != 0) { 428 Object []params = {"here", new FuncHere()}; 429 installFunction.invoke(null, params); 430 installed = true; 431 } 432 } catch (Throwable t) { 433 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Error installing function using the static installFunction method", t); 434 } 435 if(!installed) { 436 try { 437 _funcTable = new FunctionTable(); 438 Class []args = {String .class, Class .class}; 439 Method installFunction = FunctionTable.class.getMethod("installFunction", args); 440 Object []params = {"here", FuncHere.class}; 441 installFunction.invoke(_funcTable, params); 442 installed = true; 443 } catch (Throwable t) { 444 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Error installing function using the static installFunction method", t); 445 } 446 } 447 if (true) { 448 if (installed) { 449 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Registered class " + FuncHere.class.getName() 450 + " for XPath function 'here()' function in internal table"); 451 } else { 452 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Unable to register class " + FuncHere.class.getName() 453 + " for XPath function 'here()' function in internal table"); 454 } 455 } 456 } 457 } 458 | Popular Tags |