001: /*
002: * $Id: BaseXMLEventReader.java,v 1.1 2004/07/05 23:13:31 cniles Exp $
003: *
004: * Copyright (c) 2004, Christian Niles, unit12.net
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions are met:
009: *
010: * * Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * * Neither the name of Christian Niles, Unit12, nor the names of its
018: * contributors may be used to endorse or promote products derived from
019: * this software without specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
022: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
023: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
025: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
026: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
027: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
029: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
030: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
031: * POSSIBILITY OF SUCH DAMAGE.
032: *
033: */
034: package javanet.staxutils;
035:
036: import java.util.NoSuchElementException;
037:
038: import javax.xml.stream.XMLEventReader;
039: import javax.xml.stream.XMLStreamException;
040: import javax.xml.stream.events.Comment;
041: import javax.xml.stream.events.XMLEvent;
042:
043: /**
044: * Abstract base class for {@link XMLEventReader} implementations.
045: *
046: * @author Christian Niles
047: * @version $Revision: 1.1 $
048: */
049: public abstract class BaseXMLEventReader implements XMLEventReader {
050:
051: /** Whether we've been closed or not. */
052: protected boolean closed;
053:
054: public synchronized String getElementText()
055: throws XMLStreamException {
056:
057: if (closed) {
058:
059: throw new XMLStreamException("Stream has been closed");
060:
061: }
062:
063: // TODO At the moment, this simply coalesces all Characters events up to a
064: // terminal EndElement event.
065: StringBuffer buffer = new StringBuffer();
066: while (true) {
067:
068: XMLEvent event = nextEvent();
069: if (event.isCharacters()) {
070:
071: // don't return ignorable whitespace
072: if (event.getEventType() != XMLEvent.SPACE) {
073:
074: buffer.append(event.asCharacters().getData());
075:
076: }
077:
078: } else if (event.isEndElement()) {
079:
080: break;
081:
082: } else {
083:
084: throw new XMLStreamException(
085: "Non-text event encountered in getElementText(): "
086: + event);
087:
088: }
089:
090: }
091:
092: return buffer.toString();
093:
094: }
095:
096: public XMLEvent nextTag() throws XMLStreamException {
097:
098: if (closed) {
099:
100: throw new XMLStreamException("Stream has been closed");
101:
102: }
103:
104: XMLEvent event;
105: do {
106:
107: if (hasNext()) {
108:
109: event = nextEvent();
110: if (event.isStartElement() || event.isEndElement()) {
111:
112: return event;
113:
114: } else if (event.isCharacters()) {
115:
116: if (!event.asCharacters().isWhiteSpace()) {
117:
118: throw new XMLStreamException(
119: "Non-ignorable space encountered");
120:
121: }
122:
123: } else if (!(event instanceof Comment)) {
124:
125: throw new XMLStreamException(
126: "Non-ignorable event encountered: " + event);
127:
128: }
129:
130: } else {
131:
132: throw new XMLStreamException(
133: "Ran out of events in nextTag()");
134:
135: }
136:
137: } while (!event.isStartElement() && !event.isEndElement());
138:
139: return event;
140:
141: }
142:
143: public Object getProperty(String name)
144: throws IllegalArgumentException {
145:
146: throw new IllegalArgumentException("Property not supported: "
147: + name);
148:
149: }
150:
151: public synchronized void close() throws XMLStreamException {
152:
153: if (!closed) {
154:
155: closed = true;
156:
157: }
158:
159: }
160:
161: public Object next() {
162:
163: try {
164:
165: return nextEvent();
166:
167: } catch (XMLStreamException e) {
168:
169: NoSuchElementException ex = new NoSuchElementException(
170: "Error getting next event");
171: ex.initCause(e);
172: throw ex;
173:
174: }
175:
176: }
177:
178: public void remove() {
179:
180: throw new UnsupportedOperationException();
181:
182: }
183:
184: }
|