6
|
1 |
/*
|
|
2 |
* Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.xml.internal.stream;
|
|
27 |
|
|
28 |
import com.sun.xml.internal.stream.events.XMLEventAllocatorImpl;
|
|
29 |
import java.util.NoSuchElementException;
|
|
30 |
import javax.xml.stream.XMLInputFactory;
|
|
31 |
import javax.xml.stream.XMLStreamConstants;
|
|
32 |
import javax.xml.stream.XMLStreamException;
|
|
33 |
import javax.xml.stream.XMLStreamReader;
|
|
34 |
import javax.xml.stream.events.EntityReference;
|
|
35 |
import javax.xml.stream.events.XMLEvent;
|
|
36 |
import javax.xml.stream.util.XMLEventAllocator;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* @author @author Neeraj Bajaj Sun Microsystems
|
|
40 |
*
|
|
41 |
*/
|
|
42 |
|
|
43 |
public class XMLEventReaderImpl implements javax.xml.stream.XMLEventReader{
|
|
44 |
|
|
45 |
protected XMLStreamReader fXMLReader ;
|
|
46 |
protected XMLEventAllocator fXMLEventAllocator;
|
|
47 |
|
|
48 |
//only constructor will do because we delegate everything to underlying XMLStreamReader
|
|
49 |
public XMLEventReaderImpl(XMLStreamReader reader) throws XMLStreamException {
|
|
50 |
fXMLReader = reader ;
|
|
51 |
fXMLEventAllocator = (XMLEventAllocator)reader.getProperty(XMLInputFactory.ALLOCATOR);
|
|
52 |
if(fXMLEventAllocator == null){
|
|
53 |
fXMLEventAllocator = new XMLEventAllocatorImpl();
|
|
54 |
}
|
|
55 |
fPeekedEvent = fXMLEventAllocator.allocate(fXMLReader);
|
|
56 |
}
|
|
57 |
|
|
58 |
|
|
59 |
public boolean hasNext() {
|
|
60 |
//if we have the peeked event return 'true'
|
|
61 |
if(fPeekedEvent != null)return true;
|
|
62 |
//this is strange XMLStreamReader throws XMLStreamException
|
|
63 |
//XMLEventReader doesn't throw XMLStreamException
|
|
64 |
boolean next = false ;
|
|
65 |
try{
|
|
66 |
next = fXMLReader.hasNext();
|
|
67 |
}catch(XMLStreamException ex){
|
|
68 |
return false;
|
|
69 |
}
|
|
70 |
return next ;
|
|
71 |
}
|
|
72 |
|
|
73 |
|
|
74 |
public XMLEvent nextEvent() throws XMLStreamException {
|
|
75 |
//if application peeked return the peeked event
|
|
76 |
if(fPeekedEvent != null){
|
|
77 |
fLastEvent = fPeekedEvent ;
|
|
78 |
fPeekedEvent = null;
|
|
79 |
return fLastEvent ;
|
|
80 |
}
|
|
81 |
else if(fXMLReader.hasNext()){
|
|
82 |
//advance the reader to next state.
|
|
83 |
fXMLReader.next();
|
|
84 |
return fLastEvent = fXMLEventAllocator.allocate(fXMLReader);
|
|
85 |
}
|
|
86 |
else{
|
|
87 |
fLastEvent = null;
|
|
88 |
throw new NoSuchElementException();
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|
|
92 |
public void remove(){
|
|
93 |
//remove of the event is not supported.
|
|
94 |
throw new java.lang.UnsupportedOperationException();
|
|
95 |
}
|
|
96 |
|
|
97 |
|
|
98 |
public void close() throws XMLStreamException {
|
|
99 |
fXMLReader.close();
|
|
100 |
}
|
|
101 |
|
|
102 |
/** Reads the content of a text-only element. Precondition:
|
|
103 |
* the current event is START_ELEMENT. Postcondition:
|
|
104 |
* The current event is the corresponding END_ELEMENT.
|
|
105 |
* @throws XMLStreamException if the current event is not a START_ELEMENT
|
|
106 |
* or if a non text element is encountered
|
|
107 |
*/
|
|
108 |
public String getElementText() throws XMLStreamException {
|
|
109 |
//we have to keep reference to the 'last event' of the stream to be able
|
|
110 |
//to make this check - is there another way ? - nb.
|
|
111 |
if(fLastEvent.getEventType() != XMLEvent.START_ELEMENT){
|
|
112 |
throw new XMLStreamException(
|
|
113 |
"parser must be on START_ELEMENT to read next text", fLastEvent.getLocation());
|
|
114 |
}
|
|
115 |
|
|
116 |
// STag content ETag
|
|
117 |
//[43] content ::= CharData? ((element | Reference | CDSect | PI | Comment) CharData?)*
|
|
118 |
|
|
119 |
//<foo>....some long text say in KB and underlying parser reports multiple character
|
|
120 |
// but getElementText() events....</foo>
|
|
121 |
|
|
122 |
//having a peeked event makes things really worse -- we have to test the first event
|
|
123 |
if(fPeekedEvent != null){
|
|
124 |
XMLEvent event = fPeekedEvent ;
|
|
125 |
fPeekedEvent = null;
|
|
126 |
int type = event.getEventType();
|
|
127 |
|
|
128 |
|
|
129 |
String data = null;
|
|
130 |
if( type == XMLEvent.CHARACTERS || type == XMLEvent.SPACE ||
|
|
131 |
type == XMLEvent.CDATA){
|
|
132 |
data = event.asCharacters().getData();
|
|
133 |
}
|
|
134 |
else if(type == XMLEvent.ENTITY_REFERENCE){
|
|
135 |
data = ((EntityReference)event).getDeclaration().getReplacementText();
|
|
136 |
}
|
|
137 |
else if(type == XMLEvent.COMMENT || type == XMLEvent.PROCESSING_INSTRUCTION){
|
|
138 |
//ignore
|
|
139 |
} else if(type == XMLEvent.START_ELEMENT) {
|
|
140 |
throw new XMLStreamException(
|
|
141 |
"elementGetText() function expects text only elment but START_ELEMENT was encountered.", event.getLocation());
|
|
142 |
}else if(type == XMLEvent.END_ELEMENT){
|
|
143 |
return "";
|
|
144 |
}
|
|
145 |
|
|
146 |
//create the string buffer and add initial data
|
|
147 |
StringBuffer buffer = new StringBuffer();
|
|
148 |
if(data != null && data.length() > 0 ) {
|
|
149 |
buffer.append(data);
|
|
150 |
}
|
|
151 |
//get the next event -- we should stop at END_ELEMENT but it can be any thing
|
|
152 |
//things are worse when implementing this function in XMLEventReader because
|
|
153 |
//there isn't any function called getText() which can get values for
|
|
154 |
//space, cdata, characters and entity reference
|
|
155 |
//nextEvent() would also set the last event.
|
|
156 |
event = nextEvent();
|
|
157 |
while(event.getEventType() != XMLEvent.END_ELEMENT){
|
|
158 |
if( type == XMLEvent.CHARACTERS || type == XMLEvent.SPACE ||
|
|
159 |
type == XMLEvent.CDATA){
|
|
160 |
data = event.asCharacters().getData();
|
|
161 |
}
|
|
162 |
else if(type == XMLEvent.ENTITY_REFERENCE){
|
|
163 |
data = ((EntityReference)event).getDeclaration().getReplacementText();
|
|
164 |
}
|
|
165 |
else if(type == XMLEvent.COMMENT || type == XMLEvent.PROCESSING_INSTRUCTION){
|
|
166 |
//ignore
|
|
167 |
} else if(type == XMLEvent.END_DOCUMENT) {
|
|
168 |
throw new XMLStreamException("unexpected end of document when reading element text content");
|
|
169 |
} else if(type == XMLEvent.START_ELEMENT) {
|
|
170 |
throw new XMLStreamException(
|
|
171 |
"elementGetText() function expects text only elment but START_ELEMENT was encountered.", event.getLocation());
|
|
172 |
} else {
|
|
173 |
throw new XMLStreamException(
|
|
174 |
"Unexpected event type "+ type, event.getLocation());
|
|
175 |
}
|
|
176 |
//add the data to the buffer
|
|
177 |
if(data != null && data.length() > 0 ) {
|
|
178 |
buffer.append(data);
|
|
179 |
}
|
|
180 |
}
|
|
181 |
return buffer.toString();
|
|
182 |
}//if (fPeekedEvent != null)
|
|
183 |
|
|
184 |
//if there was no peeked event simply delegate everything to fXMLReader
|
|
185 |
return fXMLReader.getElementText();
|
|
186 |
}
|
|
187 |
|
|
188 |
/** Get the value of a feature/property from the underlying implementation
|
|
189 |
* @param name The name of the property
|
|
190 |
* @return The value of the property
|
|
191 |
* @throws IllegalArgumentException if the property is not supported
|
|
192 |
*/
|
|
193 |
public Object getProperty(java.lang.String name) throws java.lang.IllegalArgumentException {
|
|
194 |
return fXMLReader.getProperty(name) ;
|
|
195 |
}
|
|
196 |
|
|
197 |
/** Skips any insignificant space events until a START_ELEMENT or
|
|
198 |
* END_ELEMENT is reached. If anything other than space characters are
|
|
199 |
* encountered, an exception is thrown. This method should
|
|
200 |
* be used when processing element-only content because
|
|
201 |
* the parser is not able to recognize ignorable whitespace if
|
|
202 |
* the DTD is missing or not interpreted.
|
|
203 |
* @throws XMLStreamException if anything other than space characters are encountered
|
|
204 |
*/
|
|
205 |
public XMLEvent nextTag() throws XMLStreamException {
|
|
206 |
//its really a pain if there is peeked event before calling nextTag()
|
|
207 |
if(fPeekedEvent != null){
|
|
208 |
//check the peeked event first.
|
|
209 |
XMLEvent event = fPeekedEvent;
|
|
210 |
fPeekedEvent = null ;
|
|
211 |
int eventType = event.getEventType();
|
|
212 |
//if peeked event is whitespace move to the next event
|
|
213 |
//if peeked event is PI or COMMENT move to the next event
|
|
214 |
if( (event.isCharacters() && event.asCharacters().isWhiteSpace())
|
|
215 |
|| eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
|
|
216 |
|| eventType == XMLStreamConstants.COMMENT){
|
|
217 |
event = nextEvent();
|
|
218 |
eventType = event.getEventType();
|
|
219 |
}
|
|
220 |
|
|
221 |
//we have to have the while loop because there can be many PI or comment event in sucession
|
|
222 |
while((event.isCharacters() && event.asCharacters().isWhiteSpace())
|
|
223 |
|| eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
|
|
224 |
|| eventType == XMLStreamConstants.COMMENT){
|
|
225 |
|
|
226 |
event = nextEvent();
|
|
227 |
eventType = event.getEventType();
|
|
228 |
}
|
|
229 |
|
|
230 |
if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
|
|
231 |
throw new XMLStreamException("expected start or end tag", event.getLocation());
|
|
232 |
}
|
|
233 |
return event;
|
|
234 |
}
|
|
235 |
|
|
236 |
//if there is no peeked event -- delegate the work of getting next event to fXMLReader
|
|
237 |
fXMLReader.nextTag();
|
|
238 |
return fXMLEventAllocator.allocate(fXMLReader);
|
|
239 |
}
|
|
240 |
|
|
241 |
public Object next() {
|
|
242 |
Object object = null;
|
|
243 |
try{
|
|
244 |
object = nextEvent();
|
|
245 |
}catch(XMLStreamException streamException){
|
|
246 |
fLastEvent = null ;
|
|
247 |
//xxx: what should be done in this case ?
|
|
248 |
throw new NoSuchElementException();
|
|
249 |
}
|
|
250 |
return object;
|
|
251 |
}
|
|
252 |
|
|
253 |
public XMLEvent peek() throws XMLStreamException{
|
|
254 |
//if someone call peek() two times we should just return the peeked event
|
|
255 |
//this is reset if we call next() or nextEvent()
|
|
256 |
if(fPeekedEvent != null) return fPeekedEvent;
|
|
257 |
|
|
258 |
if(hasNext()){
|
|
259 |
//revisit: we can implement peek() by calling underlying reader to advance
|
|
260 |
// the stream and returning the event without the knowledge of the user
|
|
261 |
// that the stream was advanced but the point is we are advancing the stream
|
|
262 |
//here. -- nb.
|
|
263 |
|
|
264 |
// Is there any application that relies on this behavior ?
|
|
265 |
//Can it be an application knows that there is particularly very large 'comment' section
|
|
266 |
//or character data which it doesn't want to read or to be returned as event
|
|
267 |
//But as of now we are creating every event but it can be optimized not to create
|
|
268 |
// the event.
|
|
269 |
fXMLReader.next();
|
|
270 |
fPeekedEvent = fXMLEventAllocator.allocate(fXMLReader);
|
|
271 |
return fPeekedEvent;
|
|
272 |
}else{
|
|
273 |
return null;
|
|
274 |
}
|
|
275 |
}//peek()
|
|
276 |
|
|
277 |
private XMLEvent fPeekedEvent;
|
|
278 |
private XMLEvent fLastEvent;
|
|
279 |
|
|
280 |
}//XMLEventReaderImpl
|