001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.xml.stream;
018:
019: import java.util.Iterator;
020: import javax.xml.namespace.NamespaceContext;
021: import javax.xml.namespace.QName;
022: import javax.xml.stream.Location;
023: import javax.xml.stream.XMLEventReader;
024: import javax.xml.stream.XMLStreamException;
025: import javax.xml.stream.events.Attribute;
026: import javax.xml.stream.events.Namespace;
027: import javax.xml.stream.events.ProcessingInstruction;
028: import javax.xml.stream.events.StartDocument;
029: import javax.xml.stream.events.XMLEvent;
030:
031: /**
032: * Implementation of the <code>XMLStreamReader</code> interface that wraps a <code>XMLEventReader</code>. Useful,
033: * because the StAX <code>XMLInputFactory</code> allows one to create a event reader from a stream reader, but not
034: * vice-versa.
035: *
036: * @author Arjen Poutsma
037: * @since 1.0.0
038: */
039: public class XmlEventStreamReader extends AbstractXmlStreamReader {
040:
041: private XMLEvent event;
042:
043: private final XMLEventReader eventReader;
044:
045: public XmlEventStreamReader(XMLEventReader eventReader)
046: throws XMLStreamException {
047: this .eventReader = eventReader;
048: event = eventReader.nextEvent();
049: }
050:
051: public boolean isStandalone() {
052: if (event.isStartDocument()) {
053: return ((StartDocument) event).isStandalone();
054: } else {
055: throw new IllegalStateException();
056: }
057: }
058:
059: public String getVersion() {
060: if (event.isStartDocument()) {
061: return ((StartDocument) event).getVersion();
062: } else {
063: throw new IllegalStateException();
064: }
065: }
066:
067: public int getTextStart() {
068: return 0;
069: }
070:
071: public String getText() {
072: if (event.isCharacters()) {
073: return event.asCharacters().getData();
074: } else {
075: throw new IllegalStateException();
076: }
077: }
078:
079: public String getPITarget() {
080: if (event.isProcessingInstruction()) {
081: return ((ProcessingInstruction) event).getTarget();
082: } else {
083: throw new IllegalStateException();
084: }
085: }
086:
087: public String getPIData() {
088: if (event.isProcessingInstruction()) {
089: return ((ProcessingInstruction) event).getData();
090: } else {
091: throw new IllegalStateException();
092: }
093: }
094:
095: public int getNamespaceCount() {
096: Iterator namespaces;
097: if (event.isStartElement()) {
098: namespaces = event.asStartElement().getNamespaces();
099: } else if (event.isEndElement()) {
100: namespaces = event.asEndElement().getNamespaces();
101: } else {
102: throw new IllegalStateException();
103: }
104: return countIterator(namespaces);
105: }
106:
107: public NamespaceContext getNamespaceContext() {
108: if (event.isStartElement()) {
109: return event.asStartElement().getNamespaceContext();
110: } else {
111: throw new IllegalStateException();
112: }
113: }
114:
115: public QName getName() {
116: if (event.isStartElement()) {
117: return event.asStartElement().getName();
118: } else if (event.isEndElement()) {
119: return event.asEndElement().getName();
120: } else {
121: throw new IllegalStateException();
122: }
123: }
124:
125: public Location getLocation() {
126: return event.getLocation();
127: }
128:
129: public int getEventType() {
130: return event.getEventType();
131: }
132:
133: public String getEncoding() {
134: return null;
135: }
136:
137: public String getCharacterEncodingScheme() {
138: return null;
139: }
140:
141: public int getAttributeCount() {
142: if (!event.isStartElement()) {
143: throw new IllegalStateException();
144: }
145: Iterator attributes = event.asStartElement().getAttributes();
146: return countIterator(attributes);
147: }
148:
149: public void close() throws XMLStreamException {
150: eventReader.close();
151: }
152:
153: public QName getAttributeName(int index) {
154: return getAttribute(index).getName();
155: }
156:
157: public String getAttributeType(int index) {
158: return getAttribute(index).getDTDType();
159: }
160:
161: public String getAttributeValue(int index) {
162: return getAttribute(index).getValue();
163: }
164:
165: public String getNamespacePrefix(int index) {
166: return getNamespace(index).getPrefix();
167: }
168:
169: public String getNamespaceURI(int index) {
170: return getNamespace(index).getNamespaceURI();
171: }
172:
173: public Object getProperty(String name)
174: throws IllegalArgumentException {
175: return eventReader.getProperty(name);
176: }
177:
178: public boolean isAttributeSpecified(int index) {
179: return getAttribute(index).isSpecified();
180: }
181:
182: public int next() throws XMLStreamException {
183: event = eventReader.nextEvent();
184: return event.getEventType();
185: }
186:
187: public boolean standaloneSet() {
188: if (event.isStartDocument()) {
189: return ((StartDocument) event).standaloneSet();
190: } else {
191: throw new IllegalStateException();
192: }
193: }
194:
195: private int countIterator(Iterator iterator) {
196: int count = 0;
197: while (iterator.hasNext()) {
198: iterator.next();
199: count++;
200: }
201: return count;
202: }
203:
204: private Attribute getAttribute(int index) {
205: if (!event.isStartElement()) {
206: throw new IllegalStateException();
207: }
208: int count = 0;
209: Iterator attributes = event.asStartElement().getAttributes();
210: while (attributes.hasNext()) {
211: Attribute attribute = (Attribute) attributes.next();
212: if (count == index) {
213: return attribute;
214: } else {
215: count++;
216: }
217: }
218: throw new IllegalArgumentException();
219: }
220:
221: private Namespace getNamespace(int index) {
222: Iterator namespaces;
223: if (event.isStartElement()) {
224: namespaces = event.asStartElement().getNamespaces();
225: } else if (event.isEndElement()) {
226: namespaces = event.asEndElement().getNamespaces();
227: } else {
228: throw new IllegalStateException();
229: }
230: int count = 0;
231: while (namespaces.hasNext()) {
232: Namespace namespace = (Namespace) namespaces.next();
233: if (count == index) {
234: return namespace;
235: } else {
236: count++;
237: }
238: }
239: throw new IllegalArgumentException();
240: }
241: }
|