1 16 19 package com.sun.org.apache.xpath.internal.patterns; 20 21 import com.sun.org.apache.xml.internal.dtm.DTM; 22 import com.sun.org.apache.xml.internal.dtm.DTMFilter; 23 import com.sun.org.apache.xpath.internal.Expression; 24 import com.sun.org.apache.xpath.internal.ExpressionOwner; 25 import com.sun.org.apache.xpath.internal.XPath; 26 import com.sun.org.apache.xpath.internal.XPathContext; 27 import com.sun.org.apache.xpath.internal.XPathVisitor; 28 import com.sun.org.apache.xpath.internal.objects.XNumber; 29 import com.sun.org.apache.xpath.internal.objects.XObject; 30 31 36 public class NodeTest extends Expression 37 { 38 39 43 public static final String WILD = "*"; 44 45 49 public static final String SUPPORTS_PRE_STRIPPING = 50 "http://xml.apache.org/xpath/features/whitespace-pre-stripping"; 51 52 56 protected int m_whatToShow; 57 58 62 public static final int SHOW_BYFUNCTION = 0x00010000; 63 64 71 public int getWhatToShow() 72 { 73 return m_whatToShow; 74 } 75 76 83 public void setWhatToShow(int what) 84 { 85 m_whatToShow = what; 86 } 87 88 92 String m_namespace; 93 94 99 public String getNamespace() 100 { 101 return m_namespace; 102 } 103 104 109 public void setNamespace(String ns) 110 { 111 m_namespace = ns; 112 } 113 114 118 protected String m_name; 119 120 125 public String getLocalName() 126 { 127 return (null == m_name) ? "" : m_name; 128 } 129 130 135 public void setLocalName(String name) 136 { 137 m_name = name; 138 } 139 140 149 XNumber m_score; 150 151 155 public static final XNumber SCORE_NODETEST = 156 new XNumber(XPath.MATCH_SCORE_NODETEST); 157 158 162 public static final XNumber SCORE_NSWILD = 163 new XNumber(XPath.MATCH_SCORE_NSWILD); 164 165 170 public static final XNumber SCORE_QNAME = 171 new XNumber(XPath.MATCH_SCORE_QNAME); 172 173 178 public static final XNumber SCORE_OTHER = 179 new XNumber(XPath.MATCH_SCORE_OTHER); 180 181 185 public static final XNumber SCORE_NONE = 186 new XNumber(XPath.MATCH_SCORE_NONE); 187 188 196 public NodeTest(int whatToShow, String namespace, String name) 197 { 198 initNodeTest(whatToShow, namespace, name); 199 } 200 201 207 public NodeTest(int whatToShow) 208 { 209 initNodeTest(whatToShow); 210 } 211 212 215 public boolean deepEquals(Expression expr) 216 { 217 if(!isSameClass(expr)) 218 return false; 219 220 NodeTest nt = (NodeTest)expr; 221 222 if(null != nt.m_name) 223 { 224 if(null == m_name) 225 return false; 226 else if(!nt.m_name.equals(m_name)) 227 return false; 228 } 229 else if(null != m_name) 230 return false; 231 232 if(null != nt.m_namespace) 233 { 234 if(null == m_namespace) 235 return false; 236 else if(!nt.m_namespace.equals(m_namespace)) 237 return false; 238 } 239 else if(null != m_namespace) 240 return false; 241 242 if(m_whatToShow != nt.m_whatToShow) 243 return false; 244 245 if(m_isTotallyWild != nt.m_isTotallyWild) 246 return false; 247 248 return true; 249 } 250 251 254 public NodeTest(){} 255 256 263 public void initNodeTest(int whatToShow) 264 { 265 266 m_whatToShow = whatToShow; 267 268 calcScore(); 269 } 270 271 281 public void initNodeTest(int whatToShow, String namespace, String name) 282 { 283 284 m_whatToShow = whatToShow; 285 m_namespace = namespace; 286 m_name = name; 287 288 calcScore(); 289 } 290 291 295 private boolean m_isTotallyWild; 296 297 301 public XNumber getStaticScore() 302 { 303 return m_score; 304 } 305 306 310 public void setStaticScore(XNumber score) 311 { 312 m_score = score; 313 } 314 315 318 protected void calcScore() 319 { 320 321 if ((m_namespace == null) && (m_name == null)) 322 m_score = SCORE_NODETEST; 323 else if (((m_namespace == WILD) || (m_namespace == null)) 324 && (m_name == WILD)) 325 m_score = SCORE_NODETEST; 326 else if ((m_namespace != WILD) && (m_name == WILD)) 327 m_score = SCORE_NSWILD; 328 else 329 m_score = SCORE_QNAME; 330 331 m_isTotallyWild = (m_namespace == null && m_name == WILD); 332 } 333 334 340 public double getDefaultScore() 341 { 342 return m_score.num(); 343 } 344 345 356 public static int getNodeTypeTest(int whatToShow) 357 { 358 if (0 != (whatToShow & DTMFilter.SHOW_ELEMENT)) 360 return DTM.ELEMENT_NODE; 361 362 if (0 != (whatToShow & DTMFilter.SHOW_ATTRIBUTE)) 363 return DTM.ATTRIBUTE_NODE; 364 365 if (0 != (whatToShow & DTMFilter.SHOW_TEXT)) 366 return DTM.TEXT_NODE; 367 368 if (0 != (whatToShow & DTMFilter.SHOW_DOCUMENT)) 369 return DTM.DOCUMENT_NODE; 370 371 if (0 != (whatToShow & DTMFilter.SHOW_DOCUMENT_FRAGMENT)) 372 return DTM.DOCUMENT_FRAGMENT_NODE; 373 374 if (0 != (whatToShow & DTMFilter.SHOW_NAMESPACE)) 375 return DTM.NAMESPACE_NODE; 376 377 if (0 != (whatToShow & DTMFilter.SHOW_COMMENT)) 378 return DTM.COMMENT_NODE; 379 380 if (0 != (whatToShow & DTMFilter.SHOW_PROCESSING_INSTRUCTION)) 381 return DTM.PROCESSING_INSTRUCTION_NODE; 382 383 if (0 != (whatToShow & DTMFilter.SHOW_DOCUMENT_TYPE)) 384 return DTM.DOCUMENT_TYPE_NODE; 385 386 if (0 != (whatToShow & DTMFilter.SHOW_ENTITY)) 387 return DTM.ENTITY_NODE; 388 389 if (0 != (whatToShow & DTMFilter.SHOW_ENTITY_REFERENCE)) 390 return DTM.ENTITY_REFERENCE_NODE; 391 392 if (0 != (whatToShow & DTMFilter.SHOW_NOTATION)) 393 return DTM.NOTATION_NODE; 394 395 if (0 != (whatToShow & DTMFilter.SHOW_CDATA_SECTION)) 396 return DTM.CDATA_SECTION_NODE; 397 398 399 return 0; 400 } 401 402 403 410 public static void debugWhatToShow(int whatToShow) 411 { 412 413 java.util.Vector v = new java.util.Vector (); 414 415 if (0 != (whatToShow & DTMFilter.SHOW_ATTRIBUTE)) 416 v.addElement("SHOW_ATTRIBUTE"); 417 418 if (0 != (whatToShow & DTMFilter.SHOW_NAMESPACE)) 419 v.addElement("SHOW_NAMESPACE"); 420 421 if (0 != (whatToShow & DTMFilter.SHOW_CDATA_SECTION)) 422 v.addElement("SHOW_CDATA_SECTION"); 423 424 if (0 != (whatToShow & DTMFilter.SHOW_COMMENT)) 425 v.addElement("SHOW_COMMENT"); 426 427 if (0 != (whatToShow & DTMFilter.SHOW_DOCUMENT)) 428 v.addElement("SHOW_DOCUMENT"); 429 430 if (0 != (whatToShow & DTMFilter.SHOW_DOCUMENT_FRAGMENT)) 431 v.addElement("SHOW_DOCUMENT_FRAGMENT"); 432 433 if (0 != (whatToShow & DTMFilter.SHOW_DOCUMENT_TYPE)) 434 v.addElement("SHOW_DOCUMENT_TYPE"); 435 436 if (0 != (whatToShow & DTMFilter.SHOW_ELEMENT)) 437 v.addElement("SHOW_ELEMENT"); 438 439 if (0 != (whatToShow & DTMFilter.SHOW_ENTITY)) 440 v.addElement("SHOW_ENTITY"); 441 442 if (0 != (whatToShow & DTMFilter.SHOW_ENTITY_REFERENCE)) 443 v.addElement("SHOW_ENTITY_REFERENCE"); 444 445 if (0 != (whatToShow & DTMFilter.SHOW_NOTATION)) 446 v.addElement("SHOW_NOTATION"); 447 448 if (0 != (whatToShow & DTMFilter.SHOW_PROCESSING_INSTRUCTION)) 449 v.addElement("SHOW_PROCESSING_INSTRUCTION"); 450 451 if (0 != (whatToShow & DTMFilter.SHOW_TEXT)) 452 v.addElement("SHOW_TEXT"); 453 454 int n = v.size(); 455 456 for (int i = 0; i < n; i++) 457 { 458 if (i > 0) 459 System.out.print(" | "); 460 461 System.out.print(v.elementAt(i)); 462 } 463 464 if (0 == n) 465 System.out.print("empty whatToShow: " + whatToShow); 466 467 System.out.println(); 468 } 469 470 480 private static final boolean subPartMatch(String p, String t) 481 { 482 483 return (p == t) || ((null != p) && ((t == WILD) || p.equals(t))); 486 } 487 488 498 private static final boolean subPartMatchNS(String p, String t) 499 { 500 501 return (p == t) 502 || ((null != p) 503 && ((p.length() > 0) 504 ? ((t == WILD) || p.equals(t)) : null == t)); 505 } 506 507 522 public XObject execute(XPathContext xctxt, int context) 523 throws javax.xml.transform.TransformerException 524 { 525 526 DTM dtm = xctxt.getDTM(context); 527 short nodeType = dtm.getNodeType(context); 528 529 if (m_whatToShow == DTMFilter.SHOW_ALL) 530 return m_score; 531 532 int nodeBit = (m_whatToShow & (0x00000001 << (nodeType - 1))); 533 534 switch (nodeBit) 535 { 536 case DTMFilter.SHOW_DOCUMENT_FRAGMENT : 537 case DTMFilter.SHOW_DOCUMENT : 538 return SCORE_OTHER; 539 case DTMFilter.SHOW_COMMENT : 540 return m_score; 541 case DTMFilter.SHOW_CDATA_SECTION : 542 case DTMFilter.SHOW_TEXT : 543 544 return m_score; 548 case DTMFilter.SHOW_PROCESSING_INSTRUCTION : 549 return subPartMatch(dtm.getNodeName(context), m_name) 550 ? m_score : SCORE_NONE; 551 552 case DTMFilter.SHOW_NAMESPACE : 565 { 566 String ns = dtm.getNodeValue(context); 567 568 return (subPartMatch(ns, m_name)) ? m_score : SCORE_NONE; 569 } 570 case DTMFilter.SHOW_ATTRIBUTE : 571 case DTMFilter.SHOW_ELEMENT : 572 { 573 return (m_isTotallyWild || (subPartMatchNS(dtm.getNamespaceURI(context), m_namespace) && subPartMatch(dtm.getLocalName(context), m_name))) 574 ? m_score : SCORE_NONE; 575 } 576 default : 577 return SCORE_NONE; 578 } } 580 581 596 public XObject execute(XPathContext xctxt, int context, 597 DTM dtm, int expType) 598 throws javax.xml.transform.TransformerException 599 { 600 601 if (m_whatToShow == DTMFilter.SHOW_ALL) 602 return m_score; 603 604 int nodeBit = (m_whatToShow & (0x00000001 605 << ((dtm.getNodeType(context)) - 1))); 606 607 switch (nodeBit) 608 { 609 case DTMFilter.SHOW_DOCUMENT_FRAGMENT : 610 case DTMFilter.SHOW_DOCUMENT : 611 return SCORE_OTHER; 612 case DTMFilter.SHOW_COMMENT : 613 return m_score; 614 case DTMFilter.SHOW_CDATA_SECTION : 615 case DTMFilter.SHOW_TEXT : 616 617 return m_score; 621 case DTMFilter.SHOW_PROCESSING_INSTRUCTION : 622 return subPartMatch(dtm.getNodeName(context), m_name) 623 ? m_score : SCORE_NONE; 624 625 case DTMFilter.SHOW_NAMESPACE : 638 { 639 String ns = dtm.getNodeValue(context); 640 641 return (subPartMatch(ns, m_name)) ? m_score : SCORE_NONE; 642 } 643 case DTMFilter.SHOW_ATTRIBUTE : 644 case DTMFilter.SHOW_ELEMENT : 645 { 646 return (m_isTotallyWild || (subPartMatchNS(dtm.getNamespaceURI(context), m_namespace) && subPartMatch(dtm.getLocalName(context), m_name))) 647 ? m_score : SCORE_NONE; 648 } 649 default : 650 return SCORE_NONE; 651 } } 653 654 667 public XObject execute(XPathContext xctxt) 668 throws javax.xml.transform.TransformerException 669 { 670 return execute(xctxt, xctxt.getCurrentNode()); 671 } 672 673 676 public void fixupVariables(java.util.Vector vars, int globalsSize) 677 { 678 } 680 681 684 public void callVisitors(ExpressionOwner owner, XPathVisitor visitor) 685 { 686 assertion(false, "callVisitors should not be called for this object!!!"); 687 } 688 689 } 690 | Popular Tags |