View Javadoc

1   /*
2    * Copyright 2006 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.xml.stream;
18  
19  import java.util.Iterator;
20  import javax.xml.namespace.NamespaceContext;
21  import javax.xml.namespace.QName;
22  import javax.xml.stream.Location;
23  import javax.xml.stream.XMLEventReader;
24  import javax.xml.stream.XMLStreamException;
25  import javax.xml.stream.events.Attribute;
26  import javax.xml.stream.events.Namespace;
27  import javax.xml.stream.events.ProcessingInstruction;
28  import javax.xml.stream.events.StartDocument;
29  import javax.xml.stream.events.XMLEvent;
30  
31  /**
32   * Implementation of the <code>XMLStreamReader</code> interface that wraps a <code>XMLEventReader</code>. Useful,
33   * because the StAX <code>XMLInputFactory</code> allows one to create a event reader from a stream reader, but not
34   * vice-versa.
35   *
36   * @author Arjen Poutsma
37   * @since 1.0.0
38   */
39  public class XmlEventStreamReader extends AbstractXmlStreamReader {
40  
41      private XMLEvent event;
42  
43      private final XMLEventReader eventReader;
44  
45      public XmlEventStreamReader(XMLEventReader eventReader) throws XMLStreamException {
46          this.eventReader = eventReader;
47          event = eventReader.nextEvent();
48      }
49  
50      public boolean isStandalone() {
51          if (event.isStartDocument()) {
52              return ((StartDocument) event).isStandalone();
53          }
54          else {
55              throw new IllegalStateException();
56          }
57      }
58  
59      public String getVersion() {
60          if (event.isStartDocument()) {
61              return ((StartDocument) event).getVersion();
62          }
63          else {
64              throw new IllegalStateException();
65          }
66      }
67  
68      public int getTextStart() {
69          return 0;
70      }
71  
72      public String getText() {
73          if (event.isCharacters()) {
74              return event.asCharacters().getData();
75          }
76          else {
77              throw new IllegalStateException();
78          }
79      }
80  
81      public String getPITarget() {
82          if (event.isProcessingInstruction()) {
83              return ((ProcessingInstruction) event).getTarget();
84          }
85          else {
86              throw new IllegalStateException();
87          }
88      }
89  
90      public String getPIData() {
91          if (event.isProcessingInstruction()) {
92              return ((ProcessingInstruction) event).getData();
93          }
94          else {
95              throw new IllegalStateException();
96          }
97      }
98  
99      public int getNamespaceCount() {
100         Iterator namespaces;
101         if (event.isStartElement()) {
102             namespaces = event.asStartElement().getNamespaces();
103         }
104         else if (event.isEndElement()) {
105             namespaces = event.asEndElement().getNamespaces();
106         }
107         else {
108             throw new IllegalStateException();
109         }
110         return countIterator(namespaces);
111     }
112 
113     public NamespaceContext getNamespaceContext() {
114         if (event.isStartElement()) {
115             return event.asStartElement().getNamespaceContext();
116         }
117         else {
118             throw new IllegalStateException();
119         }
120     }
121 
122     public QName getName() {
123         if (event.isStartElement()) {
124             return event.asStartElement().getName();
125         }
126         else if (event.isEndElement()) {
127             return event.asEndElement().getName();
128         }
129         else {
130             throw new IllegalStateException();
131         }
132     }
133 
134     public Location getLocation() {
135         return event.getLocation();
136     }
137 
138     public int getEventType() {
139         return event.getEventType();
140     }
141 
142     public String getEncoding() {
143         return null;
144     }
145 
146     public String getCharacterEncodingScheme() {
147         return null;
148     }
149 
150     public int getAttributeCount() {
151         if (!event.isStartElement()) {
152             throw new IllegalStateException();
153         }
154         Iterator attributes = event.asStartElement().getAttributes();
155         return countIterator(attributes);
156     }
157 
158     public void close() throws XMLStreamException {
159         eventReader.close();
160     }
161 
162     public QName getAttributeName(int index) {
163         return getAttribute(index).getName();
164     }
165 
166     public String getAttributeType(int index) {
167         return getAttribute(index).getDTDType();
168     }
169 
170     public String getAttributeValue(int index) {
171         return getAttribute(index).getValue();
172     }
173 
174     public String getNamespacePrefix(int index) {
175         return getNamespace(index).getPrefix();
176     }
177 
178     public String getNamespaceURI(int index) {
179         return getNamespace(index).getNamespaceURI();
180     }
181 
182     public Object getProperty(String name) throws IllegalArgumentException {
183         return eventReader.getProperty(name);
184     }
185 
186     public boolean isAttributeSpecified(int index) {
187         return getAttribute(index).isSpecified();
188     }
189 
190     public int next() throws XMLStreamException {
191         event = eventReader.nextEvent();
192         return event.getEventType();
193     }
194 
195     public boolean standaloneSet() {
196         if (event.isStartDocument()) {
197             return ((StartDocument) event).standaloneSet();
198         }
199         else {
200             throw new IllegalStateException();
201         }
202     }
203 
204     private int countIterator(Iterator iterator) {
205         int count = 0;
206         while (iterator.hasNext()) {
207             iterator.next();
208             count++;
209         }
210         return count;
211     }
212 
213     private Attribute getAttribute(int index) {
214         if (!event.isStartElement()) {
215             throw new IllegalStateException();
216         }
217         int count = 0;
218         Iterator attributes = event.asStartElement().getAttributes();
219         while (attributes.hasNext()) {
220             Attribute attribute = (Attribute) attributes.next();
221             if (count == index) {
222                 return attribute;
223             }
224             else {
225                 count++;
226             }
227         }
228         throw new IllegalArgumentException();
229     }
230 
231     private Namespace getNamespace(int index) {
232         Iterator namespaces;
233         if (event.isStartElement()) {
234             namespaces = event.asStartElement().getNamespaces();
235         }
236         else if (event.isEndElement()) {
237             namespaces = event.asEndElement().getNamespaces();
238         }
239         else {
240             throw new IllegalStateException();
241         }
242         int count = 0;
243         while (namespaces.hasNext()) {
244             Namespace namespace = (Namespace) namespaces.next();
245             if (count == index) {
246                 return namespace;
247             }
248             else {
249                 count++;
250             }
251         }
252         throw new IllegalArgumentException();
253     }
254 }