Path: blob/jdk8u272-b10-aarch32-20201026/jaxp/src/com/sun/xml/internal/stream/Entity.java
83408 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2*/34/*5* Copyright 2005 The Apache Software Foundation.6*7* Licensed under the Apache License, Version 2.0 (the "License");8* you may not use this file except in compliance with the License.9* You may obtain a copy of the License at10*11* http://www.apache.org/licenses/LICENSE-2.012*13* Unless required by applicable law or agreed to in writing, software14* distributed under the License is distributed on an "AS IS" BASIS,15* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.16* See the License for the specific language governing permissions and17* limitations under the License.18*/1920package com.sun.xml.internal.stream;2122import java.io.InputStream;23import java.io.Reader;24import java.io.IOException;2526import com.sun.xml.internal.stream.util.BufferAllocator;27import com.sun.xml.internal.stream.util.ThreadLocalBufferAllocator;28import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;2930/**31* Entity information.32*33* @author34*/35public abstract class Entity {3637//38// Data39//4041//xxx why dont we declare the type of entities, like assign integer for external/ internal etc..4243/** Entity name. */44public String name;4546// whether this entity's declaration was found in the internal47// or external subset48public boolean inExternalSubset;4950//51// Constructors52//5354/** Default constructor. */55public Entity() {56clear();57} // <init>()5859/** Constructs an entity. */60public Entity(String name, boolean inExternalSubset) {61this.name = name;62this.inExternalSubset = inExternalSubset;63} // <init>(String)6465//66// Public methods67//6869/** Returns true if this entity was declared in the external subset. */70public boolean isEntityDeclInExternalSubset() {71return inExternalSubset;72}7374/** Returns true if this is an external entity. */75public abstract boolean isExternal();7677/** Returns true if this is an unparsed entity. */78public abstract boolean isUnparsed();7980/** Clears the entity. */81public void clear() {82name = null;83inExternalSubset = false;84} // clear()8586/** Sets the values of the entity. */87public void setValues(Entity entity) {88name = entity.name;89inExternalSubset = entity.inExternalSubset;90} // setValues(Entity)919293/**94* Internal entity.95*96* @author nb13116597*/98public static class InternalEntity99extends Entity {100101//102// Data103//104105/** Text value of entity. */106public String text;107108//109// Constructors110//111112/** Default constructor. */113public InternalEntity() {114clear();115} // <init>()116117/** Constructs an internal entity. */118public InternalEntity(String name, String text, boolean inExternalSubset) {119super(name,inExternalSubset);120this.text = text;121} // <init>(String,String)122123//124// Entity methods125//126127/** Returns true if this is an external entity. */128public final boolean isExternal() {129return false;130} // isExternal():boolean131132/** Returns true if this is an unparsed entity. */133public final boolean isUnparsed() {134return false;135} // isUnparsed():boolean136137/** Clears the entity. */138public void clear() {139super.clear();140text = null;141} // clear()142143/** Sets the values of the entity. */144public void setValues(Entity entity) {145super.setValues(entity);146text = null;147} // setValues(Entity)148149/** Sets the values of the entity. */150public void setValues(InternalEntity entity) {151super.setValues(entity);152text = entity.text;153} // setValues(InternalEntity)154155} // class InternalEntity156157/**158* External entity.159*160* @author nb131165161*/162public static class ExternalEntity163extends Entity {164165//166// Data167//168169/** container for all relevant entity location information. */170public XMLResourceIdentifier entityLocation;171172/** Notation name for unparsed entity. */173public String notation;174175//176// Constructors177//178179/** Default constructor. */180public ExternalEntity() {181clear();182} // <init>()183184/** Constructs an internal entity. */185public ExternalEntity(String name, XMLResourceIdentifier entityLocation,186String notation, boolean inExternalSubset) {187super(name,inExternalSubset);188this.entityLocation = entityLocation;189this.notation = notation;190} // <init>(String,XMLResourceIdentifier, String)191192//193// Entity methods194//195196/** Returns true if this is an external entity. */197public final boolean isExternal() {198return true;199} // isExternal():boolean200201/** Returns true if this is an unparsed entity. */202public final boolean isUnparsed() {203return notation != null;204} // isUnparsed():boolean205206/** Clears the entity. */207public void clear() {208super.clear();209entityLocation = null;210notation = null;211} // clear()212213/** Sets the values of the entity. */214public void setValues(Entity entity) {215super.setValues(entity);216entityLocation = null;217notation = null;218} // setValues(Entity)219220/** Sets the values of the entity. */221public void setValues(ExternalEntity entity) {222super.setValues(entity);223entityLocation = entity.entityLocation;224notation = entity.notation;225} // setValues(ExternalEntity)226227} // class ExternalEntity228229/**230* Entity state.231*232* @author nb131165233*/234public static class ScannedEntity235extends Entity {236237238/** Default buffer size (4096). */239public static final int DEFAULT_BUFFER_SIZE = 8192;240//4096;241242/**243* Buffer size. We get this value from a property. The default size244* is used if the input buffer size property is not specified.245* REVISIT: do we need a property for internal entity buffer size?246*/247public int fBufferSize = DEFAULT_BUFFER_SIZE;248249/** Default buffer size before we've finished with the XMLDecl: */250public static final int DEFAULT_XMLDECL_BUFFER_SIZE = 28;251252/** Default internal entity buffer size (1024). */253public static final int DEFAULT_INTERNAL_BUFFER_SIZE = 1024;254255//256// Data257//258259// i/o260261/** XXX let these field remain public right now, though we have defined methods for them.262* Input stream. */263public InputStream stream;264265/** XXX let these field remain public right now, though we have defined methods for them.266* Reader. */267public Reader reader;268269// locator information270271/** entity location information */272public XMLResourceIdentifier entityLocation;273274// encoding275276/** Auto-detected encoding. */277public String encoding;278279// status280281/** True if in a literal. */282public boolean literal;283284// whether this is an external or internal scanned entity285public boolean isExternal;286287//each 'external' parsed entity may have xml/text declaration containing version information288public String version ;289290// buffer291292/** Character buffer. */293public char[] ch = null;294295/** Position in character buffer at any point of time. */296public int position;297298/** Count of characters present in buffer. */299public int count;300301/** Line number. */302public int lineNumber = 1;303304/** Column number. */305public int columnNumber = 1;306307/** Encoding has been set externally for eg: using DOMInput*/308boolean declaredEncoding = false;309310// status311312/**313* Encoding has been set externally, for example314* using a SAX InputSource or a DOM LSInput.315*/316boolean externallySpecifiedEncoding = false;317318/** XML version. **/319public String xmlVersion = "1.0";320321/** This variable is used to calculate the current position in the XML stream.322* Note that fCurrentEntity.position maintains the position relative to323* the buffer.324* At any point of time absolute position in the XML stream can be calculated325* as fTotalCountTillLastLoad + fCurrentEntity.position326*/327public int fTotalCountTillLastLoad ;328329/** This variable stores the number of characters read during the load()330* operation. It is used to calculate fTotalCountTillLastLoad331*/332public int fLastCount ;333334/** Base character offset for computing absolute character offset. */335public int baseCharOffset;336337/** Start position in character buffer. */338public int startPosition;339340// to allow the reader/inputStream to behave efficiently:341public boolean mayReadChunks;342343// to know that prolog is read344public boolean xmlDeclChunkRead = false;345346// flag to indicate whether the Entity is a General Entity347public boolean isGE = false;348349/** returns the name of the current encoding350* @return current encoding name351*/352public String getEncodingName(){353return encoding ;354}355356/**each 'external' parsed entity may have xml/text declaration containing version information357* @return String version of the enity, for an internal entity version would be null358*/359public String getEntityVersion(){360return version ;361}362363/** each 'external' parsed entity may have xml/text declaration containing version information364* @param String version of the external parsed entity365*/366public void setEntityVersion(String version){367this.version = version ;368}369370/** Returns the java.io.Reader associated with this entity.Readers are used371* to read from the file. Readers wrap any particular InputStream that was372* used to open the entity.373* @return java.io.Reader Reader associated with this entity374*/375public Reader getEntityReader(){376return reader;377}378379380/** if entity was opened using the stream, return the associated inputstream381* with this entity382*@return java.io.InputStream InputStream associated with this entity383*/384public InputStream getEntityInputStream(){385return stream;386}387388//389// Constructors390//391392/** Constructs a scanned entity. */393public ScannedEntity(boolean isGE, String name,394XMLResourceIdentifier entityLocation,395InputStream stream, Reader reader,396String encoding, boolean literal, boolean mayReadChunks, boolean isExternal) {397this.isGE = isGE;398this.name = name ;399this.entityLocation = entityLocation;400this.stream = stream;401this.reader = reader;402this.encoding = encoding;403this.literal = literal;404this.mayReadChunks = mayReadChunks;405this.isExternal = isExternal;406final int size = isExternal ? fBufferSize : DEFAULT_INTERNAL_BUFFER_SIZE;407BufferAllocator ba = ThreadLocalBufferAllocator.getBufferAllocator();408ch = ba.getCharBuffer(size);409if (ch == null) {410this.ch = new char[size];411}412} // <init>(StringXMLResourceIdentifier,InputStream,Reader,String,boolean, boolean)413414/**415* Release any resources associated with this entity.416*/417public void close() throws IOException {418BufferAllocator ba = ThreadLocalBufferAllocator.getBufferAllocator();419ba.returnCharBuffer(ch);420ch = null;421reader.close();422}423424//425// Entity methods426//427428/** Returns whether the encoding of this entity was externally specified. **/429public boolean isEncodingExternallySpecified() {430return externallySpecifiedEncoding;431}432433/** Sets whether the encoding of this entity was externally specified. **/434public void setEncodingExternallySpecified(boolean value) {435externallySpecifiedEncoding = value;436}437438public boolean isDeclaredEncoding() {439return declaredEncoding;440}441442public void setDeclaredEncoding(boolean value) {443declaredEncoding = value;444}445446/** Returns true if this is an external entity. */447public final boolean isExternal() {448return isExternal;449} // isExternal():boolean450451/** Returns true if this is an unparsed entity. */452public final boolean isUnparsed() {453return false;454} // isUnparsed():boolean455456//457// Object methods458//459460/** Returns a string representation of this object. */461public String toString() {462463StringBuffer str = new StringBuffer();464str.append("name=\""+name+'"');465str.append(",ch="+ new String(ch));466str.append(",position="+position);467str.append(",count="+count);468return str.toString();469470} // toString():String471472} // class ScannedEntity473474} // class Entity475476477