1 16 package org.apache.axis2.om.impl.llom; 17 18 import org.apache.axis2.om.*; 19 import org.apache.axis2.om.impl.llom.exception.OMStreamingException; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import javax.xml.namespace.NamespaceContext ; 24 import javax.xml.namespace.QName ; 25 import javax.xml.stream.Location; 26 import javax.xml.stream.XMLStreamConstants; 27 import javax.xml.stream.XMLStreamException; 28 import javax.xml.stream.XMLStreamReader; 29 import java.util.Iterator ; 30 import java.util.Stack ; 31 32 36 public class OMStAXWrapper implements XMLStreamReader, XMLStreamConstants { 37 40 private Log log = LogFactory.getLog(getClass()); 41 42 45 private OMNavigator navigator; 46 47 50 private OMXMLParserWrapper builder; 51 52 55 private XMLStreamReader parser; 56 57 60 private OMNode rootNode; 61 62 65 private boolean isFirst = true; 66 67 72 75 private static final short NAVIGABLE = 0; 76 private static final short SWITCH_AT_NEXT = 1; 77 private static final short COMPLETED = 2; 78 private static final short SWITCHED = 3; 79 private static final short DOCUMENT_COMPLETE = 4; 80 81 82 85 private short state; 86 87 91 private int currentEvent = START_DOCUMENT; 92 93 98 101 boolean switchingAllowed = false; 102 103 106 private Stack elementStack = new Stack (); 107 108 112 115 private OMNode nextNode = null; 116 117 119 122 private OMNode currentNode = null; 123 124 126 129 private OMNode lastNode = null; 130 131 136 public void setAllowSwitching(boolean b) { 137 this.switchingAllowed = b; 138 } 139 140 145 public boolean isAllowSwitching() { 146 return switchingAllowed; 147 } 148 149 159 OMStAXWrapper(OMXMLParserWrapper builder, OMElement startNode) { 160 this(builder, startNode, false); 161 } 162 163 170 OMStAXWrapper(OMXMLParserWrapper builder, OMElement startNode, 171 boolean cache) { 172 173 this.navigator = new OMNavigator(startNode); 175 this.builder = builder; 176 this.rootNode = startNode; 177 178 currentNode = navigator.next(); 182 updateNextNode(); 183 switchingAllowed = !cache; 184 } 185 186 190 public String getPrefix() { 191 String returnStr = null; 192 if (parser != null) { 193 returnStr = parser.getPrefix(); 194 } else { 195 if ((currentEvent == START_ELEMENT) 196 || (currentEvent == END_ELEMENT)) { 197 OMNamespace ns = ((OMElement) lastNode).getNamespace(); 198 returnStr = (ns == null) 199 ? null 200 : ns.getPrefix(); 201 } 202 } 203 return returnStr; 204 } 205 206 210 public String getNamespaceURI() { 211 String returnStr = null; 212 if (parser != null) { 213 returnStr = parser.getNamespaceURI(); 214 } else { 215 if ((currentEvent == START_ELEMENT) 216 || (currentEvent == END_ELEMENT) 217 || (currentEvent == NAMESPACE)) { 218 OMNamespace ns = ((OMElement) lastNode).getNamespace(); 219 returnStr = (ns == null) 220 ? null 221 : ns.getName(); 222 } 223 } 224 return returnStr; 225 } 226 227 231 public boolean hasName() { 232 if (parser != null) { 233 return parser.hasName(); 234 } else { 235 return ((currentEvent == START_ELEMENT) 236 || (currentEvent == END_ELEMENT)); 237 } 238 } 239 240 244 public String getLocalName() { 245 String returnStr = null; 246 if (parser != null) { 247 returnStr = parser.getLocalName(); 248 } else { 249 if ((currentEvent == START_ELEMENT) 250 || (currentEvent == END_ELEMENT) 251 || (currentEvent == ENTITY_REFERENCE)) { 252 returnStr = ((OMElement) lastNode).getLocalName(); 253 } 254 } 255 return returnStr; 256 } 257 258 262 public QName getName() { 263 QName returnName = null; 264 if (parser != null) { 265 returnName = parser.getName(); 266 } else { 267 if ((currentEvent == START_ELEMENT) 268 || (currentEvent == END_ELEMENT)) { 269 returnName = getQName((OMElement) lastNode); 270 } 271 } 272 return returnName; 273 } 274 275 279 public boolean hasText() { 280 return ((currentEvent == CHARACTERS) || (currentEvent == DTD) 281 || (currentEvent == ENTITY_REFERENCE) 282 || (currentEvent == COMMENT) || (currentEvent == SPACE)); 283 } 284 285 289 public int getTextLength() { 290 int returnLength = 0; 291 if (parser != null) { 292 returnLength = parser.getTextLength(); 293 } else { 294 OMText textNode = (OMText) lastNode; 295 returnLength = textNode.getText().length(); 296 } 297 return returnLength; 298 } 299 300 304 public int getTextStart() { 305 int returnLength = 0; 306 if (parser != null) { 307 returnLength = parser.getTextStart(); 308 } 309 310 return returnLength; 312 } 313 314 323 public int getTextCharacters(int i, char[] chars, int i1, int i2) 324 throws XMLStreamException { 325 int returnLength = 0; 326 if (hasText()) { 327 if (parser != null) { 328 try { 329 returnLength = parser.getTextCharacters(i, chars, i1, i2); 330 } catch (XMLStreamException e) { 331 throw new OMStreamingException(e); 332 } 333 } 334 335 } 337 return returnLength; 338 } 339 340 344 public char[] getTextCharacters() { 345 char[] returnArray = null; 346 if (parser != null) { 347 returnArray = parser.getTextCharacters(); 348 } else { 349 if (hasText()) { 350 OMText textNode = (OMText) lastNode; 351 String str = textNode.getText(); 352 returnArray = str.toCharArray(); 353 } 354 } 355 return returnArray; 356 } 357 358 362 public String getText() { 363 String returnString = null; 364 if (parser != null) { 365 returnString = parser.getText(); 366 } else { 367 if (hasText()) { 368 OMText textNode = (OMText) lastNode; 369 returnString = textNode.getText(); 370 } 371 } 372 return returnString; 373 } 374 375 379 380 public int getEventType() { 382 return currentEvent; 383 } 384 385 390 public String getNamespaceURI(int i) { 391 String returnString = null; 392 if (parser != null) { 393 returnString = parser.getNamespaceURI(i); 394 } else { 395 if (isStartElement() || isEndElement() 396 || (currentEvent == NAMESPACE)) { 397 OMNamespace ns = (OMNamespace) getItemFromIterator( 398 ((OMElement) lastNode).getAllDeclaredNamespaces(), i); 399 returnString = (ns == null) 400 ? null 401 : ns.getName(); 402 } 403 } 404 return returnString; 405 } 406 407 412 public String getNamespacePrefix(int i) { 413 String returnString = null; 414 if (parser != null) { 415 returnString = parser.getNamespacePrefix(i); 416 } else { 417 if (isStartElement() || isEndElement() 418 || (currentEvent == NAMESPACE)) { 419 OMNamespace ns = (OMNamespace) getItemFromIterator( 420 ((OMElement) lastNode).getAllDeclaredNamespaces(), i); 421 returnString = (ns == null) 422 ? null 423 : ns.getPrefix(); 424 } 425 } 426 return returnString; 427 } 428 429 433 public int getNamespaceCount() { 434 int returnCount = 0; 435 if (parser != null) { 436 returnCount = parser.getNamespaceCount(); 437 } else { 438 if (isStartElement() || isEndElement() 439 || (currentEvent == NAMESPACE)) { 440 returnCount = 441 getCount(((OMElement) lastNode).getAllDeclaredNamespaces()); 442 } 443 } 444 return returnCount; 445 } 446 447 452 public boolean isAttributeSpecified(int i) { 453 boolean returnValue = false; 454 if (parser != null) { 455 returnValue = parser.isAttributeSpecified(i); 456 } else { 457 if (isStartElement() || (currentEvent == ATTRIBUTE)) { 458 459 } else { 461 throw new IllegalStateException ( 462 "attribute type accessed in illegal event!"); 463 } 464 } 465 return returnValue; 466 } 467 468 473 public String getAttributeValue(int i) { 474 String returnString = null; 475 if (parser != null) { 476 returnString = parser.getAttributeValue(i); 477 } else { 478 if (isStartElement() || (currentEvent == ATTRIBUTE)) { 479 OMAttribute attrib = getAttribute((OMElement) lastNode, i); 480 if (attrib != null) { 481 returnString = attrib.getValue(); 482 } 483 } else { 484 throw new IllegalStateException ( 485 "attribute type accessed in illegal event!"); 486 } 487 } 488 return returnString; 489 } 490 491 496 public String getAttributeType(int i) { 497 String returnString = null; 498 if (parser != null) { 499 returnString = parser.getAttributeType(i); 500 } else { 501 if (isStartElement() || (currentEvent == ATTRIBUTE)) { 502 503 } else { 505 throw new IllegalStateException ( 506 "attribute type accessed in illegal event!"); 507 } 508 } 509 return returnString; 510 } 511 512 517 public String getAttributePrefix(int i) { 518 String returnString = null; 519 if (parser != null) { 520 returnString = parser.getAttributePrefix(i); 521 } else { 522 if (isStartElement() || (currentEvent == ATTRIBUTE)) { 523 OMAttribute attrib = getAttribute((OMElement) lastNode, i); 524 if (attrib != null) { 525 OMNamespace nameSpace = attrib.getNamespace(); 526 if (nameSpace != null) { 527 returnString = nameSpace.getPrefix(); 528 } 529 } 530 } else { 531 throw new IllegalStateException ( 532 "attribute prefix accessed in illegal event!"); 533 } 534 } 535 return returnString; 536 } 537 538 543 public String getAttributeLocalName(int i) { 544 String returnString = null; 545 if (parser != null) { 546 returnString = parser.getAttributeLocalName(i); 547 } else { 548 if (isStartElement() || (currentEvent == ATTRIBUTE)) { 549 OMAttribute attrib = getAttribute((OMElement) lastNode, i); 550 if (attrib != null) { 551 returnString = attrib.getLocalName(); 552 } 553 } else { 554 throw new IllegalStateException ( 555 "attribute localName accessed in illegal event!"); 556 } 557 } 558 return returnString; 559 } 560 561 566 public String getAttributeNamespace(int i) { 567 String returnString = null; 568 if (parser != null) { 569 returnString = parser.getAttributeNamespace(i); 570 } else { 571 if (isStartElement() || (currentEvent == ATTRIBUTE)) { 572 OMAttribute attrib = getAttribute((OMElement) lastNode, i); 573 if (attrib != null) { 574 OMNamespace nameSpace = attrib.getNamespace(); 575 if (nameSpace != null) { 576 returnString = nameSpace.getName(); 577 } 578 } 579 } else { 580 throw new IllegalStateException ( 581 "attribute nameSpace accessed in illegal event!"); 582 } 583 } 584 return returnString; 585 } 586 587 592 public QName getAttributeName(int i) { 593 QName returnQName = null; 594 if (parser != null) { 595 returnQName = parser.getAttributeName(i); 596 } else { 597 if (isStartElement() || (currentEvent == ATTRIBUTE)) { 598 returnQName = getAttribute((OMElement) lastNode, i).getQName(); 599 } else { 600 throw new IllegalStateException ( 601 "attribute count accessed in illegal event!"); 602 } 603 } 604 return returnQName; 605 } 606 607 611 public int getAttributeCount() { 612 int returnCount = 0; 613 if (parser != null) { 614 returnCount = parser.getAttributeCount(); 615 } else { 616 if (isStartElement() || (currentEvent == ATTRIBUTE)) { 617 OMElement elt = (OMElement) lastNode; 618 returnCount = getCount(elt.getAttributes()); 619 } else { 620 throw new IllegalStateException ( 621 "attribute count accessed in illegal event!"); 622 } 623 } 624 return returnCount; 625 } 626 627 629 636 public String getAttributeValue(String s, String s1) { 637 throw new UnsupportedOperationException (); 638 } 639 640 645 public boolean isWhiteSpace() { 646 boolean b; 647 if (parser != null) { 648 b = parser.isWhiteSpace(); 649 } else { 650 b = (currentEvent == SPACE); 651 } 652 return b; 653 } 654 655 660 public boolean isCharacters() { 661 boolean b; 662 if (parser != null) { 663 b = parser.isCharacters(); 664 } else { 665 b = (currentEvent == CHARACTERS); 666 } 667 return b; 668 } 669 670 675 public boolean isEndElement() { 676 boolean b; 677 if (parser != null) { 678 b = parser.isEndElement(); 679 } else { 680 b = (currentEvent == END_ELEMENT); 681 } 682 return b; 683 } 684 685 692 public void require(int i, String s, String s1) throws XMLStreamException { 693 throw new XMLStreamException(); 694 } 695 696 701 public boolean isStartElement() { 702 boolean b; 703 if (parser != null) { 704 b = parser.isStartElement(); 705 } else { 706 b = (currentEvent == START_ELEMENT); 707 } 708 return b; 709 } 710 711 717 public String getNamespaceURI(String s) { 718 String returnString = null; 719 if (parser != null) { 720 returnString = parser.getNamespaceURI(s); 721 } else { 722 if (isStartElement() || isEndElement() 723 || (currentEvent == NAMESPACE)) { 724 725 } 727 } 728 return returnString; 729 } 730 731 736 public void close() throws XMLStreamException { 737 738 if (parser != null) { 740 parser.close(); 741 } 742 } 743 744 750 public boolean hasNext() throws XMLStreamException { 751 return !(state == DOCUMENT_COMPLETE); 752 } 753 754 762 public int nextTag() throws XMLStreamException { 763 throw new UnsupportedOperationException (); 764 } 765 766 771 public String getElementText() throws XMLStreamException { 772 String returnText = ""; 773 if (parser != null) { 774 try { 775 returnText = parser.getElementText(); 776 } catch (XMLStreamException e) { 777 throw new OMStreamingException(e); 778 } 779 } else { 780 if (currentNode.getType() == OMNode.ELEMENT_NODE) { 781 782 return null; 784 } 785 } 786 return returnText; 787 } 788 789 795 public int next() throws XMLStreamException { 796 switch (state) { 797 case DOCUMENT_COMPLETE: 798 throw new XMLStreamException("End of the document reached"); 799 case COMPLETED: 800 state = DOCUMENT_COMPLETE; 801 currentEvent = END_DOCUMENT; 802 break; 803 case SWITCH_AT_NEXT: 804 state = SWITCHED; 805 806 try { 808 parser = (XMLStreamReader) builder.getParser(); 809 } catch (Exception e) { 810 throw new UnsupportedOperationException ( 811 "incompatible parser found!"); 812 } 813 log.info( 814 "Switching to the Real Stax parser to generated the future events"); 815 816 if ((currentEvent == START_DOCUMENT) && (currentEvent == parser.getEventType())){ 817 currentEvent = parser.next(); 818 } else{ 819 currentEvent = parser.getEventType(); 820 } 821 updateCompleteStatus(); 822 break; 823 case NAVIGABLE: 824 currentEvent = generateEvents(currentNode); 825 updateCompleteStatus(); 826 updateLastNode(); 827 break; 828 case SWITCHED: 829 currentEvent = parser.next(); 830 updateCompleteStatus(); 831 break; 832 default : 833 throw new OMStreamingException("unsuppported state!"); 834 } 835 return currentEvent; 836 } 837 838 845 public Object getProperty(String s) throws IllegalArgumentException { 846 throw new UnsupportedOperationException (); 847 } 848 849 858 private void updateLastNode() throws XMLStreamException { 859 lastNode = currentNode; 860 currentNode = nextNode; 861 try { 862 updateNextNode(); 863 } catch (Exception e) { 864 throw new XMLStreamException(e); 865 } 866 } 867 868 871 private void updateNextNode() { 872 if (navigator.isNavigable()) { 873 nextNode = navigator.next(); 874 } else { 875 if (!switchingAllowed) { 876 if (navigator.isCompleted()) { 877 nextNode = null; 878 879 } else { 880 builder.next(); 881 navigator.step(); 882 nextNode = navigator.next(); 883 } 884 } else { 885 886 builder.setCache(false); 889 state = SWITCH_AT_NEXT; 890 } 891 } 892 } 893 894 897 private void updateCompleteStatus() { 898 if (state == NAVIGABLE) { 899 if (rootNode == currentNode) { 900 if (isFirst) { 901 isFirst = false; 902 } else { 903 state = COMPLETED; 904 } 905 } 906 } else { 907 state = (currentEvent == END_DOCUMENT) 908 ? DOCUMENT_COMPLETE 909 : state; 910 } 911 } 912 913 918 public NamespaceContext getNamespaceContext() { 919 throw new UnsupportedOperationException (); 920 } 921 922 927 public String getEncoding() { 928 return null; 929 } 930 931 936 public Location getLocation() { 937 throw new UnsupportedOperationException (); 938 } 939 940 945 public String getVersion() { 946 return "1.0"; } 949 950 951 956 public boolean isStandalone() { 957 return true; 958 } 959 960 965 public boolean standaloneSet() { 966 return false; 967 } 968 969 974 public String getCharacterEncodingScheme() { 975 return "utf-8"; 978 } 979 980 985 public String getPITarget() { 986 throw new UnsupportedOperationException (); 987 } 988 989 994 public String getPIData() { 995 throw new UnsupportedOperationException (); 996 } 997 998 1005 1006 1012 private int generateEvents(OMNode node) { 1013 int returnEvent = 0; 1014 int nodeType = node.getType(); 1015 switch (nodeType) { 1016 case OMNode.ELEMENT_NODE: 1017 OMElement element = (OMElement) node; 1018 returnEvent = generateElementEvents(element); 1022 break; 1023 case OMNode.TEXT_NODE: 1024 returnEvent = generateTextEvents(); 1025 break; 1026 case OMNode.COMMENT_NODE: 1027 returnEvent = generateCommentEvents(); 1028 break; 1029 case OMNode.CDATA_SECTION_NODE: 1030 returnEvent = generateCdataEvents(); 1031 break; 1032 default : 1033 break; } 1035 return returnEvent; 1036 } 1037 1038 1044 private int generateElementEvents(OMElement elt) { 1045 int returnValue = START_ELEMENT; 1046 if (!elementStack.isEmpty() && elementStack.peek().equals(elt)) { 1047 returnValue = END_ELEMENT; 1048 elementStack.pop(); 1049 } else { 1050 elementStack.push(elt); 1051 } 1052 return returnValue; 1053 } 1054 1055 1060 private int generateTextEvents() { 1061 return CHARACTERS; 1062 } 1063 1064 1069 private int generateCommentEvents() { 1070 return COMMENT; 1071 } 1072 1073 1078 private int generateCdataEvents() { 1079 return CDATA; 1080 } 1081 1082 1087 1088 1094 private int getCount(Iterator it) { 1095 int count = 0; 1096 if (it != null) { 1097 while (it.hasNext()) { 1098 it.next(); 1099 count++; 1100 } 1101 } 1102 return count; 1103 } 1104 1105 1112 private Object getItemFromIterator(Iterator it, int index) { 1113 int count = 0; 1114 Object returnObject = null; 1115 boolean found = false; 1116 if (it != null) { 1117 while (it.hasNext()) { 1118 returnObject = it.next(); 1119 if (index == count++) { 1120 found = true; 1121 break; 1122 } 1123 } 1124 } 1125 if (found) { 1126 return returnObject; 1127 } else { 1128 return null; 1129 } 1130 } 1131 1132 1138 private QName getQName(OMElement element) { 1139 QName returnName; 1140 OMNamespace ns = element.getNamespace(); 1141 String localPart = element.getLocalName(); 1142 if (ns != null) { 1143 String prefix = ns.getPrefix(); 1144 String uri = ns.getName(); 1145 if ((prefix == null) || prefix.equals("")) { 1146 returnName = new QName (uri, localPart); 1147 } else { 1148 returnName = new QName (uri, localPart, prefix); 1149 } 1150 } else { 1151 returnName = new QName (localPart); 1152 } 1153 return returnName; 1154 } 1155 1156 1161 private OMAttribute getAttribute(OMElement elt, int index) { 1162 OMAttribute returnAttrib = null; 1163 if (elt != null) { 1164 returnAttrib = 1165 (OMAttribute) getItemFromIterator(elt.getAttributes(), index); 1166 } 1167 return returnAttrib; 1168 } 1169} 1170 | Popular Tags |