001 /*
002 * Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.xml.transform.stream;
027
028 import javax.xml.transform.Result;
029
030 import java.io.File;
031 import java.io.OutputStream;
032 import java.io.Writer;
033 import java.net.MalformedURLException;
034
035 /**
036 * <p>Acts as an holder for a transformation result,
037 * which may be XML, plain Text, HTML, or some other form of markup.</p>
038 *
039 * @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
040 */
041 public class StreamResult implements Result {
042
043 /** If {@link javax.xml.transform.TransformerFactory#getFeature}
044 * returns true when passed this value as an argument,
045 * the Transformer supports Result output of this type.
046 */
047 public static final String FEATURE = "http://javax.xml.transform.stream.StreamResult/feature";
048
049 /**
050 * Zero-argument default constructor.
051 */
052 public StreamResult() {
053 }
054
055 /**
056 * Construct a StreamResult from a byte stream. Normally,
057 * a stream should be used rather than a reader, so that
058 * the transformer may use instructions contained in the
059 * transformation instructions to control the encoding.
060 *
061 * @param outputStream A valid OutputStream reference.
062 */
063 public StreamResult(OutputStream outputStream) {
064 setOutputStream(outputStream);
065 }
066
067 /**
068 * Construct a StreamResult from a character stream. Normally,
069 * a stream should be used rather than a reader, so that
070 * the transformer may use instructions contained in the
071 * transformation instructions to control the encoding. However,
072 * there are times when it is useful to write to a character
073 * stream, such as when using a StringWriter.
074 *
075 * @param writer A valid Writer reference.
076 */
077 public StreamResult(Writer writer) {
078 setWriter(writer);
079 }
080
081 /**
082 * Construct a StreamResult from a URL.
083 *
084 * @param systemId Must be a String that conforms to the URI syntax.
085 */
086 public StreamResult(String systemId) {
087 this .systemId = systemId;
088 }
089
090 /**
091 * Construct a StreamResult from a File.
092 *
093 * @param f Must a non-null File reference.
094 */
095 public StreamResult(File f) {
096 //convert file to appropriate URI, f.toURI().toASCIIString()
097 //converts the URI to string as per rule specified in
098 //RFC 2396,
099 setSystemId(f.toURI().toASCIIString());
100 }
101
102 /**
103 * Set the ByteStream that is to be written to. Normally,
104 * a stream should be used rather than a reader, so that
105 * the transformer may use instructions contained in the
106 * transformation instructions to control the encoding.
107 *
108 * @param outputStream A valid OutputStream reference.
109 */
110 public void setOutputStream(OutputStream outputStream) {
111 this .outputStream = outputStream;
112 }
113
114 /**
115 * Get the byte stream that was set with setOutputStream.
116 *
117 * @return The byte stream that was set with setOutputStream, or null
118 * if setOutputStream or the ByteStream constructor was not called.
119 */
120 public OutputStream getOutputStream() {
121 return outputStream;
122 }
123
124 /**
125 * Set the writer that is to receive the result. Normally,
126 * a stream should be used rather than a writer, so that
127 * the transformer may use instructions contained in the
128 * 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 */
134 public void setWriter(Writer writer) {
135 this .writer = writer;
136 }
137
138 /**
139 * Get the character stream that was set with setWriter.
140 *
141 * @return The character stream that was set with setWriter, or null
142 * if setWriter or the Writer constructor was not called.
143 */
144 public Writer getWriter() {
145 return writer;
146 }
147
148 /**
149 * Set the systemID that may be used in association
150 * with the byte or character stream, or, if neither is set, use
151 * this value as a writeable URI (probably a file name).
152 *
153 * @param systemId The system identifier as a URI string.
154 */
155 public void setSystemId(String systemId) {
156 this .systemId = systemId;
157 }
158
159 /**
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 */
165 public void setSystemId(File f) {
166 //convert file to appropriate URI, f.toURI().toASCIIString()
167 //converts the URI to string as per rule specified in
168 //RFC 2396,
169 this .systemId = f.toURI().toASCIIString();
170 }
171
172 /**
173 * Get the system identifier that was set with setSystemId.
174 *
175 * @return The system identifier that was set with setSystemId, or null
176 * if setSystemId was not called.
177 */
178 public String getSystemId() {
179 return systemId;
180 }
181
182 //////////////////////////////////////////////////////////////////////
183 // Internal state.
184 //////////////////////////////////////////////////////////////////////
185
186 /**
187 * The systemID that may be used in association
188 * with the byte or character stream, or, if neither is set, use
189 * this value as a writeable URI (probably a file name).
190 */
191 private String systemId;
192
193 /**
194 * The byte stream that is to be written to.
195 */
196 private OutputStream outputStream;
197
198 /**
199 * The character stream that is to be written to.
200 */
201 private Writer writer;
202 }
|