1 package net.sf.saxon.value; 2 import net.sf.saxon.Configuration; 3 import net.sf.saxon.event.Receiver; 4 import net.sf.saxon.om.*; 5 import net.sf.saxon.pattern.NodeTest; 6 import net.sf.saxon.trans.XPathException; 7 import net.sf.saxon.type.Type; 8 9 import javax.xml.transform.SourceLocator ; 10 11 14 15 public final class TextFragmentValue implements DocumentInfo, FingerprintedNode, SourceLocator { 16 17 private CharSequence text; 18 private String systemId; 19 private TextFragmentTextNode textNode = null; private Configuration config; 21 private int documentNumber; 22 23 27 28 public TextFragmentValue(CharSequence value, String systemId) { 29 this.text = value; 30 this.systemId = systemId; 31 } 32 33 36 37 public void setConfiguration(Configuration config) { 38 this.config = config; 39 documentNumber = -1; } 42 43 47 48 public Configuration getConfiguration() { 49 return config; 50 } 51 52 55 56 public NamePool getNamePool() { 57 return config.getNamePool(); 58 } 59 60 63 64 public int getDocumentNumber() { 65 if (documentNumber == -1) { 66 documentNumber = config.getDocumentNumberAllocator().allocateDocumentNumber(); 67 } 70 return documentNumber; 71 } 72 73 77 78 public final int getNodeKind() { 79 return Type.DOCUMENT; 80 } 81 82 85 86 public String getStringValue() { 87 return text.toString(); 88 } 89 90 94 95 public CharSequence getStringValueCS() { 96 return text; 97 } 98 99 104 105 public boolean isSameNodeInfo(NodeInfo other) { 106 return this==other; 107 } 108 109 113 114 public String generateId() { 115 return "tt" + getDocumentNumber(); 116 } 117 118 121 122 public void setSystemId(String systemId) { 123 this.systemId = systemId; 124 } 125 126 129 130 public String getSystemId() { 131 return systemId; 132 } 133 134 138 139 public String getBaseURI() { 140 return systemId; 141 } 142 143 151 152 public int compareOrder(NodeInfo other) { 153 if (this==other) return 0; 154 return -1; 155 } 156 157 160 161 public int getNameCode() { 162 return -1; 163 } 164 165 168 169 public int getFingerprint() { 170 return -1; 171 } 172 173 174 178 179 public String getPrefix() { 180 return ""; 181 } 182 183 189 190 public String getURI() { 191 return ""; 192 } 193 194 200 201 public String getDisplayName() { 202 return ""; 203 } 204 205 210 211 public String getLocalPart() { 212 return ""; 213 } 214 215 220 221 public boolean hasChildNodes() { 222 return !("".equals(text)); 223 } 224 225 231 232 public int getLineNumber() { 233 return -1; 234 } 235 236 244 245 public int getTypeAnnotation() { 246 return -1; 247 } 248 249 256 257 public void sendNamespaceDeclarations(Receiver out, boolean includeAncestors) { 258 } 259 260 275 276 public int[] getDeclaredNamespaces(int[] buffer) { 277 return null; 278 } 279 280 285 286 public SequenceIterator getTypedValue() { 287 return SingletonIterator.makeIterator(new UntypedAtomicValue(text)); 288 } 289 290 300 301 public Value atomize() { 302 return new UntypedAtomicValue(text); 303 } 304 305 316 public String getPublicId() { 317 return null; 318 } 319 320 335 public int getColumnNumber() { 336 return -1; 337 } 338 339 344 345 public String getAttributeValue(int fingerprint) { 346 return null; 347 } 348 349 355 356 public AxisIterator iterateAxis(byte axisNumber) { 357 switch (axisNumber) { 358 case Axis.ANCESTOR: 359 case Axis.ATTRIBUTE: 360 case Axis.FOLLOWING: 361 case Axis.FOLLOWING_SIBLING: 362 case Axis.NAMESPACE: 363 case Axis.PARENT: 364 case Axis.PRECEDING: 365 case Axis.PRECEDING_SIBLING: 366 case Axis.PRECEDING_OR_ANCESTOR: 367 return EmptyIterator.getInstance(); 368 369 case Axis.SELF: 370 case Axis.ANCESTOR_OR_SELF: 371 return SingletonIterator.makeIterator(this); 372 373 case Axis.CHILD: 374 case Axis.DESCENDANT: 375 return SingletonIterator.makeIterator(getTextNode()); 376 377 case Axis.DESCENDANT_OR_SELF: 378 Item[] nodes = {this, getTextNode()}; 379 return new ArrayIterator(nodes); 380 381 default: 382 throw new IllegalArgumentException ("Unknown axis number " + axisNumber); 383 } 384 } 385 386 393 394 public AxisIterator iterateAxis(byte axisNumber, NodeTest nodeTest) { 395 switch (axisNumber) { 396 case Axis.ANCESTOR: 397 case Axis.ATTRIBUTE: 398 case Axis.FOLLOWING: 399 case Axis.FOLLOWING_SIBLING: 400 case Axis.NAMESPACE: 401 case Axis.PARENT: 402 case Axis.PRECEDING: 403 case Axis.PRECEDING_SIBLING: 404 case Axis.PRECEDING_OR_ANCESTOR: 405 return EmptyIterator.getInstance(); 406 407 case Axis.SELF: 408 case Axis.ANCESTOR_OR_SELF: 409 return SingletonIterator.makeIterator(this); 410 411 case Axis.CHILD: 412 case Axis.DESCENDANT: 413 NodeInfo textNode = getTextNode(); 414 if (nodeTest.matches(textNode)) { 415 return SingletonIterator.makeIterator(textNode); 416 } else { 417 return EmptyIterator.getInstance(); 418 } 419 420 case Axis.DESCENDANT_OR_SELF: 421 NodeInfo textNode2 = getTextNode(); 422 if (nodeTest.matches(textNode2)) { 423 Item[] nodes = {this, textNode2}; 424 return new ArrayIterator(nodes); 425 } else { 426 return SingletonIterator.makeIterator(this); 427 } 428 429 default: 430 throw new IllegalArgumentException ("Unknown axis number " + axisNumber); 431 } 432 } 433 434 438 439 public NodeInfo getParent() { 440 return null; 441 } 442 443 447 448 public NodeInfo getRoot() { 449 return this; 450 } 451 452 456 457 public DocumentInfo getDocumentRoot() { 458 return this; 459 } 460 461 464 465 public void copy(Receiver out, int whichNamespaces, boolean copyAnnotations, int locationId) 466 throws XPathException { 467 out.characters(text, 0, 0); 468 } 469 470 475 476 public NodeInfo selectID(String id) { 477 return null; 478 } 479 480 485 486 public String [] getUnparsedEntity(String name) { 487 return null; 488 } 489 490 491 494 495 private TextFragmentTextNode getTextNode() { 496 if (textNode==null) { 497 textNode = new TextFragmentTextNode(); 498 } 499 return textNode; 500 } 501 502 505 506 private class TextFragmentTextNode implements NodeInfo, FingerprintedNode, SourceLocator { 507 508 511 512 public void setSystemId(String systemId) {} 513 514 517 518 public Configuration getConfiguration() { 519 return config; 520 } 521 522 526 527 public NamePool getNamePool() { 528 return config.getNamePool(); 529 } 530 531 535 536 public final int getNodeKind() { 537 return Type.TEXT; 538 } 539 540 543 544 public String getStringValue() { 545 return text.toString(); 546 } 547 548 552 553 public CharSequence getStringValueCS() { 554 return text; 555 } 556 557 562 563 public boolean isSameNodeInfo(NodeInfo other) { 564 return this==other; 565 } 566 567 571 572 public String generateId() { 573 return "tt" + getDocumentNumber() + "t1"; 574 } 575 576 579 580 public String getSystemId() { 581 return systemId; 582 } 583 584 588 589 public String getBaseURI() { 590 return systemId; 591 } 592 593 601 602 public int compareOrder(NodeInfo other) { 603 if (this==other) return 0; 604 return +1; 605 } 606 607 610 611 public int getNameCode() { 612 return -1; 613 } 614 615 618 619 public int getFingerprint() { 620 return -1; 621 } 622 623 624 628 629 public String getPrefix() { 630 return ""; 631 } 632 633 639 640 public String getURI() { 641 return ""; 642 } 643 644 650 651 public String getDisplayName() { 652 return ""; 653 } 654 655 660 661 public String getLocalPart() { 662 return ""; 663 } 664 665 670 671 public boolean hasChildNodes() { 672 return false; 673 } 674 675 680 681 public String getAttributeValue(int fingerprint) { 682 return null; 683 } 684 685 691 692 public int getLineNumber() { 693 return -1; 694 } 695 696 704 705 public int getTypeAnnotation() { 706 return -1; 707 } 708 709 713 714 public int getDocumentNumber() { 715 return getDocumentRoot().getDocumentNumber(); 716 } 717 718 725 726 public void sendNamespaceDeclarations(Receiver out, boolean includeAncestors) { 727 } 728 729 744 745 public int[] getDeclaredNamespaces(int[] buffer) { 746 return null; 747 } 748 749 757 758 public SequenceIterator getTypedValue() throws XPathException { 759 return SingletonIterator.makeIterator(new UntypedAtomicValue(text)); 760 } 761 762 772 773 public Value atomize() throws XPathException { 774 return new UntypedAtomicValue(text); 775 } 776 777 788 public String getPublicId() { 789 return null; 790 } 791 792 807 public int getColumnNumber() { 808 return -1; 809 } 810 811 816 817 public AxisIterator iterateAxis(byte axisNumber) { 818 switch (axisNumber) { 819 case Axis.ANCESTOR: 820 case Axis.PARENT: 821 case Axis.PRECEDING_OR_ANCESTOR: 822 return SingletonIterator.makeIterator(TextFragmentValue.this); 823 824 case Axis.ANCESTOR_OR_SELF: 825 Item[] nodes = {this, TextFragmentValue.this}; 826 return new ArrayIterator(nodes); 827 828 case Axis.ATTRIBUTE: 829 case Axis.CHILD: 830 case Axis.DESCENDANT: 831 case Axis.FOLLOWING: 832 case Axis.FOLLOWING_SIBLING: 833 case Axis.NAMESPACE: 834 case Axis.PRECEDING: 835 case Axis.PRECEDING_SIBLING: 836 return EmptyIterator.getInstance(); 837 838 case Axis.SELF: 839 case Axis.DESCENDANT_OR_SELF: 840 return SingletonIterator.makeIterator(this); 841 842 default: 843 throw new IllegalArgumentException ("Unknown axis number " + axisNumber); 844 } 845 } 846 847 848 854 855 public AxisIterator iterateAxis( byte axisNumber, NodeTest nodeTest) { 856 switch (axisNumber) { 857 case Axis.ANCESTOR: 858 case Axis.PARENT: 859 case Axis.PRECEDING_OR_ANCESTOR: 860 if (nodeTest.matches(TextFragmentValue.this)) { 861 return SingletonIterator.makeIterator(TextFragmentValue.this); 862 } else { 863 return EmptyIterator.getInstance(); 864 } 865 866 case Axis.ANCESTOR_OR_SELF: 867 boolean matchesDoc = nodeTest.matches(TextFragmentValue.this); 868 boolean matchesText = nodeTest.matches(this); 869 if (matchesDoc && matchesText) { 870 Item[] nodes = {this, TextFragmentValue.this}; 871 return new ArrayIterator(nodes); 872 } else if (matchesDoc && !matchesText) { 873 return SingletonIterator.makeIterator(TextFragmentValue.this); 874 } else if (matchesText && !matchesDoc) { 875 return SingletonIterator.makeIterator(this); 876 } else { 877 return EmptyIterator.getInstance(); 878 } 879 880 case Axis.ATTRIBUTE: 881 case Axis.CHILD: 882 case Axis.DESCENDANT: 883 case Axis.FOLLOWING: 884 case Axis.FOLLOWING_SIBLING: 885 case Axis.NAMESPACE: 886 case Axis.PRECEDING: 887 case Axis.PRECEDING_SIBLING: 888 return EmptyIterator.getInstance(); 889 890 case Axis.SELF: 891 case Axis.DESCENDANT_OR_SELF: 892 if (nodeTest.matches(this)) { 893 return SingletonIterator.makeIterator(this); 894 } else { 895 return EmptyIterator.getInstance(); 896 } 897 898 default: 899 throw new IllegalArgumentException ("Unknown axis number " + axisNumber); 900 } 901 } 902 903 907 908 public NodeInfo getParent() { 909 return TextFragmentValue.this; 910 } 911 912 916 917 public NodeInfo getRoot() { 918 return TextFragmentValue.this; 919 } 920 921 925 926 public DocumentInfo getDocumentRoot() { 927 return TextFragmentValue.this; 928 } 929 930 933 934 public void copy(Receiver out, int namespaces, boolean copyAnnotations, int locationId) 935 throws XPathException { 936 out.characters(text, 0, 0); 937 } 938 939 } 940 941 } 942 943 961 | Popular Tags |