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: package org.apache.axis2.jaxws.message.util;
020:
021: import javax.xml.namespace.NamespaceContext;
022: import javax.xml.namespace.QName;
023: import javax.xml.stream.Location;
024: import javax.xml.stream.XMLStreamException;
025: import javax.xml.stream.XMLStreamReader;
026: import java.util.Stack;
027:
028: /**
029: * StackableReader A StackableStreamReader provides an additional method push(XMLStreamReader)
030: * <p/>
031: * You can call push(...) to add a new XMLStreamReader. The next event will use the pushed stream
032: * reader. After the XMLStreamReader is consumed, it is automatically popped off of the stack.
033: * <p/>
034: * Note the information returned by the StackableReader is only applicable for the topmost
035: * XMLStreamReader. For example the NamespaceContext that is returned is not a combination of all
036: * the namespace contexts on the stack.
037: */
038: public class StackableReader implements XMLStreamReader {
039:
040: Stack<XMLStreamReader> stack = new Stack<XMLStreamReader>();
041: XMLStreamReader current = null;
042:
043: /**
044: * Create a stackable reader with the initial reader
045: *
046: * @param first
047: */
048: public StackableReader(XMLStreamReader first) {
049: current = first;
050: }
051:
052: /**
053: * Push a new StreamReader
054: *
055: * @param streamReader
056: */
057: public void push(XMLStreamReader streamReader)
058: throws XMLStreamException {
059: // Push the current reader if it is not consumed
060: if (current != null && current.hasNext()) {
061: stack.push(current);
062: }
063: current = streamReader;
064: }
065:
066: public void close() throws XMLStreamException {
067: current.close();
068: }
069:
070: public int getAttributeCount() {
071: return current.getAttributeCount();
072: }
073:
074: public String getAttributeLocalName(int arg0) {
075: return current.getAttributeLocalName(arg0);
076: }
077:
078: public QName getAttributeName(int arg0) {
079: return current.getAttributeName(arg0);
080: }
081:
082: public String getAttributeNamespace(int arg0) {
083: return current.getAttributeNamespace(arg0);
084: }
085:
086: public String getAttributePrefix(int arg0) {
087: return current.getAttributePrefix(arg0);
088: }
089:
090: public String getAttributeType(int arg0) {
091: return current.getAttributeType(arg0);
092: }
093:
094: public String getAttributeValue(int arg0) {
095: return current.getAttributeValue(arg0);
096: }
097:
098: public String getAttributeValue(String arg0, String arg1) {
099: return current.getAttributeValue(arg0, arg1);
100: }
101:
102: public String getCharacterEncodingScheme() {
103: return current.getCharacterEncodingScheme();
104: }
105:
106: public String getElementText() throws XMLStreamException {
107: return current.getElementText();
108: }
109:
110: public String getEncoding() {
111: return current.getEncoding();
112: }
113:
114: public int getEventType() {
115: return current.getEventType();
116: }
117:
118: public String getLocalName() {
119: return current.getLocalName();
120: }
121:
122: public Location getLocation() {
123: return current.getLocation();
124: }
125:
126: public QName getName() {
127: return current.getName();
128: }
129:
130: public NamespaceContext getNamespaceContext() {
131: return current.getNamespaceContext();
132: }
133:
134: public int getNamespaceCount() {
135: return current.getNamespaceCount();
136: }
137:
138: public String getNamespacePrefix(int arg0) {
139: return current.getNamespacePrefix(arg0);
140: }
141:
142: public String getNamespaceURI() {
143: return current.getNamespaceURI();
144: }
145:
146: public String getNamespaceURI(int arg0) {
147: return current.getNamespaceURI(arg0);
148: }
149:
150: public String getNamespaceURI(String arg0) {
151: return current.getNamespaceURI(arg0);
152: }
153:
154: public String getPIData() {
155: return current.getPIData();
156: }
157:
158: public String getPITarget() {
159: return current.getPITarget();
160: }
161:
162: public String getPrefix() {
163: return current.getPrefix();
164: }
165:
166: public Object getProperty(String arg0)
167: throws IllegalArgumentException {
168: return current.getProperty(arg0);
169: }
170:
171: public String getText() {
172: return current.getText();
173: }
174:
175: public char[] getTextCharacters() {
176: return current.getTextCharacters();
177: }
178:
179: public int getTextCharacters(int arg0, char[] arg1, int arg2,
180: int arg3) throws XMLStreamException {
181: return current.getTextCharacters(arg0, arg1, arg2, arg3);
182: }
183:
184: public int getTextLength() {
185: return current.getTextLength();
186: }
187:
188: public int getTextStart() {
189: return current.getTextStart();
190: }
191:
192: public String getVersion() {
193: return current.getVersion();
194: }
195:
196: public boolean hasName() {
197: return current.hasName();
198: }
199:
200: public boolean hasNext() throws XMLStreamException {
201: // This code assumes that the stack only contains readers that are not consumed
202: if (!current.hasNext() && !stack.isEmpty()) {
203: return stack.peek().hasNext();
204: }
205: return current.hasNext();
206: }
207:
208: public boolean hasText() {
209: return current.hasText();
210: }
211:
212: public boolean isAttributeSpecified(int arg0) {
213: return current.isAttributeSpecified(arg0);
214: }
215:
216: public boolean isCharacters() {
217: return current.isCharacters();
218: }
219:
220: public boolean isEndElement() {
221: return current.isEndElement();
222: }
223:
224: public boolean isStandalone() {
225: return current.isStandalone();
226: }
227:
228: public boolean isStartElement() {
229: return current.isStartElement();
230: }
231:
232: public boolean isWhiteSpace() {
233: return current.isWhiteSpace();
234: }
235:
236: public int next() throws XMLStreamException {
237: // Only next is allowed to pop the stack
238: if (!current.hasNext() && !stack.isEmpty()) {
239: current = stack.pop();
240: }
241: // The assumption is that the event on the stream reader was processed
242: // prior to pushing a new xmlstreamreader. thus we proceed to the next
243: // event in all cases
244: int tag = current.next();
245:
246: // Skip start document and end document events for
247: // stacked stream readers
248: if ((tag == this .START_DOCUMENT || tag == this .END_DOCUMENT)
249: && !stack.isEmpty()) {
250: tag = next();
251: }
252:
253: return tag;
254: }
255:
256: public int nextTag() throws XMLStreamException {
257: if (!current.hasNext() && !stack.isEmpty()) {
258: return stack.peek().nextTag();
259: }
260: return current.nextTag();
261: }
262:
263: public void require(int arg0, String arg1, String arg2)
264: throws XMLStreamException {
265: current.require(arg0, arg1, arg2);
266: }
267:
268: public boolean standaloneSet() {
269: return current.standaloneSet();
270: }
271:
272: }
|