KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > stax > events > EventBase


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package com.sun.xml.fastinfoset.stax.events ;
41
42 import javax.xml.stream.XMLStreamConstants;
43 import javax.xml.stream.Location;
44 import javax.xml.stream.events.XMLEvent;
45 import javax.xml.stream.events.Characters;
46 import javax.xml.stream.events.EndElement;
47 import javax.xml.stream.events.StartElement;
48 import javax.xml.namespace.QName JavaDoc;
49 import java.io.Writer JavaDoc;
50 import com.sun.xml.fastinfoset.CommonResourceBundle;
51
52
53 public abstract class EventBase implements XMLEvent {
54     
55     /* Event type this event corresponds to */
56     protected int _eventType;
57     protected Location _location = null;
58     
59     public EventBase() {
60         
61     }
62     
63     public EventBase(int eventType) {
64         _eventType = eventType;
65     }
66     
67     /**
68     * Returns an integer code for this event.
69     */

70     public int getEventType() {
71         return _eventType;
72     }
73     
74     protected void setEventType(int eventType){
75         _eventType = eventType;
76     }
77     
78     
79     public boolean isStartElement() {
80         return _eventType == START_ELEMENT;
81     }
82     
83     public boolean isEndElement() {
84         return _eventType == END_ELEMENT;
85     }
86     
87     public boolean isEntityReference() {
88         return _eventType == ENTITY_REFERENCE;
89     }
90     
91     public boolean isProcessingInstruction() {
92         return _eventType == PROCESSING_INSTRUCTION;
93     }
94     
95     public boolean isStartDocument() {
96         return _eventType == START_DOCUMENT;
97     }
98     
99     public boolean isEndDocument() {
100         return _eventType == END_DOCUMENT;
101     }
102     
103   /**
104    * Return the location of this event. The Location
105    * returned from this method is non-volatile and
106    * will retain its information.
107    * @see javax.xml.stream.Location
108    */

109     public Location getLocation(){
110         return _location;
111     }
112     
113     public void setLocation(Location loc){
114         _location = loc;
115     }
116     public String JavaDoc getSystemId() {
117         if(_location == null )
118             return "";
119         else
120             return _location.getSystemId();
121     }
122     
123     /** Returns this event as Characters, may result in
124      * a class cast exception if this event is not Characters.
125      */

126     public Characters asCharacters() {
127         if (isCharacters()) {
128             return (Characters)this;
129         } else
130             throw new ClassCastException JavaDoc(CommonResourceBundle.getInstance().getString("message.charactersCast", new Object JavaDoc[]{getEventTypeString()}));
131     }
132     
133     /** Returns this event as an end element event, may result in
134      * a class cast exception if this event is not a end element.
135      */

136     public EndElement asEndElement() {
137         if (isEndElement()) {
138             return (EndElement)this;
139         } else
140             throw new ClassCastException JavaDoc(CommonResourceBundle.getInstance().getString("message.endElementCase", new Object JavaDoc[]{getEventTypeString()}));
141     }
142     
143   /**
144    * Returns this event as a start element event, may result in
145    * a class cast exception if this event is not a start element.
146    */

147     public StartElement asStartElement() {
148         if (isStartElement()) {
149             return (StartElement)this;
150         } else
151             throw new ClassCastException JavaDoc(CommonResourceBundle.getInstance().getString("message.startElementCase", new Object JavaDoc[]{getEventTypeString()}));
152     }
153     
154     /**
155     * This method is provided for implementations to provide
156     * optional type information about the associated event.
157     * It is optional and will return null if no information
158     * is available.
159     */

160     public QName JavaDoc getSchemaType() {
161         return null;
162     }
163     
164     /** A utility function to check if this event is an Attribute.
165      * @see Attribute
166      */

167     public boolean isAttribute() {
168         return _eventType == ATTRIBUTE;
169     }
170     
171     /** A utility function to check if this event is Characters.
172      * @see Characters
173      */

174     public boolean isCharacters() {
175         return _eventType == CHARACTERS;
176     }
177     
178     /** A utility function to check if this event is a Namespace.
179      * @see Namespace
180      */

181     public boolean isNamespace() {
182         return _eventType == NAMESPACE;
183     }
184     
185
186     /**
187     * This method will write the XMLEvent as per the XML 1.0 specification as Unicode characters.
188     * No indentation or whitespace should be outputted.
189     *
190     * Any user defined event type SHALL have this method
191     * called when being written to on an output stream.
192     * Built in Event types MUST implement this method,
193     * but implementations MAY choose not call these methods
194     * for optimizations reasons when writing out built in
195     * Events to an output stream.
196     * The output generated MUST be equivalent in terms of the
197     * infoset expressed.
198     *
199     * @param writer The writer that will output the data
200     * @throws XMLStreamException if there is a fatal error writing the event
201     */

202     public void writeAsEncodedUnicode(Writer JavaDoc writer) throws javax.xml.stream.XMLStreamException {
203     }
204     
205     private String JavaDoc getEventTypeString() {
206         switch (_eventType){
207             case START_ELEMENT:
208                 return "StartElementEvent";
209             case END_ELEMENT:
210                 return "EndElementEvent";
211             case PROCESSING_INSTRUCTION:
212                 return "ProcessingInstructionEvent";
213             case CHARACTERS:
214                 return "CharacterEvent";
215             case COMMENT:
216                 return "CommentEvent";
217             case START_DOCUMENT:
218                 return "StartDocumentEvent";
219             case END_DOCUMENT:
220                 return "EndDocumentEvent";
221             case ENTITY_REFERENCE:
222                 return "EntityReferenceEvent";
223             case ATTRIBUTE:
224                 return "AttributeBase";
225             case DTD:
226                 return "DTDEvent";
227             case CDATA:
228                 return "CDATA";
229         }
230         return "UNKNOWN_EVENT_TYPE";
231     }
232     
233 }
234
Popular Tags