286N/A/*
286N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
286N/A */
286N/A
286N/A/*
286N/A * Copyright 2005 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.xml.internal.stream;
286N/A
286N/Aimport java.io.InputStream;
286N/Aimport java.io.Reader;
286N/Aimport java.io.IOException;
286N/A
286N/Aimport com.sun.xml.internal.stream.util.BufferAllocator;
286N/Aimport com.sun.xml.internal.stream.util.ThreadLocalBufferAllocator;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
286N/A
286N/A/**
286N/A * Entity information.
286N/A *
286N/A * @author
286N/A */
286N/Apublic abstract class Entity {
286N/A
286N/A //
286N/A // Data
286N/A //
286N/A
286N/A //xxx why dont we declare the type of entities, like assign integer for external/ internal etc..
286N/A
286N/A /** Entity name. */
286N/A public String name;
286N/A
286N/A // whether this entity's declaration was found in the internal
286N/A // or external subset
286N/A public boolean inExternalSubset;
286N/A
286N/A //
286N/A // Constructors
286N/A //
286N/A
286N/A /** Default constructor. */
286N/A public Entity() {
286N/A clear();
286N/A } // <init>()
286N/A
286N/A /** Constructs an entity. */
286N/A public Entity(String name, boolean inExternalSubset) {
286N/A this.name = name;
286N/A this.inExternalSubset = inExternalSubset;
286N/A } // <init>(String)
286N/A
286N/A //
286N/A // Public methods
286N/A //
286N/A
286N/A /** Returns true if this entity was declared in the external subset. */
286N/A public boolean isEntityDeclInExternalSubset() {
286N/A return inExternalSubset;
286N/A }
286N/A
286N/A /** Returns true if this is an external entity. */
286N/A public abstract boolean isExternal();
286N/A
286N/A /** Returns true if this is an unparsed entity. */
286N/A public abstract boolean isUnparsed();
286N/A
286N/A /** Clears the entity. */
286N/A public void clear() {
286N/A name = null;
286N/A inExternalSubset = false;
286N/A } // clear()
286N/A
286N/A /** Sets the values of the entity. */
286N/A public void setValues(Entity entity) {
286N/A name = entity.name;
286N/A inExternalSubset = entity.inExternalSubset;
286N/A } // setValues(Entity)
286N/A
286N/A
286N/A /**
286N/A * Internal entity.
286N/A *
286N/A * @author nb131165
286N/A */
286N/A public static class InternalEntity
286N/A extends Entity {
286N/A
286N/A //
286N/A // Data
286N/A //
286N/A
286N/A /** Text value of entity. */
286N/A public String text;
286N/A
286N/A //
286N/A // Constructors
286N/A //
286N/A
286N/A /** Default constructor. */
286N/A public InternalEntity() {
286N/A clear();
286N/A } // <init>()
286N/A
286N/A /** Constructs an internal entity. */
286N/A public InternalEntity(String name, String text, boolean inExternalSubset) {
286N/A super(name,inExternalSubset);
286N/A this.text = text;
286N/A } // <init>(String,String)
286N/A
286N/A //
286N/A // Entity methods
286N/A //
286N/A
286N/A /** Returns true if this is an external entity. */
286N/A public final boolean isExternal() {
286N/A return false;
286N/A } // isExternal():boolean
286N/A
286N/A /** Returns true if this is an unparsed entity. */
286N/A public final boolean isUnparsed() {
286N/A return false;
286N/A } // isUnparsed():boolean
286N/A
286N/A /** Clears the entity. */
286N/A public void clear() {
286N/A super.clear();
286N/A text = null;
286N/A } // clear()
286N/A
286N/A /** Sets the values of the entity. */
286N/A public void setValues(Entity entity) {
286N/A super.setValues(entity);
286N/A text = null;
286N/A } // setValues(Entity)
286N/A
286N/A /** Sets the values of the entity. */
286N/A public void setValues(InternalEntity entity) {
286N/A super.setValues(entity);
286N/A text = entity.text;
286N/A } // setValues(InternalEntity)
286N/A
286N/A } // class InternalEntity
286N/A
286N/A /**
286N/A * External entity.
286N/A *
286N/A * @author nb131165
286N/A */
286N/A public static class ExternalEntity
286N/A extends Entity {
286N/A
286N/A //
286N/A // Data
286N/A //
286N/A
286N/A /** container for all relevant entity location information. */
286N/A public XMLResourceIdentifier entityLocation;
286N/A
286N/A /** Notation name for unparsed entity. */
286N/A public String notation;
286N/A
286N/A //
286N/A // Constructors
286N/A //
286N/A
286N/A /** Default constructor. */
286N/A public ExternalEntity() {
286N/A clear();
286N/A } // <init>()
286N/A
286N/A /** Constructs an internal entity. */
286N/A public ExternalEntity(String name, XMLResourceIdentifier entityLocation,
286N/A String notation, boolean inExternalSubset) {
286N/A super(name,inExternalSubset);
286N/A this.entityLocation = entityLocation;
286N/A this.notation = notation;
286N/A } // <init>(String,XMLResourceIdentifier, String)
286N/A
286N/A //
286N/A // Entity methods
286N/A //
286N/A
286N/A /** Returns true if this is an external entity. */
286N/A public final boolean isExternal() {
286N/A return true;
286N/A } // isExternal():boolean
286N/A
286N/A /** Returns true if this is an unparsed entity. */
286N/A public final boolean isUnparsed() {
286N/A return notation != null;
286N/A } // isUnparsed():boolean
286N/A
286N/A /** Clears the entity. */
286N/A public void clear() {
286N/A super.clear();
286N/A entityLocation = null;
286N/A notation = null;
286N/A } // clear()
286N/A
286N/A /** Sets the values of the entity. */
286N/A public void setValues(Entity entity) {
286N/A super.setValues(entity);
286N/A entityLocation = null;
286N/A notation = null;
286N/A } // setValues(Entity)
286N/A
286N/A /** Sets the values of the entity. */
286N/A public void setValues(ExternalEntity entity) {
286N/A super.setValues(entity);
286N/A entityLocation = entity.entityLocation;
286N/A notation = entity.notation;
286N/A } // setValues(ExternalEntity)
286N/A
286N/A } // class ExternalEntity
286N/A
286N/A /**
286N/A * Entity state.
286N/A *
286N/A * @author nb131165
286N/A */
286N/A public static class ScannedEntity
286N/A extends Entity {
286N/A
286N/A
286N/A /** Default buffer size (4096). */
286N/A public static final int DEFAULT_BUFFER_SIZE = 8192;
286N/A //4096;
286N/A
286N/A /**
286N/A * Buffer size. We get this value from a property. The default size
286N/A * is used if the input buffer size property is not specified.
286N/A * REVISIT: do we need a property for internal entity buffer size?
286N/A */
286N/A public int fBufferSize = DEFAULT_BUFFER_SIZE;
286N/A
286N/A /** Default buffer size before we've finished with the XMLDecl: */
286N/A public static final int DEFAULT_XMLDECL_BUFFER_SIZE = 28;
286N/A
286N/A /** Default internal entity buffer size (1024). */
286N/A public static final int DEFAULT_INTERNAL_BUFFER_SIZE = 1024;
286N/A
286N/A //
286N/A // Data
286N/A //
286N/A
286N/A // i/o
286N/A
286N/A /** XXX let these field remain public right now, though we have defined methods for them.
286N/A * Input stream. */
286N/A public InputStream stream;
286N/A
286N/A /** XXX let these field remain public right now, though we have defined methods for them.
286N/A * Reader. */
286N/A public Reader reader;
286N/A
286N/A // locator information
286N/A
286N/A /** entity location information */
286N/A public XMLResourceIdentifier entityLocation;
286N/A
286N/A // encoding
286N/A
286N/A /** Auto-detected encoding. */
286N/A public String encoding;
286N/A
286N/A // status
286N/A
286N/A /** True if in a literal. */
286N/A public boolean literal;
286N/A
286N/A // whether this is an external or internal scanned entity
286N/A public boolean isExternal;
286N/A
286N/A //each 'external' parsed entity may have xml/text declaration containing version information
286N/A public String version ;
286N/A
286N/A // buffer
286N/A
286N/A /** Character buffer. */
286N/A public char[] ch = null;
286N/A
286N/A /** Position in character buffer at any point of time. */
286N/A public int position;
286N/A
286N/A /** Count of characters present in buffer. */
286N/A public int count;
286N/A
286N/A /** Line number. */
286N/A public int lineNumber = 1;
286N/A
286N/A /** Column number. */
286N/A public int columnNumber = 1;
286N/A
286N/A /** Encoding has been set externally for eg: using DOMInput*/
286N/A boolean declaredEncoding = false;
286N/A
286N/A // status
286N/A
286N/A /**
286N/A * Encoding has been set externally, for example
286N/A * using a SAX InputSource or a DOM LSInput.
286N/A */
286N/A boolean externallySpecifiedEncoding = false;
286N/A
286N/A /** XML version. **/
286N/A public String xmlVersion = "1.0";
286N/A
286N/A /** This variable is used to calculate the current position in the XML stream.
286N/A * Note that fCurrentEntity.position maintains the position relative to
286N/A * the buffer.
286N/A * At any point of time absolute position in the XML stream can be calculated
286N/A * as fTotalCountTillLastLoad + fCurrentEntity.position
286N/A */
286N/A public int fTotalCountTillLastLoad ;
286N/A
286N/A /** This variable stores the number of characters read during the load()
286N/A * operation. It is used to calculate fTotalCountTillLastLoad
286N/A */
286N/A public int fLastCount ;
286N/A
286N/A /** Base character offset for computing absolute character offset. */
286N/A public int baseCharOffset;
286N/A
286N/A /** Start position in character buffer. */
286N/A public int startPosition;
286N/A
286N/A // to allow the reader/inputStream to behave efficiently:
286N/A public boolean mayReadChunks;
286N/A
286N/A // to know that prolog is read
286N/A public boolean xmlDeclChunkRead = false;
286N/A
286N/A /** returns the name of the current encoding
286N/A * @return current encoding name
286N/A */
286N/A public String getEncodingName(){
286N/A return encoding ;
286N/A }
286N/A
286N/A /**each 'external' parsed entity may have xml/text declaration containing version information
286N/A * @return String version of the enity, for an internal entity version would be null
286N/A */
286N/A public String getEntityVersion(){
286N/A return version ;
286N/A }
286N/A
286N/A /** each 'external' parsed entity may have xml/text declaration containing version information
286N/A * @param String version of the external parsed entity
286N/A */
286N/A public void setEntityVersion(String version){
286N/A this.version = version ;
286N/A }
286N/A
286N/A /** Returns the java.io.Reader associated with this entity.Readers are used
286N/A * to read from the file. Readers wrap any particular InputStream that was
286N/A * used to open the entity.
286N/A * @return java.io.Reader Reader associated with this entity
286N/A */
286N/A public Reader getEntityReader(){
286N/A return reader;
286N/A }
286N/A
286N/A
286N/A /** if entity was opened using the stream, return the associated inputstream
286N/A * with this entity
286N/A *@return java.io.InputStream InputStream associated with this entity
286N/A */
286N/A public InputStream getEntityInputStream(){
286N/A return stream;
286N/A }
286N/A
286N/A //
286N/A // Constructors
286N/A //
286N/A
286N/A /** Constructs a scanned entity. */
286N/A public ScannedEntity(String name,
286N/A XMLResourceIdentifier entityLocation,
286N/A InputStream stream, Reader reader,
286N/A String encoding, boolean literal, boolean mayReadChunks, boolean isExternal) {
286N/A this.name = name ;
286N/A this.entityLocation = entityLocation;
286N/A this.stream = stream;
286N/A this.reader = reader;
286N/A this.encoding = encoding;
286N/A this.literal = literal;
286N/A this.mayReadChunks = mayReadChunks;
286N/A this.isExternal = isExternal;
286N/A final int size = isExternal ? fBufferSize : DEFAULT_INTERNAL_BUFFER_SIZE;
286N/A BufferAllocator ba = ThreadLocalBufferAllocator.getBufferAllocator();
286N/A ch = ba.getCharBuffer(size);
286N/A if (ch == null) {
286N/A this.ch = new char[size];
286N/A }
286N/A } // <init>(StringXMLResourceIdentifier,InputStream,Reader,String,boolean, boolean)
286N/A
286N/A /**
286N/A * Release any resources associated with this entity.
286N/A */
286N/A public void close() throws IOException {
286N/A BufferAllocator ba = ThreadLocalBufferAllocator.getBufferAllocator();
286N/A ba.returnCharBuffer(ch);
286N/A ch = null;
286N/A reader.close();
286N/A }
286N/A
286N/A //
286N/A // Entity methods
286N/A //
286N/A
286N/A /** Returns whether the encoding of this entity was externally specified. **/
286N/A public boolean isEncodingExternallySpecified() {
286N/A return externallySpecifiedEncoding;
286N/A }
286N/A
286N/A /** Sets whether the encoding of this entity was externally specified. **/
286N/A public void setEncodingExternallySpecified(boolean value) {
286N/A externallySpecifiedEncoding = value;
286N/A }
286N/A
286N/A public boolean isDeclaredEncoding() {
286N/A return declaredEncoding;
286N/A }
286N/A
286N/A public void setDeclaredEncoding(boolean value) {
286N/A declaredEncoding = value;
286N/A }
286N/A
286N/A /** Returns true if this is an external entity. */
286N/A public final boolean isExternal() {
286N/A return isExternal;
286N/A } // isExternal():boolean
286N/A
286N/A /** Returns true if this is an unparsed entity. */
286N/A public final boolean isUnparsed() {
286N/A return false;
286N/A } // isUnparsed():boolean
286N/A
286N/A //
286N/A // Object methods
286N/A //
286N/A
286N/A /** Returns a string representation of this object. */
286N/A public String toString() {
286N/A
286N/A StringBuffer str = new StringBuffer();
286N/A str.append("name=\""+name+'"');
286N/A str.append(",ch="+ new String(ch));
286N/A str.append(",position="+position);
286N/A str.append(",count="+count);
286N/A return str.toString();
286N/A
286N/A } // toString():String
286N/A
286N/A } // class ScannedEntity
286N/A
286N/A} // class Entity