Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/com/sun/xml/internal/stream/events/DummyEvent.java
86414 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.xml.internal.stream.events ;2627import java.io.IOException;28import java.io.Writer;29import javax.xml.stream.events.XMLEvent;30import javax.xml.stream.events.Characters;31import javax.xml.stream.events.EndElement;32import javax.xml.stream.events.StartElement;33import javax.xml.namespace.QName;34import javax.xml.stream.Location;35import javax.xml.stream.XMLStreamException;3637/** DummyEvent is an abstract class. It provides functionality for most of the38* function of XMLEvent.39*40* @author Neeraj Bajaj Sun Microsystems,Inc.41* @author K.Venugopal Sun Microsystems,Inc.42*43*/4445public abstract class DummyEvent implements XMLEvent {46// Make sure that getLocation() never returns null. Instead, return this dummy location47// that indicates "nowhere" as effectively as possible.48private static DummyLocation nowhere = new DummyLocation();4950/* Event type this event corresponds to */51private int fEventType;52protected Location fLocation = (Location) nowhere;5354public DummyEvent() {55}5657public DummyEvent(int i) {58fEventType = i;59}6061public int getEventType() {62return fEventType;63}6465protected void setEventType(int eventType){66fEventType = eventType;67}686970public boolean isStartElement() {71return fEventType == XMLEvent.START_ELEMENT;72}7374public boolean isEndElement() {75return fEventType == XMLEvent.END_ELEMENT;76}7778public boolean isEntityReference() {79return fEventType == XMLEvent.ENTITY_REFERENCE;80}8182public boolean isProcessingInstruction() {83return fEventType == XMLEvent.PROCESSING_INSTRUCTION;84}8586public boolean isCharacterData() {87return fEventType == XMLEvent.CHARACTERS;88}8990public boolean isStartDocument() {91return fEventType == XMLEvent.START_DOCUMENT;92}9394public boolean isEndDocument() {95return fEventType == XMLEvent.END_DOCUMENT;96}9798public Location getLocation(){99return fLocation;100}101102void setLocation(Location loc){103if (loc == null) {104fLocation = nowhere;105} else {106fLocation = loc;107}108}109110/** Returns this event as Characters, may result in111* a class cast exception if this event is not Characters.112*/113public Characters asCharacters() {114return (Characters)this;115}116117/** Returns this event as an end element event, may result in118* a class cast exception if this event is not a end element.119*/120public EndElement asEndElement() {121return (EndElement)this;122}123124/** Returns this event as a start element event, may result in125* a class cast exception if this event is not a start element.126*/127public StartElement asStartElement() {128return (StartElement)this;129}130131/** This method is provided for implementations to provide132* optional type information about the associated event.133* It is optional and will return null if no information134* is available.135*/136public QName getSchemaType() {137//Base class will take care of providing extra information about this event.138return null;139}140141/** A utility function to check if this event is an Attribute.142* @see Attribute143*/144public boolean isAttribute() {145return fEventType == XMLEvent.ATTRIBUTE;146}147148/** A utility function to check if this event is Characters.149* @see Characters150*/151public boolean isCharacters() {152return fEventType == XMLEvent.CHARACTERS;153}154155/** A utility function to check if this event is a Namespace.156* @see Namespace157*/158public boolean isNamespace() {159return fEventType == XMLEvent.NAMESPACE;160}161162/** This method will write the XMLEvent as per the XML 1.0 specification as Unicode characters.163* No indentation or whitespace should be outputted.164*165* Any user defined event type SHALL have this method166* called when being written to on an output stream.167* Built in Event types MUST implement this method,168* but implementations MAY choose not call these methods169* for optimizations reasons when writing out built in170* Events to an output stream.171* The output generated MUST be equivalent in terms of the172* infoset expressed.173*174* @param writer The writer that will output the data175* @throws XMLStreamException if there is a fatal error writing the event176*/177public void writeAsEncodedUnicode(Writer writer) throws XMLStreamException {178try {179writeAsEncodedUnicodeEx(writer);180} catch (IOException e) {181throw new XMLStreamException(e);182}183}184/** Helper method in order to expose IOException.185* @param writer The writer that will output the data186* @throws XMLStreamException if there is a fatal error writing the event187* @throws IOException if there is an IO error188*/189protected abstract void writeAsEncodedUnicodeEx(Writer writer)190throws IOException, XMLStreamException;191192/** Helper method to escape < > & for characters event and193* quotes, lt and amps for Entity194*/195protected void charEncode(Writer writer, String data)196throws IOException197{198if (data == null || data == "") return;199int i = 0, start = 0;200int len = data.length();201202loop:203for (; i < len; ++i) {204switch (data.charAt(i)) {205case '<':206writer.write(data, start, i - start);207writer.write("<");208start = i + 1;209break;210211case '&':212writer.write(data, start, i - start);213writer.write("&");214start = i + 1;215break;216217case '>':218writer.write(data, start, i - start);219writer.write(">");220start = i + 1;221break;222case '"':223writer.write(data, start, i - start);224writer.write(""");225start = i + 1;226break;227}228}229// Write any pending data230writer.write(data, start, len - start);231}232233static class DummyLocation implements Location {234public DummyLocation() {235}236237public int getCharacterOffset() {238return -1;239}240241public int getColumnNumber() {242return -1;243}244245public int getLineNumber() {246return -1;247}248249public String getPublicId() {250return null;251}252253public String getSystemId() {254return null;255}256}257}258259260