1. // SAX input source.
  2. // No warranty; no copyright -- use this as you will.
  3. // $Id: InputSource.java,v 1.1 2001/05/20 03:12:56 curcuru Exp $
  4. package org.xml.sax;
  5. import java.io.Reader;
  6. import java.io.InputStream;
  7. /**
  8. * A single input source for an XML entity.
  9. *
  10. * <blockquote>
  11. * <em>This module, both source code and documentation, is in the
  12. * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
  13. * </blockquote>
  14. *
  15. * <p>This class allows a SAX application to encapsulate information
  16. * about an input source in a single object, which may include
  17. * a public identifier, a system identifier, a byte stream (possibly
  18. * with a specified encoding), and/or a character stream.</p>
  19. *
  20. * <p>There are two places that the application will deliver this
  21. * input source to the parser: as the argument to the Parser.parse
  22. * method, or as the return value of the EntityResolver.resolveEntity
  23. * method.</p>
  24. *
  25. * <p>The SAX parser will use the InputSource object to determine how
  26. * to read XML input. If there is a character stream available, the
  27. * parser will read that stream directly; if not, the parser will use
  28. * a byte stream, if available; if neither a character stream nor a
  29. * byte stream is available, the parser will attempt to open a URI
  30. * connection to the resource identified by the system
  31. * identifier.</p>
  32. *
  33. * <p>An InputSource object belongs to the application: the SAX parser
  34. * shall never modify it in any way (it may modify a copy if
  35. * necessary).</p>
  36. *
  37. * @since SAX 1.0
  38. * @author David Megginson,
  39. * <a href="mailto:sax@megginson.com">sax@megginson.com</a>
  40. * @version 2.0r2pre
  41. * @see org.xml.sax.Parser#parse
  42. * @see org.xml.sax.EntityResolver#resolveEntity
  43. * @see java.io.InputStream
  44. * @see java.io.Reader
  45. */
  46. public class InputSource {
  47. /**
  48. * Zero-argument default constructor.
  49. *
  50. * @see #setPublicId
  51. * @see #setSystemId
  52. * @see #setByteStream
  53. * @see #setCharacterStream
  54. * @see #setEncoding
  55. */
  56. public InputSource ()
  57. {
  58. }
  59. /**
  60. * Create a new input source with a system identifier.
  61. *
  62. * <p>Applications may use setPublicId to include a
  63. * public identifier as well, or setEncoding to specify
  64. * the character encoding, if known.</p>
  65. *
  66. * <p>If the system identifier is a URL, it must be full resolved.</p>
  67. *
  68. * @param systemId The system identifier (URI).
  69. * @see #setPublicId
  70. * @see #setSystemId
  71. * @see #setByteStream
  72. * @see #setEncoding
  73. * @see #setCharacterStream
  74. */
  75. public InputSource (String systemId)
  76. {
  77. setSystemId(systemId);
  78. }
  79. /**
  80. * Create a new input source with a byte stream.
  81. *
  82. * <p>Application writers may use setSystemId to provide a base
  83. * for resolving relative URIs, setPublicId to include a
  84. * public identifier, and/or setEncoding to specify the object's
  85. * character encoding.</p>
  86. *
  87. * @param byteStream The raw byte stream containing the document.
  88. * @see #setPublicId
  89. * @see #setSystemId
  90. * @see #setEncoding
  91. * @see #setByteStream
  92. * @see #setCharacterStream
  93. */
  94. public InputSource (InputStream byteStream)
  95. {
  96. setByteStream(byteStream);
  97. }
  98. /**
  99. * Create a new input source with a character stream.
  100. *
  101. * <p>Application writers may use setSystemId() to provide a base
  102. * for resolving relative URIs, and setPublicId to include a
  103. * public identifier.</p>
  104. *
  105. * <p>The character stream shall not include a byte order mark.</p>
  106. *
  107. * @see #setPublicId
  108. * @see #setSystemId
  109. * @see #setByteStream
  110. * @see #setCharacterStream
  111. */
  112. public InputSource (Reader characterStream)
  113. {
  114. setCharacterStream(characterStream);
  115. }
  116. /**
  117. * Set the public identifier for this input source.
  118. *
  119. * <p>The public identifier is always optional: if the application
  120. * writer includes one, it will be provided as part of the
  121. * location information.</p>
  122. *
  123. * @param publicId The public identifier as a string.
  124. * @see #getPublicId
  125. * @see org.xml.sax.Locator#getPublicId
  126. * @see org.xml.sax.SAXParseException#getPublicId
  127. */
  128. public void setPublicId (String publicId)
  129. {
  130. this.publicId = publicId;
  131. }
  132. /**
  133. * Get the public identifier for this input source.
  134. *
  135. * @return The public identifier, or null if none was supplied.
  136. * @see #setPublicId
  137. */
  138. public String getPublicId ()
  139. {
  140. return publicId;
  141. }
  142. /**
  143. * Set the system identifier for this input source.
  144. *
  145. * <p>The system identifier is optional if there is a byte stream
  146. * or a character stream, but it is still useful to provide one,
  147. * since the application can use it to resolve relative URIs
  148. * and can include it in error messages and warnings (the parser
  149. * will attempt to open a connection to the URI only if
  150. * there is no byte stream or character stream specified).</p>
  151. *
  152. * <p>If the application knows the character encoding of the
  153. * object pointed to by the system identifier, it can register
  154. * the encoding using the setEncoding method.</p>
  155. *
  156. * <p>If the system ID is a URL, it must be fully resolved.</p>
  157. *
  158. * @param systemId The system identifier as a string.
  159. * @see #setEncoding
  160. * @see #getSystemId
  161. * @see org.xml.sax.Locator#getSystemId
  162. * @see org.xml.sax.SAXParseException#getSystemId
  163. */
  164. public void setSystemId (String systemId)
  165. {
  166. this.systemId = systemId;
  167. }
  168. /**
  169. * Get the system identifier for this input source.
  170. *
  171. * <p>The getEncoding method will return the character encoding
  172. * of the object pointed to, or null if unknown.</p>
  173. *
  174. * <p>If the system ID is a URL, it will be fully resolved.</p>
  175. *
  176. * @return The system identifier, or null if none was supplied.
  177. * @see #setSystemId
  178. * @see #getEncoding
  179. */
  180. public String getSystemId ()
  181. {
  182. return systemId;
  183. }
  184. /**
  185. * Set the byte stream for this input source.
  186. *
  187. * <p>The SAX parser will ignore this if there is also a character
  188. * stream specified, but it will use a byte stream in preference
  189. * to opening a URI connection itself.</p>
  190. *
  191. * <p>If the application knows the character encoding of the
  192. * byte stream, it should set it with the setEncoding method.</p>
  193. *
  194. * @param byteStream A byte stream containing an XML document or
  195. * other entity.
  196. * @see #setEncoding
  197. * @see #getByteStream
  198. * @see #getEncoding
  199. * @see java.io.InputStream
  200. */
  201. public void setByteStream (InputStream byteStream)
  202. {
  203. this.byteStream = byteStream;
  204. }
  205. /**
  206. * Get the byte stream for this input source.
  207. *
  208. * <p>The getEncoding method will return the character
  209. * encoding for this byte stream, or null if unknown.</p>
  210. *
  211. * @return The byte stream, or null if none was supplied.
  212. * @see #getEncoding
  213. * @see #setByteStream
  214. */
  215. public InputStream getByteStream ()
  216. {
  217. return byteStream;
  218. }
  219. /**
  220. * Set the character encoding, if known.
  221. *
  222. * <p>The encoding must be a string acceptable for an
  223. * XML encoding declaration (see section 4.3.3 of the XML 1.0
  224. * recommendation).</p>
  225. *
  226. * <p>This method has no effect when the application provides a
  227. * character stream.</p>
  228. *
  229. * @param encoding A string describing the character encoding.
  230. * @see #setSystemId
  231. * @see #setByteStream
  232. * @see #getEncoding
  233. */
  234. public void setEncoding (String encoding)
  235. {
  236. this.encoding = encoding;
  237. }
  238. /**
  239. * Get the character encoding for a byte stream or URI.
  240. *
  241. * @return The encoding, or null if none was supplied.
  242. * @see #setByteStream
  243. * @see #getSystemId
  244. * @see #getByteStream
  245. */
  246. public String getEncoding ()
  247. {
  248. return encoding;
  249. }
  250. /**
  251. * Set the character stream for this input source.
  252. *
  253. * <p>If there is a character stream specified, the SAX parser
  254. * will ignore any byte stream and will not attempt to open
  255. * a URI connection to the system identifier.</p>
  256. *
  257. * @param characterStream The character stream containing the
  258. * XML document or other entity.
  259. * @see #getCharacterStream
  260. * @see java.io.Reader
  261. */
  262. public void setCharacterStream (Reader characterStream)
  263. {
  264. this.characterStream = characterStream;
  265. }
  266. /**
  267. * Get the character stream for this input source.
  268. *
  269. * @return The character stream, or null if none was supplied.
  270. * @see #setCharacterStream
  271. */
  272. public Reader getCharacterStream ()
  273. {
  274. return characterStream;
  275. }
  276. ////////////////////////////////////////////////////////////////////
  277. // Internal state.
  278. ////////////////////////////////////////////////////////////////////
  279. private String publicId;
  280. private String systemId;
  281. private InputStream byteStream;
  282. private String encoding;
  283. private Reader characterStream;
  284. }
  285. // end of InputSource.java