1/* 2 * Copyright (c) 2005, 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 26package com.sun.xml.internal.stream.events ; 27 28import java.io.IOException; 29import java.io.Writer; 30import javax.xml.stream.events.XMLEvent; 31import javax.xml.stream.events.Characters; 32import javax.xml.stream.events.EndElement; 33import javax.xml.stream.events.StartElement; 34import javax.xml.namespace.QName; 35import javax.xml.stream.Location; 36import javax.xml.stream.XMLStreamException; 37 38/** DummyEvent is an abstract class. It provides functionality for most of the 39 * function of XMLEvent. 40 * 41 * @author Neeraj Bajaj Sun Microsystems,Inc. 42 * @author K.Venugopal Sun Microsystems,Inc. 43 * 44 */ 45 46public abstract class DummyEvent implements XMLEvent { 47 // Make sure that getLocation() never returns null. Instead, return this dummy location 48 // that indicates "nowhere" as effectively as possible. 49 private static DummyLocation nowhere = new DummyLocation(); 50 51 /* Event type this event corresponds to */ 52 private int fEventType; 53 protected Location fLocation = (Location) nowhere; 54 55 public DummyEvent() { 56 } 57 58 public DummyEvent(int i) { 59 fEventType = i; 60 } 61 62 public int getEventType() { 63 return fEventType; 64 } 65 66 protected void setEventType(int eventType){ 67 fEventType = eventType; 68 } 69 70 71 public boolean isStartElement() { 72 return fEventType == XMLEvent.START_ELEMENT; 73 } 74 75 public boolean isEndElement() { 76 return fEventType == XMLEvent.END_ELEMENT; 77 } 78 79 public boolean isEntityReference() { 80 return fEventType == XMLEvent.ENTITY_REFERENCE; 81 } 82 83 public boolean isProcessingInstruction() { 84 return fEventType == XMLEvent.PROCESSING_INSTRUCTION; 85 } 86 87 public boolean isCharacterData() { 88 return fEventType == XMLEvent.CHARACTERS; 89 } 90 91 public boolean isStartDocument() { 92 return fEventType == XMLEvent.START_DOCUMENT; 93 } 94 95 public boolean isEndDocument() { 96 return fEventType == XMLEvent.END_DOCUMENT; 97 } 98 99 public Location getLocation(){ 100 return fLocation; 101 } 102 103 void setLocation(Location loc){ 104 if (loc == null) { 105 fLocation = nowhere; 106 } else { 107 fLocation = loc; 108 } 109 } 110 111 /** Returns this event as Characters, may result in 112 * a class cast exception if this event is not Characters. 113 */ 114 public Characters asCharacters() { 115 return (Characters)this; 116 } 117 118 /** Returns this event as an end element event, may result in 119 * a class cast exception if this event is not a end element. 120 */ 121 public EndElement asEndElement() { 122 return (EndElement)this; 123 } 124 125 /** Returns this event as a start element event, may result in 126 * a class cast exception if this event is not a start element. 127 */ 128 public StartElement asStartElement() { 129 return (StartElement)this; 130 } 131 132 /** This method is provided for implementations to provide 133 * optional type information about the associated event. 134 * It is optional and will return null if no information 135 * is available. 136 */ 137 public QName getSchemaType() { 138 //Base class will take care of providing extra information about this event. 139 return null; 140 } 141 142 /** A utility function to check if this event is an Attribute. 143 * @see Attribute 144 */ 145 public boolean isAttribute() { 146 return fEventType == XMLEvent.ATTRIBUTE; 147 } 148 149 /** A utility function to check if this event is Characters. 150 * @see Characters 151 */ 152 public boolean isCharacters() { 153 return fEventType == XMLEvent.CHARACTERS; 154 } 155 156 /** A utility function to check if this event is a Namespace. 157 * @see Namespace 158 */ 159 public boolean isNamespace() { 160 return fEventType == XMLEvent.NAMESPACE; 161 } 162 163 /** This method will write the XMLEvent as per the XML 1.0 specification as Unicode characters. 164 * No indentation or whitespace should be outputted. 165 * 166 * Any user defined event type SHALL have this method 167 * called when being written to on an output stream. 168 * Built in Event types MUST implement this method, 169 * but implementations MAY choose not call these methods 170 * for optimizations reasons when writing out built in 171 * Events to an output stream. 172 * The output generated MUST be equivalent in terms of the 173 * infoset expressed. 174 * 175 * @param writer The writer that will output the data 176 * @throws XMLStreamException if there is a fatal error writing the event 177 */ 178 public void writeAsEncodedUnicode(Writer writer) throws XMLStreamException { 179 try { 180 writeAsEncodedUnicodeEx(writer); 181 } catch (IOException e) { 182 throw new XMLStreamException(e); 183 } 184 } 185 /** Helper method in order to expose IOException. 186 * @param writer The writer that will output the data 187 * @throws XMLStreamException if there is a fatal error writing the event 188 * @throws IOException if there is an IO error 189 */ 190 protected abstract void writeAsEncodedUnicodeEx(Writer writer) 191 throws IOException, XMLStreamException; 192 193 /** Helper method to escape < > & for characters event and 194 * quotes, lt and amps for Entity 195 */ 196 protected void charEncode(Writer writer, String data) 197 throws IOException 198 { 199 if (data == null || data == "") return; 200 int i = 0, start = 0; 201 int len = data.length(); 202 203 loop: 204 for (; i < len; ++i) { 205 switch (data.charAt(i)) { 206 case '<': 207 writer.write(data, start, i - start); 208 writer.write("<"); 209 start = i + 1; 210 break; 211 212 case '&': 213 writer.write(data, start, i - start); 214 writer.write("&"); 215 start = i + 1; 216 break; 217 218 case '>': 219 writer.write(data, start, i - start); 220 writer.write(">"); 221 start = i + 1; 222 break; 223 case '"': 224 writer.write(data, start, i - start); 225 writer.write("""); 226 start = i + 1; 227 break; 228 } 229 } 230 // Write any pending data 231 writer.write(data, start, len - start); 232 } 233 234 static class DummyLocation implements Location { 235 public DummyLocation() { 236 } 237 238 public int getCharacterOffset() { 239 return -1; 240 } 241 242 public int getColumnNumber() { 243 return -1; 244 } 245 246 public int getLineNumber() { 247 return -1; 248 } 249 250 public String getPublicId() { 251 return null; 252 } 253 254 public String getSystemId() { 255 return null; 256 } 257 } 258} 259