1 16 19 package com.sun.org.apache.xpath.internal.objects; 20 21 import java.io.Serializable ; 22 23 import com.sun.org.apache.xalan.internal.res.XSLMessages; 24 import com.sun.org.apache.xml.internal.dtm.DTM; 25 import com.sun.org.apache.xml.internal.dtm.DTMIterator; 26 import com.sun.org.apache.xml.internal.utils.XMLString; 27 import com.sun.org.apache.xpath.internal.Expression; 28 import com.sun.org.apache.xpath.internal.ExpressionOwner; 29 import com.sun.org.apache.xpath.internal.NodeSetDTM; 30 import com.sun.org.apache.xpath.internal.XPathContext; 31 import com.sun.org.apache.xpath.internal.XPathException; 32 import com.sun.org.apache.xpath.internal.XPathVisitor; 33 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources; 34 35 import org.w3c.dom.DocumentFragment ; 36 import org.w3c.dom.NodeList ; 37 import org.w3c.dom.traversal.NodeIterator; 38 39 46 public class XObject extends Expression implements Serializable , Cloneable 47 { 48 49 53 protected Object m_obj; 55 58 public XObject(){} 59 60 66 public XObject(Object obj) 67 { 68 m_obj = obj; 69 } 70 71 80 public XObject execute(XPathContext xctxt) 81 throws javax.xml.transform.TransformerException 82 { 83 return this; 84 } 85 86 96 public void allowDetachToRelease(boolean allowRelease){} 97 98 105 public void detach(){} 106 107 111 public void destruct() 112 { 113 114 if (null != m_obj) 115 { 116 allowDetachToRelease(true); 117 detach(); 118 119 m_obj = null; 120 } 121 } 122 123 126 public void reset() 127 { 128 } 129 130 141 public void dispatchCharactersEvents(org.xml.sax.ContentHandler ch) 142 throws org.xml.sax.SAXException 143 { 144 xstr().dispatchCharactersEvents(ch); 145 } 146 147 156 static public XObject create(Object val) 157 { 158 return XObjectFactory.create(val); 159 } 160 161 171 static public XObject create(Object val, XPathContext xctxt) 172 { 173 return XObjectFactory.create(val, xctxt); 174 } 175 176 177 public static final int CLASS_NULL = -1; 178 179 180 public static final int CLASS_UNKNOWN = 0; 181 182 183 public static final int CLASS_BOOLEAN = 1; 184 185 186 public static final int CLASS_NUMBER = 2; 187 188 189 public static final int CLASS_STRING = 3; 190 191 192 public static final int CLASS_NODESET = 4; 193 194 195 public static final int CLASS_RTREEFRAG = 5; 196 197 198 public static final int CLASS_UNRESOLVEDVARIABLE = 600; 199 200 205 public int getType() 206 { 207 return CLASS_UNKNOWN; 208 } 209 210 216 public String getTypeString() 217 { 218 return "#UNKNOWN (" + object().getClass().getName() + ")"; 219 } 220 221 228 public double num() throws javax.xml.transform.TransformerException 229 { 230 231 error(XPATHErrorResources.ER_CANT_CONVERT_TO_NUMBER, 232 new Object []{ getTypeString() }); 234 return 0.0; 235 } 236 237 244 public double numWithSideEffects() throws javax.xml.transform.TransformerException 245 { 246 return num(); 247 } 248 249 256 public boolean bool() throws javax.xml.transform.TransformerException 257 { 258 259 error(XPATHErrorResources.ER_CANT_CONVERT_TO_NUMBER, 260 new Object []{ getTypeString() }); 262 return false; 263 } 264 265 271 public boolean boolWithSideEffects() throws javax.xml.transform.TransformerException 272 { 273 return bool(); 274 } 275 276 277 282 public XMLString xstr() 283 { 284 return XMLStringFactoryImpl.getFactory().newstr(str()); 285 } 286 287 292 public String str() 293 { 294 return (m_obj != null) ? m_obj.toString() : ""; 295 } 296 297 303 public String toString() 304 { 305 return str(); 306 } 307 308 315 public int rtf(XPathContext support) 316 { 317 318 int result = rtf(); 319 320 if (DTM.NULL == result) 321 { 322 DTM frag = support.createDocumentFragment(); 323 324 frag.appendTextChild(str()); 326 327 result = frag.getDocument(); 328 } 329 330 return result; 331 } 332 333 340 public DocumentFragment rtree(XPathContext support) 341 { 342 DocumentFragment docFrag = null; 343 int result = rtf(); 344 345 if (DTM.NULL == result) 346 { 347 DTM frag = support.createDocumentFragment(); 348 349 frag.appendTextChild(str()); 351 352 docFrag = (DocumentFragment )frag.getNode(frag.getDocument()); 353 } 354 else 355 { 356 DTM frag = support.getDTM(result); 357 docFrag = (DocumentFragment )frag.getNode(frag.getDocument()); 358 } 359 360 return docFrag; 361 } 362 363 364 369 public DocumentFragment rtree() 370 { 371 return null; 372 } 373 374 379 public int rtf() 380 { 381 return DTM.NULL; 382 } 383 384 390 public Object object() 391 { 392 return m_obj; 393 } 394 395 402 public DTMIterator iter() throws javax.xml.transform.TransformerException 403 { 404 405 error(XPATHErrorResources.ER_CANT_CONVERT_TO_NODELIST, 406 new Object []{ getTypeString() }); 408 return null; 409 } 410 411 416 public XObject getFresh() 417 { 418 return this; 419 } 420 421 422 429 public NodeIterator nodeset() throws javax.xml.transform.TransformerException 430 { 431 432 error(XPATHErrorResources.ER_CANT_CONVERT_TO_NODELIST, 433 new Object []{ getTypeString() }); 435 return null; 436 } 437 438 445 public NodeList nodelist() throws javax.xml.transform.TransformerException 446 { 447 448 error(XPATHErrorResources.ER_CANT_CONVERT_TO_NODELIST, 449 new Object []{ getTypeString() }); 451 return null; 452 } 453 454 455 462 public NodeSetDTM mutableNodeset() 463 throws javax.xml.transform.TransformerException 464 { 465 466 error(XPATHErrorResources.ER_CANT_CONVERT_TO_MUTABLENODELIST, 467 new Object []{ getTypeString() }); 469 return (NodeSetDTM) m_obj; 470 } 471 472 482 public Object castToType(int t, XPathContext support) 483 throws javax.xml.transform.TransformerException 484 { 485 486 Object result; 487 488 switch (t) 489 { 490 case CLASS_STRING : 491 result = str(); 492 break; 493 case CLASS_NUMBER : 494 result = new Double (num()); 495 break; 496 case CLASS_NODESET : 497 result = iter(); 498 break; 499 case CLASS_BOOLEAN : 500 result = new Boolean (bool()); 501 break; 502 case CLASS_UNKNOWN : 503 result = m_obj; 504 break; 505 506 default : 511 error(XPATHErrorResources.ER_CANT_CONVERT_TO_TYPE, 512 new Object []{ getTypeString(), 513 Integer.toString(t) }); 515 result = null; 516 } 517 518 return result; 519 } 520 521 530 public boolean lessThan(XObject obj2) 531 throws javax.xml.transform.TransformerException 532 { 533 534 if (obj2.getType() == XObject.CLASS_NODESET) 540 return obj2.greaterThan(this); 541 542 return this.num() < obj2.num(); 543 } 544 545 554 public boolean lessThanOrEqual(XObject obj2) 555 throws javax.xml.transform.TransformerException 556 { 557 558 if (obj2.getType() == XObject.CLASS_NODESET) 564 return obj2.greaterThanOrEqual(this); 565 566 return this.num() <= obj2.num(); 567 } 568 569 578 public boolean greaterThan(XObject obj2) 579 throws javax.xml.transform.TransformerException 580 { 581 582 if (obj2.getType() == XObject.CLASS_NODESET) 588 return obj2.lessThan(this); 589 590 return this.num() > obj2.num(); 591 } 592 593 602 public boolean greaterThanOrEqual(XObject obj2) 603 throws javax.xml.transform.TransformerException 604 { 605 606 if (obj2.getType() == XObject.CLASS_NODESET) 612 return obj2.lessThanOrEqual(this); 613 614 return this.num() >= obj2.num(); 615 } 616 617 626 public boolean equals(XObject obj2) 627 { 628 629 if (obj2.getType() == XObject.CLASS_NODESET) 633 return obj2.equals(this); 634 635 if (null != m_obj) 636 { 637 return m_obj.equals(obj2.m_obj); 638 } 639 else 640 { 641 return obj2.m_obj == null; 642 } 643 } 644 645 654 public boolean notEquals(XObject obj2) 655 throws javax.xml.transform.TransformerException 656 { 657 658 if (obj2.getType() == XObject.CLASS_NODESET) 662 return obj2.notEquals(this); 663 664 return !equals(obj2); 665 } 666 667 675 protected void error(String msg) 676 throws javax.xml.transform.TransformerException 677 { 678 error(msg, null); 679 } 680 681 690 protected void error(String msg, Object [] args) 691 throws javax.xml.transform.TransformerException 692 { 693 694 String fmsg = XSLMessages.createXPATHMessage(msg, args); 695 696 { 702 throw new XPathException(fmsg, this); 703 } 704 } 705 706 707 710 public void fixupVariables(java.util.Vector vars, int globalsSize) 711 { 712 } 714 715 716 723 public void appendToFsb(com.sun.org.apache.xml.internal.utils.FastStringBuffer fsb) 724 { 725 fsb.append(str()); 726 } 727 728 731 public void callVisitors(ExpressionOwner owner, XPathVisitor visitor) 732 { 733 assertion(false, "callVisitors should not be called for this object!!!"); 734 } 735 738 public boolean deepEquals(Expression expr) 739 { 740 if(!isSameClass(expr)) 741 return false; 742 743 if(!this.equals((XObject)expr)) 747 return false; 748 749 return true; 750 } 751 752 } 753 | Popular Tags |