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/Apackage javax.xml.transform.stream;
286N/A
286N/Aimport java.io.File;
286N/Aimport java.io.InputStream;
286N/Aimport java.io.Reader;
286N/A
286N/Aimport javax.xml.transform.Source;
286N/A
286N/A/**
286N/A * <p>Acts as an holder for a transformation Source in the form
286N/A * of a stream of XML markup.</p>
286N/A *
286N/A * <p><em>Note:</em> Due to their internal use of either a {@link Reader} or {@link InputStream} instance,
286N/A * <code>StreamSource</code> instances may only be used once.</p>
286N/A *
286N/A * @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
286N/A */
286N/Apublic class StreamSource implements Source {
286N/A
286N/A /** If {@link javax.xml.transform.TransformerFactory#getFeature}
286N/A * returns true when passed this value as an argument,
286N/A * the Transformer supports Source input of this type.
286N/A */
286N/A public static final String FEATURE =
286N/A "http://javax.xml.transform.stream.StreamSource/feature";
286N/A
286N/A /**
286N/A * <p>Zero-argument default constructor. If this constructor is used, and
286N/A * no Stream source is set using
286N/A * {@link #setInputStream(java.io.InputStream inputStream)} or
286N/A * {@link #setReader(java.io.Reader reader)}, then the
286N/A * <code>Transformer</code> will
286N/A * create an empty source {@link java.io.InputStream} using
286N/A * {@link java.io.InputStream#InputStream() new InputStream()}.</p>
286N/A *
286N/A * @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)
286N/A */
286N/A public StreamSource() { }
286N/A
286N/A /**
286N/A * Construct a StreamSource from a byte stream. Normally,
286N/A * a stream should be used rather than a reader, so
286N/A * the XML parser can resolve character encoding specified
286N/A * by the XML declaration.
286N/A *
286N/A * <p>If this constructor is used to process a stylesheet, normally
286N/A * setSystemId should also be called, so that relative URI references
286N/A * can be resolved.</p>
286N/A *
286N/A * @param inputStream A valid InputStream reference to an XML stream.
286N/A */
286N/A public StreamSource(InputStream inputStream) {
286N/A setInputStream(inputStream);
286N/A }
286N/A
286N/A /**
286N/A * Construct a StreamSource from a byte stream. Normally,
286N/A * a stream should be used rather than a reader, so that
286N/A * the XML parser can resolve character encoding specified
286N/A * by the XML declaration.
286N/A *
286N/A * <p>This constructor allows the systemID to be set in addition
286N/A * to the input stream, which allows relative URIs
286N/A * to be processed.</p>
286N/A *
286N/A * @param inputStream A valid InputStream reference to an XML stream.
286N/A * @param systemId Must be a String that conforms to the URI syntax.
286N/A */
286N/A public StreamSource(InputStream inputStream, String systemId) {
286N/A setInputStream(inputStream);
286N/A setSystemId(systemId);
286N/A }
286N/A
286N/A /**
286N/A * Construct a StreamSource from a character reader. Normally,
286N/A * a stream should be used rather than a reader, so that
286N/A * the XML parser can resolve character encoding specified
286N/A * by the XML declaration. However, in many cases the encoding
286N/A * of the input stream is already resolved, as in the case of
286N/A * reading XML from a StringReader.
286N/A *
286N/A * @param reader A valid Reader reference to an XML character stream.
286N/A */
286N/A public StreamSource(Reader reader) {
286N/A setReader(reader);
286N/A }
286N/A
286N/A /**
286N/A * Construct a StreamSource from a character reader. Normally,
286N/A * a stream should be used rather than a reader, so that
286N/A * the XML parser may resolve character encoding specified
286N/A * by the XML declaration. However, in many cases the encoding
286N/A * of the input stream is already resolved, as in the case of
286N/A * reading XML from a StringReader.
286N/A *
286N/A * @param reader A valid Reader reference to an XML character stream.
286N/A * @param systemId Must be a String that conforms to the URI syntax.
286N/A */
286N/A public StreamSource(Reader reader, String systemId) {
286N/A setReader(reader);
286N/A setSystemId(systemId);
286N/A }
286N/A
286N/A /**
286N/A * Construct a StreamSource from a URL.
286N/A *
286N/A * @param systemId Must be a String that conforms to the URI syntax.
286N/A */
286N/A public StreamSource(String systemId) {
286N/A this.systemId = systemId;
286N/A }
286N/A
286N/A /**
286N/A * Construct a StreamSource from a File.
286N/A *
286N/A * @param f Must a non-null File reference.
286N/A */
286N/A public StreamSource(File f) {
286N/A //convert file to appropriate URI, f.toURI().toASCIIString()
286N/A //converts the URI to string as per rule specified in
286N/A //RFC 2396,
286N/A setSystemId(f.toURI().toASCIIString());
286N/A }
286N/A
286N/A /**
286N/A * Set the byte stream to be used as input. Normally,
286N/A * a stream should be used rather than a reader, so that
286N/A * the XML parser can resolve character encoding specified
286N/A * by the XML declaration.
286N/A *
286N/A * <p>If this Source object is used to process a stylesheet, normally
286N/A * setSystemId should also be called, so that relative URL references
286N/A * can be resolved.</p>
286N/A *
286N/A * @param inputStream A valid InputStream reference to an XML stream.
286N/A */
286N/A public void setInputStream(InputStream inputStream) {
286N/A this.inputStream = inputStream;
286N/A }
286N/A
286N/A /**
286N/A * Get the byte stream that was set with setByteStream.
286N/A *
286N/A * @return The byte stream that was set with setByteStream, or null
286N/A * if setByteStream or the ByteStream constructor was not called.
286N/A */
286N/A public InputStream getInputStream() {
286N/A return inputStream;
286N/A }
286N/A
286N/A /**
286N/A * Set the input to be a character reader. Normally,
286N/A * a stream should be used rather than a reader, so that
286N/A * the XML parser can resolve character encoding specified
286N/A * by the XML declaration. However, in many cases the encoding
286N/A * of the input stream is already resolved, as in the case of
286N/A * reading XML from a StringReader.
286N/A *
286N/A * @param reader A valid Reader reference to an XML CharacterStream.
286N/A */
286N/A public void setReader(Reader reader) {
286N/A this.reader = reader;
286N/A }
286N/A
286N/A /**
286N/A * Get the character stream that was set with setReader.
286N/A *
286N/A * @return The character stream that was set with setReader, or null
286N/A * if setReader or the Reader constructor was not called.
286N/A */
286N/A public Reader getReader() {
286N/A return reader;
286N/A }
286N/A
286N/A /**
286N/A * Set the public identifier for this 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 */
286N/A public void setPublicId(String publicId) {
286N/A this.publicId = publicId;
286N/A }
286N/A
286N/A /**
286N/A * Get the public identifier that was set with setPublicId.
286N/A *
286N/A * @return The public identifier that was set with setPublicId, or null
286N/A * if setPublicId was not called.
286N/A */
286N/A public String getPublicId() {
286N/A return publicId;
286N/A }
286N/A
286N/A /**
286N/A * Set the system identifier for this 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 * @param systemId The system identifier as a URL string.
286N/A */
286N/A public void setSystemId(String systemId) {
286N/A this.systemId = systemId;
286N/A }
286N/A
286N/A /**
286N/A * Get the system identifier that was set with setSystemId.
286N/A *
286N/A * @return The system identifier that was set with setSystemId, or null
286N/A * if setSystemId was not called.
286N/A */
286N/A public String getSystemId() {
286N/A return systemId;
286N/A }
286N/A
286N/A /**
286N/A * Set the system ID from a File reference.
286N/A *
286N/A * @param f Must a non-null File reference.
286N/A */
286N/A public void setSystemId(File f) {
286N/A //convert file to appropriate URI, f.toURI().toASCIIString()
286N/A //converts the URI to string as per rule specified in
286N/A //RFC 2396,
286N/A this.systemId = f.toURI().toASCIIString();
286N/A }
286N/A
286N/A //////////////////////////////////////////////////////////////////////
286N/A // Internal state.
286N/A //////////////////////////////////////////////////////////////////////
286N/A
286N/A /**
286N/A * The public identifier for this input source, or null.
286N/A */
286N/A private String publicId;
286N/A
286N/A /**
286N/A * The system identifier as a URL string, or null.
286N/A */
286N/A private String systemId;
286N/A
286N/A /**
286N/A * The byte stream for this Source, or null.
286N/A */
286N/A private InputStream inputStream;
286N/A
286N/A /**
286N/A * The character stream for this Source, or null.
286N/A */
286N/A private Reader reader;
286N/A}