Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/transform/stream/StreamResult.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 javax.xml.transform.Result;2829import java.io.File;30import java.io.OutputStream;31import java.io.Writer;32import java.net.MalformedURLException;3334/**35* <p>Acts as an holder for a transformation result,36* which may be XML, plain Text, HTML, or some other form of markup.</p>37*38* @author <a href="[email protected]">Jeff Suttor</a>39*/40public class StreamResult implements Result {4142/** If {@link javax.xml.transform.TransformerFactory#getFeature}43* returns true when passed this value as an argument,44* the Transformer supports Result output of this type.45*/46public static final String FEATURE =47"http://javax.xml.transform.stream.StreamResult/feature";4849/**50* Zero-argument default constructor.51*/52public StreamResult() {53}5455/**56* Construct a StreamResult from a byte stream. Normally,57* a stream should be used rather than a reader, so that58* the transformer may use instructions contained in the59* transformation instructions to control the encoding.60*61* @param outputStream A valid OutputStream reference.62*/63public StreamResult(OutputStream outputStream) {64setOutputStream(outputStream);65}6667/**68* Construct a StreamResult from a character stream. Normally,69* a stream should be used rather than a reader, so that70* the transformer may use instructions contained in the71* transformation instructions to control the encoding. However,72* there are times when it is useful to write to a character73* stream, such as when using a StringWriter.74*75* @param writer A valid Writer reference.76*/77public StreamResult(Writer writer) {78setWriter(writer);79}8081/**82* Construct a StreamResult from a URL.83*84* @param systemId Must be a String that conforms to the URI syntax.85*/86public StreamResult(String systemId) {87this.systemId = systemId;88}8990/**91* Construct a StreamResult from a File.92*93* @param f Must a non-null File reference.94*/95public StreamResult(File f) {96//convert file to appropriate URI, f.toURI().toASCIIString()97//converts the URI to string as per rule specified in98//RFC 2396,99setSystemId(f.toURI().toASCIIString());100}101102/**103* Set the ByteStream that is to be written to. Normally,104* a stream should be used rather than a reader, so that105* the transformer may use instructions contained in the106* transformation instructions to control the encoding.107*108* @param outputStream A valid OutputStream reference.109*/110public void setOutputStream(OutputStream outputStream) {111this.outputStream = outputStream;112}113114/**115* Get the byte stream that was set with setOutputStream.116*117* @return The byte stream that was set with setOutputStream, or null118* if setOutputStream or the ByteStream constructor was not called.119*/120public OutputStream getOutputStream() {121return outputStream;122}123124/**125* Set the writer that is to receive the result. Normally,126* a stream should be used rather than a writer, so that127* the transformer may use instructions contained in the128* transformation instructions to control the encoding. However,129* there are times when it is useful to write to a writer,130* such as when using a StringWriter.131*132* @param writer A valid Writer reference.133*/134public void setWriter(Writer writer) {135this.writer = writer;136}137138/**139* Get the character stream that was set with setWriter.140*141* @return The character stream that was set with setWriter, or null142* if setWriter or the Writer constructor was not called.143*/144public Writer getWriter() {145return writer;146}147148/**149* Set the systemID that may be used in association150* with the byte or character stream, or, if neither is set, use151* this value as a writeable URI (probably a file name).152*153* @param systemId The system identifier as a URI string.154*/155public void setSystemId(String systemId) {156this.systemId = systemId;157}158159/**160* <p>Set the system ID from a <code>File</code> reference.</p>161*162*163* @param f Must a non-null File reference.164*/165public void setSystemId(File f) {166//convert file to appropriate URI, f.toURI().toASCIIString()167//converts the URI to string as per rule specified in168//RFC 2396,169this.systemId = f.toURI().toASCIIString();170}171172/**173* Get the system identifier that was set with setSystemId.174*175* @return The system identifier that was set with setSystemId, or null176* if setSystemId was not called.177*/178public String getSystemId() {179return systemId;180}181182//////////////////////////////////////////////////////////////////////183// Internal state.184//////////////////////////////////////////////////////////////////////185186/**187* The systemID that may be used in association188* with the byte or character stream, or, if neither is set, use189* this value as a writeable URI (probably a file name).190*/191private String systemId;192193/**194* The byte stream that is to be written to.195*/196private OutputStream outputStream;197198/**199* The character stream that is to be written to.200*/201private Writer writer;202}203204205