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