1 package org.codehaus.xfire.util;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.Stack;
6
7 import javax.xml.namespace.NamespaceContext;
8 import javax.xml.namespace.QName;
9 import javax.xml.stream.Location;
10 import javax.xml.stream.XMLStreamException;
11 import javax.xml.stream.XMLStreamReader;
12
13 import org.codehaus.yom.Attribute;
14 import org.codehaus.yom.Element;
15 import org.codehaus.yom.Node;
16 import org.codehaus.yom.Text;
17
18 public class ElementStreamReader
19 implements XMLStreamReader
20 {
21 public Map properties = new HashMap();
22
23 private Stack frames = new Stack();
24 private ElementFrame frame;
25 private String text;
26 private int currentEvent;
27
28 public class ElementFrame
29 {
30 Element element;
31
32 boolean started = false;
33 boolean ended = false;
34
35 int currentChild = -1;
36 int currentAttribute = -1;
37 int currentNamespace = -1;
38 }
39
40 public ElementStreamReader(Element e)
41 {
42 frame = new ElementFrame();
43 frame.element = e;
44 frames.push(this.frame);
45 }
46
47 public Object getProperty(String key)
48 throws IllegalArgumentException
49 {
50 return properties.get(key);
51 }
52
53 public int next()
54 throws XMLStreamException
55 {
56 if (!frame.started)
57 {
58 frame.started = true;
59 currentEvent = START_ELEMENT;
60 }
61 else if (frame.currentAttribute < frame.element.getAttributeCount() - 1)
62 {
63 frame.currentAttribute++;
64 currentEvent = ATTRIBUTE;
65 }
66 else if (frame.currentNamespace < frame.element.getNamespaceDeclarationCount() - 1)
67 {
68 frame.currentNamespace++;
69 currentEvent = NAMESPACE;
70 }
71 else if (frame.currentChild < frame.element.getChildCount() - 1)
72 {
73 frame.currentChild++;
74 Node currentChildNode = frame.element.getChild(frame.currentChild);
75
76 if (currentChildNode instanceof Text)
77 {
78 text = currentChildNode.getValue();
79 currentEvent = CHARACTERS;
80 }
81 else if (currentChildNode instanceof Element)
82 {
83 ElementFrame newFrame = new ElementFrame();
84 newFrame.element = (Element) currentChildNode;
85 newFrame.started = true;
86 frame = newFrame;
87 frames.push(this.frame);
88
89 currentEvent = START_ELEMENT;
90 }
91 else
92 {
93 throw new RuntimeException("Unknown node type: " +
94 currentChildNode.getClass().getName());
95 }
96 }
97 else
98 {
99 frame = (ElementFrame) frames.pop();
100
101 currentEvent = END_ELEMENT;
102 }
103
104 return currentEvent;
105 }
106
107 public void require(int arg0, String arg1, String arg2)
108 throws XMLStreamException
109 {
110
111
112 }
113
114 public String getElementText()
115 throws XMLStreamException
116 {
117 return text;
118 }
119
120 public int nextTag()
121 throws XMLStreamException
122 {
123 while (hasNext())
124 {
125 if (START_ELEMENT == next()) return START_ELEMENT;
126 }
127
128 return currentEvent;
129 }
130
131 public boolean hasNext()
132 throws XMLStreamException
133 {
134 return !(frames.size() == 0 && currentEvent == -1);
135 }
136
137 public void close()
138 throws XMLStreamException
139 {
140 }
141
142 public String getNamespaceURI(String prefix)
143 {
144 return frame.element.getNamespaceURI(prefix);
145 }
146
147 public boolean isStartElement()
148 {
149 return (currentEvent == START_ELEMENT);
150 }
151
152 public boolean isEndElement()
153 {
154 return (currentEvent == END_ELEMENT);
155 }
156
157 public boolean isCharacters()
158 {
159 return (currentEvent == CHARACTERS);
160 }
161
162 public boolean isWhiteSpace()
163 {
164 return (currentEvent == SPACE);
165 }
166
167 public String getAttributeValue(String ns, String local)
168 {
169 Attribute att = frame.element.getAttribute(local, ns);
170 if (att != null) return att.getValue();
171
172 return null;
173 }
174
175 public int getAttributeCount()
176 {
177 return frame.element.getAttributeCount();
178 }
179
180 public QName getAttributeName(int index)
181 {
182 Attribute att = frame.element.getAttribute(index);
183
184 String local = att.getLocalName();
185 String prefix = att.getNamespacePrefix();
186 String ns = att.getNamespaceURI();
187
188 if (ns != null)
189 {
190 if (prefix != null)
191 {
192 return new QName(ns, local, prefix);
193 }
194 else
195 {
196 return new QName(ns, local);
197 }
198 }
199
200 return new QName(local);
201 }
202
203 public String getAttributeNamespace(int index)
204 {
205 return frame.element.getAttribute(index).getNamespaceURI();
206 }
207
208 public String getAttributeLocalName(int index)
209 {
210 return frame.element.getAttribute(index).getLocalName();
211 }
212
213 public String getAttributePrefix(int index)
214 {
215 return frame.element.getAttribute(index).getNamespacePrefix();
216 }
217
218 public String getAttributeType(int index)
219 {
220
221 return null;
222 }
223
224 public String getAttributeValue(int index)
225 {
226 return frame.element.getAttribute(index).getValue();
227 }
228
229 public boolean isAttributeSpecified(int index)
230 {
231
232 return false;
233 }
234
235 public int getNamespaceCount()
236 {
237 return frame.element.getNamespaceDeclarationCount();
238 }
239
240 public String getNamespacePrefix(int ns)
241 {
242 return frame.element.getNamespacePrefix(ns);
243 }
244
245 public String getNamespaceURI(int count)
246 {
247 Element e = frame.element;
248 return e.getNamespaceURI(e.getNamespacePrefix(count));
249 }
250
251 public NamespaceContext getNamespaceContext()
252 {
253 throw new UnsupportedOperationException();
254 }
255
256 public int getEventType()
257 {
258 return currentEvent;
259 }
260
261 public String getText()
262 {
263 return text;
264 }
265
266 public char[] getTextCharacters()
267 {
268 return text.toCharArray();
269 }
270
271 public int getTextCharacters(int arg0, char[] arg1, int arg2, int arg3)
272 throws XMLStreamException
273 {
274
275 return 0;
276 }
277
278 public int getTextStart()
279 {
280
281 return 0;
282 }
283
284 public int getTextLength()
285 {
286
287 return 0;
288 }
289
290 public String getEncoding()
291 {
292 return null;
293 }
294
295 public boolean hasText()
296 {
297
298 return false;
299 }
300
301 public Location getLocation()
302 {
303 return null;
304 }
305
306 public QName getName()
307 {
308 String local = frame.element.getLocalName();
309 String prefix = frame.element.getNamespacePrefix();
310 String ns = frame.element.getNamespaceURI();
311
312 if (ns != null)
313 {
314 if (prefix != null)
315 {
316 return new QName(ns, local, prefix);
317 }
318 else
319 {
320 return new QName(ns, local);
321 }
322 }
323
324 return new QName(local);
325 }
326
327 public String getLocalName()
328 {
329 return frame.element.getLocalName();
330 }
331
332 public boolean hasName()
333 {
334 return (currentEvent == START_ELEMENT || currentEvent == END_ELEMENT);
335 }
336
337 public String getNamespaceURI()
338 {
339 return frame.element.getNamespaceURI();
340 }
341
342 public String getPrefix()
343 {
344 return frame.element.getNamespacePrefix();
345 }
346
347 public String getVersion()
348 {
349 return null;
350 }
351
352 public boolean isStandalone()
353 {
354 return false;
355 }
356
357 public boolean standaloneSet()
358 {
359
360 return false;
361 }
362
363 public String getCharacterEncodingScheme()
364 {
365
366 return null;
367 }
368
369 public String getPITarget()
370 {
371
372 return null;
373 }
374
375 public String getPIData()
376 {
377
378 return null;
379 }
380
381 }