aoqi@0: /* aoqi@0: * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.fastinfoset.stax.events ; aoqi@0: aoqi@0: import javax.xml.stream.Location; aoqi@0: import javax.xml.stream.events.XMLEvent; aoqi@0: import javax.xml.stream.events.Characters; aoqi@0: import javax.xml.stream.events.EndElement; aoqi@0: import javax.xml.stream.events.StartElement; aoqi@0: import javax.xml.namespace.QName; aoqi@0: import java.io.Writer; aoqi@0: import com.sun.xml.internal.fastinfoset.CommonResourceBundle; aoqi@0: aoqi@0: aoqi@0: public abstract class EventBase implements XMLEvent { aoqi@0: aoqi@0: /* Event type this event corresponds to */ aoqi@0: protected int _eventType; aoqi@0: protected Location _location = null; aoqi@0: aoqi@0: public EventBase() { aoqi@0: aoqi@0: } aoqi@0: aoqi@0: public EventBase(int eventType) { aoqi@0: _eventType = eventType; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns an integer code for this event. aoqi@0: */ aoqi@0: public int getEventType() { aoqi@0: return _eventType; aoqi@0: } aoqi@0: aoqi@0: protected void setEventType(int eventType){ aoqi@0: _eventType = eventType; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: public boolean isStartElement() { aoqi@0: return _eventType == START_ELEMENT; aoqi@0: } aoqi@0: aoqi@0: public boolean isEndElement() { aoqi@0: return _eventType == END_ELEMENT; aoqi@0: } aoqi@0: aoqi@0: public boolean isEntityReference() { aoqi@0: return _eventType == ENTITY_REFERENCE; aoqi@0: } aoqi@0: aoqi@0: public boolean isProcessingInstruction() { aoqi@0: return _eventType == PROCESSING_INSTRUCTION; aoqi@0: } aoqi@0: aoqi@0: public boolean isStartDocument() { aoqi@0: return _eventType == START_DOCUMENT; aoqi@0: } aoqi@0: aoqi@0: public boolean isEndDocument() { aoqi@0: return _eventType == END_DOCUMENT; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the location of this event. The Location aoqi@0: * returned from this method is non-volatile and aoqi@0: * will retain its information. aoqi@0: * @see javax.xml.stream.Location aoqi@0: */ aoqi@0: public Location getLocation(){ aoqi@0: return _location; aoqi@0: } aoqi@0: aoqi@0: public void setLocation(Location loc){ aoqi@0: _location = loc; aoqi@0: } aoqi@0: public String getSystemId() { aoqi@0: if(_location == null ) aoqi@0: return ""; aoqi@0: else aoqi@0: return _location.getSystemId(); aoqi@0: } aoqi@0: aoqi@0: /** Returns this event as Characters, may result in aoqi@0: * a class cast exception if this event is not Characters. aoqi@0: */ aoqi@0: public Characters asCharacters() { aoqi@0: if (isCharacters()) { aoqi@0: return (Characters)this; aoqi@0: } else aoqi@0: throw new ClassCastException(CommonResourceBundle.getInstance().getString("message.charactersCast", new Object[]{getEventTypeString()})); aoqi@0: } aoqi@0: aoqi@0: /** Returns this event as an end element event, may result in aoqi@0: * a class cast exception if this event is not a end element. aoqi@0: */ aoqi@0: public EndElement asEndElement() { aoqi@0: if (isEndElement()) { aoqi@0: return (EndElement)this; aoqi@0: } else aoqi@0: throw new ClassCastException(CommonResourceBundle.getInstance().getString("message.endElementCase", new Object[]{getEventTypeString()})); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns this event as a start element event, may result in aoqi@0: * a class cast exception if this event is not a start element. aoqi@0: */ aoqi@0: public StartElement asStartElement() { aoqi@0: if (isStartElement()) { aoqi@0: return (StartElement)this; aoqi@0: } else aoqi@0: throw new ClassCastException(CommonResourceBundle.getInstance().getString("message.startElementCase", new Object[]{getEventTypeString()})); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * This method is provided for implementations to provide aoqi@0: * optional type information about the associated event. aoqi@0: * It is optional and will return null if no information aoqi@0: * is available. aoqi@0: */ aoqi@0: public QName getSchemaType() { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: /** A utility function to check if this event is an Attribute. aoqi@0: * @see javax.xml.stream.events.Attribute aoqi@0: */ aoqi@0: public boolean isAttribute() { aoqi@0: return _eventType == ATTRIBUTE; aoqi@0: } aoqi@0: aoqi@0: /** A utility function to check if this event is Characters. aoqi@0: * @see javax.xml.stream.events.Characters aoqi@0: */ aoqi@0: public boolean isCharacters() { aoqi@0: return _eventType == CHARACTERS; aoqi@0: } aoqi@0: aoqi@0: /** A utility function to check if this event is a Namespace. aoqi@0: * @see javax.xml.stream.events.Namespace aoqi@0: */ aoqi@0: public boolean isNamespace() { aoqi@0: return _eventType == NAMESPACE; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * This method will write the XMLEvent as per the XML 1.0 specification as Unicode characters. aoqi@0: * No indentation or whitespace should be outputted. aoqi@0: * aoqi@0: * Any user defined event type SHALL have this method aoqi@0: * called when being written to on an output stream. aoqi@0: * Built in Event types MUST implement this method, aoqi@0: * but implementations MAY choose not call these methods aoqi@0: * for optimizations reasons when writing out built in aoqi@0: * Events to an output stream. aoqi@0: * The output generated MUST be equivalent in terms of the aoqi@0: * infoset expressed. aoqi@0: * aoqi@0: * @param writer The writer that will output the data aoqi@0: * @throws XMLStreamException if there is a fatal error writing the event aoqi@0: */ aoqi@0: public void writeAsEncodedUnicode(Writer writer) throws javax.xml.stream.XMLStreamException { aoqi@0: } aoqi@0: aoqi@0: private String getEventTypeString() { aoqi@0: switch (_eventType){ aoqi@0: case START_ELEMENT: aoqi@0: return "StartElementEvent"; aoqi@0: case END_ELEMENT: aoqi@0: return "EndElementEvent"; aoqi@0: case PROCESSING_INSTRUCTION: aoqi@0: return "ProcessingInstructionEvent"; aoqi@0: case CHARACTERS: aoqi@0: return "CharacterEvent"; aoqi@0: case COMMENT: aoqi@0: return "CommentEvent"; aoqi@0: case START_DOCUMENT: aoqi@0: return "StartDocumentEvent"; aoqi@0: case END_DOCUMENT: aoqi@0: return "EndDocumentEvent"; aoqi@0: case ENTITY_REFERENCE: aoqi@0: return "EntityReferenceEvent"; aoqi@0: case ATTRIBUTE: aoqi@0: return "AttributeBase"; aoqi@0: case DTD: aoqi@0: return "DTDEvent"; aoqi@0: case CDATA: aoqi@0: return "CDATA"; aoqi@0: } aoqi@0: return "UNKNOWN_EVENT_TYPE"; aoqi@0: } aoqi@0: aoqi@0: }