KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > w3c > css > sac > InputSource


1 /*
2  * Copyright (c) 1999 World Wide Web Consortium
3  * (Massachusetts Institute of Technology, Institut National de Recherche
4  * en Informatique et en Automatique, Keio University).
5  * All Rights Reserved. http://www.w3.org/Consortium/Legal/
6  *
7  * The original version of this interface comes from SAX :
8  * http://www.megginson.com/SAX/
9  *
10  * $Id: InputSource.java,v 1.1.1.1 2003/12/28 21:23:49 davidsch Exp $
11  */

12 package org.w3c.css.sac;
13
14 import java.io.InputStream JavaDoc;
15 import java.io.Reader JavaDoc;
16
17 /**
18  * A single input source for a CSS source.
19  *
20  * <p>This class allows a CSS application to encapsulate information about an
21  * input source in a single object, which may include a URI, a byte stream
22  * (possibly with a specified encoding), and/or a character stream.</p>
23  *
24  * <p>The CSS parser will use the InputSource object to determine how
25  * to read CSS input. If there is a character stream available, the
26  * parser will read that stream directly; if not, the parser will use
27  * a byte stream, if available; if neither a character stream nor a
28  * byte stream is available, the parser will attempt to open a URI
29  * connection to the resource identified by the URI.</p>
30  *
31  * <p>An InputSource object belongs to the application: the CSS parser
32  * shall never modify it in any way (it may modify a copy if
33  * necessary).</p>
34  *
35  * @version $Revision: 1.1.1.1 $
36  * @author Philippe Le Hegaret
37  */

