Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/transform/stream/StreamSource.java
32288 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*/2425package javax.xml.transform.stream;2627import java.io.File;28import java.io.InputStream;29import java.io.Reader;3031import javax.xml.transform.Source;3233/**34* <p>Acts as an holder for a transformation Source in the form35* of a stream of XML markup.</p>36*37* <p><em>Note:</em> Due to their internal use of either a {@link Reader} or {@link InputStream} instance,38* <code>StreamSource</code> instances may only be used once.</p>39*40* @author <a href="[email protected]">Jeff Suttor</a>41*/42public class StreamSource implements Source {4344/** If {@link javax.xml.transform.TransformerFactory#getFeature}45* returns true when passed this value as an argument,46* the Transformer supports Source input of this type.47*/48public static final String FEATURE =49"http://javax.xml.transform.stream.StreamSource/feature";5051/**52* <p>Zero-argument default constructor. If this constructor is used, and53* no Stream source is set using54* {@link #setInputStream(java.io.InputStream inputStream)} or55* {@link #setReader(java.io.Reader reader)}, then the56* <code>Transformer</code> will57* create an empty source {@link java.io.InputStream} using58* {@link java.io.InputStream#InputStream() new InputStream()}.</p>59*60* @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)61*/62public StreamSource() { }6364/**65* Construct a StreamSource from a byte stream. Normally,66* a stream should be used rather than a reader, so67* the XML parser can resolve character encoding specified68* by the XML declaration.69*70* <p>If this constructor is used to process a stylesheet, normally71* setSystemId should also be called, so that relative URI references72* can be resolved.</p>73*74* @param inputStream A valid InputStream reference to an XML stream.75*/76public StreamSource(InputStream inputStream) {77setInputStream(inputStream);78}7980/**81* Construct a StreamSource from a byte stream. Normally,82* a stream should be used rather than a reader, so that83* the XML parser can resolve character encoding specified84* by the XML declaration.85*86* <p>This constructor allows the systemID to be set in addition87* to the input stream, which allows relative URIs88* to be processed.</p>89*90* @param inputStream A valid InputStream reference to an XML stream.91* @param systemId Must be a String that conforms to the URI syntax.92*/93public StreamSource(InputStream inputStream, String systemId) {94setInputStream(inputStream);95setSystemId(systemId);96}9798/**99* Construct a StreamSource from a character reader. Normally,100* a stream should be used rather than a reader, so that101* the XML parser can resolve character encoding specified102* by the XML declaration. However, in many cases the encoding103* of the input stream is already resolved, as in the case of104* reading XML from a StringReader.105*106* @param reader A valid Reader reference to an XML character stream.107*/108public StreamSource(Reader reader) {109setReader(reader);110}111112/**113* Construct a StreamSource from a character reader. Normally,114* a stream should be used rather than a reader, so that115* the XML parser may resolve character encoding specified116* by the XML declaration. However, in many cases the encoding117* of the input stream is already resolved, as in the case of118* reading XML from a StringReader.119*120* @param reader A valid Reader reference to an XML character stream.121* @param systemId Must be a String that conforms to the URI syntax.122*/123public StreamSource(Reader reader, String systemId) {124setReader(reader);125setSystemId(systemId);126}127128/**129* Construct a StreamSource from a URL.130*131* @param systemId Must be a String that conforms to the URI syntax.132*/133public StreamSource(String systemId) {134this.systemId = systemId;135}136137/**138* Construct a StreamSource from a File.139*140* @param f Must a non-null File reference.141*/142public StreamSource(File f) {143//convert file to appropriate URI, f.toURI().toASCIIString()144//converts the URI to string as per rule specified in145//RFC 2396,146setSystemId(f.toURI().toASCIIString());147}148149/**150* Set the byte stream to be used as input. Normally,151* a stream should be used rather than a reader, so that152* the XML parser can resolve character encoding specified153* by the XML declaration.154*155* <p>If this Source object is used to process a stylesheet, normally156* setSystemId should also be called, so that relative URL references157* can be resolved.</p>158*159* @param inputStream A valid InputStream reference to an XML stream.160*/161public void setInputStream(InputStream inputStream) {162this.inputStream = inputStream;163}164165/**166* Get the byte stream that was set with setByteStream.167*168* @return The byte stream that was set with setByteStream, or null169* if setByteStream or the ByteStream constructor was not called.170*/171public InputStream getInputStream() {172return inputStream;173}174175/**176* Set the input to be a character reader. Normally,177* a stream should be used rather than a reader, so that178* the XML parser can resolve character encoding specified179* by the XML declaration. However, in many cases the encoding180* of the input stream is already resolved, as in the case of181* reading XML from a StringReader.182*183* @param reader A valid Reader reference to an XML CharacterStream.184*/185public void setReader(Reader reader) {186this.reader = reader;187}188189/**190* Get the character stream that was set with setReader.191*192* @return The character stream that was set with setReader, or null193* if setReader or the Reader constructor was not called.194*/195public Reader getReader() {196return reader;197}198199/**200* Set the public identifier for this Source.201*202* <p>The public identifier is always optional: if the application203* writer includes one, it will be provided as part of the204* location information.</p>205*206* @param publicId The public identifier as a string.207*/208public void setPublicId(String publicId) {209this.publicId = publicId;210}211212/**213* Get the public identifier that was set with setPublicId.214*215* @return The public identifier that was set with setPublicId, or null216* if setPublicId was not called.217*/218public String getPublicId() {219return publicId;220}221222/**223* Set the system identifier for this Source.224*225* <p>The system identifier is optional if there is a byte stream226* or a character stream, but it is still useful to provide one,227* since the application can use it to resolve relative URIs228* and can include it in error messages and warnings (the parser229* will attempt to open a connection to the URI only if230* there is no byte stream or character stream specified).</p>231*232* @param systemId The system identifier as a URL string.233*/234public void setSystemId(String systemId) {235this.systemId = systemId;236}237238/**239* Get the system identifier that was set with setSystemId.240*241* @return The system identifier that was set with setSystemId, or null242* if setSystemId was not called.243*/244public String getSystemId() {245return systemId;246}247248/**249* Set the system ID from a File reference.250*251* @param f Must a non-null File reference.252*/253public void setSystemId(File f) {254//convert file to appropriate URI, f.toURI().toASCIIString()255//converts the URI to string as per rule specified in256//RFC 2396,257this.systemId = f.toURI().toASCIIString();258}259260//////////////////////////////////////////////////////////////////////261// Internal state.262//////////////////////////////////////////////////////////////////////263264/**265* The public identifier for this input source, or null.266*/267private String publicId;268269/**270* The system identifier as a URL string, or null.271*/272private String systemId;273274/**275* The byte stream for this Source, or null.276*/277private InputStream inputStream;278279/**280* The character stream for this Source, or null.281*/282private Reader reader;283}284285286