1 17 package org.apache.servicemix.jbi.jaxp; 18 19 23 24 import java.util.HashMap ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import javax.xml.stream.Location; 29 import javax.xml.stream.XMLStreamConstants; 30 import javax.xml.stream.XMLStreamException; 31 import javax.xml.stream.XMLStreamReader; 32 33 import org.apache.servicemix.jbi.util.FastStack; 34 35 41 public abstract class DOMStreamReader implements XMLStreamReader { 42 public Map properties = new HashMap (); 43 44 private FastStack frames = new FastStack(); 45 46 private ElementFrame frame; 47 48 private int currentEvent = XMLStreamConstants.START_DOCUMENT; 49 50 53 public static class ElementFrame { 54 public ElementFrame(Object element, ElementFrame parent) { 55 this.element = element; 56 this.parent = parent; 57 } 58 59 Object element; 60 61 boolean started = false; 62 63 boolean ended = false; 64 65 int currentChild = -1; 66 67 int currentAttribute = -1; 68 69 int currentNamespace = -1; 70 71 int currentElement = -1; 72 73 List uris; 74 75 List prefixes; 76 77 List attributes; 78 79 List allAttributes; 80 81 final ElementFrame parent; 82 } 83 84 87 public DOMStreamReader(ElementFrame frame) { 88 this.frame = frame; 89 frames.push(this.frame); 90 newFrame(frame); 91 } 92 93 protected ElementFrame getCurrentFrame() { 94 return frame; 95 } 96 97 100 public Object getProperty(String key) throws IllegalArgumentException { 101 return properties.get(key); 102 } 103 104 107 public int next() throws XMLStreamException { 108 if (frame.ended) { 109 frames.pop(); 110 if (!frames.empty()) { 111 frame = (ElementFrame) frames.peek(); 112 } else { 113 currentEvent = END_DOCUMENT; 114 return currentEvent; 115 } 116 } 117 118 if (!frame.started) { 119 frame.started = true; 120 currentEvent = START_ELEMENT; 121 } else if (frame.currentAttribute < getAttributeCount() - 1) { 122 frame.currentAttribute++; 123 currentEvent = ATTRIBUTE; 124 } else if (frame.currentNamespace < getNamespaceCount() - 1) { 125 frame.currentNamespace++; 126 currentEvent = NAMESPACE; 127 } else if (frame.currentChild < getChildCount() - 1) { 128 frame.currentChild++; 129 130 currentEvent = moveToChild(frame.currentChild); 131 132 if (currentEvent == START_ELEMENT) { 133 ElementFrame newFrame = getChildFrame(frame.currentChild); 134 newFrame.started = true; 135 frame = newFrame; 136 frames.push(this.frame); 137 currentEvent = START_ELEMENT; 138 139 newFrame(newFrame); 140 } 141 } else { 142 frame.ended = true; 143 currentEvent = END_ELEMENT; 144 endElement(); 145 } 146 return currentEvent; 147 } 148 149 protected void newFrame(ElementFrame newFrame) { 150 } 151 152 protected void endElement() { 153 } 154 155 protected abstract int moveToChild(int currentChild); 156 157 protected abstract ElementFrame getChildFrame(int currentChild); 158 159 protected abstract int getChildCount(); 160 161 164 public void require(int arg0, String arg1, String arg2) throws XMLStreamException { 165 throw new UnsupportedOperationException (); 166 } 167 168 171 public abstract String getElementText() throws XMLStreamException; 172 173 176 public int nextTag() throws XMLStreamException { 177 while (hasNext()) { 178 if (START_ELEMENT == next()) 179 return START_ELEMENT; 180 } 181 182 return currentEvent; 183 } 184 185 188 public boolean hasNext() throws XMLStreamException { 189 return !(frames.size() == 0 && frame.ended); 190 191 } 192 193 196 public void close() throws XMLStreamException { 197 } 198 199 202 public abstract String getNamespaceURI(String prefix); 203 204 207 public boolean isStartElement() { 208 return (currentEvent == START_ELEMENT); 209 } 210 211 214 public boolean isEndElement() { 215 return (currentEvent == END_ELEMENT); 216 } 217 218 221 public boolean isCharacters() { 222 return (currentEvent == CHARACTERS); 223 } 224 225 228 public boolean isWhiteSpace() { 229 return (currentEvent == SPACE); 230 } 231 232 public int getEventType() { 233 return currentEvent; 234 } 235 236 public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) throws XMLStreamException { 237 char[] src = getText().toCharArray(); 238 239 if (sourceStart + length >= src.length) 240 length = src.length - sourceStart; 241 242 for (int i = 0; i < length; i++) { 243 target[targetStart + i] = src[i + sourceStart]; 244 } 245 246 return length; 247 } 248 249 public boolean hasText() { 250 return (currentEvent == CHARACTERS || currentEvent == DTD || currentEvent == ENTITY_REFERENCE 251 || currentEvent == COMMENT || currentEvent == SPACE); 252 } 253 254 public Location getLocation() { 255 return new Location() { 256 257 public int getCharacterOffset() { 258 return 0; 259 } 260 261 public int getColumnNumber() { 262 return 0; 263 } 264 265 public int getLineNumber() { 266 return 0; 267 } 268 269 public String getPublicId() { 270 return null; 271 } 272 273 public String getSystemId() { 274 return null; 275 } 276 277 }; 278 } 279 280 public boolean hasName() { 281 return (currentEvent == START_ELEMENT || currentEvent == END_ELEMENT); 282 } 283 284 public String getVersion() { 285 return null; 286 } 287 288 public boolean isStandalone() { 289 return false; 290 } 291 292 public boolean standaloneSet() { 293 return false; 295 } 296 297 public String getCharacterEncodingScheme() { 298 return null; 300 } 301 } 302 | Popular Tags |