KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > transform > stream > StreamResult


1 // $Id: StreamResult.java,v 1.3.22.4 2004/07/13 22:27:51 jsuttor Exp $
2
/*
3  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
4  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
5  */

6
7 /*
8  * @(#)StreamResult.java 1.15 04/07/13
9  */

10 package javax.xml.transform.stream;
11
12 import javax.xml.transform.Result JavaDoc;
13
14 import java.io.File JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.io.Writer JavaDoc;
17 import java.net.MalformedURLException JavaDoc;
18
19 /**
20  * <p>Acts as an holder for a transformation result,
21  * which may be XML, plain Text, HTML, or some other form of markup.</p>
22  *
23  * @author <a HREF="Jeff.Suttor@Sun.com">Jeff Suttor</a>
24  */

25 public class StreamResult implements Result JavaDoc {
26
27     /** If {@link javax.xml.transform.TransformerFactory#getFeature}
28      * returns true when passed this value as an argument,
29      * the Transformer supports Result output of this type.
30      */

31     public static final String JavaDoc FEATURE =
32         "http://javax.xml.transform.stream.StreamResult/feature";
33
34     /**
35      * Zero-argument default constructor.
36      */

37     public StreamResult() {
38     }
39
40     /**
41      * Construct a StreamResult from a byte stream. Normally,
42      * a stream should be used rather than a reader, so that
43      * the transformer may use instructions contained in the
44      * transformation instructions to control the encoding.
45      *
46      * @param outputStream A valid OutputStream reference.
47      */

48     public StreamResult(OutputStream JavaDoc outputStream) {
49         setOutputStream(outputStream);
50     }
51
52     /**
53      * Construct a StreamResult from a character stream. Normally,
54      * a stream should be used rather than a reader, so that
55      * the transformer may use instructions contained in the
56      * transformation instructions to control the encoding. However,
57      * there are times when it is useful to write to a character
58      * stream, such as when using a StringWriter.
59      *
60      * @param writer A valid Writer reference.
61      */

62     public StreamResult(Writer JavaDoc writer) {
63         setWriter(writer);
64     }
65
66     /**
67      * Construct a StreamResult from a URL.
68      *
69      * @param systemId Must be a String that conforms to the URI syntax.
70      */

71     public StreamResult(String JavaDoc systemId) {
72         this.systemId = systemId;
73     }
74
75     /**
76      * Construct a StreamResult from a File.
77      *
78      * @param f Must a non-null File reference.
79      */

80     public StreamResult(File JavaDoc f) {
81         setSystemId(f);
82     }
83
84     /**
85      * Set the ByteStream that is to be written to. Normally,
86      * a stream should be used rather than a reader, so that
87      * the transformer may use instructions contained in the
88      * transformation instructions to control the encoding.
89      *
90      * @param outputStream A valid OutputStream reference.
91      */

92     public void setOutputStream(OutputStream JavaDoc outputStream) {
93         this.outputStream = outputStream;
94     }
95
96     /**
97      * Get the byte stream that was set with setOutputStream.
98      *
99      * @return The byte stream that was set with setOutputStream, or null
100      * if setOutputStream or the ByteStream constructor was not called.
101      */

102     public OutputStream JavaDoc getOutputStream() {
103         return outputStream;
104     }
105
106     /**
107      * Set the writer that is to receive the result. Normally,
108      * a stream should be used rather than a writer, so that
109      * the transformer may use instructions contained in the
110      * transformation instructions to control the encoding. However,
111      * there are times when it is useful to write to a writer,
112      * such as when using a StringWriter.
113      *
114      * @param writer A valid Writer reference.
115      */

116     public void setWriter(Writer JavaDoc writer) {
117         this.writer = writer;
118     }
119
120     /**
121      * Get the character stream that was set with setWriter.
122      *
123      * @return The character stream that was set with setWriter, or null
124      * if setWriter or the Writer constructor was not called.
125      */

126     public Writer JavaDoc getWriter() {
127         return writer;
128     }
129
130     /**
131      * Set the systemID that may be used in association
132      * with the byte or character stream, or, if neither is set, use
133      * this value as a writeable URI (probably a file name).
134      *
135      * @param systemId The system identifier as a URI string.
136      */

137     public void setSystemId(String JavaDoc systemId) {
138         this.systemId = systemId;
139     }
140
141     /**
142      * <p>Set the system ID from a <code>File</code> reference.</p>
143      *
144      * <p>Note the use of {@link File#toURI()} and {@link File#toURL()}.
145      * <code>toURI()</code> is prefered and used if possible.
146      * To allow JAXP 1.3 to run on J2SE 1.3, <code>toURL()</code>
147      * is used if a {@link NoSuchMethodException} is thrown by the attempt
148      * to use <code>toURI()</code>.</p>
149      *
150      * @param f Must a non-null File reference.
151      */

152     public void setSystemId(File JavaDoc f) {
153         
154         try {
155             // assume >= 1.4
156
this.systemId = f.toURI().toString();
157         } catch (java.lang.NoSuchMethodError JavaDoc nme) {
158             // running on J2SE 1.3?
159
try {
160                 this.systemId = f.toURL().toString();
161             } catch (MalformedURLException JavaDoc malformedURLException) {
162                 this.systemId = null;
163                 throw new RuntimeException JavaDoc(
164                         "javax.xml.transform.stream.StreamResult#setSystemId(File f) with MalformedURLException: "
165                         + malformedURLException.toString()
166                 );
167             }
168         } catch (Exception JavaDoc exception) {
169             throw new RuntimeException JavaDoc(
170                     "javax.xml.transform.stream.StreamResult#setSystemId(File f):"
171                     + " unexpected Exception: " + exception.toString()
172             );
173             
174         }
175     }
176
177     /**
178      * Get the system identifier that was set with setSystemId.
179      *
180      * @return The system identifier that was set with setSystemId, or null
181      * if setSystemId was not called.
182      */

183     public String JavaDoc getSystemId() {
184         return systemId;
185     }
186
187     //////////////////////////////////////////////////////////////////////
188
// Internal state.
189
//////////////////////////////////////////////////////////////////////
190

191     /**
192      * The systemID that may be used in association
193      * with the byte or character stream, or, if neither is set, use
194      * this value as a writeable URI (probably a file name).
195      */

196     private String JavaDoc systemId;
197
198     /**
199      * The byte stream that is to be written to.
200      */

201     private OutputStream JavaDoc outputStream;
202
203     /**
204      * The character stream that is to be written to.
205      */

206     private Writer JavaDoc writer;
207 }
208
Popular Tags