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