286N/A/*
286N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/Apackage com.sun.xml.internal.stream.events ;
286N/A
286N/Aimport java.io.IOException;
286N/Aimport java.io.Writer;
286N/Aimport javax.xml.stream.events.XMLEvent;
286N/Aimport javax.xml.stream.events.Characters;
286N/Aimport javax.xml.stream.events.EndElement;
286N/Aimport javax.xml.stream.events.StartElement;
286N/Aimport javax.xml.namespace.QName;
286N/Aimport javax.xml.stream.Location;
286N/Aimport javax.xml.stream.XMLStreamException;
286N/A
286N/A/** DummyEvent is an abstract class. It provides functionality for most of the
286N/A * function of XMLEvent.
286N/A *
286N/A * @author Neeraj Bajaj Sun Microsystems,Inc.
286N/A * @author K.Venugopal Sun Microsystems,Inc.
286N/A *
286N/A */
286N/A
286N/Apublic abstract class DummyEvent implements XMLEvent {
286N/A // Make sure that getLocation() never returns null. Instead, return this dummy location
286N/A // that indicates "nowhere" as effectively as possible.
286N/A private static DummyLocation nowhere = new DummyLocation();
286N/A
286N/A /* Event type this event corresponds to */
286N/A private int fEventType;
286N/A protected Location fLocation = (Location) nowhere;
286N/A
286N/A public DummyEvent() {
286N/A }
286N/A
286N/A public DummyEvent(int i) {
286N/A fEventType = i;
286N/A }
286N/A
286N/A public int getEventType() {
286N/A return fEventType;
286N/A }
286N/A
286N/A protected void setEventType(int eventType){
286N/A fEventType = eventType;
286N/A }
286N/A
286N/A
286N/A public boolean isStartElement() {
286N/A return fEventType == XMLEvent.START_ELEMENT;
286N/A }
286N/A
286N/A public boolean isEndElement() {
286N/A return fEventType == XMLEvent.END_ELEMENT;
286N/A }
286N/A
286N/A public boolean isEntityReference() {
286N/A return fEventType == XMLEvent.ENTITY_REFERENCE;
286N/A }
286N/A
286N/A public boolean isProcessingInstruction() {
286N/A return fEventType == XMLEvent.PROCESSING_INSTRUCTION;
286N/A }
286N/A
286N/A public boolean isCharacterData() {
286N/A return fEventType == XMLEvent.CHARACTERS;
286N/A }
286N/A
286N/A public boolean isStartDocument() {
286N/A return fEventType == XMLEvent.START_DOCUMENT;
286N/A }
286N/A
286N/A public boolean isEndDocument() {
286N/A return fEventType == XMLEvent.END_DOCUMENT;
286N/A }
286N/A
286N/A public Location getLocation(){
286N/A return fLocation;
286N/A }
286N/A
286N/A void setLocation(Location loc){
286N/A if (loc == null) {
286N/A fLocation = nowhere;
286N/A } else {
286N/A fLocation = loc;
286N/A }
286N/A }
286N/A
286N/A /** Returns this event as Characters, may result in
286N/A * a class cast exception if this event is not Characters.
286N/A */
286N/A public Characters asCharacters() {
286N/A return (Characters)this;
286N/A }
286N/A
286N/A /** Returns this event as an end element event, may result in
286N/A * a class cast exception if this event is not a end element.
286N/A */
286N/A public EndElement asEndElement() {
286N/A return (EndElement)this;
286N/A }
286N/A
286N/A /** Returns this event as a start element event, may result in
286N/A * a class cast exception if this event is not a start element.
286N/A */
286N/A public StartElement asStartElement() {
286N/A return (StartElement)this;
286N/A }
286N/A
286N/A /** This method is provided for implementations to provide
286N/A * optional type information about the associated event.
286N/A * It is optional and will return null if no information
286N/A * is available.
286N/A */
286N/A public QName getSchemaType() {
286N/A //Base class will take care of providing extra information about this event.
286N/A return null;
286N/A }
286N/A
286N/A /** A utility function to check if this event is an Attribute.
286N/A * @see Attribute
286N/A */
286N/A public boolean isAttribute() {
286N/A return fEventType == XMLEvent.ATTRIBUTE;
286N/A }
286N/A
286N/A /** A utility function to check if this event is Characters.
286N/A * @see Characters
286N/A */
286N/A public boolean isCharacters() {
286N/A return fEventType == XMLEvent.CHARACTERS;
286N/A }
286N/A
286N/A /** A utility function to check if this event is a Namespace.
286N/A * @see Namespace
286N/A */
286N/A public boolean isNamespace() {
286N/A return fEventType == XMLEvent.NAMESPACE;
286N/A }
286N/A
286N/A /** This method will write the XMLEvent as per the XML 1.0 specification as Unicode characters.
286N/A * No indentation or whitespace should be outputted.
286N/A *
286N/A * Any user defined event type SHALL have this method
286N/A * called when being written to on an output stream.
286N/A * Built in Event types MUST implement this method,
286N/A * but implementations MAY choose not call these methods
286N/A * for optimizations reasons when writing out built in
286N/A * Events to an output stream.
286N/A * The output generated MUST be equivalent in terms of the
286N/A * infoset expressed.
286N/A *
286N/A * @param writer The writer that will output the data
286N/A * @throws XMLStreamException if there is a fatal error writing the event
286N/A */
286N/A public void writeAsEncodedUnicode(Writer writer) throws XMLStreamException {
286N/A try {
286N/A writeAsEncodedUnicodeEx(writer);
286N/A } catch (IOException e) {
286N/A throw new XMLStreamException(e);
286N/A }
286N/A }
286N/A /** Helper method in order to expose IOException.
286N/A * @param writer The writer that will output the data
286N/A * @throws XMLStreamException if there is a fatal error writing the event
286N/A * @throws IOException if there is an IO error
286N/A */
286N/A protected abstract void writeAsEncodedUnicodeEx(Writer writer)
286N/A throws IOException, XMLStreamException;
286N/A
286N/A /** Helper method to escape < > & for characters event and
286N/A * quotes, lt and amps for Entity
286N/A */
286N/A protected void charEncode(Writer writer, String data)
286N/A throws IOException
286N/A {
286N/A if (data == null || data == "") return;
286N/A int i = 0, start = 0;
286N/A int len = data.length();
286N/A
286N/A loop:
286N/A for (; i < len; ++i) {
286N/A switch (data.charAt(i)) {
286N/A case '<':
286N/A writer.write(data, start, i - start);
286N/A writer.write("&lt;");
286N/A start = i + 1;
286N/A break;
286N/A
286N/A case '&':
286N/A writer.write(data, start, i - start);
286N/A writer.write("&amp;");
286N/A start = i + 1;
286N/A break;
286N/A
286N/A case '>':
286N/A writer.write(data, start, i - start);
286N/A writer.write("&gt;");
286N/A start = i + 1;
286N/A break;
286N/A case '"':
286N/A writer.write(data, start, i - start);
286N/A writer.write("&quot;");
286N/A start = i + 1;
286N/A break;
286N/A }
286N/A }
286N/A // Write any pending data
286N/A writer.write(data, start, len - start);
286N/A }
286N/A
286N/A static class DummyLocation implements Location {
286N/A public DummyLocation() {
286N/A }
286N/A
286N/A public int getCharacterOffset() {
286N/A return -1;
286N/A }
286N/A
286N/A public int getColumnNumber() {
286N/A return -1;
286N/A }
286N/A
286N/A public int getLineNumber() {
286N/A return -1;
286N/A }
286N/A
286N/A public String getPublicId() {
286N/A return null;
286N/A }
286N/A
286N/A public String getSystemId() {
286N/A return null;
286N/A }
286N/A }
286N/A}