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: */
019:
020: package org.apache.axis2.util;
021:
022: import javax.xml.namespace.NamespaceContext;
023: import javax.xml.namespace.QName;
024: import javax.xml.stream.Location;
025: import javax.xml.stream.XMLStreamException;
026: import javax.xml.stream.XMLStreamReader;
027:
028: public class StreamWrapper implements XMLStreamReader {
029: private static final int STATE_SWITCHED = 0;
030: private static final int STATE_INIT = 1;
031: private static final int STATE_SWITCH_AT_NEXT = 2;
032: private static final int STATE_COMPLETE_AT_NEXT = 3;
033: private static final int STATE_COMPLETED = 4;
034: private XMLStreamReader realReader = null;
035: private int state = STATE_INIT;
036: private int prevState = state;
037:
038: public StreamWrapper(XMLStreamReader realReader) {
039: if (realReader == null) {
040: throw new UnsupportedOperationException(
041: "Reader cannot be null");
042: }
043:
044: this .realReader = realReader;
045: }
046:
047: public void close() throws XMLStreamException {
048: if (state != STATE_INIT) {
049: realReader.close();
050: } else {
051: throw new XMLStreamException();
052: }
053: }
054:
055: public int next() throws XMLStreamException {
056: prevState = state;
057: int returnEvent = -1;
058:
059: switch (state) {
060: case STATE_INIT:
061: if (realReader.getEventType() == START_DOCUMENT) {
062: state = STATE_SWITCHED;
063: returnEvent = realReader.next();
064: } else {
065: state = STATE_SWITCHED;
066: returnEvent = realReader.getEventType();
067: }
068: break;
069: case STATE_SWITCHED:
070: returnEvent = realReader.next();
071: if (returnEvent == END_DOCUMENT) {
072: state = STATE_COMPLETED;
073: } else if (!realReader.hasNext()) {
074: state = STATE_COMPLETE_AT_NEXT;
075: }
076: break;
077: // case STATE_SWITCH_AT_NEXT:
078: // state = STATE_SWITCHED;
079: // returnEvent = realReader.getEventType();
080: // break;
081: case STATE_COMPLETE_AT_NEXT:
082: state = STATE_COMPLETED;
083: returnEvent = END_DOCUMENT;
084: break;
085: case STATE_COMPLETED:
086: //oops - no way we can go beyond this
087: throw new XMLStreamException("end reached!");
088: default:
089: throw new UnsupportedOperationException();
090: }
091:
092: return returnEvent;
093: }
094:
095: public int nextTag() throws XMLStreamException {
096: if (prevState != STATE_INIT) {
097: return realReader.nextTag();
098: } else {
099: throw new XMLStreamException();
100: }
101: }
102:
103: public void require(int i, String s, String s1)
104: throws XMLStreamException {
105: if (state != STATE_INIT) {
106: realReader.require(i, s, s1);
107: }
108: }
109:
110: public boolean standaloneSet() {
111: if (state != STATE_INIT) {
112: return realReader.standaloneSet();
113: } else {
114: return false;
115: }
116: }
117:
118: public int getAttributeCount() {
119: if (state != STATE_INIT) {
120: return realReader.getAttributeCount();
121: } else {
122: return 0;
123: }
124: }
125:
126: public String getAttributeLocalName(int i) {
127: if (state != STATE_INIT) {
128: return realReader.getAttributeLocalName(i);
129: } else {
130: return null;
131: }
132: }
133:
134: public QName getAttributeName(int i) {
135: if (state != STATE_INIT) {
136: return realReader.getAttributeName(i);
137: } else {
138: return null;
139: }
140: }
141:
142: public String getAttributeNamespace(int i) {
143: if (state != STATE_INIT) {
144: return realReader.getAttributeNamespace(i);
145: } else {
146: return null;
147: }
148: }
149:
150: public String getAttributePrefix(int i) {
151: if (state != STATE_INIT) {
152: return realReader.getAttributePrefix(i);
153: } else {
154: return null;
155: }
156: }
157:
158: public String getAttributeType(int i) {
159: if (state != STATE_INIT) {
160: return realReader.getAttributeType(i);
161: } else {
162: return null;
163: }
164: }
165:
166: public String getAttributeValue(int i) {
167: if (state != STATE_INIT) {
168: return realReader.getAttributeValue(i);
169: } else {
170: return null;
171: }
172: }
173:
174: public String getAttributeValue(String s, String s1) {
175: if (state != STATE_INIT) {
176: return realReader.getAttributeValue(s, s1);
177: } else {
178: return null;
179: }
180: }
181:
182: public String getCharacterEncodingScheme() {
183: if (state != STATE_INIT) {
184: return realReader.getCharacterEncodingScheme();
185: } else {
186: return null;
187: }
188: }
189:
190: public String getElementText() throws XMLStreamException {
191: if (state != STATE_INIT) {
192: return realReader.getElementText();
193: } else {
194: throw new XMLStreamException();
195: }
196: }
197:
198: public String getEncoding() {
199: if (state != STATE_INIT) {
200: return realReader.getEncoding();
201: } else {
202: return null;
203: }
204: }
205:
206: public int getEventType() {
207: if (state == STATE_INIT) {
208: return START_DOCUMENT;
209: } else {
210: return realReader.getEventType();
211: }
212: }
213:
214: public String getLocalName() {
215: if (state != STATE_INIT) {
216: return realReader.getLocalName();
217: } else {
218: return null;
219: }
220: }
221:
222: public Location getLocation() {
223: if (state != STATE_INIT) {
224: return realReader.getLocation();
225: } else {
226: return null;
227: }
228: }
229:
230: public QName getName() {
231: if (state != STATE_INIT) {
232: return realReader.getName();
233: } else {
234: return null;
235: }
236: }
237:
238: public NamespaceContext getNamespaceContext() {
239: if (state != STATE_INIT) {
240: return realReader.getNamespaceContext();
241: } else {
242: return null;
243: }
244: }
245:
246: public int getNamespaceCount() {
247: if (state != STATE_INIT) {
248: return realReader.getNamespaceCount();
249: } else {
250: return 0;
251: }
252: }
253:
254: public String getNamespacePrefix(int i) {
255: if (state != STATE_INIT) {
256: return realReader.getNamespacePrefix(i);
257: } else {
258: return null;
259: }
260: }
261:
262: public String getNamespaceURI() {
263: if (state != STATE_INIT) {
264: return realReader.getNamespaceURI();
265: } else {
266: return null;
267: }
268: }
269:
270: public String getNamespaceURI(int i) {
271: if (state != STATE_INIT) {
272: return realReader.getNamespaceURI(i);
273: } else {
274: return null;
275: }
276: }
277:
278: public String getNamespaceURI(String s) {
279: if (state != STATE_INIT) {
280: return realReader.getNamespaceURI(s);
281: } else {
282: return null;
283: }
284: }
285:
286: public String getPIData() {
287: if (state != STATE_INIT) {
288: return realReader.getPIData();
289: } else {
290: return null;
291: }
292: }
293:
294: public String getPITarget() {
295: if (state != STATE_INIT) {
296: return realReader.getPITarget();
297: } else {
298: return null;
299: }
300: }
301:
302: public String getPrefix() {
303: if (state != STATE_INIT) {
304: return realReader.getPrefix();
305: } else {
306: return null;
307: }
308: }
309:
310: public Object getProperty(String s) throws IllegalArgumentException {
311: if (state != STATE_INIT) {
312: return realReader.getProperty(s);
313: } else {
314: throw new IllegalArgumentException();
315: }
316: }
317:
318: public String getText() {
319: if (state != STATE_INIT) {
320: return realReader.getText();
321: } else {
322: return null;
323: }
324: }
325:
326: public char[] getTextCharacters() {
327: if (state != STATE_INIT) {
328: return realReader.getTextCharacters();
329: } else {
330: return new char[0];
331: }
332: }
333:
334: public int getTextCharacters(int i, char[] chars, int i1, int i2)
335: throws XMLStreamException {
336: if (state != STATE_INIT) {
337: return realReader.getTextCharacters(i, chars, i1, i2);
338: } else {
339: return 0;
340: }
341: }
342:
343: public int getTextLength() {
344: if (state != STATE_INIT) {
345: return realReader.getTextLength();
346: } else {
347: return 0;
348: }
349: }
350:
351: public int getTextStart() {
352: if (state != STATE_INIT) {
353: return realReader.getTextStart();
354: } else {
355: return 0;
356: }
357: }
358:
359: public String getVersion() {
360: if (state != STATE_INIT) {
361: return realReader.getVersion();
362: } else {
363: return null;
364: }
365: }
366:
367: public boolean hasName() {
368: if (state != STATE_INIT) {
369: return realReader.hasName();
370: } else {
371: return false;
372: }
373: }
374:
375: public boolean hasNext() throws XMLStreamException {
376: if (state == STATE_COMPLETE_AT_NEXT) {
377: return true;
378: } else if (state == STATE_COMPLETED) {
379: return false;
380: } else if (state != STATE_INIT) {
381: return realReader.hasNext();
382: } else {
383: return true;
384: }
385: }
386:
387: public boolean hasText() {
388: if (state != STATE_INIT) {
389: return realReader.hasText();
390: } else {
391: return false;
392: }
393: }
394:
395: public boolean isAttributeSpecified(int i) {
396: if (state != STATE_INIT) {
397: return realReader.isAttributeSpecified(i);
398: } else {
399: return false;
400: }
401: }
402:
403: public boolean isCharacters() {
404: if (state != STATE_INIT) {
405: return realReader.isCharacters();
406: } else {
407: return false;
408: }
409: }
410:
411: public boolean isEndElement() {
412: if (state != STATE_INIT) {
413: return realReader.isEndElement();
414: } else {
415: return false;
416: }
417: }
418:
419: public boolean isStandalone() {
420: if (state != STATE_INIT) {
421: return realReader.isStandalone();
422: } else {
423: return false;
424: }
425: }
426:
427: public boolean isStartElement() {
428: if (state != STATE_INIT) {
429: return realReader.isStartElement();
430: } else {
431: return false;
432: }
433: }
434:
435: public boolean isWhiteSpace() {
436: if (state != STATE_INIT) {
437: return realReader.isWhiteSpace();
438: } else {
439: return false;
440: }
441: }
442: }
|