KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > jaxp > DOMStreamReader


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.jaxp;
18
19 /*
20  * This implementation comes from the XFire project
21  * https://svn.codehaus.org/xfire/trunk/xfire/xfire-core/src/main/org/codehaus/xfire/util/stax/
22  */

23
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
36  * Abstract logic for creating XMLStreamReader from DOM documents.
37  * Its works using adapters for Element, Node and Attribute ( @see ElementAdapter }
38  *
39  * @author <a HREF="mailto:tsztelak@gmail.com">Tomasz Sztelak</a>
40  */

41 public abstract class DOMStreamReader implements XMLStreamReader {
42     public Map JavaDoc properties = new HashMap JavaDoc();
43
44     private FastStack frames = new FastStack();
45
46     private ElementFrame frame;
47
48     private int currentEvent = XMLStreamConstants.START_DOCUMENT;
49
50     /**
51      *
52      */

53     public static class ElementFrame {
54         public ElementFrame(Object JavaDoc element, ElementFrame parent) {
55             this.element = element;
56             this.parent = parent;
57         }
58
59         Object JavaDoc 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 JavaDoc uris;
74
75         List JavaDoc prefixes;
76
77         List JavaDoc attributes;
78
79         List JavaDoc allAttributes;
80
81         final ElementFrame parent;
82     }
83
84     /**
85      * @param element
86      */

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     /* (non-Javadoc)
98      * @see javax.xml.stream.XMLStreamReader#getProperty(java.lang.String)
99      */

100     public Object JavaDoc getProperty(String JavaDoc key) throws IllegalArgumentException JavaDoc {
101         return properties.get(key);
102     }
103
104     /* (non-Javadoc)
105      * @see javax.xml.stream.XMLStreamReader#next()
106      */

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     /* (non-Javadoc)
162      * @see javax.xml.stream.XMLStreamReader#require(int, java.lang.String, java.lang.String)
163      */

164     public void require(int arg0, String JavaDoc arg1, String JavaDoc arg2) throws XMLStreamException {
165         throw new UnsupportedOperationException JavaDoc();
166     }
167
168     /* (non-Javadoc)
169      * @see javax.xml.stream.XMLStreamReader#getElementText()
170      */

171     public abstract String JavaDoc getElementText() throws XMLStreamException;
172
173     /* (non-Javadoc)
174      * @see javax.xml.stream.XMLStreamReader#nextTag()
175      */

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     /* (non-Javadoc)
186      * @see javax.xml.stream.XMLStreamReader#hasNext()
187      */

188     public boolean hasNext() throws XMLStreamException {
189         return !(frames.size() == 0 && frame.ended);
190
191     }
192
193     /* (non-Javadoc)
194      * @see javax.xml.stream.XMLStreamReader#close()
195      */

196     public void close() throws XMLStreamException {
197     }
198
199     /* (non-Javadoc)
200      * @see javax.xml.stream.XMLStreamReader#getNamespaceURI(java.lang.String)
201      */

202     public abstract String JavaDoc getNamespaceURI(String JavaDoc prefix);
203
204     /* (non-Javadoc)
205      * @see javax.xml.stream.XMLStreamReader#isStartElement()
206      */

207     public boolean isStartElement() {
208         return (currentEvent == START_ELEMENT);
209     }
210
211     /* (non-Javadoc)
212      * @see javax.xml.stream.XMLStreamReader#isEndElement()
213      */

214     public boolean isEndElement() {
215         return (currentEvent == END_ELEMENT);
216     }
217
218     /* (non-Javadoc)
219      * @see javax.xml.stream.XMLStreamReader#isCharacters()
220      */

221     public boolean isCharacters() {
222         return (currentEvent == CHARACTERS);
223     }
224
225     /* (non-Javadoc)
226      * @see javax.xml.stream.XMLStreamReader#isWhiteSpace()
227      */

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 JavaDoc getPublicId() {
270                 return null;
271             }
272
273             public String JavaDoc 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 JavaDoc getVersion() {
285         return null;
286     }
287
288     public boolean isStandalone() {
289         return false;
290     }
291
292     public boolean standaloneSet() {
293         // TODO Auto-generated method stub
294
return false;
295     }
296
297     public String JavaDoc getCharacterEncodingScheme() {
298         // TODO Auto-generated method stub
299
return null;
300     }
301 }
302
Popular Tags