1 16 19 package org.apache.xpath; 20 21 import java.io.Serializable ; 22 23 import javax.xml.transform.ErrorListener ; 24 import javax.xml.transform.SourceLocator ; 25 import javax.xml.transform.TransformerException ; 26 27 import org.apache.xalan.res.XSLMessages; 28 import org.apache.xml.dtm.DTM; 29 import org.apache.xml.utils.PrefixResolver; 30 import org.apache.xml.utils.SAXSourceLocator; 31 import org.apache.xpath.compiler.Compiler; 32 import org.apache.xpath.compiler.FunctionTable; 33 import org.apache.xpath.compiler.XPathParser; 34 import org.apache.xpath.functions.Function; 35 import org.apache.xpath.objects.XObject; 36 import org.apache.xpath.res.XPATHErrorResources; 37 38 43 public class XPath implements Serializable , ExpressionOwner 44 { 45 46 48 private Expression m_mainExp; 49 50 56 public Expression getExpression() 57 { 58 return m_mainExp; 59 } 60 61 71 public void fixupVariables(java.util.Vector vars, int globalsSize) 72 { 73 m_mainExp.fixupVariables(vars, globalsSize); 74 } 75 76 82 public void setExpression(Expression exp) 83 { 84 if(null != m_mainExp) 85 exp.exprSetParent(m_mainExp.exprGetParent()); m_mainExp = exp; 87 } 88 89 95 public SourceLocator getLocator() 96 { 97 return m_mainExp; 98 } 99 100 113 115 String m_patternString; 116 117 123 public String getPatternString() 124 { 125 return m_patternString; 126 } 127 128 129 public static final int SELECT = 0; 130 131 132 public static final int MATCH = 1; 133 134 148 public XPath( 149 String exprString, SourceLocator locator, PrefixResolver prefixResolver, int type, 150 ErrorListener errorListener) 151 throws javax.xml.transform.TransformerException 152 { 153 if(null == errorListener) 154 errorListener = new org.apache.xml.utils.DefaultErrorHandler(); 155 156 m_patternString = exprString; 157 158 XPathParser parser = new XPathParser(errorListener, locator); 159 Compiler compiler = new Compiler (errorListener, locator); 160 161 if (SELECT == type) 162 parser.initXPath(compiler, exprString, prefixResolver); 163 else if (MATCH == type) 164 parser.initMatchPattern(compiler, exprString, prefixResolver); 165 else 166 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_CANNOT_DEAL_XPATH_TYPE, new Object []{Integer.toString(type)})); 168 Expression expr = compiler.compile(0); 170 171 this.setExpression(expr); 173 174 if((null != locator) && locator instanceof ExpressionNode) 175 { 176 expr.exprSetParent((ExpressionNode)locator); 177 } 178 179 } 180 181 194 public XPath( 195 String exprString, SourceLocator locator, PrefixResolver prefixResolver, int type) 196 throws javax.xml.transform.TransformerException 197 { 198 this(exprString, locator, prefixResolver, type, null); 199 } 200 201 208 public XPath(Expression expr) 209 { 210 this.setExpression(expr); 211 } 212 213 229 public XObject execute( 230 XPathContext xctxt, org.w3c.dom.Node contextNode, 231 PrefixResolver namespaceContext) 232 throws javax.xml.transform.TransformerException 233 { 234 return execute( 235 xctxt, xctxt.getDTMHandleFromNode(contextNode), 236 namespaceContext); 237 } 238 239 240 255 public XObject execute( 256 XPathContext xctxt, int contextNode, PrefixResolver namespaceContext) 257 throws javax.xml.transform.TransformerException 258 { 259 260 xctxt.pushNamespaceContext(namespaceContext); 261 262 xctxt.pushCurrentNodeAndExpression(contextNode, contextNode); 263 264 XObject xobj = null; 265 266 try 267 { 268 xobj = m_mainExp.execute(xctxt); 269 } 270 catch (TransformerException te) 271 { 272 te.setLocator(this.getLocator()); 273 ErrorListener el = xctxt.getErrorListener(); 274 if(null != el) { 276 el.error(te); 277 } 278 else 279 throw te; 280 } 281 catch (Exception e) 282 { 283 while (e instanceof org.apache.xml.utils.WrappedRuntimeException) 284 { 285 e = ((org.apache.xml.utils.WrappedRuntimeException) e).getException(); 286 } 287 289 String msg = e.getMessage(); 290 291 if (msg == null || msg.length() == 0) { 292 msg = XSLMessages.createXPATHMessage( 293 XPATHErrorResources.ER_XPATH_ERROR, null); 294 295 } 296 TransformerException te = new TransformerException (msg, 297 getLocator(), e); 298 ErrorListener el = xctxt.getErrorListener(); 299 if(null != el) { 302 el.fatalError(te); 303 } 304 else 305 throw te; 306 } 307 finally 308 { 309 xctxt.popNamespaceContext(); 310 311 xctxt.popCurrentNodeAndExpression(); 312 } 313 314 return xobj; 315 } 316 317 332 public boolean bool( 333 XPathContext xctxt, int contextNode, PrefixResolver namespaceContext) 334 throws javax.xml.transform.TransformerException 335 { 336 337 xctxt.pushNamespaceContext(namespaceContext); 338 339 xctxt.pushCurrentNodeAndExpression(contextNode, contextNode); 340 341 try 342 { 343 return m_mainExp.bool(xctxt); 344 } 345 catch (TransformerException te) 346 { 347 te.setLocator(this.getLocator()); 348 ErrorListener el = xctxt.getErrorListener(); 349 if(null != el) { 351 el.error(te); 352 } 353 else 354 throw te; 355 } 356 catch (Exception e) 357 { 358 while (e instanceof org.apache.xml.utils.WrappedRuntimeException) 359 { 360 e = ((org.apache.xml.utils.WrappedRuntimeException) e).getException(); 361 } 362 364 String msg = e.getMessage(); 365 366 if (msg == null || msg.length() == 0) { 367 msg = XSLMessages.createXPATHMessage( 368 XPATHErrorResources.ER_XPATH_ERROR, null); 369 370 } 371 372 TransformerException te = new TransformerException (msg, 373 getLocator(), e); 374 ErrorListener el = xctxt.getErrorListener(); 375 if(null != el) { 378 el.fatalError(te); 379 } 380 else 381 throw te; 382 } 383 finally 384 { 385 xctxt.popNamespaceContext(); 386 387 xctxt.popCurrentNodeAndExpression(); 388 } 389 390 return false; 391 } 392 393 395 private static final boolean DEBUG_MATCHES = false; 396 397 409 public double getMatchScore(XPathContext xctxt, int context) 410 throws javax.xml.transform.TransformerException 411 { 412 413 xctxt.pushCurrentNode(context); 414 xctxt.pushCurrentExpressionNode(context); 415 416 try 417 { 418 XObject score = m_mainExp.execute(xctxt); 419 420 if (DEBUG_MATCHES) 421 { 422 DTM dtm = xctxt.getDTM(context); 423 System.out.println("score: " + score.num() + " for " 424 + dtm.getNodeName(context) + " for xpath " 425 + this.getPatternString()); 426 } 427 428 return score.num(); 429 } 430 finally 431 { 432 xctxt.popCurrentNode(); 433 xctxt.popCurrentExpressionNode(); 434 } 435 436 } 438 439 446 public void installFunction(String name, int funcIndex, Function func) 447 { 448 FunctionTable.installFunction(func, funcIndex); 449 } 450 451 465 public void warn( 466 XPathContext xctxt, int sourceNode, String msg, Object [] args) 467 throws javax.xml.transform.TransformerException 468 { 469 470 String fmsg = XSLMessages.createXPATHWarning(msg, args); 471 ErrorListener ehandler = xctxt.getErrorListener(); 472 473 if (null != ehandler) 474 { 475 476 ehandler.warning(new TransformerException (fmsg, (SAXSourceLocator)xctxt.getSAXLocator())); 478 } 479 } 480 481 490 public void assertion(boolean b, String msg) 491 { 492 493 if (!b) 494 { 495 String fMsg = XSLMessages.createXPATHMessage( 496 XPATHErrorResources.ER_INCORRECT_PROGRAMMER_ASSERTION, 497 new Object []{ msg }); 498 499 throw new RuntimeException (fMsg); 500 } 501 } 502 503 518 public void error( 519 XPathContext xctxt, int sourceNode, String msg, Object [] args) 520 throws javax.xml.transform.TransformerException 521 { 522 523 String fmsg = XSLMessages.createXPATHMessage(msg, args); 524 ErrorListener ehandler = xctxt.getErrorListener(); 525 526 if (null != ehandler) 527 { 528 ehandler.fatalError(new TransformerException (fmsg, 529 (SAXSourceLocator)xctxt.getSAXLocator())); 530 } 531 else 532 { 533 SourceLocator slocator = xctxt.getSAXLocator(); 534 System.out.println(fmsg + "; file " + slocator.getSystemId() 535 + "; line " + slocator.getLineNumber() + "; column " 536 + slocator.getColumnNumber()); 537 } 538 } 539 540 549 public void callVisitors(ExpressionOwner owner, XPathVisitor visitor) 550 { 551 m_mainExp.callVisitors(this, visitor); 552 } 553 554 558 public static final double MATCH_SCORE_NONE = Double.NEGATIVE_INFINITY; 559 560 565 public static final double MATCH_SCORE_QNAME = 0.0; 566 567 571 public static final double MATCH_SCORE_NSWILD = -0.25; 572 573 577 public static final double MATCH_SCORE_NODETEST = -0.5; 578 579 584 public static final double MATCH_SCORE_OTHER = 0.5; 585 } 586 | Popular Tags |