1. // XMLReader.java - read an XML document.
  2. // http://www.saxproject.org
  3. // Written by David Megginson
  4. // NO WARRANTY! This class is in the Public Domain.
  5. // $Id: XMLReader.java,v 1.2.22.1 2004/05/01 08:34:40 jsuttor Exp $
  6. package org.xml.sax;
  7. import java.io.IOException;
  8. /**
  9. * Interface for reading an XML document using callbacks.
  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><strong>Note:</strong> despite its name, this interface does
  19. * <em>not</em> extend the standard Java {@link java.io.Reader Reader}
  20. * interface, because reading XML is a fundamentally different activity
  21. * than reading character data.</p>
  22. *
  23. * <p>XMLReader is the interface that an XML parser's SAX2 driver must
  24. * implement. This interface allows an application to set and
  25. * query features and properties in the parser, to register
  26. * event handlers for document processing, and to initiate
  27. * a document parse.</p>
  28. *
  29. * <p>All SAX interfaces are assumed to be synchronous: the
  30. * {@link #parse parse} methods must not return until parsing
  31. * is complete, and readers must wait for an event-handler callback
  32. * to return before reporting the next event.</p>
  33. *
  34. * <p>This interface replaces the (now deprecated) SAX 1.0 {@link
  35. * org.xml.sax.Parser Parser} interface. The XMLReader interface
  36. * contains two important enhancements over the old Parser
  37. * interface (as well as some minor ones):</p>
  38. *
  39. * <ol>
  40. * <li>it adds a standard way to query and set features and
  41. * properties; and</li>
  42. * <li>it adds Namespace support, which is required for many
  43. * higher-level XML standards.</li>
  44. * </ol>
  45. *
  46. * <p>There are adapters available to convert a SAX1 Parser to
  47. * a SAX2 XMLReader and vice-versa.</p>
  48. *
  49. * @since SAX 2.0
  50. * @author David Megginson
  51. * @version 2.0.1+ (sax2r3pre1)
  52. * @see org.xml.sax.XMLFilter
  53. * @see org.xml.sax.helpers.ParserAdapter
  54. * @see org.xml.sax.helpers.XMLReaderAdapter
  55. */
  56. public interface XMLReader
  57. {
  58. ////////////////////////////////////////////////////////////////////
  59. // Configuration.
  60. ////////////////////////////////////////////////////////////////////
  61. /**
  62. * Look up the value of a feature flag.
  63. *
  64. * <p>The feature name is any fully-qualified URI. It is
  65. * possible for an XMLReader to recognize a feature name but
  66. * temporarily be unable to return its value.
  67. * Some feature values may be available only in specific
  68. * contexts, such as before, during, or after a parse.
  69. * Also, some feature values may not be programmatically accessible.
  70. * (In the case of an adapter for SAX1 {@link Parser}, there is no
  71. * implementation-independent way to expose whether the underlying
  72. * parser is performing validation, expanding external entities,
  73. * and so forth.) </p>
  74. *
  75. * <p>All XMLReaders are required to recognize the
  76. * http://xml.org/sax/features/namespaces and the
  77. * http://xml.org/sax/features/namespace-prefixes feature names.</p>
  78. *
  79. * <p>Typical usage is something like this:</p>
  80. *
  81. * <pre>
  82. * XMLReader r = new MySAXDriver();
  83. *
  84. * // try to activate validation
  85. * try {
  86. * r.setFeature("http://xml.org/sax/features/validation", true);
  87. * } catch (SAXException e) {
  88. * System.err.println("Cannot activate validation.");
  89. * }
  90. *
  91. * // register event handlers
  92. * r.setContentHandler(new MyContentHandler());
  93. * r.setErrorHandler(new MyErrorHandler());
  94. *
  95. * // parse the first document
  96. * try {
  97. * r.parse("http://www.foo.com/mydoc.xml");
  98. * } catch (IOException e) {
  99. * System.err.println("I/O exception reading XML document");
  100. * } catch (SAXException e) {
  101. * System.err.println("XML exception reading document.");
  102. * }
  103. * </pre>
  104. *
  105. * <p>Implementors are free (and encouraged) to invent their own features,
  106. * using names built on their own URIs.</p>
  107. *
  108. * @param name The feature name, which is a fully-qualified URI.
  109. * @return The current value of the feature (true or false).
  110. * @exception org.xml.sax.SAXNotRecognizedException If the feature
  111. * value can't be assigned or retrieved.
  112. * @exception org.xml.sax.SAXNotSupportedException When the
  113. * XMLReader recognizes the feature name but
  114. * cannot determine its value at this time.
  115. * @see #setFeature
  116. */
  117. public boolean getFeature (String name)
  118. throws SAXNotRecognizedException, SAXNotSupportedException;
  119. /**
  120. * Set the value of a feature flag.
  121. *
  122. * <p>The feature name is any fully-qualified URI. It is
  123. * possible for an XMLReader to expose a feature value but
  124. * to be unable to change the current value.
  125. * Some feature values may be immutable or mutable only
  126. * in specific contexts, such as before, during, or after
  127. * a parse.</p>
  128. *
  129. * <p>All XMLReaders are required to support setting
  130. * http://xml.org/sax/features/namespaces to true and
  131. * http://xml.org/sax/features/namespace-prefixes to false.</p>
  132. *
  133. * @param name The feature name, which is a fully-qualified URI.
  134. * @param value The requested value of the feature (true or false).
  135. * @exception org.xml.sax.SAXNotRecognizedException If the feature
  136. * value can't be assigned or retrieved.
  137. * @exception org.xml.sax.SAXNotSupportedException When the
  138. * XMLReader recognizes the feature name but
  139. * cannot set the requested value.
  140. * @see #getFeature
  141. */
  142. public void setFeature (String name, boolean value)
  143. throws SAXNotRecognizedException, SAXNotSupportedException;
  144. /**
  145. * Look up the value of a property.
  146. *
  147. * <p>The property name is any fully-qualified URI. It is
  148. * possible for an XMLReader to recognize a property name but
  149. * temporarily be unable to return its value.
  150. * Some property values may be available only in specific
  151. * contexts, such as before, during, or after a parse.</p>
  152. *
  153. * <p>XMLReaders are not required to recognize any specific
  154. * property names, though an initial core set is documented for
  155. * SAX2.</p>
  156. *
  157. * <p>Implementors are free (and encouraged) to invent their own properties,
  158. * using names built on their own URIs.</p>
  159. *
  160. * @param name The property name, which is a fully-qualified URI.
  161. * @return The current value of the property.
  162. * @exception org.xml.sax.SAXNotRecognizedException If the property
  163. * value can't be assigned or retrieved.
  164. * @exception org.xml.sax.SAXNotSupportedException When the
  165. * XMLReader recognizes the property name but
  166. * cannot determine its value at this time.
  167. * @see #setProperty
  168. */
  169. public Object getProperty (String name)
  170. throws SAXNotRecognizedException, SAXNotSupportedException;
  171. /**
  172. * Set the value of a property.
  173. *
  174. * <p>The property name is any fully-qualified URI. It is
  175. * possible for an XMLReader to recognize a property name but
  176. * to be unable to change the current value.
  177. * Some property values may be immutable or mutable only
  178. * in specific contexts, such as before, during, or after
  179. * a parse.</p>
  180. *
  181. * <p>XMLReaders are not required to recognize setting
  182. * any specific property names, though a core set is defined by
  183. * SAX2.</p>
  184. *
  185. * <p>This method is also the standard mechanism for setting
  186. * extended handlers.</p>
  187. *
  188. * @param name The property name, which is a fully-qualified URI.
  189. * @param value The requested value for the property.
  190. * @exception org.xml.sax.SAXNotRecognizedException If the property
  191. * value can't be assigned or retrieved.
  192. * @exception org.xml.sax.SAXNotSupportedException When the
  193. * XMLReader recognizes the property name but
  194. * cannot set the requested value.
  195. */
  196. public void setProperty (String name, Object value)
  197. throws SAXNotRecognizedException, SAXNotSupportedException;
  198. ////////////////////////////////////////////////////////////////////
  199. // Event handlers.
  200. ////////////////////////////////////////////////////////////////////
  201. /**
  202. * Allow an application to register an entity resolver.
  203. *
  204. * <p>If the application does not register an entity resolver,
  205. * the XMLReader will perform its own default resolution.</p>
  206. *
  207. * <p>Applications may register a new or different resolver in the
  208. * middle of a parse, and the SAX parser must begin using the new
  209. * resolver immediately.</p>
  210. *
  211. * @param resolver The entity resolver.
  212. * @see #getEntityResolver
  213. */
  214. public void setEntityResolver (EntityResolver resolver);
  215. /**
  216. * Return the current entity resolver.
  217. *
  218. * @return The current entity resolver, or null if none
  219. * has been registered.
  220. * @see #setEntityResolver
  221. */
  222. public EntityResolver getEntityResolver ();
  223. /**
  224. * Allow an application to register a DTD event handler.
  225. *
  226. * <p>If the application does not register a DTD handler, all DTD
  227. * events reported by the SAX parser will be silently ignored.</p>
  228. *
  229. * <p>Applications may register a new or different handler in the
  230. * middle of a parse, and the SAX parser must begin using the new
  231. * handler immediately.</p>
  232. *
  233. * @param handler The DTD handler.
  234. * @see #getDTDHandler
  235. */
  236. public void setDTDHandler (DTDHandler handler);
  237. /**
  238. * Return the current DTD handler.
  239. *
  240. * @return The current DTD handler, or null if none
  241. * has been registered.
  242. * @see #setDTDHandler
  243. */
  244. public DTDHandler getDTDHandler ();
  245. /**
  246. * Allow an application to register a content event handler.
  247. *
  248. * <p>If the application does not register a content handler, all
  249. * content events reported by the SAX parser will be silently
  250. * ignored.</p>
  251. *
  252. * <p>Applications may register a new or different handler in the
  253. * middle of a parse, and the SAX parser must begin using the new
  254. * handler immediately.</p>
  255. *
  256. * @param handler The content handler.
  257. * @see #getContentHandler
  258. */
  259. public void setContentHandler (ContentHandler handler);
  260. /**
  261. * Return the current content handler.
  262. *
  263. * @return The current content handler, or null if none
  264. * has been registered.
  265. * @see #setContentHandler
  266. */
  267. public ContentHandler getContentHandler ();
  268. /**
  269. * Allow an application to register an error event handler.
  270. *
  271. * <p>If the application does not register an error handler, all
  272. * error events reported by the SAX parser will be silently
  273. * ignored; however, normal processing may not continue. It is
  274. * highly recommended that all SAX applications implement an
  275. * error handler to avoid unexpected bugs.</p>
  276. *
  277. * <p>Applications may register a new or different handler in the
  278. * middle of a parse, and the SAX parser must begin using the new
  279. * handler immediately.</p>
  280. *
  281. * @param handler The error handler.
  282. * @see #getErrorHandler
  283. */
  284. public void setErrorHandler (ErrorHandler handler);
  285. /**
  286. * Return the current error handler.
  287. *
  288. * @return The current error handler, or null if none
  289. * has been registered.
  290. * @see #setErrorHandler
  291. */
  292. public ErrorHandler getErrorHandler ();
  293. ////////////////////////////////////////////////////////////////////
  294. // Parsing.
  295. ////////////////////////////////////////////////////////////////////
  296. /**
  297. * Parse an XML document.
  298. *
  299. * <p>The application can use this method to instruct the XML
  300. * reader to begin parsing an XML document from any valid input
  301. * source (a character stream, a byte stream, or a URI).</p>
  302. *
  303. * <p>Applications may not invoke this method while a parse is in
  304. * progress (they should create a new XMLReader instead for each
  305. * nested XML document). Once a parse is complete, an
  306. * application may reuse the same XMLReader object, possibly with a
  307. * different input source.
  308. * Configuration of the XMLReader object (such as handler bindings and
  309. * values established for feature flags and properties) is unchanged
  310. * by completion of a parse, unless the definition of that aspect of
  311. * the configuration explicitly specifies other behavior.
  312. * (For example, feature flags or properties exposing
  313. * characteristics of the document being parsed.)
  314. * </p>
  315. *
  316. * <p>During the parse, the XMLReader will provide information
  317. * about the XML document through the registered event
  318. * handlers.</p>
  319. *
  320. * <p>This method is synchronous: it will not return until parsing
  321. * has ended. If a client application wants to terminate
  322. * parsing early, it should throw an exception.</p>
  323. *
  324. * @param input The input source for the top-level of the
  325. * XML document.
  326. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  327. * wrapping another exception.
  328. * @exception java.io.IOException An IO exception from the parser,
  329. * possibly from a byte stream or character stream
  330. * supplied by the application.
  331. * @see org.xml.sax.InputSource
  332. * @see #parse(java.lang.String)
  333. * @see #setEntityResolver
  334. * @see #setDTDHandler
  335. * @see #setContentHandler
  336. * @see #setErrorHandler
  337. */
  338. public void parse (InputSource input)
  339. throws IOException, SAXException;
  340. /**
  341. * Parse an XML document from a system identifier (URI).
  342. *
  343. * <p>This method is a shortcut for the common case of reading a
  344. * document from a system identifier. It is the exact
  345. * equivalent of the following:</p>
  346. *
  347. * <pre>
  348. * parse(new InputSource(systemId));
  349. * </pre>
  350. *
  351. * <p>If the system identifier is a URL, it must be fully resolved
  352. * by the application before it is passed to the parser.</p>
  353. *
  354. * @param systemId The system identifier (URI).
  355. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  356. * wrapping another exception.
  357. * @exception java.io.IOException An IO exception from the parser,
  358. * possibly from a byte stream or character stream
  359. * supplied by the application.
  360. * @see #parse(org.xml.sax.InputSource)
  361. */
  362. public void parse (String systemId)
  363. throws IOException, SAXException;
  364. }