1 16 19 package org.apache.xalan.xsltc.dom; 20 21 import org.apache.xalan.xsltc.DOM; 22 import org.apache.xalan.xsltc.TransletException; 23 import org.apache.xalan.xsltc.StripFilter; 24 import org.apache.xalan.xsltc.runtime.Hashtable; 25 26 import org.apache.xml.dtm.DTM; 27 import org.apache.xml.dtm.DTMAxisIterator; 28 import org.apache.xml.dtm.DTMAxisTraverser; 29 import org.apache.xml.dtm.DTMManager; 30 import org.apache.xml.dtm.ref.DTMAxisIteratorBase; 31 import org.apache.xml.dtm.ref.DTMManagerDefault; 32 import org.apache.xml.serializer.EmptySerializer; 33 import org.apache.xml.serializer.SerializationHandler; 34 import org.apache.xml.utils.XMLString; 35 import org.apache.xml.utils.XMLStringDefault; 36 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.NodeList ; 39 40 import org.xml.sax.SAXException ; 41 42 import javax.xml.transform.SourceLocator ; 43 44 59 public class SimpleResultTreeImpl extends EmptySerializer implements DOM, DTM 60 { 61 62 70 public final class SimpleIterator extends DTMAxisIteratorBase 71 { 72 static final int DIRECTION_UP = 0; 73 static final int DIRECTION_DOWN = 1; 74 static final int NO_TYPE = -1; 75 76 int _direction = DIRECTION_DOWN; 79 80 int _type = NO_TYPE; 81 int _currentNode; 82 83 public SimpleIterator() 84 { 85 } 86 87 public SimpleIterator(int direction) 88 { 89 _direction = direction; 90 } 91 92 public SimpleIterator(int direction, int type) 93 { 94 _direction = direction; 95 _type = type; 96 } 97 98 public int next() 99 { 100 if (_direction == DIRECTION_DOWN) { 103 while (_currentNode < NUMBER_OF_NODES) { 104 if (_type != NO_TYPE) { 105 if ((_currentNode == RTF_ROOT && _type == DTM.ROOT_NODE) 106 || (_currentNode == RTF_TEXT && _type == DTM.TEXT_NODE)) 107 return returnNode(getNodeHandle(_currentNode++)); 108 else 109 _currentNode++; 110 } 111 else 112 return returnNode(getNodeHandle(_currentNode++)); 113 } 114 115 return END; 116 } 117 else { 119 while (_currentNode >= 0) { 120 if (_type != NO_TYPE) { 121 if ((_currentNode == RTF_ROOT && _type == DTM.ROOT_NODE) 122 || (_currentNode == RTF_TEXT && _type == DTM.TEXT_NODE)) 123 return returnNode(getNodeHandle(_currentNode--)); 124 else 125 _currentNode--; 126 } 127 else 128 return returnNode(getNodeHandle(_currentNode--)); 129 } 130 131 return END; 132 } 133 } 134 135 public DTMAxisIterator setStartNode(int nodeHandle) 136 { 137 int nodeID = getNodeIdent(nodeHandle); 138 _startNode = nodeID; 139 140 if (!_includeSelf && nodeID != DTM.NULL) { 142 if (_direction == DIRECTION_DOWN) 143 nodeID++; 144 else if (_direction == DIRECTION_UP) 145 nodeID--; 146 } 147 148 _currentNode = nodeID; 149 return this; 150 } 151 152 public void setMark() 153 { 154 _markedNode = _currentNode; 155 } 156 157 public void gotoMark() 158 { 159 _currentNode = _markedNode; 160 } 161 162 } 164 167 public final class SingletonIterator extends DTMAxisIteratorBase 168 { 169 static final int NO_TYPE = -1; 170 int _type = NO_TYPE; 171 int _currentNode; 172 173 public SingletonIterator() 174 { 175 } 176 177 public SingletonIterator(int type) 178 { 179 _type = type; 180 } 181 182 public void setMark() 183 { 184 _markedNode = _currentNode; 185 } 186 187 public void gotoMark() 188 { 189 _currentNode = _markedNode; 190 } 191 192 public DTMAxisIterator setStartNode(int nodeHandle) 193 { 194 _currentNode = _startNode = getNodeIdent(nodeHandle); 195 return this; 196 } 197 198 public int next() 199 { 200 if (_currentNode == END) 201 return END; 202 203 _currentNode = END; 204 205 if (_type != NO_TYPE) { 206 if ((_currentNode == RTF_ROOT && _type == DTM.ROOT_NODE) 207 || (_currentNode == RTF_TEXT && _type == DTM.TEXT_NODE)) 208 return getNodeHandle(_currentNode); 209 } 210 else 211 return getNodeHandle(_currentNode); 212 213 return END; 214 } 215 216 } 218 private final static DTMAxisIterator EMPTY_ITERATOR = 220 new DTMAxisIteratorBase() { 221 public DTMAxisIterator reset() { return this; } 222 public DTMAxisIterator setStartNode(int node) { return this; } 223 public int next() { return DTM.NULL; } 224 public void setMark() {} 225 public void gotoMark() {} 226 public int getLast() { return 0; } 227 public int getPosition() { return 0; } 228 public DTMAxisIterator cloneIterator() { return this; } 229 public void setRestartable(boolean isRestartable) { } 230 }; 231 232 233 public static final int RTF_ROOT = 0; 235 236 public static final int RTF_TEXT = 1; 238 239 public static final int NUMBER_OF_NODES = 2; 241 242 private static int _documentURIIndex = 0; 244 245 private static final String EMPTY_STR = ""; 247 248 private String _text; 251 252 protected String [] _textArray; 256 257 protected XSLTCDTMManager _dtmManager; 259 260 protected int _size = 0; 262 263 private int _documentID; 265 266 private BitArray _dontEscape = null; 268 269 private boolean _escaping = true; 271 272 public SimpleResultTreeImpl(XSLTCDTMManager dtmManager, int documentID) 274 { 275 _dtmManager = dtmManager; 276 _documentID = documentID; 277 _textArray = new String [4]; 278 } 279 280 public DTMManagerDefault getDTMManager() 281 { 282 return _dtmManager; 283 } 284 285 public int getDocument() 287 { 288 return _documentID; 289 } 290 291 public String getStringValue() 293 { 294 return _text; 295 } 296 297 public DTMAxisIterator getIterator() 298 { 299 return new SingletonIterator(getDocument()); 300 } 301 302 public DTMAxisIterator getChildren(final int node) 303 { 304 return new SimpleIterator().setStartNode(node); 305 } 306 307 public DTMAxisIterator getTypedChildren(final int type) 308 { 309 return new SimpleIterator(SimpleIterator.DIRECTION_DOWN, type); 310 } 311 312 public DTMAxisIterator getAxisIterator(final int axis) 315 { 316 switch (axis) 317 { 318 case Axis.CHILD: 319 case Axis.DESCENDANT: 320 return new SimpleIterator(SimpleIterator.DIRECTION_DOWN); 321 case Axis.PARENT: 322 case Axis.ANCESTOR: 323 return new SimpleIterator(SimpleIterator.DIRECTION_UP); 324 case Axis.ANCESTORORSELF: 325 return (new SimpleIterator(SimpleIterator.DIRECTION_UP)).includeSelf(); 326 case Axis.DESCENDANTORSELF: 327 return (new SimpleIterator(SimpleIterator.DIRECTION_DOWN)).includeSelf(); 328 case Axis.SELF: 329 return new SingletonIterator(); 330 default: 331 return EMPTY_ITERATOR; 332 } 333 } 334 335 public DTMAxisIterator getTypedAxisIterator(final int axis, final int type) 336 { 337 switch (axis) 338 { 339 case Axis.CHILD: 340 case Axis.DESCENDANT: 341 return new SimpleIterator(SimpleIterator.DIRECTION_DOWN, type); 342 case Axis.PARENT: 343 case Axis.ANCESTOR: 344 return new SimpleIterator(SimpleIterator.DIRECTION_UP, type); 345 case Axis.ANCESTORORSELF: 346 return (new SimpleIterator(SimpleIterator.DIRECTION_UP, type)).includeSelf(); 347 case Axis.DESCENDANTORSELF: 348 return (new SimpleIterator(SimpleIterator.DIRECTION_DOWN, type)).includeSelf(); 349 case Axis.SELF: 350 return new SingletonIterator(type); 351 default: 352 return EMPTY_ITERATOR; 353 } 354 } 355 356 public DTMAxisIterator getNthDescendant(int node, int n, boolean includeself) 358 { 359 return null; 360 } 361 362 public DTMAxisIterator getNamespaceAxisIterator(final int axis, final int ns) 363 { 364 return null; 365 } 366 367 public DTMAxisIterator getNodeValueIterator(DTMAxisIterator iter, int returnType, 369 String value, boolean op) 370 { 371 return null; 372 } 373 374 public DTMAxisIterator orderNodes(DTMAxisIterator source, int node) 375 { 376 return source; 377 } 378 379 public String getNodeName(final int node) 380 { 381 if (getNodeIdent(node) == RTF_TEXT) 382 return "#text"; 383 else 384 return EMPTY_STR; 385 } 386 387 public String getNodeNameX(final int node) 388 { 389 return EMPTY_STR; 390 } 391 392 public String getNamespaceName(final int node) 393 { 394 return EMPTY_STR; 395 } 396 397 public int getExpandedTypeID(final int nodeHandle) 399 { 400 int nodeID = getNodeIdent(nodeHandle); 401 if (nodeID == RTF_TEXT) 402 return DTM.TEXT_NODE; 403 else if (nodeID == RTF_ROOT) 404 return DTM.ROOT_NODE; 405 else 406 return DTM.NULL; 407 } 408 409 public int getNamespaceType(final int node) 410 { 411 return 0; 412 } 413 414 public int getParent(final int nodeHandle) 415 { 416 int nodeID = getNodeIdent(nodeHandle); 417 return (nodeID == RTF_TEXT) ? getNodeHandle(RTF_ROOT) : DTM.NULL; 418 } 419 420 public int getAttributeNode(final int gType, final int element) 421 { 422 return DTM.NULL; 423 } 424 425 public String getStringValueX(final int nodeHandle) 426 { 427 int nodeID = getNodeIdent(nodeHandle); 428 if (nodeID == RTF_ROOT || nodeID == RTF_TEXT) 429 return _text; 430 else 431 return EMPTY_STR; 432 } 433 434 public void copy(final int node, SerializationHandler handler) 435 throws TransletException 436 { 437 characters(node, handler); 438 } 439 440 public void copy(DTMAxisIterator nodes, SerializationHandler handler) 441 throws TransletException 442 { 443 int node; 444 while ((node = nodes.next()) != DTM.NULL) 445 { 446 copy(node, handler); 447 } 448 } 449 450 public String shallowCopy(final int node, SerializationHandler handler) 451 throws TransletException 452 { 453 characters(node, handler); 454 return null; 455 } 456 457 public boolean lessThan(final int node1, final int node2) 458 { 459 if (node1 == DTM.NULL) { 460 return false; 461 } 462 else if (node2 == DTM.NULL) { 463 return true; 464 } 465 else 466 return (node1 < node2); 467 } 468 469 475 public void characters(final int node, SerializationHandler handler) 476 throws TransletException 477 { 478 int nodeID = getNodeIdent(node); 479 if (nodeID == RTF_ROOT || nodeID == RTF_TEXT) { 480 boolean escapeBit = false; 481 boolean oldEscapeSetting = false; 482 483 try { 484 for (int i = 0; i < _size; i++) { 485 486 if (_dontEscape != null) { 487 escapeBit = _dontEscape.getBit(i); 488 if (escapeBit) { 489 oldEscapeSetting = handler.setEscaping(false); 490 } 491 } 492 493 handler.characters(_textArray[i]); 494 495 if (escapeBit) { 496 handler.setEscaping(oldEscapeSetting); 497 } 498 } 499 } catch (SAXException e) { 500 throw new TransletException(e); 501 } 502 } 503 } 504 505 public Node makeNode(int index) 507 { 508 return null; 509 } 510 511 public Node makeNode(DTMAxisIterator iter) 512 { 513 return null; 514 } 515 516 public NodeList makeNodeList(int index) 517 { 518 return null; 519 } 520 521 public NodeList makeNodeList(DTMAxisIterator iter) 522 { 523 return null; 524 } 525 526 public String getLanguage(int node) 527 { 528 return null; 529 } 530 531 public int getSize() 532 { 533 return 2; 534 } 535 536 public String getDocumentURI(int node) 537 { 538 return "simple_rtf" + _documentURIIndex++; 539 } 540 541 public void setFilter(StripFilter filter) 542 { 543 } 544 545 public void setupMapping(String [] names, String [] uris, int[] types, String [] namespaces) 546 { 547 } 548 549 public boolean isElement(final int node) 550 { 551 return false; 552 } 553 554 public boolean isAttribute(final int node) 555 { 556 return false; 557 } 558 559 public String lookupNamespace(int node, String prefix) 560 throws TransletException 561 { 562 return null; 563 } 564 565 568 public int getNodeIdent(final int nodehandle) 569 { 570 return (nodehandle != DTM.NULL) ? (nodehandle - _documentID) : DTM.NULL; 571 } 572 573 576 public int getNodeHandle(final int nodeId) 577 { 578 return (nodeId != DTM.NULL) ? (nodeId + _documentID) : DTM.NULL; 579 } 580 581 public DOM getResultTreeFrag(int initialSize, int rtfType) 582 { 583 return null; 584 } 585 586 public DOM getResultTreeFrag(int initialSize, int rtfType, boolean addToManager) 587 { 588 return null; 589 } 590 591 public SerializationHandler getOutputDomBuilder() 592 { 593 return this; 594 } 595 596 public int getNSType(int node) 597 { 598 return 0; 599 } 600 601 public String getUnparsedEntityURI(String name) 602 { 603 return null; 604 } 605 606 public Hashtable getElementsWithIDs() 607 { 608 return null; 609 } 610 611 612 613 618 619 public void startDocument() throws SAXException 620 { 621 622 } 623 624 public void endDocument() throws SAXException 625 { 626 if (_size == 1) 628 _text = _textArray[0]; 629 else { 630 StringBuffer buffer = new StringBuffer (); 631 for (int i = 0; i < _size; i++) { 632 buffer.append(_textArray[i]); 633 } 634 _text = buffer.toString(); 635 } 636 } 637 638 public void characters(String str) throws SAXException 639 { 640 if (_size >= _textArray.length) { 642 String [] newTextArray = new String [_textArray.length * 2]; 643 System.arraycopy(_textArray, 0, newTextArray, 0, _textArray.length); 644 _textArray = newTextArray; 645 } 646 647 if (!_escaping) { 650 if (_dontEscape == null) { 652 _dontEscape = new BitArray(8); 653 } 654 655 if (_size >= _dontEscape.size()) 657 _dontEscape.resize(_dontEscape.size() * 2); 658 659 _dontEscape.setBit(_size); 660 } 661 662 _textArray[_size++] = str; 663 } 664 665 public void characters(char[] ch, int offset, int length) 666 throws SAXException 667 { 668 if (_size >= _textArray.length) { 669 String [] newTextArray = new String [_textArray.length * 2]; 670 System.arraycopy(_textArray, 0, newTextArray, 0, _textArray.length); 671 _textArray = newTextArray; 672 } 673 674 if (!_escaping) { 675 if (_dontEscape == null) { 676 _dontEscape = new BitArray(8); 677 } 678 679 if (_size >= _dontEscape.size()) 680 _dontEscape.resize(_dontEscape.size() * 2); 681 682 _dontEscape.setBit(_size); 683 } 684 685 _textArray[_size++] = new String (ch, offset, length); 686 687 } 688 689 public boolean setEscaping(boolean escape) throws SAXException 690 { 691 final boolean temp = _escaping; 692 _escaping = escape; 693 return temp; 694 } 695 696 697 698 706 707 public void setFeature(String featureId, boolean state) 708 { 709 } 710 711 public void setProperty(String property, Object value) 712 { 713 } 714 715 public DTMAxisTraverser getAxisTraverser(final int axis) 716 { 717 return null; 718 } 719 720 public boolean hasChildNodes(int nodeHandle) 721 { 722 return (getNodeIdent(nodeHandle) == RTF_ROOT); 723 } 724 725 public int getFirstChild(int nodeHandle) 726 { 727 int nodeID = getNodeIdent(nodeHandle); 728 if (nodeID == RTF_ROOT) 729 return getNodeHandle(RTF_TEXT); 730 else 731 return DTM.NULL; 732 } 733 734 public int getLastChild(int nodeHandle) 735 { 736 return getFirstChild(nodeHandle); 737 } 738 739 public int getAttributeNode(int elementHandle, String namespaceURI, String name) 740 { 741 return DTM.NULL; 742 } 743 744 public int getFirstAttribute(int nodeHandle) 745 { 746 return DTM.NULL; 747 } 748 749 public int getFirstNamespaceNode(int nodeHandle, boolean inScope) 750 { 751 return DTM.NULL; 752 } 753 754 public int getNextSibling(int nodeHandle) 755 { 756 return DTM.NULL; 757 } 758 759 public int getPreviousSibling(int nodeHandle) 760 { 761 return DTM.NULL; 762 } 763 764 public int getNextAttribute(int nodeHandle) 765 { 766 return DTM.NULL; 767 } 768 769 public int getNextNamespaceNode(int baseHandle, int namespaceHandle, 770 boolean inScope) 771 { 772 return DTM.NULL; 773 } 774 775 public int getOwnerDocument(int nodeHandle) 776 { 777 return getDocument(); 778 } 779 780 public int getDocumentRoot(int nodeHandle) 781 { 782 return getDocument(); 783 } 784 785 public XMLString getStringValue(int nodeHandle) 786 { 787 return new XMLStringDefault(getStringValueX(nodeHandle)); 788 } 789 790 public int getStringValueChunkCount(int nodeHandle) 791 { 792 return 0; 793 } 794 795 public char[] getStringValueChunk(int nodeHandle, int chunkIndex, 796 int[] startAndLen) 797 { 798 return null; 799 } 800 801 public int getExpandedTypeID(String namespace, String localName, int type) 802 { 803 return DTM.NULL; 804 } 805 806 public String getLocalNameFromExpandedNameID(int ExpandedNameID) 807 { 808 return EMPTY_STR; 809 } 810 811 public String getNamespaceFromExpandedNameID(int ExpandedNameID) 812 { 813 return EMPTY_STR; 814 } 815 816 public String getLocalName(int nodeHandle) 817 { 818 return EMPTY_STR; 819 } 820 821 public String getPrefix(int nodeHandle) 822 { 823 return null; 824 } 825 826 public String getNamespaceURI(int nodeHandle) 827 { 828 return EMPTY_STR; 829 } 830 831 public String getNodeValue(int nodeHandle) 832 { 833 return (getNodeIdent(nodeHandle) == RTF_TEXT) ? _text : null; 834 } 835 836 public short getNodeType(int nodeHandle) 837 { 838 int nodeID = getNodeIdent(nodeHandle); 839 if (nodeID == RTF_TEXT) 840 return DTM.TEXT_NODE; 841 else if (nodeID == RTF_ROOT) 842 return DTM.ROOT_NODE; 843 else 844 return DTM.NULL; 845 846 } 847 848 public short getLevel(int nodeHandle) 849 { 850 int nodeID = getNodeIdent(nodeHandle); 851 if (nodeID == RTF_TEXT) 852 return 2; 853 else if (nodeID == RTF_ROOT) 854 return 1; 855 else 856 return DTM.NULL; 857 } 858 859 public boolean isSupported(String feature, String version) 860 { 861 return false; 862 } 863 864 public String getDocumentBaseURI() 865 { 866 return EMPTY_STR; 867 } 868 869 public void setDocumentBaseURI(String baseURI) 870 { 871 } 872 873 public String getDocumentSystemIdentifier(int nodeHandle) 874 { 875 return null; 876 } 877 878 public String getDocumentEncoding(int nodeHandle) 879 { 880 return null; 881 } 882 883 public String getDocumentStandalone(int nodeHandle) 884 { 885 return null; 886 } 887 888 public String getDocumentVersion(int documentHandle) 889 { 890 return null; 891 } 892 893 public boolean getDocumentAllDeclarationsProcessed() 894 { 895 return false; 896 } 897 898 public String getDocumentTypeDeclarationSystemIdentifier() 899 { 900 return null; 901 } 902 903 public String getDocumentTypeDeclarationPublicIdentifier() 904 { 905 return null; 906 } 907 908 public int getElementById(String elementId) 909 { 910 return DTM.NULL; 911 } 912 913 public boolean supportsPreStripping() 914 { 915 return false; 916 } 917 918 public boolean isNodeAfter(int firstNodeHandle, int secondNodeHandle) 919 { 920 return lessThan(firstNodeHandle, secondNodeHandle); 921 } 922 923 public boolean isCharacterElementContentWhitespace(int nodeHandle) 924 { 925 return false; 926 } 927 928 public boolean isDocumentAllDeclarationsProcessed(int documentHandle) 929 { 930 return false; 931 } 932 933 public boolean isAttributeSpecified(int attributeHandle) 934 { 935 return false; 936 } 937 938 public void dispatchCharactersEvents( 939 int nodeHandle, 940 org.xml.sax.ContentHandler ch, 941 boolean normalize) 942 throws org.xml.sax.SAXException 943 { 944 } 945 946 public void dispatchToEvents(int nodeHandle, org.xml.sax.ContentHandler ch) 947 throws org.xml.sax.SAXException 948 { 949 } 950 951 public org.w3c.dom.Node getNode(int nodeHandle) 952 { 953 return makeNode(nodeHandle); 954 } 955 956 public boolean needsTwoThreads() 957 { 958 return false; 959 } 960 961 public org.xml.sax.ContentHandler getContentHandler() 962 { 963 return null; 964 } 965 966 public org.xml.sax.ext.LexicalHandler getLexicalHandler() 967 { 968 return null; 969 } 970 971 public org.xml.sax.EntityResolver getEntityResolver() 972 { 973 return null; 974 } 975 976 public org.xml.sax.DTDHandler getDTDHandler() 977 { 978 return null; 979 } 980 981 public org.xml.sax.ErrorHandler getErrorHandler() 982 { 983 return null; 984 } 985 986 public org.xml.sax.ext.DeclHandler getDeclHandler() 987 { 988 return null; 989 } 990 991 public void appendChild(int newChild, boolean clone, boolean cloneDepth) 992 { 993 } 994 995 public void appendTextChild(String str) 996 { 997 } 998 999 public SourceLocator getSourceLocatorFor(int node) 1000 { 1001 return null; 1002 } 1003 1004 public void documentRegistration() 1005 { 1006 } 1007 1008 public void documentRelease() 1009 { 1010 } 1011 1012 public void migrateTo(DTMManager manager) 1013 { 1014 } 1015} 1016 | Popular Tags |