Path: blob/master/src/java.xml/share/classes/org/xml/sax/InputSource.java
40948 views
/*1* Copyright (c) 2000, 2020, 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 org.xml.sax;2627import java.io.IOException;28import java.io.Reader;29import java.io.InputStream;3031/**32* A single input source for an XML entity.33*34* <p>This class allows a SAX application to encapsulate information35* about an input source in a single object, which may include36* a public identifier, a system identifier, a byte stream (possibly37* with a specified encoding), and/or a character stream.</p>38*39* <p>There are two places that the application can deliver an40* input source to the parser: as the argument to the Parser.parse41* method, or as the return value of the EntityResolver.resolveEntity42* method.</p>43*44* <p>The SAX parser will use the InputSource object to determine how45* to read XML input. If there is a character stream available, the46* parser will read that stream directly, disregarding any text47* encoding declaration found in that stream.48* If there is no character stream, but there is49* a byte stream, the parser will use that byte stream, using the50* encoding specified in the InputSource or else (if no encoding is51* specified) autodetecting the character encoding using an algorithm52* such as the one in the XML specification. If neither a character53* stream nor a54* byte stream is available, the parser will attempt to open a URI55* connection to the resource identified by the system56* identifier.</p>57*58* <p>An InputSource object belongs to the application: the SAX parser59* shall never modify it in any way (it may modify a copy if60* necessary). However, standard processing of both byte and61* character streams is to close them on as part of end-of-parse cleanup,62* so applications should not attempt to re-use such streams after they63* have been handed to a parser. </p>64*65* @since 1.4, SAX 1.066* @author David Megginson67* @see org.xml.sax.XMLReader#parse(org.xml.sax.InputSource)68* @see org.xml.sax.EntityResolver#resolveEntity69* @see java.io.InputStream70* @see java.io.Reader71*/72public class InputSource {7374/**75* Zero-argument default constructor.76*77* @see #setPublicId78* @see #setSystemId79* @see #setByteStream80* @see #setCharacterStream81* @see #setEncoding82*/83public InputSource ()84{85}868788/**89* Create a new input source with a system identifier.90*91* <p>Applications may use setPublicId to include a92* public identifier as well, or setEncoding to specify93* the character encoding, if known.</p>94*95* <p>If the system identifier is a URL, it must be fully96* resolved (it may not be a relative URL).</p>97*98* @param systemId The system identifier (URI).99* @see #setPublicId100* @see #setSystemId101* @see #setByteStream102* @see #setEncoding103* @see #setCharacterStream104*/105public InputSource (String systemId)106{107setSystemId(systemId);108}109110111/**112* Create a new input source with a byte stream.113*114* <p>Application writers should use setSystemId() to provide a base115* for resolving relative URIs, may use setPublicId to include a116* public identifier, and may use setEncoding to specify the object's117* character encoding.</p>118*119* @param byteStream The raw byte stream containing the document.120* @see #setPublicId121* @see #setSystemId122* @see #setEncoding123* @see #setByteStream124* @see #setCharacterStream125*/126public InputSource (InputStream byteStream)127{128setByteStream(byteStream);129}130131132/**133* Create a new input source with a character stream.134*135* <p>Application writers should use setSystemId() to provide a base136* for resolving relative URIs, and may use setPublicId to include a137* public identifier.</p>138*139* <p>The character stream shall not include a byte order mark.</p>140*141* @param characterStream the character stream142* @see #setPublicId143* @see #setSystemId144* @see #setByteStream145* @see #setCharacterStream146*/147public InputSource (Reader characterStream)148{149setCharacterStream(characterStream);150}151152153/**154* Set the public identifier for this input source.155*156* <p>The public identifier is always optional: if the application157* writer includes one, it will be provided as part of the158* location information.</p>159*160* @param publicId The public identifier as a string.161* @see #getPublicId162* @see org.xml.sax.Locator#getPublicId163* @see org.xml.sax.SAXParseException#getPublicId164*/165public void setPublicId (String publicId)166{167this.publicId = publicId;168}169170171/**172* Get the public identifier for this input source.173*174* @return The public identifier, or null if none was supplied.175* @see #setPublicId176*/177public String getPublicId ()178{179return publicId;180}181182183/**184* Set the system identifier for this input source.185*186* <p>The system identifier is optional if there is a byte stream187* or a character stream, but it is still useful to provide one,188* since the application can use it to resolve relative URIs189* and can include it in error messages and warnings (the parser190* will attempt to open a connection to the URI only if191* there is no byte stream or character stream specified).</p>192*193* <p>If the application knows the character encoding of the194* object pointed to by the system identifier, it can register195* the encoding using the setEncoding method.</p>196*197* <p>If the system identifier is a URL, it must be fully198* resolved (it may not be a relative URL).</p>199*200* @param systemId The system identifier as a string.201* @see #setEncoding202* @see #getSystemId203* @see org.xml.sax.Locator#getSystemId204* @see org.xml.sax.SAXParseException#getSystemId205*/206public void setSystemId (String systemId)207{208this.systemId = systemId;209}210211212/**213* Get the system identifier for this input source.214*215* <p>The getEncoding method will return the character encoding216* of the object pointed to, or null if unknown.</p>217*218* <p>If the system ID is a URL, it will be fully resolved.</p>219*220* @return The system identifier, or null if none was supplied.221* @see #setSystemId222* @see #getEncoding223*/224public String getSystemId ()225{226return systemId;227}228229230/**231* Set the byte stream for this input source.232*233* <p>The SAX parser will ignore this if there is also a character234* stream specified, but it will use a byte stream in preference235* to opening a URI connection itself.</p>236*237* <p>If the application knows the character encoding of the238* byte stream, it should set it with the setEncoding method.</p>239*240* @param byteStream A byte stream containing an XML document or241* other entity.242* @see #setEncoding243* @see #getByteStream244* @see #getEncoding245* @see java.io.InputStream246*/247public void setByteStream (InputStream byteStream)248{249this.byteStream = byteStream;250}251252253/**254* Get the byte stream for this input source.255*256* <p>The getEncoding method will return the character257* encoding for this byte stream, or null if unknown.</p>258*259* @return The byte stream, or null if none was supplied.260* @see #getEncoding261* @see #setByteStream262*/263public InputStream getByteStream ()264{265return byteStream;266}267268269/**270* Set the character encoding, if known.271*272* <p>The encoding must be a string acceptable for an273* XML encoding declaration (see section 4.3.3 of the XML 1.0274* recommendation).</p>275*276* <p>This method has no effect when the application provides a277* character stream.</p>278*279* @param encoding A string describing the character encoding.280* @see #setSystemId281* @see #setByteStream282* @see #getEncoding283*/284public void setEncoding (String encoding)285{286this.encoding = encoding;287}288289290/**291* Get the character encoding for a byte stream or URI.292* This value will be ignored when the application provides a293* character stream.294*295* @return The encoding, or null if none was supplied.296* @see #setByteStream297* @see #getSystemId298* @see #getByteStream299*/300public String getEncoding ()301{302return encoding;303}304305306/**307* Set the character stream for this input source.308*309* <p>If there is a character stream specified, the SAX parser310* will ignore any byte stream and will not attempt to open311* a URI connection to the system identifier.</p>312*313* @param characterStream The character stream containing the314* XML document or other entity.315* @see #getCharacterStream316* @see java.io.Reader317*/318public void setCharacterStream (Reader characterStream)319{320this.characterStream = characterStream;321}322323324/**325* Get the character stream for this input source.326*327* @return The character stream, or null if none was supplied.328* @see #setCharacterStream329*/330public Reader getCharacterStream ()331{332return characterStream;333}334335/**336* Indicates whether the {@code InputSource} object is empty. Empty is337* defined as follows:338* <ul>339* <li>All of the input sources, including the public identifier, system340* identifier, byte stream, and character stream, are {@code null}.341* </li>342* <li>The public identifier and system identifier are {@code null}, and343* byte and character stream are either {@code null} or contain no byte344* or character.345* <p>346* Note that this method will reset the byte stream if it is provided, or347* the character stream if the byte stream is not provided.348* </li>349* </ul>350* <p>351* In case of error while checking the byte or character stream, the method352* will return false to allow the XML processor to handle the error.353*354* @return true if the {@code InputSource} object is empty, false otherwise355*/356public boolean isEmpty() {357return (publicId == null && systemId == null && isStreamEmpty());358}359360private boolean isStreamEmpty() {361boolean empty = true;362try {363if (byteStream != null) {364byteStream.reset();365int bytesRead = byteStream.available();366if (bytesRead > 0) {367return false;368}369}370371if (characterStream != null) {372characterStream.reset();373int c = characterStream.read();374characterStream.reset();375if (c != -1) {376return false;377}378}379} catch (IOException ex) {380//in case of error, return false381return false;382}383384return empty;385}386////////////////////////////////////////////////////////////////////387// Internal state.388////////////////////////////////////////////////////////////////////389390private String publicId;391private String systemId;392private InputStream byteStream;393private String encoding;394private Reader characterStream;395396}397398// end of InputSource.java399400401