286N/A/*
286N/A * Copyright (c) 2000, 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/A// SAX input source.
286N/A// http://www.saxproject.org
286N/A// No warranty; no copyright -- use this as you will.
286N/A// $Id: InputSource.java,v 1.2 2004/11/03 22:55:32 jsuttor Exp $
286N/A
286N/Apackage org.xml.sax;
286N/A
286N/Aimport java.io.Reader;
286N/Aimport java.io.InputStream;
286N/A
286N/A/**
286N/A * A single input source for an XML entity.
286N/A *
286N/A * <blockquote>
286N/A * <em>This module, both source code and documentation, is in the
286N/A * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
286N/A * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
286N/A * for further information.
286N/A * </blockquote>
286N/A *
286N/A * <p>This class allows a SAX application to encapsulate information
286N/A * about an input source in a single object, which may include
286N/A * a public identifier, a system identifier, a byte stream (possibly
286N/A * with a specified encoding), and/or a character stream.</p>
286N/A *
286N/A * <p>There are two places that the application can deliver an
286N/A * input source to the parser: as the argument to the Parser.parse
286N/A * method, or as the return value of the EntityResolver.resolveEntity
286N/A * method.</p>
286N/A *
286N/A * <p>The SAX parser will use the InputSource object to determine how
286N/A * to read XML input. If there is a character stream available, the
286N/A * parser will read that stream directly, disregarding any text
286N/A * encoding declaration found in that stream.
286N/A * If there is no character stream, but there is
286N/A * a byte stream, the parser will use that byte stream, using the
286N/A * encoding specified in the InputSource or else (if no encoding is
286N/A * specified) autodetecting the character encoding using an algorithm
286N/A * such as the one in the XML specification. If neither a character
286N/A * stream nor a
286N/A * byte stream is available, the parser will attempt to open a URI
286N/A * connection to the resource identified by the system
286N/A * identifier.</p>
286N/A *
286N/A * <p>An InputSource object belongs to the application: the SAX parser
286N/A * shall never modify it in any way (it may modify a copy if
286N/A * necessary). However, standard processing of both byte and
286N/A * character streams is to close them on as part of end-of-parse cleanup,
286N/A * so applications should not attempt to re-use such streams after they
286N/A * have been handed to a parser. </p>
286N/A *
286N/A * @since SAX 1.0
286N/A * @author David Megginson
286N/A * @see org.xml.sax.XMLReader#parse(org.xml.sax.InputSource)
286N/A * @see org.xml.sax.EntityResolver#resolveEntity
286N/A * @see java.io.InputStream
286N/A * @see java.io.Reader
286N/A */
286N/Apublic class InputSource {
286N/A
286N/A /**
286N/A * Zero-argument default constructor.
286N/A *
286N/A * @see #setPublicId
286N/A * @see #setSystemId
286N/A * @see #setByteStream
286N/A * @see #setCharacterStream
286N/A * @see #setEncoding
286N/A */
286N/A public InputSource ()
286N/A {
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Create a new input source with a system identifier.
286N/A *
286N/A * <p>Applications may use setPublicId to include a
286N/A * public identifier as well, or setEncoding to specify
286N/A * the character encoding, if known.</p>
286N/A *
286N/A * <p>If the system identifier is a URL, it must be fully
286N/A * resolved (it may not be a relative URL).</p>
286N/A *
286N/A * @param systemId The system identifier (URI).
286N/A * @see #setPublicId
286N/A * @see #setSystemId
286N/A * @see #setByteStream
286N/A * @see #setEncoding
286N/A * @see #setCharacterStream
286N/A */
286N/A public InputSource (String systemId)
286N/A {
286N/A setSystemId(systemId);
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Create a new input source with a byte stream.
286N/A *
286N/A * <p>Application writers should use setSystemId() to provide a base
286N/A * for resolving relative URIs, may use setPublicId to include a
286N/A * public identifier, and may use setEncoding to specify the object's
286N/A * character encoding.</p>
286N/A *
286N/A * @param byteStream The raw byte stream containing the document.
286N/A * @see #setPublicId
286N/A * @see #setSystemId
286N/A * @see #setEncoding
286N/A * @see #setByteStream
286N/A * @see #setCharacterStream
286N/A */
286N/A public InputSource (InputStream byteStream)
286N/A {
286N/A setByteStream(byteStream);
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Create a new input source with a character stream.
286N/A *
286N/A * <p>Application writers should use setSystemId() to provide a base
286N/A * for resolving relative URIs, and may use setPublicId to include a
286N/A * public identifier.</p>
286N/A *
286N/A * <p>The character stream shall not include a byte order mark.</p>
286N/A *
286N/A * @see #setPublicId
286N/A * @see #setSystemId
286N/A * @see #setByteStream
286N/A * @see #setCharacterStream
286N/A */
286N/A public InputSource (Reader characterStream)
286N/A {
286N/A setCharacterStream(characterStream);
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Set the public identifier for this input source.
286N/A *
286N/A * <p>The public identifier is always optional: if the application
286N/A * writer includes one, it will be provided as part of the
286N/A * location information.</p>
286N/A *
286N/A * @param publicId The public identifier as a string.
286N/A * @see #getPublicId
286N/A * @see org.xml.sax.Locator#getPublicId
286N/A * @see org.xml.sax.SAXParseException#getPublicId
286N/A */
286N/A public void setPublicId (String publicId)
286N/A {
286N/A this.publicId = publicId;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Get the public identifier for this input source.
286N/A *
286N/A * @return The public identifier, or null if none was supplied.
286N/A * @see #setPublicId
286N/A */
286N/A public String getPublicId ()
286N/A {
286N/A return publicId;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Set the system identifier for this input source.
286N/A *
286N/A * <p>The system identifier is optional if there is a byte stream
286N/A * or a character stream, but it is still useful to provide one,
286N/A * since the application can use it to resolve relative URIs
286N/A * and can include it in error messages and warnings (the parser
286N/A * will attempt to open a connection to the URI only if
286N/A * there is no byte stream or character stream specified).</p>
286N/A *
286N/A * <p>If the application knows the character encoding of the
286N/A * object pointed to by the system identifier, it can register
286N/A * the encoding using the setEncoding method.</p>
286N/A *
286N/A * <p>If the system identifier is a URL, it must be fully
286N/A * resolved (it may not be a relative URL).</p>
286N/A *
286N/A * @param systemId The system identifier as a string.
286N/A * @see #setEncoding
286N/A * @see #getSystemId
286N/A * @see org.xml.sax.Locator#getSystemId
286N/A * @see org.xml.sax.SAXParseException#getSystemId
286N/A */
286N/A public void setSystemId (String systemId)
286N/A {
286N/A this.systemId = systemId;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Get the system identifier for this input source.
286N/A *
286N/A * <p>The getEncoding method will return the character encoding
286N/A * of the object pointed to, or null if unknown.</p>
286N/A *
286N/A * <p>If the system ID is a URL, it will be fully resolved.</p>
286N/A *
286N/A * @return The system identifier, or null if none was supplied.
286N/A * @see #setSystemId
286N/A * @see #getEncoding
286N/A */
286N/A public String getSystemId ()
286N/A {
286N/A return systemId;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Set the byte stream for this input source.
286N/A *
286N/A * <p>The SAX parser will ignore this if there is also a character
286N/A * stream specified, but it will use a byte stream in preference
286N/A * to opening a URI connection itself.</p>
286N/A *
286N/A * <p>If the application knows the character encoding of the
286N/A * byte stream, it should set it with the setEncoding method.</p>
286N/A *
286N/A * @param byteStream A byte stream containing an XML document or
286N/A * other entity.
286N/A * @see #setEncoding
286N/A * @see #getByteStream
286N/A * @see #getEncoding
286N/A * @see java.io.InputStream
286N/A */
286N/A public void setByteStream (InputStream byteStream)
286N/A {
286N/A this.byteStream = byteStream;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Get the byte stream for this input source.
286N/A *
286N/A * <p>The getEncoding method will return the character
286N/A * encoding for this byte stream, or null if unknown.</p>
286N/A *
286N/A * @return The byte stream, or null if none was supplied.
286N/A * @see #getEncoding
286N/A * @see #setByteStream
286N/A */
286N/A public InputStream getByteStream ()
286N/A {
286N/A return byteStream;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Set the character encoding, if known.
286N/A *
286N/A * <p>The encoding must be a string acceptable for an
286N/A * XML encoding declaration (see section 4.3.3 of the XML 1.0
286N/A * recommendation).</p>
286N/A *
286N/A * <p>This method has no effect when the application provides a
286N/A * character stream.</p>
286N/A *
286N/A * @param encoding A string describing the character encoding.
286N/A * @see #setSystemId
286N/A * @see #setByteStream
286N/A * @see #getEncoding
286N/A */
286N/A public void setEncoding (String encoding)
286N/A {
286N/A this.encoding = encoding;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Get the character encoding for a byte stream or URI.
286N/A * This value will be ignored when the application provides a
286N/A * character stream.
286N/A *
286N/A * @return The encoding, or null if none was supplied.
286N/A * @see #setByteStream
286N/A * @see #getSystemId
286N/A * @see #getByteStream
286N/A */
286N/A public String getEncoding ()
286N/A {
286N/A return encoding;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Set the character stream for this input source.
286N/A *
286N/A * <p>If there is a character stream specified, the SAX parser
286N/A * will ignore any byte stream and will not attempt to open
286N/A * a URI connection to the system identifier.</p>
286N/A *
286N/A * @param characterStream The character stream containing the
286N/A * XML document or other entity.
286N/A * @see #getCharacterStream
286N/A * @see java.io.Reader
286N/A */
286N/A public void setCharacterStream (Reader characterStream)
286N/A {
286N/A this.characterStream = characterStream;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Get the character stream for this input source.
286N/A *
286N/A * @return The character stream, or null if none was supplied.
286N/A * @see #setCharacterStream
286N/A */
286N/A public Reader getCharacterStream ()
286N/A {
286N/A return characterStream;
286N/A }
286N/A
286N/A
286N/A
286N/A ////////////////////////////////////////////////////////////////////
286N/A // Internal state.
286N/A ////////////////////////////////////////////////////////////////////
286N/A
286N/A private String publicId;
286N/A private String systemId;
286N/A private InputStream byteStream;
286N/A private String encoding;
286N/A private Reader characterStream;
286N/A
286N/A}
286N/A
286N/A// end of InputSource.java