001: /*
002: * Copyright (c) 1999 World Wide Web Consortium
003: * (Massachusetts Institute of Technology, Institut National de Recherche
004: * en Informatique et en Automatique, Keio University).
005: * All Rights Reserved. http://www.w3.org/Consortium/Legal/
006: *
007: * The original version of this interface comes from SAX :
008: * http://www.megginson.com/SAX/
009: *
010: * $Id$
011: */
012: package org.w3c.css.sac;
013:
014: import java.io.InputStream;
015: import java.io.Reader;
016:
017: /**
018: * A single input source for a CSS source.
019: *
020: * <p>This class allows a CSS application to encapsulate information about an
021: * input source in a single object, which may include a URI, a byte stream
022: * (possibly with a specified encoding), and/or a character stream.</p>
023: *
024: * <p>The CSS parser will use the InputSource object to determine how
025: * to read CSS input. If there is a character stream available, the
026: * parser will read that stream directly; if not, the parser will use
027: * a byte stream, if available; if neither a character stream nor a
028: * byte stream is available, the parser will attempt to open a URI
029: * connection to the resource identified by the URI.</p>
030: *
031: * <p>An InputSource object belongs to the application: the CSS parser
032: * shall never modify it in any way (it may modify a copy if
033: * necessary).</p>
034: *
035: * @version $Revision$
036: * @author Philippe Le Hegaret
037: */
038: public class InputSource {
039:
040: private String uri;
041: private InputStream byteStream;
042: private String encoding;
043: private Reader characterStream;
044: private String title;
045: private String media;
046:
047: /**
048: * Zero-argument default constructor.
049: *
050: * @see #setURI
051: * @see #setByteStream
052: * @see #setCharacterStream
053: * @see #setEncoding
054: */
055: public InputSource() {
056: }
057:
058: /**
059: * Create a new input source with a URI.
060: *
061: * <p>The URI must be full resolved.</p>
062: *
063: * @param uri The URI.
064: * @see #setURI
065: * @see #setByteStream
066: * @see #setEncoding
067: * @see #setCharacterStream
068: */
069: public InputSource(String uri) {
070: setURI(uri);
071: }
072:
073: /**
074: * Create a new input source with a character stream.
075: *
076: * <p>Application writers may use setURI() to provide a base
077: * for resolving relative URIs, and setPublicId to include a
078: * public identifier.</p>
079: *
080: * <p>The character stream shall not include a byte order mark.</p>
081: *
082: * @see #setURI
083: * @see #setByteStream
084: * @see #setCharacterStream
085: */
086: public InputSource(Reader characterStream) {
087: setCharacterStream(characterStream);
088: }
089:
090: /**
091: * Set the URI for this input source.
092: *
093: * <p>The URI is optional if there is a byte stream or a character stream,
094: * but it is still useful to provide one, since the application can use it
095: * to resolve relative URIs and can include it in error messages and
096: * warnings (the parser will attempt to open a connection to the URI only
097: * if there is no byte stream or character stream specified).</p>
098: *
099: * <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 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 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 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 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 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 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 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 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 title) {
229: this .title = title;
230: }
231:
232: /**
233: * Returns the title for this input source.
234: */
235: public String 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 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 getMedia() {
253: if (media == null) {
254: return "all";
255: }
256: return media;
257: }
258: }
|