001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: *
025: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
026: */
027:
028: /*
029: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
030: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
031: *
032: * This code is free software; you can redistribute it and/or modify it
033: * under the terms of the GNU General Public License version 2 only, as
034: * published by the Free Software Foundation. Sun designates this
035: * particular file as subject to the "Classpath" exception as provided
036: * by Sun in the LICENSE file that accompanied this code.
037: *
038: * This code is distributed in the hope that it will be useful, but WITHOUT
039: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
040: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
041: * version 2 for more details (a copy is included in the LICENSE file that
042: * accompanied this code).
043: *
044: * You should have received a copy of the GNU General Public License version
045: * 2 along with this work; if not, write to the Free Software Foundation,
046: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
047: *
048: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
049: * CA 95054 USA or visit www.sun.com if you need additional information or
050: * have any questions.
051: *
052: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
053: *
054: */
055:
056: package com.sun.xml.internal.fastinfoset.stax.events;
057:
058: import com.sun.xml.internal.fastinfoset.stax.*;
059: import java.util.NoSuchElementException;
060: import javax.xml.stream.XMLInputFactory;
061: import javax.xml.stream.XMLStreamException;
062: import javax.xml.stream.XMLStreamReader;
063: import javax.xml.stream.events.XMLEvent;
064: import javax.xml.stream.util.XMLEventAllocator;
065: import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
066:
067: public class StAXEventReader implements javax.xml.stream.XMLEventReader {
068: protected XMLStreamReader _streamReader;
069: protected XMLEventAllocator _eventAllocator;
070: private XMLEvent _currentEvent; //the current event
071: private XMLEvent[] events = new XMLEvent[3];
072: private int size = 3;
073: private int currentIndex = 0;
074: private boolean hasEvent = false; //true when current event exists, false initially & at end
075:
076: //only constructor will do because we delegate everything to underlying XMLStreamReader
077: public StAXEventReader(XMLStreamReader reader)
078: throws XMLStreamException {
079: _streamReader = reader;
080: _eventAllocator = (XMLEventAllocator) reader
081: .getProperty(XMLInputFactory.ALLOCATOR);
082: if (_eventAllocator == null) {
083: _eventAllocator = new StAXEventAllocatorBase();
084: }
085: //initialize
086: if (_streamReader.hasNext()) {
087: _streamReader.next();
088: _currentEvent = _eventAllocator.allocate(_streamReader);
089: events[0] = _currentEvent;
090: hasEvent = true;
091: } else {
092: throw new XMLStreamException(CommonResourceBundle
093: .getInstance().getString("message.noElement"));
094: }
095: }
096:
097: public boolean hasNext() {
098: return hasEvent;
099: }
100:
101: public XMLEvent nextEvent() throws XMLStreamException {
102: XMLEvent event = null;
103: XMLEvent nextEvent = null;
104: if (hasEvent) {
105: event = events[currentIndex];
106: events[currentIndex] = null;
107: if (_streamReader.hasNext()) {
108: //advance and read the next
109: _streamReader.next();
110: nextEvent = _eventAllocator.allocate(_streamReader);
111: if (++currentIndex == size)
112: currentIndex = 0;
113: events[currentIndex] = nextEvent;
114: hasEvent = true;
115: } else {
116: _currentEvent = null;
117: hasEvent = false;
118: }
119: return event;
120: } else {
121: throw new NoSuchElementException();
122: }
123: }
124:
125: public void remove() {
126: //stream reader is read-only.
127: throw new java.lang.UnsupportedOperationException();
128: }
129:
130: public void close() throws XMLStreamException {
131: _streamReader.close();
132: }
133:
134: /** Reads the content of a text-only element. Precondition:
135: * the current event is START_ELEMENT. Postcondition:
136: * The current event is the corresponding END_ELEMENT.
137: * @throws XMLStreamException if the current event is not a START_ELEMENT
138: * or if a non text element is encountered
139: */
140: public String getElementText() throws XMLStreamException {
141: if (!hasEvent) {
142: throw new NoSuchElementException();
143: }
144:
145: if (!_currentEvent.isStartElement()) {
146: StAXDocumentParser parser = (StAXDocumentParser) _streamReader;
147: return parser.getElementText(true);
148: } else {
149: return _streamReader.getElementText();
150: }
151: }
152:
153: /** Get the value of a feature/property from the underlying implementation
154: * @param name The name of the property
155: * @return The value of the property
156: * @throws IllegalArgumentException if the property is not supported
157: */
158: public Object getProperty(java.lang.String name)
159: throws java.lang.IllegalArgumentException {
160: return _streamReader.getProperty(name);
161: }
162:
163: /** Skips any insignificant space events until a START_ELEMENT or
164: * END_ELEMENT is reached. If anything other than space characters are
165: * encountered, an exception is thrown. This method should
166: * be used when processing element-only content because
167: * the parser is not able to recognize ignorable whitespace if
168: * the DTD is missing or not interpreted.
169: * @throws XMLStreamException if anything other than space characters are encountered
170: */
171: public XMLEvent nextTag() throws XMLStreamException {
172: if (!hasEvent) {
173: throw new NoSuchElementException();
174: }
175: StAXDocumentParser parser = (StAXDocumentParser) _streamReader;
176: parser.nextTag(true);
177: return _eventAllocator.allocate(_streamReader);
178: }
179:
180: //XMLEventReader extends Iterator;
181: public Object next() {
182: try {
183: return nextEvent();
184: } catch (XMLStreamException streamException) {
185: return null;
186: }
187: }
188:
189: public XMLEvent peek() throws XMLStreamException {
190: if (!hasEvent)
191: throw new XMLStreamException(CommonResourceBundle
192: .getInstance().getString("message.noElement"));
193: _currentEvent = events[currentIndex];
194: return _currentEvent;
195: }
196:
197: public void setAllocator(XMLEventAllocator allocator) {
198: if (allocator == null)
199: throw new IllegalArgumentException(CommonResourceBundle
200: .getInstance().getString(
201: "message.nullXMLEventAllocator"));
202:
203: _eventAllocator = allocator;
204: }
205:
206: }
|