KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: StreamSource.java,v 1.6.12.3 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  * @(#)StreamSource.java 1.16 04/07/13
9  */

10 package javax.xml.transform.stream;
11
12 import java.io.File JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.io.Reader JavaDoc;
15
16 import javax.xml.transform.Source JavaDoc;
17
18 /**
19  * <p>Acts as an holder for a transformation Source in the form
20  * of a stream of XML markup.</p>
21  *
22  * <p><em>Note:</em> Due to their internal use of either a {@link Reader} or {@link InputStream} instance,
23  * <code>StreamSource</code> instances may only be used once.</p>
24  *
25  * @author <a HREF="Jeff.Suttor@Sun.com">Jeff Suttor</a>
26  * @version $Revision: 1.6.12.3 $, $Date: 2004/07/13 22:27:51 $
27  */

28 public class StreamSource implements Source JavaDoc {
29
30     /** If {@link javax.xml.transform.TransformerFactory#getFeature}
31      * returns true when passed this value as an argument,
32      * the Transformer supports Source input of this type.
33      */

34     public static final String JavaDoc FEATURE =
35         "http://javax.xml.transform.stream.StreamSource/feature";
36     
37     /**
38      * <p>Zero-argument default constructor. If this constructor is used, and
39      * no Stream source is set using
40      * {@link #setInputStream(java.io.InputStream inputStream)} or
41      * {@link #setReader(java.io.Reader reader)}, then the
42      * <code>Transformer</code> will
43      * create an empty source {@link java.io.InputStream} using
44      * {@link java.io.InputStream#InputStream() new InputStream()}.</p>
45      *
46      * @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)
47      */

48     public StreamSource() { }
49
50     /**
51      * Construct a StreamSource from a byte stream. Normally,
52      * a stream should be used rather than a reader, so
53      * the XML parser can resolve character encoding specified
54      * by the XML declaration.
55      *
56      * <p>If this constructor is used to process a stylesheet, normally
57      * setSystemId should also be called, so that relative URI references
58      * can be resolved.</p>
59      *
60      * @param inputStream A valid InputStream reference to an XML stream.
61      */

62     public StreamSource(InputStream JavaDoc inputStream) {
63         setInputStream(inputStream);
64     }
65
66     /**
67      * Construct a StreamSource from a byte stream. Normally,
68      * a stream should be used rather than a reader, so that
69      * the XML parser can resolve character encoding specified
70      * by the XML declaration.
71      *
72      * <p>This constructor allows the systemID to be set in addition
73      * to the input stream, which allows relative URIs
74      * to be processed.</p>
75      *
76      * @param inputStream A valid InputStream reference to an XML stream.
77      * @param systemId Must be a String that conforms to the URI syntax.
78      */

79     public StreamSource(InputStream JavaDoc inputStream, String JavaDoc systemId) {
80         setInputStream(inputStream);
81         setSystemId(systemId);
82     }
83
84     /**
85      * Construct a StreamSource from a character reader. Normally,
86      * a stream should be used rather than a reader, so that
87      * the XML parser can resolve character encoding specified
88      * by the XML declaration. However, in many cases the encoding
89      * of the input stream is already resolved, as in the case of
90      * reading XML from a StringReader.
91      *
92      * @param reader A valid Reader reference to an XML character stream.
93      */

94     public StreamSource(Reader JavaDoc reader) {
95         setReader(reader);
96     }
97
98     /**
99      * Construct a StreamSource from a character reader. Normally,
100      * a stream should be used rather than a reader, so that
101      * the XML parser may resolve character encoding specified
102      * by the XML declaration. However, in many cases the encoding
103      * of the input stream is already resolved, as in the case of
104      * reading XML from a StringReader.
105      *
106      * @param reader A valid Reader reference to an XML character stream.
107      * @param systemId Must be a String that conforms to the URI syntax.
108      */

109     public StreamSource(Reader JavaDoc reader, String JavaDoc systemId) {
110         setReader(reader);
111         setSystemId(systemId);
112     }
113
114     /**
115      * Construct a StreamSource from a URL.
116      *
117      * @param systemId Must be a String that conforms to the URI syntax.
118      */

119     public StreamSource(String JavaDoc systemId) {
120         this.systemId = systemId;
121     }
122
123     /**
124      * Construct a StreamSource from a File.
125      *
126      * @param f Must a non-null File reference.
127      */

128     public StreamSource(File JavaDoc f) {
129         setSystemId(f);
130     }
131
132     /**
133      * Set the byte stream to be used as input. Normally,
134      * a stream should be used rather than a reader, so that
135      * the XML parser can resolve character encoding specified
136      * by the XML declaration.
137      *
138      * <p>If this Source object is used to process a stylesheet, normally
139      * setSystemId should also be called, so that relative URL references
140      * can be resolved.</p>
141      *
142      * @param inputStream A valid InputStream reference to an XML stream.
143      */

144     public void setInputStream(InputStream JavaDoc inputStream) {
145         this.inputStream = inputStream;
146     }
147
148     /**
149      * Get the byte stream that was set with setByteStream.
150      *
151      * @return The byte stream that was set with setByteStream, or null
152      * if setByteStream or the ByteStream constructor was not called.
153      */

154     public InputStream JavaDoc getInputStream() {
155         return inputStream;
156     }
157
158     /**
159      * Set the input to be a character reader. Normally,
160      * a stream should be used rather than a reader, so that
161      * the XML parser can resolve character encoding specified
162      * by the XML declaration. However, in many cases the encoding
163      * of the input stream is already resolved, as in the case of
164      * reading XML from a StringReader.
165      *
166      * @param reader A valid Reader reference to an XML CharacterStream.
167      */

168     public void setReader(Reader JavaDoc reader) {
169         this.reader = reader;
170     }
171
172     /**
173      * Get the character stream that was set with setReader.
174      *
175      * @return The character stream that was set with setReader, or null
176      * if setReader or the Reader constructor was not called.
177      */

178     public Reader JavaDoc getReader() {
179         return reader;
180     }
181
182     /**
183      * Set the public identifier for this Source.
184      *
185      * <p>The public identifier is always optional: if the application
186      * writer includes one, it will be provided as part of the
187      * location information.</p>
188      *
189      * @param publicId The public identifier as a string.
190      */

191     public void setPublicId(String JavaDoc publicId) {
192         this.publicId = publicId;
193     }
194
195     /**
196      * Get the public identifier that was set with setPublicId.
197      *
198      * @return The public identifier that was set with setPublicId, or null
199      * if setPublicId was not called.
200      */

201     public String JavaDoc getPublicId() {
202         return publicId;
203     }
204
205     /**
206      * Set the system identifier for this Source.
207      *
208      * <p>The system identifier is optional if there is a byte stream
209      * or a character stream, but it is still useful to provide one,
210      * since the application can use it to resolve relative URIs
211      * and can include it in error messages and warnings (the parser
212      * will attempt to open a connection to the URI only if
213      * there is no byte stream or character stream specified).</p>
214      *
215      * @param systemId The system identifier as a URL string.
216      */

217     public void setSystemId(String JavaDoc systemId) {
218         this.systemId = systemId;
219     }
220
221     /**
222      * Get the system identifier that was set with setSystemId.
223      *
224      * @return The system identifier that was set with setSystemId, or null
225      * if setSystemId was not called.
226      */

227     public String JavaDoc getSystemId() {
228         return systemId;
229     }
230
231     /**
232      * Set the system ID from a File reference.
233      *
234      * @param f Must a non-null File reference.
235      */

236     public void setSystemId(File JavaDoc f) {
237         String JavaDoc fpath=f.getAbsolutePath();
238         if (File.separatorChar != '/') {
239             fpath = fpath.replace(File.separatorChar, '/');
240         }
241         if( fpath.startsWith("/"))
242           this.systemId= "file://" + fpath;
243         else
244           this.systemId = "file:///" + fpath;
245     }
246
247     //////////////////////////////////////////////////////////////////////
248
// Internal state.
249
//////////////////////////////////////////////////////////////////////
250

251     /**
252      * The public identifier for this input source, or null.
253      */

254     private String JavaDoc publicId;
255
256     /**
257      * The system identifier as a URL string, or null.
258      */

259     private String JavaDoc systemId;
260
261     /**
262      * The byte stream for this Source, or null.
263      */

264     private InputStream JavaDoc inputStream;
265
266     /**
267      * The character stream for this Source, or null.
268      */

269     private Reader JavaDoc reader;
270 }
271
Popular Tags