1 17 package org.apache.servicemix.jbi.jaxp; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import javax.xml.namespace.NamespaceContext ; 24 import javax.xml.stream.XMLStreamConstants; 25 import javax.xml.stream.XMLStreamException; 26 import javax.xml.stream.XMLStreamReader; 27 import javax.xml.stream.util.StreamReaderDelegate; 28 29 public class FragmentStreamReader extends StreamReaderDelegate implements XMLStreamReader { 30 31 private static final int STATE_START_DOC = 0; 32 private static final int STATE_FIRST_ELEM = 1; 33 private static final int STATE_FIRST_RUN = 2; 34 private static final int STATE_RUN = 3; 35 private static final int STATE_END_DOC = 4; 36 37 private int depth; 38 private int state = STATE_START_DOC; 39 private int event = START_DOCUMENT; 40 private List rootPrefixes; 41 42 public FragmentStreamReader(XMLStreamReader parent) { 43 super(parent); 44 rootPrefixes = new ArrayList (); 45 NamespaceContext ctx = getParent().getNamespaceContext(); 46 if (ctx instanceof ExtendedNamespaceContext) { 47 Iterator it = ((ExtendedNamespaceContext) ctx).getPrefixes(); 48 while (it.hasNext()) { 49 String prefix = (String ) it.next(); 50 rootPrefixes.add(prefix); 51 } 52 } 53 } 54 55 public int getEventType() { 56 return event; 57 } 58 59 public int next() throws XMLStreamException { 60 switch (state) { 61 case STATE_START_DOC: 62 state = STATE_FIRST_ELEM; 63 event = START_DOCUMENT; 64 break; 65 case STATE_FIRST_ELEM: 66 state = STATE_FIRST_RUN; 67 depth++; 68 event = START_ELEMENT; 69 break; 70 case STATE_FIRST_RUN: 71 state = STATE_RUN; 72 case STATE_RUN: 74 event = getParent().next(); 75 if (event == START_ELEMENT) { 76 depth++; 77 } else if (event == END_ELEMENT) { 78 depth--; 79 if (depth == 0) { 80 state = STATE_END_DOC; 81 } 82 } 83 break; 84 case STATE_END_DOC: 85 event = END_DOCUMENT; 86 break; 87 default: 88 throw new IllegalStateException (); 89 } 90 return event; 91 } 92 93 public int nextTag() throws XMLStreamException { 94 int eventType = next(); 95 while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) || (eventType == XMLStreamConstants.CDATA && isWhiteSpace()) || eventType == XMLStreamConstants.SPACE 98 || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION 99 || eventType == XMLStreamConstants.COMMENT) { 100 eventType = next(); 101 } 102 if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) { 103 throw new XMLStreamException("expected start or end tag", getLocation()); 104 } 105 return eventType; 106 } 107 108 public int getNamespaceCount() { 109 if (state == STATE_FIRST_RUN) { 110 return rootPrefixes.size(); 111 } else { 112 return getParent().getNamespaceCount(); 113 } 114 } 115 116 public String getNamespacePrefix(int i) { 117 if (state == STATE_FIRST_RUN) { 118 return (String ) rootPrefixes.get(i); 119 } else { 120 return getParent().getNamespacePrefix(i); 121 } 122 } 123 124 public String getNamespaceURI(int i) { 125 if (state == STATE_FIRST_RUN) { 126 return getParent().getNamespaceContext().getNamespaceURI((String ) rootPrefixes.get(i)); 127 } else { 128 return getParent().getNamespaceURI(i); 129 } 130 } 131 132 public String getNamespaceURI(String prefix) { 133 if (state == STATE_FIRST_RUN) { 134 return getParent().getNamespaceContext().getNamespaceURI(prefix); 135 } else { 136 return getParent().getNamespaceURI(prefix); 137 } 138 } 139 140 } 141 | Popular Tags |