1/*
2 * Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package javax.xml.transform.stream;
27
28import javax.xml.transform.Result;
29
30import java.io.File;
31import java.io.OutputStream;
32import java.io.Writer;
33import java.net.MalformedURLException;
34
35/**
36 * <p>Acts as an holder for a transformation result,
37 * which may be XML, plain Text, HTML, or some other form of markup.</p>
38 *
39 * @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
40 * @since 1.4
41 */
42public class StreamResult implements Result {
43
44    /** If {@link javax.xml.transform.TransformerFactory#getFeature}
45     * returns true when passed this value as an argument,
46     * the Transformer supports Result output of this type.
47     */
48    public static final String FEATURE =
49        "http://javax.xml.transform.stream.StreamResult/feature";
50
51    /**
52     * Zero-argument default constructor.
53     */
54    public StreamResult() {
55    }
56
57    /**
58     * Construct a StreamResult from a byte stream.  Normally,
59     * a stream should be used rather than a reader, so that
60     * the transformer may use instructions contained in the
61     * transformation instructions to control the encoding.
62     *
63     * @param outputStream A valid OutputStream reference.
64     */
65    public StreamResult(OutputStream outputStream) {
66        setOutputStream(outputStream);
67    }
68
69    /**
70     * Construct a StreamResult from a character stream.  Normally,
71     * a stream should be used rather than a reader, so that
72     * the transformer may use instructions contained in the
73     * transformation instructions to control the encoding.  However,
74     * there are times when it is useful to write to a character
75     * stream, such as when using a StringWriter.
76     *
77     * @param writer  A valid Writer reference.
78     */
79    public StreamResult(Writer writer) {
80        setWriter(writer);
81    }
82
83    /**
84     * Construct a StreamResult from a URL.
85     *
86     * @param systemId Must be a String that conforms to the URI syntax.
87     */
88    public StreamResult(String systemId) {
89        this.systemId = systemId;
90    }
91
92    /**
93     * Construct a StreamResult from a File.
94     *
95     * @param f Must a non-null File reference.
96     */
97    public StreamResult(File f) {
98        //convert file to appropriate URI, f.toURI().toASCIIString()
99        //converts the URI to string as per rule specified in
100        //RFC 2396,
101        setSystemId(f.toURI().toASCIIString());
102    }
103
104    /**
105     * Set the ByteStream that is to be written to.  Normally,
106     * a stream should be used rather than a reader, so that
107     * the transformer may use instructions contained in the
108     * transformation instructions to control the encoding.
109     *
110     * @param outputStream A valid OutputStream reference.
111     */
112    public void setOutputStream(OutputStream outputStream) {
113        this.outputStream = outputStream;
114    }
115
116    /**
117     * Get the byte stream that was set with setOutputStream.
118     *
119     * @return The byte stream that was set with setOutputStream, or null
120     * if setOutputStream or the ByteStream constructor was not called.
121     */
122    public OutputStream getOutputStream() {
123        return outputStream;
124    }
125
126    /**
127     * Set the writer that is to receive the result.  Normally,
128     * a stream should be used rather than a writer, so that
129     * the transformer may use instructions contained in the
130     * transformation instructions to control the encoding.  However,
131     * there are times when it is useful to write to a writer,
132     * such as when using a StringWriter.
133     *
134     * @param writer  A valid Writer reference.
135     */
136    public void setWriter(Writer writer) {
137        this.writer = writer;
138    }
139
140    /**
141     * Get the character stream that was set with setWriter.
142     *
143     * @return The character stream that was set with setWriter, or null
144     * if setWriter or the Writer constructor was not called.
145     */
146    public Writer getWriter() {
147        return writer;
148    }
149
150    /**
151     * Set the systemID that may be used in association
152     * with the byte or character stream, or, if neither is set, use
153     * this value as a writeable URI (probably a file name).
154     *
155     * @param systemId The system identifier as a URI string.
156     */
157    public void setSystemId(String systemId) {
158        this.systemId = systemId;
159    }
160
161    /**
162     * <p>Set the system ID from a <code>File</code> reference.</p>
163     *
164     *
165     * @param f Must a non-null File reference.
166     */
167    public void setSystemId(File f) {
168        //convert file to appropriate URI, f.toURI().toASCIIString()
169        //converts the URI to string as per rule specified in
170        //RFC 2396,
171        this.systemId = f.toURI().toASCIIString();
172    }
173
174    /**
175     * Get the system identifier that was set with setSystemId.
176     *
177     * @return The system identifier that was set with setSystemId, or null
178     * if setSystemId was not called.
179     */
180    public String getSystemId() {
181        return systemId;
182    }
183
184    //////////////////////////////////////////////////////////////////////
185    // Internal state.
186    //////////////////////////////////////////////////////////////////////
187
188    /**
189     * The systemID that may be used in association
190     * with the byte or character stream, or, if neither is set, use
191     * this value as a writeable URI (probably a file name).
192     */
193    private String systemId;
194
195    /**
196     * The byte stream that is to be written to.
197     */
198    private OutputStream outputStream;
199
200    /**
201     * The character stream that is to be written to.
202     */
203    private Writer writer;
204}
205