Home | History | Annotate | Download | only in stream
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 // $Id: StreamResult.java 829970 2009-10-26 21:15:29Z mrglavas $
     19 
     20 package javax.xml.transform.stream;
     21 
     22 import java.io.File;
     23 import java.io.OutputStream;
     24 import java.io.Writer;
     25 import javax.xml.transform.Result;
     26 
     27 /**
     28  * <p>Acts as an holder for a transformation result,
     29  * which may be XML, plain Text, HTML, or some other form of markup.</p>
     30  *
     31  * @author <a href="Jeff.Suttor (at) Sun.com">Jeff Suttor</a>
     32  */
     33 public class StreamResult implements Result {
     34 
     35     /** If {@link javax.xml.transform.TransformerFactory#getFeature}
     36      * returns true when passed this value as an argument,
     37      * the Transformer supports Result output of this type.
     38      */
     39     public static final String FEATURE =
     40         "http://javax.xml.transform.stream.StreamResult/feature";
     41 
     42     /**
     43      * Zero-argument default constructor.
     44      */
     45     public StreamResult() {
     46     }
     47 
     48     /**
     49      * Construct a StreamResult from a byte stream.  Normally,
     50      * a stream should be used rather than a reader, so that
     51      * the transformer may use instructions contained in the
     52      * transformation instructions to control the encoding.
     53      *
     54      * @param outputStream A valid OutputStream reference.
     55      */
     56     public StreamResult(OutputStream outputStream) {
     57         setOutputStream(outputStream);
     58     }
     59 
     60     /**
     61      * Construct a StreamResult from a character stream.  Normally,
     62      * a stream should be used rather than a reader, so that
     63      * the transformer may use instructions contained in the
     64      * transformation instructions to control the encoding.  However,
     65      * there are times when it is useful to write to a character
     66      * stream, such as when using a StringWriter.
     67      *
     68      * @param writer  A valid Writer reference.
     69      */
     70     public StreamResult(Writer writer) {
     71         setWriter(writer);
     72     }
     73 
     74     /**
     75      * Construct a StreamResult from a URL.
     76      *
     77      * @param systemId Must be a String that conforms to the URI syntax.
     78      */
     79     public StreamResult(String systemId) {
     80         this.systemId = systemId;
     81     }
     82 
     83     /**
     84      * Construct a StreamResult from a File.
     85      *
     86      * @param f Must a non-null File reference.
     87      */
     88     public StreamResult(File f) {
     89         setSystemId(f);
     90     }
     91 
     92     /**
     93      * Set the ByteStream that is to be written to.  Normally,
     94      * a stream should be used rather than a reader, so that
     95      * the transformer may use instructions contained in the
     96      * transformation instructions to control the encoding.
     97      *
     98      * @param outputStream A valid OutputStream reference.
     99      */
    100     public void setOutputStream(OutputStream outputStream) {
    101         this.outputStream = outputStream;
    102     }
    103 
    104     /**
    105      * Get the byte stream that was set with setOutputStream.
    106      *
    107      * @return The byte stream that was set with setOutputStream, or null
    108      * if setOutputStream or the ByteStream constructor was not called.
    109      */
    110     public OutputStream getOutputStream() {
    111         return outputStream;
    112     }
    113 
    114     /**
    115      * Set the writer that is to receive the result.  Normally,
    116      * a stream should be used rather than a writer, so that
    117      * the transformer may use instructions contained in the
    118      * transformation instructions to control the encoding.  However,
    119      * there are times when it is useful to write to a writer,
    120      * such as when using a StringWriter.
    121      *
    122      * @param writer  A valid Writer reference.
    123      */
    124     public void setWriter(Writer writer) {
    125         this.writer = writer;
    126     }
    127 
    128     /**
    129      * Get the character stream that was set with setWriter.
    130      *
    131      * @return The character stream that was set with setWriter, or null
    132      * if setWriter or the Writer constructor was not called.
    133      */
    134     public Writer getWriter() {
    135         return writer;
    136     }
    137 
    138     /**
    139      * Set the systemID that may be used in association
    140      * with the byte or character stream, or, if neither is set, use
    141      * this value as a writeable URI (probably a file name).
    142      *
    143      * @param systemId The system identifier as a URI string.
    144      */
    145     public void setSystemId(String systemId) {
    146         this.systemId = systemId;
    147     }
    148 
    149     /**
    150      * <p>Set the system ID from a <code>File</code> reference.</p>
    151      *
    152      * <p>Note the use of {@link File#toURI()} and {@link File#toURL()}.
    153      * <code>toURI()</code> is preferred and used if possible.
    154      * To allow JAXP 1.3 to run on J2SE 1.3, <code>toURL()</code>
    155      * is used if a {@link NoSuchMethodException} is thrown by the attempt
    156      * to use <code>toURI()</code>.</p>
    157      *
    158      * @param f Must a non-null File reference.
    159      */
    160     public void setSystemId(File f) {
    161         this.systemId = FilePathToURI.filepath2URI(f.getAbsolutePath());
    162     }
    163 
    164     /**
    165      * Get the system identifier that was set with setSystemId.
    166      *
    167      * @return The system identifier that was set with setSystemId, or null
    168      * if setSystemId was not called.
    169      */
    170     public String getSystemId() {
    171         return systemId;
    172     }
    173 
    174     //////////////////////////////////////////////////////////////////////
    175     // Internal state.
    176     //////////////////////////////////////////////////////////////////////
    177 
    178     /**
    179      * The systemID that may be used in association
    180      * with the byte or character stream, or, if neither is set, use
    181      * this value as a writeable URI (probably a file name).
    182      */
    183     private String systemId;
    184 
    185     /**
    186      * The byte stream that is to be written to.
    187      */
    188     private OutputStream outputStream;
    189 
    190     /**
    191      * The character stream that is to be written to.
    192      */
    193     private Writer writer;
    194 }
    195