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