38 public class InputSource {
39     
40     private String JavaDoc uri;
41     private InputStream JavaDoc byteStream;
42     private String JavaDoc encoding;
43     private Reader JavaDoc characterStream;
44     private String JavaDoc title;
45     private String JavaDoc media;
46     
47     /**
48      * Zero-argument default constructor.
49      *
50      * @see #setURI
51      * @see #setByteStream
52      * @see #setCharacterStream
53      * @see #setEncoding
54      */

55     public InputSource() {
56     }
57     
58     /**
59      * Create a new input source with a URI.
60      *
61      * <p>The URI must be full resolved.</p>
62      *
63      * @param uri The URI.
64      * @see #setURI
65      * @see #setByteStream
66      * @see #setEncoding
67      * @see #setCharacterStream
68      */

69     public InputSource(String JavaDoc uri) {
70     setURI(uri);
71     }
72     
73     /**
74      * Create a new input source with a character stream.
75      *
76      * <p>Application writers may use setURI() to provide a base
77      * for resolving relative URIs, and setPublicId to include a
78      * public identifier.</p>
79      *
80      * <p>The character stream shall not include a byte order mark.</p>
81      *
82      * @see #setURI
83      * @see #setByteStream
84      * @see #setCharacterStream
85      */

86     public InputSource(Reader JavaDoc characterStream) {
87     setCharacterStream(characterStream);
88     }
89     
90     /**
91      * Set the URI for this input source.
92      *
93      * <p>The URI is optional if there is a byte stream or a character stream,
94      * but it is still useful to provide one, since the application can use it
95      * to resolve relative URIs and can include it in error messages and
96      * warnings (the parser will attempt to open a connection to the URI only
97      * if there is no byte stream or character stream specified).</p>
98      *
99      * <p>If the application knows the character encoding of the
100      * object pointed to by the URI, it can register
101      * the encoding using the setEncoding method.</p>
102      *
103      * <p>The URI must be fully resolved.</p>
104      *
105      * @param uri The URI as a string.
106      * @see #setEncoding
107      * @see #getURI
108      * @see Locator#getURI
109      * @see CSSParseException#getURI
110      */

111     public void setURI(String JavaDoc uri) {
112     this.uri = uri;
113     }
114     
115     /**
116      * Get the URI for this input source.
117      *
118      * <p>The getEncoding method will return the character encoding
119      * of the object pointed to, or null if unknown.</p>
120      *
121      * <p>The URI will be fully resolved.</p>
122      *
123      * @return The URI.
124      * @see #setURI
125      * @see #getEncoding
126      */

127     public String JavaDoc getURI() {
128     return uri;
129     }
130     
131     /**
132      * Set the byte stream for this input source.
133      *
134      * <p>The SAX parser will ignore this if there is also a character
135      * stream specified, but it will use a byte stream in preference
136      * to opening a URI connection itself.</p>
137      *
138      * <p>If the application knows the character encoding of the
139      * byte stream, it should set it with the setEncoding method.</p>
140      *
141      * @param byteStream A byte stream containing an CSS document or
142      * other entity.
143      * @see #setEncoding
144      * @see #getByteStream
145      * @see #getEncoding
146      */

147     public void setByteStream(InputStream JavaDoc byteStream) {
148     this.byteStream = byteStream;
149     }
150     
151     /**
152      * Get the byte stream for this input source.
153      *
154      * <p>The getEncoding method will return the character
155      * encoding for this byte stream, or null if unknown.</p>
156      *
157      * @return The byte stream, or null if none was supplied.
158      * @see #getEncoding
159      * @see #setByteStream
160      */

161     public InputStream JavaDoc getByteStream() {
162     return byteStream;
163     }
164     
165     /**
166      * Set the character encoding, if known.
167      *
168      * <p>The encoding must be a string acceptable for an
169      * CHARSET encoding declaration (see section 4.4 of the CSS
170      * recommendation Level 2).</p>
171      *
172      * <p>This method has no effect when the application provides a
173      * character stream.</p>
174      *
175      * @param encoding A string describing the character encoding.
176      * @see #setURI
177      * @see #setByteStream
178      * @see #getEncoding
179      */

180     public void setEncoding(String JavaDoc encoding) {
181     this.encoding = encoding;
182     }
183     
184     /**
185      * Get the character encoding for a byte stream or URI.
186      *
187      * @return The encoding, or null if none was supplied.
188      * @see #setByteStream
189      * @see #getURI
190      * @see #getByteStream
191      */

192     public String JavaDoc getEncoding() {
193     return encoding;
194     }
195     
196     /**
197      * Set the character stream for this input source.
198      *
199      * <p>If there is a character stream specified, the SAX parser
200      * will ignore any byte stream and will not attempt to open
201      * a URI connection to the URI.</p>
202      *
203      * @param characterStream The character stream containing the
204      * CSS document or other entity.
205      * @see #getCharacterStream
206      */

207     public void setCharacterStream(Reader JavaDoc characterStream) {
208     this.characterStream = characterStream;
209     }
210     
211     /**
212      * Get the character stream for this input source.
213      *
214      * @return The character stream, or null if none was supplied.
215      * @see #setCharacterStream
216      */

217     public Reader JavaDoc getCharacterStream() {
218     return characterStream;
219     }
220
221     /**
222      * Set the title for this input source.
223      * @param title The advisory title. See the title attribute definition
224      * for the <a HREF="http://www.w3.org/TR/REC-html40/struct/links.html#edef-LINK">LINK</A>
225      * element in HTML 4.0, and the title pseudo-attribute for the XML
226      * style sheet processing instruction.
227      */

228     public void setTitle(String JavaDoc title) {
229     this.title = title;
230     }
231
232     /**
233      * Returns the title for this input source.
234      */

235     public String JavaDoc getTitle() {
236     return title;
237     }
238
239     /**
240      * Set the media for this input source.
241      * @param media A comma separated list with all media.
242      */

243     public void setMedia(String JavaDoc media) {
244     this.media = media;
245     }
246
247     /**
248      * Returns the media associated to the input source or <code>null</code>
249      * if media are currently unknown.
250      * @return the media associated to this input source.
251      */

252     public String JavaDoc getMedia() {
253     if (media == null) {
254         return "all";
255     }
256     return media;
257     }
258 }
259
Popular Tags