001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.staxutils;
019:
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Map;
023:
024: import javax.xml.stream.Location;
025: import javax.xml.stream.XMLStreamConstants;
026: import javax.xml.stream.XMLStreamException;
027: import javax.xml.stream.XMLStreamReader;
028:
029: /**
030: * Abstract logic for creating XMLStreamReader from DOM documents. Its works
031: * using adapters for Element, Node and Attribute (
032: *
033: * @see ElementAdapter }
034: * @author <a href="mailto:tsztelak@gmail.com">Tomasz Sztelak</a>
035: */
036: public abstract class AbstractDOMStreamReader implements
037: XMLStreamReader {
038: protected int currentEvent = XMLStreamConstants.START_DOCUMENT;
039:
040: private Map properties = new HashMap();
041:
042: private FastStack<ElementFrame> frames = new FastStack<ElementFrame>();
043:
044: private ElementFrame frame;
045:
046: /**
047: *
048: */
049: public static class ElementFrame {
050: Object element;
051:
052: boolean started;
053:
054: boolean ended;
055:
056: int currentChild = -1;
057:
058: int currentAttribute = -1;
059:
060: int currentNamespace = -1;
061:
062: int currentElement = -1;
063: List<String> uris;
064: List<String> prefixes;
065: List<Object> attributes;
066: List<Object> allAttributes;
067:
068: final ElementFrame parent;
069:
070: public ElementFrame(Object element, ElementFrame parent) {
071: this .element = element;
072: this .parent = parent;
073: }
074:
075: public Object getElement() {
076: return element;
077: }
078:
079: }
080:
081: /**
082: * @param element
083: */
084: public AbstractDOMStreamReader(ElementFrame frame) {
085: this .frame = frame;
086: frames.push(this .frame);
087: }
088:
089: protected ElementFrame getCurrentFrame() {
090: return frame;
091: }
092:
093: /*
094: * (non-Javadoc)
095: *
096: * @see javax.xml.stream.XMLStreamReader#getProperty(java.lang.String)
097: */
098: public Object getProperty(String key)
099: throws IllegalArgumentException {
100: return properties.get(key);
101: }
102:
103: /*
104: * (non-Javadoc)
105: *
106: * @see javax.xml.stream.XMLStreamReader#next()
107: */
108: public int next() throws XMLStreamException {
109: if (frame.ended) {
110: frames.pop();
111: if (!frames.empty()) {
112: frame = (ElementFrame) frames.peek();
113: } else {
114: currentEvent = END_DOCUMENT;
115: return currentEvent;
116: }
117: }
118:
119: if (!frame.started) {
120: frame.started = true;
121: currentEvent = START_ELEMENT;
122: } else if (frame.currentAttribute < getAttributeCount() - 1) {
123: frame.currentAttribute++;
124: currentEvent = ATTRIBUTE;
125: } else if (frame.currentNamespace < getNamespaceCount() - 1) {
126: frame.currentNamespace++;
127: currentEvent = NAMESPACE;
128: } else if (frame.currentChild < getChildCount() - 1) {
129: frame.currentChild++;
130:
131: currentEvent = moveToChild(frame.currentChild);
132:
133: if (currentEvent == START_ELEMENT) {
134: ElementFrame newFrame = getChildFrame(frame.currentChild);
135: newFrame.started = true;
136: frame = newFrame;
137: frames.push(this .frame);
138: currentEvent = START_ELEMENT;
139:
140: newFrame(newFrame);
141: }
142: } else {
143: frame.ended = true;
144: currentEvent = END_ELEMENT;
145: endElement();
146: }
147: return currentEvent;
148: }
149:
150: protected void newFrame(ElementFrame newFrame) {
151: }
152:
153: protected void endElement() {
154: }
155:
156: protected abstract int moveToChild(int currentChild);
157:
158: protected abstract ElementFrame getChildFrame(int currentChild);
159:
160: protected abstract int getChildCount();
161:
162: /*
163: * (non-Javadoc)
164: *
165: * @see javax.xml.stream.XMLStreamReader#require(int, java.lang.String,
166: * java.lang.String)
167: */
168: public void require(int arg0, String arg1, String arg2)
169: throws XMLStreamException {
170: throw new UnsupportedOperationException();
171: }
172:
173: /*
174: * (non-Javadoc)
175: *
176: * @see javax.xml.stream.XMLStreamReader#getElementText()
177: */
178: public abstract String getElementText() throws XMLStreamException;
179:
180: /*
181: * (non-Javadoc)
182: *
183: * @see javax.xml.stream.XMLStreamReader#nextTag()
184: */
185: public int nextTag() throws XMLStreamException {
186: while (hasNext()) {
187: if (START_ELEMENT == next()) {
188: return START_ELEMENT;
189: }
190: }
191:
192: return currentEvent;
193: }
194:
195: /*
196: * (non-Javadoc)
197: *
198: * @see javax.xml.stream.XMLStreamReader#hasNext()
199: */
200: public boolean hasNext() throws XMLStreamException {
201: return !(frames.size() == 0 && frame.ended);
202:
203: }
204:
205: /*
206: * (non-Javadoc)
207: *
208: * @see javax.xml.stream.XMLStreamReader#close()
209: */
210: public void close() throws XMLStreamException {
211: }
212:
213: /*
214: * (non-Javadoc)
215: *
216: * @see javax.xml.stream.XMLStreamReader#getNamespaceURI(java.lang.String)
217: */
218: public abstract String getNamespaceURI(String prefix);
219:
220: /*
221: * (non-Javadoc)
222: *
223: * @see javax.xml.stream.XMLStreamReader#isStartElement()
224: */
225: public boolean isStartElement() {
226: return currentEvent == START_ELEMENT;
227: }
228:
229: /*
230: * (non-Javadoc)
231: *
232: * @see javax.xml.stream.XMLStreamReader#isEndElement()
233: */
234: public boolean isEndElement() {
235: return currentEvent == END_ELEMENT;
236: }
237:
238: /*
239: * (non-Javadoc)
240: *
241: * @see javax.xml.stream.XMLStreamReader#isCharacters()
242: */
243: public boolean isCharacters() {
244: return currentEvent == CHARACTERS;
245: }
246:
247: /*
248: * (non-Javadoc)
249: *
250: * @see javax.xml.stream.XMLStreamReader#isWhiteSpace()
251: */
252: public boolean isWhiteSpace() {
253: return currentEvent == SPACE;
254: }
255:
256: public int getEventType() {
257: return currentEvent;
258: }
259:
260: public int getTextCharacters(int sourceStart, char[] target,
261: int targetStart, int length) throws XMLStreamException {
262: char[] src = getText().toCharArray();
263:
264: if (sourceStart + length >= src.length) {
265: length = src.length - sourceStart;
266: }
267:
268: for (int i = 0; i < length; i++) {
269: target[targetStart + i] = src[i + sourceStart];
270: }
271:
272: return length;
273: }
274:
275: public boolean hasText() {
276: return currentEvent == CHARACTERS || currentEvent == DTD
277: || currentEvent == ENTITY_REFERENCE
278: || currentEvent == COMMENT || currentEvent == SPACE;
279: }
280:
281: public Location getLocation() {
282: return new Location() {
283:
284: public int getCharacterOffset() {
285: return 0;
286: }
287:
288: public int getColumnNumber() {
289: return 0;
290: }
291:
292: public int getLineNumber() {
293: return 0;
294: }
295:
296: public String getPublicId() {
297: return null;
298: }
299:
300: public String getSystemId() {
301: return null;
302: }
303:
304: };
305: }
306:
307: public boolean hasName() {
308: return currentEvent == START_ELEMENT
309: || currentEvent == END_ELEMENT;
310: }
311:
312: public String getVersion() {
313: return null;
314: }
315:
316: public boolean isStandalone() {
317: return false;
318: }
319:
320: public boolean standaloneSet() {
321: // TODO Auto-generated method stub
322: return false;
323: }
324:
325: public String getCharacterEncodingScheme() {
326: // TODO Auto-generated method stub
327: return null;
328: }
329: }
|