1. // ContentHandler.java - handle main document content.
  2. // Written by David Megginson, sax@megginson.com
  3. // NO WARRANTY! This class is in the public domain.
  4. // $Id: ContentHandler.java,v 1.1 2001/05/20 03:12:56 curcuru Exp $
  5. package org.xml.sax;
  6. /**
  7. * Receive notification of the logical content of a document.
  8. *
  9. * <blockquote>
  10. * <em>This module, both source code and documentation, is in the
  11. * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
  12. * </blockquote>
  13. *
  14. * <p>This is the main interface that most SAX applications
  15. * implement: if the application needs to be informed of basic parsing
  16. * events, it implements this interface and registers an instance with
  17. * the SAX parser using the {@link org.xml.sax.XMLReader#setContentHandler
  18. * setContentHandler} method. The parser uses the instance to report
  19. * basic document-related events like the start and end of elements
  20. * and character data.</p>
  21. *
  22. * <p>The order of events in this interface is very important, and
  23. * mirrors the order of information in the document itself. For
  24. * example, all of an element's content (character data, processing
  25. * instructions, and/or subelements) will appear, in order, between
  26. * the startElement event and the corresponding endElement event.</p>
  27. *
  28. * <p>This interface is similar to the now-deprecated SAX 1.0
  29. * DocumentHandler interface, but it adds support for Namespaces
  30. * and for reporting skipped entities (in non-validating XML
  31. * processors).</p>
  32. *
  33. * <p>Implementors should note that there is also a Java class
  34. * {@link java.net.ContentHandler ContentHandler} in the java.net
  35. * package; that means that it's probably a bad idea to do</p>
  36. *
  37. * <blockquote>
  38. * import java.net.*;
  39. * import org.xml.sax.*;
  40. * </blockquote>
  41. *
  42. * <p>In fact, "import ...*" is usually a sign of sloppy programming
  43. * anyway, so the user should consider this a feature rather than a
  44. * bug.</p>
  45. *
  46. * @since SAX 2.0
  47. * @author David Megginson,
  48. * <a href="mailto:sax@megginson.com">sax@megginson.com</a>
  49. * @version 2.0r2pre
  50. * @see org.xml.sax.XMLReader
  51. * @see org.xml.sax.DTDHandler
  52. * @see org.xml.sax.ErrorHandler
  53. */
  54. public interface ContentHandler
  55. {
  56. /**
  57. * Receive an object for locating the origin of SAX document events.
  58. *
  59. * <p>SAX parsers are strongly encouraged (though not absolutely
  60. * required) to supply a locator: if it does so, it must supply
  61. * the locator to the application by invoking this method before
  62. * invoking any of the other methods in the ContentHandler
  63. * interface.</p>
  64. *
  65. * <p>The locator allows the application to determine the end
  66. * position of any document-related event, even if the parser is
  67. * not reporting an error. Typically, the application will
  68. * use this information for reporting its own errors (such as
  69. * character content that does not match an application's
  70. * business rules). The information returned by the locator
  71. * is probably not sufficient for use with a search engine.</p>
  72. *
  73. * <p>Note that the locator will return correct information only
  74. * during the invocation of the events in this interface. The
  75. * application should not attempt to use it at any other time.</p>
  76. *
  77. * @param locator An object that can return the location of
  78. * any SAX document event.
  79. * @see org.xml.sax.Locator
  80. */
  81. public void setDocumentLocator (Locator locator);
  82. /**
  83. * Receive notification of the beginning of a document.
  84. *
  85. * <p>The SAX parser will invoke this method only once, before any
  86. * other methods in this interface or in {@link org.xml.sax.DTDHandler
  87. * DTDHandler} (except for {@link #setDocumentLocator
  88. * setDocumentLocator}).</p>
  89. *
  90. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  91. * wrapping another exception.
  92. * @see #endDocument
  93. */
  94. public void startDocument ()
  95. throws SAXException;
  96. /**
  97. * Receive notification of the end of a document.
  98. *
  99. * <p>The SAX parser will invoke this method only once, and it will
  100. * be the last method invoked during the parse. The parser shall
  101. * not invoke this method until it has either abandoned parsing
  102. * (because of an unrecoverable error) or reached the end of
  103. * input.</p>
  104. *
  105. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  106. * wrapping another exception.
  107. * @see #startDocument
  108. */
  109. public void endDocument()
  110. throws SAXException;
  111. /**
  112. * Begin the scope of a prefix-URI Namespace mapping.
  113. *
  114. * <p>The information from this event is not necessary for
  115. * normal Namespace processing: the SAX XML reader will
  116. * automatically replace prefixes for element and attribute
  117. * names when the <code>http://xml.org/sax/features/namespaces</code>
  118. * feature is <var>true</var> (the default).</p>
  119. *
  120. * <p>There are cases, however, when applications need to
  121. * use prefixes in character data or in attribute values,
  122. * where they cannot safely be expanded automatically; the
  123. * start/endPrefixMapping event supplies the information
  124. * to the application to expand prefixes in those contexts
  125. * itself, if necessary.</p>
  126. *
  127. * <p>Note that start/endPrefixMapping events are not
  128. * guaranteed to be properly nested relative to each-other:
  129. * all startPrefixMapping events will occur before the
  130. * corresponding {@link #startElement startElement} event,
  131. * and all {@link #endPrefixMapping endPrefixMapping}
  132. * events will occur after the corresponding {@link #endElement
  133. * endElement} event, but their order is not otherwise
  134. * guaranteed.</p>
  135. *
  136. * <p>There should never be start/endPrefixMapping events for the
  137. * "xml" prefix, since it is predeclared and immutable.</p>
  138. *
  139. * @param prefix The Namespace prefix being declared.
  140. * @param uri The Namespace URI the prefix is mapped to.
  141. * @exception org.xml.sax.SAXException The client may throw
  142. * an exception during processing.
  143. * @see #endPrefixMapping
  144. * @see #startElement
  145. */
  146. public void startPrefixMapping (String prefix, String uri)
  147. throws SAXException;
  148. /**
  149. * End the scope of a prefix-URI mapping.
  150. *
  151. * <p>See {@link #startPrefixMapping startPrefixMapping} for
  152. * details. This event will always occur after the corresponding
  153. * {@link #endElement endElement} event, but the order of
  154. * {@link #endPrefixMapping endPrefixMapping} events is not otherwise
  155. * guaranteed.</p>
  156. *
  157. * @param prefix The prefix that was being mapping.
  158. * @exception org.xml.sax.SAXException The client may throw
  159. * an exception during processing.
  160. * @see #startPrefixMapping
  161. * @see #endElement
  162. */
  163. public void endPrefixMapping (String prefix)
  164. throws SAXException;
  165. /**
  166. * Receive notification of the beginning of an element.
  167. *
  168. * <p>The Parser will invoke this method at the beginning of every
  169. * element in the XML document; there will be a corresponding
  170. * {@link #endElement endElement} event for every startElement event
  171. * (even when the element is empty). All of the element's content will be
  172. * reported, in order, before the corresponding endElement
  173. * event.</p>
  174. *
  175. * <p>This event allows up to three name components for each
  176. * element:</p>
  177. *
  178. * <ol>
  179. * <li>the Namespace URI;</li>
  180. * <li>the local name; and</li>
  181. * <li>the qualified (prefixed) name.</li>
  182. * </ol>
  183. *
  184. * <p>Any or all of these may be provided, depending on the
  185. * values of the <var>http://xml.org/sax/features/namespaces</var>
  186. * and the <var>http://xml.org/sax/features/namespace-prefixes</var>
  187. * properties:</p>
  188. *
  189. * <ul>
  190. * <li>the Namespace URI and local name are required when
  191. * the namespaces property is <var>true</var> (the default), and are
  192. * optional when the namespaces property is <var>false</var> (if one is
  193. * specified, both must be);</li>
  194. * <li>the qualified name is required when the namespace-prefixes property
  195. * is <var>true</var>, and is optional when the namespace-prefixes property
  196. * is <var>false</var> (the default).</li>
  197. * </ul>
  198. *
  199. * <p>Note that the attribute list provided will contain only
  200. * attributes with explicit values (specified or defaulted):
  201. * #IMPLIED attributes will be omitted. The attribute list
  202. * will contain attributes used for Namespace declarations
  203. * (xmlns* attributes) only if the
  204. * <code>http://xml.org/sax/features/namespace-prefixes</code>
  205. * property is true (it is false by default, and support for a
  206. * true value is optional).</p>
  207. *
  208. * @param uri The Namespace URI, or the empty string if the
  209. * element has no Namespace URI or if Namespace
  210. * processing is not being performed.
  211. * @param localName The local name (without prefix), or the
  212. * empty string if Namespace processing is not being
  213. * performed.
  214. * @param qName The qualified name (with prefix), or the
  215. * empty string if qualified names are not available.
  216. * @param atts The attributes attached to the element. If
  217. * there are no attributes, it shall be an empty
  218. * Attributes object.
  219. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  220. * wrapping another exception.
  221. * @see #endElement
  222. * @see org.xml.sax.Attributes
  223. */
  224. public void startElement (String namespaceURI, String localName,
  225. String qName, Attributes atts)
  226. throws SAXException;
  227. /**
  228. * Receive notification of the end of an element.
  229. *
  230. * <p>The SAX parser will invoke this method at the end of every
  231. * element in the XML document; there will be a corresponding
  232. * {@link #startElement startElement} event for every endElement
  233. * event (even when the element is empty).</p>
  234. *
  235. * <p>For information on the names, see startElement.</p>
  236. *
  237. * @param uri The Namespace URI, or the empty string if the
  238. * element has no Namespace URI or if Namespace
  239. * processing is not being performed.
  240. * @param localName The local name (without prefix), or the
  241. * empty string if Namespace processing is not being
  242. * performed.
  243. * @param qName The qualified XML 1.0 name (with prefix), or the
  244. * empty string if qualified names are not available.
  245. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  246. * wrapping another exception.
  247. */
  248. public void endElement (String namespaceURI, String localName,
  249. String qName)
  250. throws SAXException;
  251. /**
  252. * Receive notification of character data.
  253. *
  254. * <p>The Parser will call this method to report each chunk of
  255. * character data. SAX parsers may return all contiguous character
  256. * data in a single chunk, or they may split it into several
  257. * chunks; however, all of the characters in any single event
  258. * must come from the same external entity so that the Locator
  259. * provides useful information.</p>
  260. *
  261. * <p>The application must not attempt to read from the array
  262. * outside of the specified range.</p>
  263. *
  264. * <p>Note that some parsers will report whitespace in element
  265. * content using the {@link #ignorableWhitespace ignorableWhitespace}
  266. * method rather than this one (validating parsers <em>must</em>
  267. * do so).</p>
  268. *
  269. * @param ch The characters from the XML document.
  270. * @param start The start position in the array.
  271. * @param length The number of characters to read from the array.
  272. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  273. * wrapping another exception.
  274. * @see #ignorableWhitespace
  275. * @see org.xml.sax.Locator
  276. */
  277. public void characters (char ch[], int start, int length)
  278. throws SAXException;
  279. /**
  280. * Receive notification of ignorable whitespace in element content.
  281. *
  282. * <p>Validating Parsers must use this method to report each chunk
  283. * of whitespace in element content (see the W3C XML 1.0 recommendation,
  284. * section 2.10): non-validating parsers may also use this method
  285. * if they are capable of parsing and using content models.</p>
  286. *
  287. * <p>SAX parsers may return all contiguous whitespace in a single
  288. * chunk, or they may split it into several chunks; however, all of
  289. * the characters in any single event must come from the same
  290. * external entity, so that the Locator provides useful
  291. * information.</p>
  292. *
  293. * <p>The application must not attempt to read from the array
  294. * outside of the specified range.</p>
  295. *
  296. * @param ch The characters from the XML document.
  297. * @param start The start position in the array.
  298. * @param length The number of characters to read from the array.
  299. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  300. * wrapping another exception.
  301. * @see #characters
  302. */
  303. public void ignorableWhitespace (char ch[], int start, int length)
  304. throws SAXException;
  305. /**
  306. * Receive notification of a processing instruction.
  307. *
  308. * <p>The Parser will invoke this method once for each processing
  309. * instruction found: note that processing instructions may occur
  310. * before or after the main document element.</p>
  311. *
  312. * <p>A SAX parser must never report an XML declaration (XML 1.0,
  313. * section 2.8) or a text declaration (XML 1.0, section 4.3.1)
  314. * using this method.</p>
  315. *
  316. * @param target The processing instruction target.
  317. * @param data The processing instruction data, or null if
  318. * none was supplied. The data does not include any
  319. * whitespace separating it from the target.
  320. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  321. * wrapping another exception.
  322. */
  323. public void processingInstruction (String target, String data)
  324. throws SAXException;
  325. /**
  326. * Receive notification of a skipped entity.
  327. *
  328. * <p>The Parser will invoke this method once for each entity
  329. * skipped. Non-validating processors may skip entities if they
  330. * have not seen the declarations (because, for example, the
  331. * entity was declared in an external DTD subset). All processors
  332. * may skip external entities, depending on the values of the
  333. * <code>http://xml.org/sax/features/external-general-entities</code>
  334. * and the
  335. * <code>http://xml.org/sax/features/external-parameter-entities</code>
  336. * properties.</p>
  337. *
  338. * @param name The name of the skipped entity. If it is a
  339. * parameter entity, the name will begin with '%', and if
  340. * it is the external DTD subset, it will be the string
  341. * "[dtd]".
  342. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  343. * wrapping another exception.
  344. */
  345. public void skippedEntity (String name)
  346. throws SAXException;
  347. }
  348. // end of ContentHandler.java