001: /*
002: * $Id: AbstractXMLEvent.java,v 1.3 2004/07/15 02:11:02 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.events;
035:
036: import java.io.IOException;
037: import java.io.Serializable;
038: import java.io.StringWriter;
039: import java.io.Writer;
040:
041: import javax.xml.namespace.QName;
042: import javax.xml.stream.Location;
043: import javax.xml.stream.XMLStreamException;
044: import javax.xml.stream.XMLStreamWriter;
045: import javax.xml.stream.events.Characters;
046: import javax.xml.stream.events.EndElement;
047: import javax.xml.stream.events.StartElement;
048: import javax.xml.stream.events.XMLEvent;
049:
050: import javanet.staxutils.helpers.EventMatcher;
051: import javanet.staxutils.helpers.UnknownLocation;
052: import javanet.staxutils.io.XMLWriterUtils;
053:
054: /**
055: * Abstract base class for {@link XMLEvent} implementations.
056: *
057: * @author Christian Niles
058: * @version $Revision: 1.3 $
059: */
060: public abstract class AbstractXMLEvent implements ExtendedXMLEvent,
061: Serializable, Cloneable {
062:
063: /** The event location. */
064: protected Location location;
065:
066: /** The schema type. */
067: protected QName schemaType;
068:
069: public AbstractXMLEvent() {
070:
071: }
072:
073: public AbstractXMLEvent(Location location) {
074:
075: this .location = location;
076:
077: }
078:
079: public AbstractXMLEvent(Location location, QName schemaType) {
080:
081: this .location = location;
082: this .schemaType = schemaType;
083:
084: }
085:
086: public AbstractXMLEvent(XMLEvent that) {
087:
088: this .location = that.getLocation();
089: this .schemaType = that.getSchemaType();
090:
091: }
092:
093: public Location getLocation() {
094:
095: return (location == null ? UnknownLocation.INSTANCE : location);
096:
097: }
098:
099: public QName getSchemaType() {
100:
101: return schemaType;
102:
103: }
104:
105: public Characters asCharacters() {
106:
107: return (Characters) this ;
108:
109: }
110:
111: public EndElement asEndElement() {
112:
113: return (EndElement) this ;
114:
115: }
116:
117: public StartElement asStartElement() {
118:
119: return (StartElement) this ;
120:
121: }
122:
123: public boolean isAttribute() {
124:
125: return getEventType() == ATTRIBUTE;
126:
127: }
128:
129: public boolean isCharacters() {
130:
131: switch (getEventType()) {
132:
133: case CHARACTERS:
134: case SPACE:
135: case CDATA:
136: return true;
137:
138: default:
139: return false;
140:
141: }
142:
143: }
144:
145: public boolean isEndDocument() {
146:
147: return getEventType() == END_DOCUMENT;
148:
149: }
150:
151: public boolean isEndElement() {
152:
153: return getEventType() == END_ELEMENT;
154:
155: }
156:
157: public boolean isEntityReference() {
158:
159: return getEventType() == ENTITY_REFERENCE;
160:
161: }
162:
163: public boolean isNamespace() {
164:
165: return getEventType() == NAMESPACE;
166:
167: }
168:
169: public boolean isProcessingInstruction() {
170:
171: return getEventType() == PROCESSING_INSTRUCTION;
172:
173: }
174:
175: public boolean isStartDocument() {
176:
177: return getEventType() == START_DOCUMENT;
178:
179: }
180:
181: public boolean isStartElement() {
182:
183: return getEventType() == START_ELEMENT;
184:
185: }
186:
187: public Object clone() {
188:
189: try {
190:
191: return super .clone();
192:
193: } catch (CloneNotSupportedException e) {
194:
195: // should never happen since we implement Cloneable
196: throw new RuntimeException(
197: "Unexpected exception cloning XMLEvent", e);
198:
199: }
200:
201: }
202:
203: public boolean matches(XMLEvent event) {
204:
205: return EventMatcher.eventsMatch(this , event);
206:
207: }
208:
209: public void writeEvent(XMLStreamWriter writer)
210: throws XMLStreamException {
211:
212: XMLWriterUtils.writeEvent(this , writer);
213:
214: }
215:
216: public void writeAsEncodedUnicode(Writer writer)
217: throws XMLStreamException {
218:
219: try {
220:
221: XMLWriterUtils.writeEvent(this , writer);
222:
223: } catch (IOException e) {
224:
225: throw new XMLStreamException(e);
226:
227: }
228:
229: }
230:
231: public String toString() {
232:
233: StringWriter writer = new StringWriter();
234: try {
235:
236: this .writeAsEncodedUnicode(writer);
237:
238: } catch (XMLStreamException e) {
239: // shouldn't happen?
240: }
241:
242: return writer.toString();
243:
244: }
245:
246: }
|