|
1 /* |
|
2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 * |
|
25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
|
26 */ |
|
27 |
|
28 package com.sun.xml.internal.fastinfoset.stax.events ; |
|
29 |
|
30 import javax.xml.stream.Location; |
|
31 import javax.xml.stream.events.XMLEvent; |
|
32 import javax.xml.stream.events.Characters; |
|
33 import javax.xml.stream.events.EndElement; |
|
34 import javax.xml.stream.events.StartElement; |
|
35 import javax.xml.namespace.QName; |
|
36 import java.io.Writer; |
|
37 import com.sun.xml.internal.fastinfoset.CommonResourceBundle; |
|
38 |
|
39 |
|
40 public abstract class EventBase implements XMLEvent { |
|
41 |
|
42 /* Event type this event corresponds to */ |
|
43 protected int _eventType; |
|
44 protected Location _location = null; |
|
45 |
|
46 public EventBase() { |
|
47 |
|
48 } |
|
49 |
|
50 public EventBase(int eventType) { |
|
51 _eventType = eventType; |
|
52 } |
|
53 |
|
54 /** |
|
55 * Returns an integer code for this event. |
|
56 */ |
|
57 public int getEventType() { |
|
58 return _eventType; |
|
59 } |
|
60 |
|
61 protected void setEventType(int eventType){ |
|
62 _eventType = eventType; |
|
63 } |
|
64 |
|
65 |
|
66 public boolean isStartElement() { |
|
67 return _eventType == START_ELEMENT; |
|
68 } |
|
69 |
|
70 public boolean isEndElement() { |
|
71 return _eventType == END_ELEMENT; |
|
72 } |
|
73 |
|
74 public boolean isEntityReference() { |
|
75 return _eventType == ENTITY_REFERENCE; |
|
76 } |
|
77 |
|
78 public boolean isProcessingInstruction() { |
|
79 return _eventType == PROCESSING_INSTRUCTION; |
|
80 } |
|
81 |
|
82 public boolean isStartDocument() { |
|
83 return _eventType == START_DOCUMENT; |
|
84 } |
|
85 |
|
86 public boolean isEndDocument() { |
|
87 return _eventType == END_DOCUMENT; |
|
88 } |
|
89 |
|
90 /** |
|
91 * Return the location of this event. The Location |
|
92 * returned from this method is non-volatile and |
|
93 * will retain its information. |
|
94 * @see javax.xml.stream.Location |
|
95 */ |
|
96 public Location getLocation(){ |
|
97 return _location; |
|
98 } |
|
99 |
|
100 public void setLocation(Location loc){ |
|
101 _location = loc; |
|
102 } |
|
103 public String getSystemId() { |
|
104 if(_location == null ) |
|
105 return ""; |
|
106 else |
|
107 return _location.getSystemId(); |
|
108 } |
|
109 |
|
110 /** Returns this event as Characters, may result in |
|
111 * a class cast exception if this event is not Characters. |
|
112 */ |
|
113 public Characters asCharacters() { |
|
114 if (isCharacters()) { |
|
115 return (Characters)this; |
|
116 } else |
|
117 throw new ClassCastException(CommonResourceBundle.getInstance().getString("message.charactersCast", new Object[]{getEventTypeString()})); |
|
118 } |
|
119 |
|
120 /** Returns this event as an end element event, may result in |
|
121 * a class cast exception if this event is not a end element. |
|
122 */ |
|
123 public EndElement asEndElement() { |
|
124 if (isEndElement()) { |
|
125 return (EndElement)this; |
|
126 } else |
|
127 throw new ClassCastException(CommonResourceBundle.getInstance().getString("message.endElementCase", new Object[]{getEventTypeString()})); |
|
128 } |
|
129 |
|
130 /** |
|
131 * Returns this event as a start element event, may result in |
|
132 * a class cast exception if this event is not a start element. |
|
133 */ |
|
134 public StartElement asStartElement() { |
|
135 if (isStartElement()) { |
|
136 return (StartElement)this; |
|
137 } else |
|
138 throw new ClassCastException(CommonResourceBundle.getInstance().getString("message.startElementCase", new Object[]{getEventTypeString()})); |
|
139 } |
|
140 |
|
141 /** |
|
142 * This method is provided for implementations to provide |
|
143 * optional type information about the associated event. |
|
144 * It is optional and will return null if no information |
|
145 * is available. |
|
146 */ |
|
147 public QName getSchemaType() { |
|
148 return null; |
|
149 } |
|
150 |
|
151 /** A utility function to check if this event is an Attribute. |
|
152 * @see javax.xml.stream.events.Attribute |
|
153 */ |
|
154 public boolean isAttribute() { |
|
155 return _eventType == ATTRIBUTE; |
|
156 } |
|
157 |
|
158 /** A utility function to check if this event is Characters. |
|
159 * @see javax.xml.stream.events.Characters |
|
160 */ |
|
161 public boolean isCharacters() { |
|
162 return _eventType == CHARACTERS; |
|
163 } |
|
164 |
|
165 /** A utility function to check if this event is a Namespace. |
|
166 * @see javax.xml.stream.events.Namespace |
|
167 */ |
|
168 public boolean isNamespace() { |
|
169 return _eventType == NAMESPACE; |
|
170 } |
|
171 |
|
172 |
|
173 /** |
|
174 * This method will write the XMLEvent as per the XML 1.0 specification as Unicode characters. |
|
175 * No indentation or whitespace should be outputted. |
|
176 * |
|
177 * Any user defined event type SHALL have this method |
|
178 * called when being written to on an output stream. |
|
179 * Built in Event types MUST implement this method, |
|
180 * but implementations MAY choose not call these methods |
|
181 * for optimizations reasons when writing out built in |
|
182 * Events to an output stream. |
|
183 * The output generated MUST be equivalent in terms of the |
|
184 * infoset expressed. |
|
185 * |
|
186 * @param writer The writer that will output the data |
|
187 * @throws XMLStreamException if there is a fatal error writing the event |
|
188 */ |
|
189 public void writeAsEncodedUnicode(Writer writer) throws javax.xml.stream.XMLStreamException { |
|
190 } |
|
191 |
|
192 private String getEventTypeString() { |
|
193 switch (_eventType){ |
|
194 case START_ELEMENT: |
|
195 return "StartElementEvent"; |
|
196 case END_ELEMENT: |
|
197 return "EndElementEvent"; |
|
198 case PROCESSING_INSTRUCTION: |
|
199 return "ProcessingInstructionEvent"; |
|
200 case CHARACTERS: |
|
201 return "CharacterEvent"; |
|
202 case COMMENT: |
|
203 return "CommentEvent"; |
|
204 case START_DOCUMENT: |
|
205 return "StartDocumentEvent"; |
|
206 case END_DOCUMENT: |
|
207 return "EndDocumentEvent"; |
|
208 case ENTITY_REFERENCE: |
|
209 return "EntityReferenceEvent"; |
|
210 case ATTRIBUTE: |
|
211 return "AttributeBase"; |
|
212 case DTD: |
|
213 return "DTDEvent"; |
|
214 case CDATA: |
|
215 return "CDATA"; |
|
216 } |
|
217 return "UNKNOWN_EVENT_TYPE"; |
|
218 } |
|
219 |
|
220 } |