001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * 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, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from such communities and third parties that govern the
024: * use of those portions, and any licenses granted hereunder do not alter any
025: * rights and obligations you may have under such open source licenses,
026: * however, the disclaimer of warranty and limitation of liability provisions
027: * in this License will apply to all Software in this distribution.
028: *
029: * You acknowledge that the Software is not designed, licensed or intended
030: * for use in the design, construction, operation or maintenance of any nuclear
031: * facility.
032: *
033: * Apache License
034: * Version 2.0, January 2004
035: * http://www.apache.org/licenses/
036: *
037: */
038:
039: package com.sun.xml.fastinfoset.stax.events;
040:
041: import com.sun.xml.fastinfoset.stax.*;
042: import java.util.NoSuchElementException;
043: import javax.xml.stream.XMLInputFactory;
044: import javax.xml.stream.XMLStreamException;
045: import javax.xml.stream.XMLStreamReader;
046: import javax.xml.stream.events.XMLEvent;
047: import javax.xml.stream.util.XMLEventAllocator;
048: import com.sun.xml.fastinfoset.CommonResourceBundle;
049:
050: public class StAXEventReader implements javax.xml.stream.XMLEventReader {
051: protected XMLStreamReader _streamReader;
052: protected XMLEventAllocator _eventAllocator;
053: private XMLEvent _currentEvent; //the current event
054: private XMLEvent[] events = new XMLEvent[3];
055: private int size = 3;
056: private int currentIndex = 0;
057: private boolean hasEvent = false; //true when current event exists, false initially & at end
058:
059: //only constructor will do because we delegate everything to underlying XMLStreamReader
060: public StAXEventReader(XMLStreamReader reader)
061: throws XMLStreamException {
062: _streamReader = reader;
063: _eventAllocator = (XMLEventAllocator) reader
064: .getProperty(XMLInputFactory.ALLOCATOR);
065: if (_eventAllocator == null) {
066: _eventAllocator = new StAXEventAllocatorBase();
067: }
068: //initialize
069: if (_streamReader.hasNext()) {
070: _streamReader.next();
071: _currentEvent = _eventAllocator.allocate(_streamReader);
072: events[0] = _currentEvent;
073: hasEvent = true;
074: } else {
075: throw new XMLStreamException(CommonResourceBundle
076: .getInstance().getString("message.noElement"));
077: }
078: }
079:
080: public boolean hasNext() {
081: return hasEvent;
082: }
083:
084: public XMLEvent nextEvent() throws XMLStreamException {
085: XMLEvent event = null;
086: XMLEvent nextEvent = null;
087: if (hasEvent) {
088: event = events[currentIndex];
089: events[currentIndex] = null;
090: if (_streamReader.hasNext()) {
091: //advance and read the next
092: _streamReader.next();
093: nextEvent = _eventAllocator.allocate(_streamReader);
094: if (++currentIndex == size)
095: currentIndex = 0;
096: events[currentIndex] = nextEvent;
097: hasEvent = true;
098: } else {
099: _currentEvent = null;
100: hasEvent = false;
101: }
102: return event;
103: } else {
104: throw new NoSuchElementException();
105: }
106: }
107:
108: public void remove() {
109: //stream reader is read-only.
110: throw new java.lang.UnsupportedOperationException();
111: }
112:
113: public void close() throws XMLStreamException {
114: _streamReader.close();
115: }
116:
117: /** Reads the content of a text-only element. Precondition:
118: * the current event is START_ELEMENT. Postcondition:
119: * The current event is the corresponding END_ELEMENT.
120: * @throws XMLStreamException if the current event is not a START_ELEMENT
121: * or if a non text element is encountered
122: */
123: public String getElementText() throws XMLStreamException {
124: if (!hasEvent) {
125: throw new NoSuchElementException();
126: }
127:
128: if (!_currentEvent.isStartElement()) {
129: StAXDocumentParser parser = (StAXDocumentParser) _streamReader;
130: return parser.getElementText(true);
131: } else {
132: return _streamReader.getElementText();
133: }
134: }
135:
136: /** Get the value of a feature/property from the underlying implementation
137: * @param name The name of the property
138: * @return The value of the property
139: * @throws IllegalArgumentException if the property is not supported
140: */
141: public Object getProperty(java.lang.String name)
142: throws java.lang.IllegalArgumentException {
143: return _streamReader.getProperty(name);
144: }
145:
146: /** Skips any insignificant space events until a START_ELEMENT or
147: * END_ELEMENT is reached. If anything other than space characters are
148: * encountered, an exception is thrown. This method should
149: * be used when processing element-only content because
150: * the parser is not able to recognize ignorable whitespace if
151: * the DTD is missing or not interpreted.
152: * @throws XMLStreamException if anything other than space characters are encountered
153: */
154: public XMLEvent nextTag() throws XMLStreamException {
155: if (!hasEvent) {
156: throw new NoSuchElementException();
157: }
158: StAXDocumentParser parser = (StAXDocumentParser) _streamReader;
159: parser.nextTag(true);
160: return _eventAllocator.allocate(_streamReader);
161: }
162:
163: //XMLEventReader extends Iterator;
164: public Object next() {
165: try {
166: return nextEvent();
167: } catch (XMLStreamException streamException) {
168: return null;
169: }
170: }
171:
172: public XMLEvent peek() throws XMLStreamException {
173: if (!hasEvent)
174: throw new XMLStreamException(CommonResourceBundle
175: .getInstance().getString("message.noElement"));
176: _currentEvent = events[currentIndex];
177: return _currentEvent;
178: }
179:
180: public void setAllocator(XMLEventAllocator allocator) {
181: if (allocator == null)
182: throw new IllegalArgumentException(CommonResourceBundle
183: .getInstance().getString(
184: "message.nullXMLEventAllocator"));
185:
186: _eventAllocator = allocator;
187: }
188:
189: }
|