Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/org/xml/sax/InputSource.java
48534 views
/*1* Copyright (c) 2000, 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*/2425// SAX input source.26// http://www.saxproject.org27// No warranty; no copyright -- use this as you will.28// $Id: InputSource.java,v 1.2 2004/11/03 22:55:32 jsuttor Exp $2930package org.xml.sax;3132import java.io.Reader;33import java.io.InputStream;3435/**36* A single input source for an XML entity.37*38* <blockquote>39* <em>This module, both source code and documentation, is in the40* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>41* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>42* for further information.43* </blockquote>44*45* <p>This class allows a SAX application to encapsulate information46* about an input source in a single object, which may include47* a public identifier, a system identifier, a byte stream (possibly48* with a specified encoding), and/or a character stream.</p>49*50* <p>There are two places that the application can deliver an51* input source to the parser: as the argument to the Parser.parse52* method, or as the return value of the EntityResolver.resolveEntity53* method.</p>54*55* <p>The SAX parser will use the InputSource object to determine how56* to read XML input. If there is a character stream available, the57* parser will read that stream directly, disregarding any text58* encoding declaration found in that stream.59* If there is no character stream, but there is60* a byte stream, the parser will use that byte stream, using the61* encoding specified in the InputSource or else (if no encoding is62* specified) autodetecting the character encoding using an algorithm63* such as the one in the XML specification. If neither a character64* stream nor a65* byte stream is available, the parser will attempt to open a URI66* connection to the resource identified by the system67* identifier.</p>68*69* <p>An InputSource object belongs to the application: the SAX parser70* shall never modify it in any way (it may modify a copy if71* necessary). However, standard processing of both byte and72* character streams is to close them on as part of end-of-parse cleanup,73* so applications should not attempt to re-use such streams after they74* have been handed to a parser. </p>75*76* @since SAX 1.077* @author David Megginson78* @see org.xml.sax.XMLReader#parse(org.xml.sax.InputSource)79* @see org.xml.sax.EntityResolver#resolveEntity80* @see java.io.InputStream81* @see java.io.Reader82*/83public class InputSource {8485/**86* Zero-argument default constructor.87*88* @see #setPublicId89* @see #setSystemId90* @see #setByteStream91* @see #setCharacterStream92* @see #setEncoding93*/94public InputSource ()95{96}979899/**100* Create a new input source with a system identifier.101*102* <p>Applications may use setPublicId to include a103* public identifier as well, or setEncoding to specify104* the character encoding, if known.</p>105*106* <p>If the system identifier is a URL, it must be fully107* resolved (it may not be a relative URL).</p>108*109* @param systemId The system identifier (URI).110* @see #setPublicId111* @see #setSystemId112* @see #setByteStream113* @see #setEncoding114* @see #setCharacterStream115*/116public InputSource (String systemId)117{118setSystemId(systemId);119}120121122/**123* Create a new input source with a byte stream.124*125* <p>Application writers should use setSystemId() to provide a base126* for resolving relative URIs, may use setPublicId to include a127* public identifier, and may use setEncoding to specify the object's128* character encoding.</p>129*130* @param byteStream The raw byte stream containing the document.131* @see #setPublicId132* @see #setSystemId133* @see #setEncoding134* @see #setByteStream135* @see #setCharacterStream136*/137public InputSource (InputStream byteStream)138{139setByteStream(byteStream);140}141142143/**144* Create a new input source with a character stream.145*146* <p>Application writers should use setSystemId() to provide a base147* for resolving relative URIs, and may use setPublicId to include a148* public identifier.</p>149*150* <p>The character stream shall not include a byte order mark.</p>151*152* @see #setPublicId153* @see #setSystemId154* @see #setByteStream155* @see #setCharacterStream156*/157public InputSource (Reader characterStream)158{159setCharacterStream(characterStream);160}161162163/**164* Set the public identifier for this input source.165*166* <p>The public identifier is always optional: if the application167* writer includes one, it will be provided as part of the168* location information.</p>169*170* @param publicId The public identifier as a string.171* @see #getPublicId172* @see org.xml.sax.Locator#getPublicId173* @see org.xml.sax.SAXParseException#getPublicId174*/175public void setPublicId (String publicId)176{177this.publicId = publicId;178}179180181/**182* Get the public identifier for this input source.183*184* @return The public identifier, or null if none was supplied.185* @see #setPublicId186*/187public String getPublicId ()188{189return publicId;190}191192193/**194* Set the system identifier for this input source.195*196* <p>The system identifier is optional if there is a byte stream197* or a character stream, but it is still useful to provide one,198* since the application can use it to resolve relative URIs199* and can include it in error messages and warnings (the parser200* will attempt to open a connection to the URI only if201* there is no byte stream or character stream specified).</p>202*203* <p>If the application knows the character encoding of the204* object pointed to by the system identifier, it can register205* the encoding using the setEncoding method.</p>206*207* <p>If the system identifier is a URL, it must be fully208* resolved (it may not be a relative URL).</p>209*210* @param systemId The system identifier as a string.211* @see #setEncoding212* @see #getSystemId213* @see org.xml.sax.Locator#getSystemId214* @see org.xml.sax.SAXParseException#getSystemId215*/216public void setSystemId (String systemId)217{218this.systemId = systemId;219}220221222/**223* Get the system identifier for this input source.224*225* <p>The getEncoding method will return the character encoding226* of the object pointed to, or null if unknown.</p>227*228* <p>If the system ID is a URL, it will be fully resolved.</p>229*230* @return The system identifier, or null if none was supplied.231* @see #setSystemId232* @see #getEncoding233*/234public String getSystemId ()235{236return systemId;237}238239240/**241* Set the byte stream for this input source.242*243* <p>The SAX parser will ignore this if there is also a character244* stream specified, but it will use a byte stream in preference245* to opening a URI connection itself.</p>246*247* <p>If the application knows the character encoding of the248* byte stream, it should set it with the setEncoding method.</p>249*250* @param byteStream A byte stream containing an XML document or251* other entity.252* @see #setEncoding253* @see #getByteStream254* @see #getEncoding255* @see java.io.InputStream256*/257public void setByteStream (InputStream byteStream)258{259this.byteStream = byteStream;260}261262263/**264* Get the byte stream for this input source.265*266* <p>The getEncoding method will return the character267* encoding for this byte stream, or null if unknown.</p>268*269* @return The byte stream, or null if none was supplied.270* @see #getEncoding271* @see #setByteStream272*/273public InputStream getByteStream ()274{275return byteStream;276}277278279/**280* Set the character encoding, if known.281*282* <p>The encoding must be a string acceptable for an283* XML encoding declaration (see section 4.3.3 of the XML 1.0284* recommendation).</p>285*286* <p>This method has no effect when the application provides a287* character stream.</p>288*289* @param encoding A string describing the character encoding.290* @see #setSystemId291* @see #setByteStream292* @see #getEncoding293*/294public void setEncoding (String encoding)295{296this.encoding = encoding;297}298299300/**301* Get the character encoding for a byte stream or URI.302* This value will be ignored when the application provides a303* character stream.304*305* @return The encoding, or null if none was supplied.306* @see #setByteStream307* @see #getSystemId308* @see #getByteStream309*/310public String getEncoding ()311{312return encoding;313}314315316/**317* Set the character stream for this input source.318*319* <p>If there is a character stream specified, the SAX parser320* will ignore any byte stream and will not attempt to open321* a URI connection to the system identifier.</p>322*323* @param characterStream The character stream containing the324* XML document or other entity.325* @see #getCharacterStream326* @see java.io.Reader327*/328public void setCharacterStream (Reader characterStream)329{330this.characterStream = characterStream;331}332333334/**335* Get the character stream for this input source.336*337* @return The character stream, or null if none was supplied.338* @see #setCharacterStream339*/340public Reader getCharacterStream ()341{342return characterStream;343}344345346347////////////////////////////////////////////////////////////////////348// Internal state.349////////////////////////////////////////////////////////////////////350351private String publicId;352private String systemId;353private InputStream byteStream;354private String encoding;355private Reader characterStream;356357}358359// end of InputSource.java360361362