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