1. // DefaultHandler.java - default implementation of the core handlers.
  2. // Written by David Megginson, sax@megginson.com
  3. // NO WARRANTY! This class is in the public domain.
  4. // $Id: DefaultHandler.java,v 1.2 2001/08/01 06:43:19 tcng Exp $
  5. package org.xml.sax.helpers;
  6. import org.xml.sax.InputSource;
  7. import org.xml.sax.Locator;
  8. import org.xml.sax.Attributes;
  9. import org.xml.sax.EntityResolver;
  10. import org.xml.sax.DTDHandler;
  11. import org.xml.sax.ContentHandler;
  12. import org.xml.sax.ErrorHandler;
  13. import org.xml.sax.SAXException;
  14. import org.xml.sax.SAXParseException;
  15. /**
  16. * Default base class for SAX2 event handlers.
  17. *
  18. * <blockquote>
  19. * <em>This module, both source code and documentation, is in the
  20. * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
  21. * </blockquote>
  22. *
  23. * <p>This class is available as a convenience base class for SAX2
  24. * applications: it provides default implementations for all of the
  25. * callbacks in the four core SAX2 handler classes:</p>
  26. *
  27. * <ul>
  28. * <li>{@link org.xml.sax.EntityResolver EntityResolver}</li>
  29. * <li>{@link org.xml.sax.DTDHandler DTDHandler}</li>
  30. * <li>{@link org.xml.sax.ContentHandler ContentHandler}</li>
  31. * <li>{@link org.xml.sax.ErrorHandler ErrorHandler}</li>
  32. * </ul>
  33. *
  34. * <p>Application writers can extend this class when they need to
  35. * implement only part of an interface; parser writers can
  36. * instantiate this class to provide default handlers when the
  37. * application has not supplied its own.</p>
  38. *
  39. * <p>This class replaces the deprecated SAX1
  40. * {@link org.xml.sax.HandlerBase HandlerBase} class.</p>
  41. *
  42. * @since SAX 2.0
  43. * @author David Megginson,
  44. * <a href="mailto:sax@megginson.com">sax@megginson.com</a>
  45. * @version 2.0
  46. * @see org.xml.sax.EntityResolver
  47. * @see org.xml.sax.DTDHandler
  48. * @see org.xml.sax.ContentHandler
  49. * @see org.xml.sax.ErrorHandler
  50. */
  51. public class DefaultHandler
  52. implements EntityResolver, DTDHandler, ContentHandler, ErrorHandler
  53. {
  54. ////////////////////////////////////////////////////////////////////
  55. // Default implementation of the EntityResolver interface.
  56. ////////////////////////////////////////////////////////////////////
  57. /**
  58. * Resolve an external entity.
  59. *
  60. * <p>Always return null, so that the parser will use the system
  61. * identifier provided in the XML document. This method implements
  62. * the SAX default behaviour: application writers can override it
  63. * in a subclass to do special translations such as catalog lookups
  64. * or URI redirection.</p>
  65. *
  66. * @param publicId The public identifer, or null if none is
  67. * available.
  68. * @param systemId The system identifier provided in the XML
  69. * document.
  70. * @return The new input source, or null to require the
  71. * default behaviour.
  72. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  73. * wrapping another exception.
  74. * @see org.xml.sax.EntityResolver#resolveEntity
  75. */
  76. public InputSource resolveEntity (String publicId, String systemId)
  77. throws SAXException
  78. {
  79. return null;
  80. }
  81. ////////////////////////////////////////////////////////////////////
  82. // Default implementation of DTDHandler interface.
  83. ////////////////////////////////////////////////////////////////////
  84. /**
  85. * Receive notification of a notation declaration.
  86. *
  87. * <p>By default, do nothing. Application writers may override this
  88. * method in a subclass if they wish to keep track of the notations
  89. * declared in a document.</p>
  90. *
  91. * @param name The notation name.
  92. * @param publicId The notation public identifier, or null if not
  93. * available.
  94. * @param systemId The notation system identifier.
  95. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  96. * wrapping another exception.
  97. * @see org.xml.sax.DTDHandler#notationDecl
  98. */
  99. public void notationDecl (String name, String publicId, String systemId)
  100. throws SAXException
  101. {
  102. // no op
  103. }
  104. /**
  105. * Receive notification of an unparsed entity declaration.
  106. *
  107. * <p>By default, do nothing. Application writers may override this
  108. * method in a subclass to keep track of the unparsed entities
  109. * declared in a document.</p>
  110. *
  111. * @param name The entity name.
  112. * @param publicId The entity public identifier, or null if not
  113. * available.
  114. * @param systemId The entity system identifier.
  115. * @param notationName The name of the associated notation.
  116. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  117. * wrapping another exception.
  118. * @see org.xml.sax.DTDHandler#unparsedEntityDecl
  119. */
  120. public void unparsedEntityDecl (String name, String publicId,
  121. String systemId, String notationName)
  122. throws SAXException
  123. {
  124. // no op
  125. }
  126. ////////////////////////////////////////////////////////////////////
  127. // Default implementation of ContentHandler interface.
  128. ////////////////////////////////////////////////////////////////////
  129. /**
  130. * Receive a Locator object for document events.
  131. *
  132. * <p>By default, do nothing. Application writers may override this
  133. * method in a subclass if they wish to store the locator for use
  134. * with other document events.</p>
  135. *
  136. * @param locator A locator for all SAX document events.
  137. * @see org.xml.sax.ContentHandler#setDocumentLocator
  138. * @see org.xml.sax.Locator
  139. */
  140. public void setDocumentLocator (Locator locator)
  141. {
  142. // no op
  143. }
  144. /**
  145. * Receive notification of the beginning of the document.
  146. *
  147. * <p>By default, do nothing. Application writers may override this
  148. * method in a subclass to take specific actions at the beginning
  149. * of a document (such as allocating the root node of a tree or
  150. * creating an output file).</p>
  151. *
  152. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  153. * wrapping another exception.
  154. * @see org.xml.sax.ContentHandler#startDocument
  155. */
  156. public void startDocument ()
  157. throws SAXException
  158. {
  159. // no op
  160. }
  161. /**
  162. * Receive notification of the end of the document.
  163. *
  164. * <p>By default, do nothing. Application writers may override this
  165. * method in a subclass to take specific actions at the end
  166. * of a document (such as finalising a tree or closing an output
  167. * file).</p>
  168. *
  169. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  170. * wrapping another exception.
  171. * @see org.xml.sax.ContentHandler#endDocument
  172. */
  173. public void endDocument ()
  174. throws SAXException
  175. {
  176. // no op
  177. }
  178. /**
  179. * Receive notification of the start of a Namespace mapping.
  180. *
  181. * <p>By default, do nothing. Application writers may override this
  182. * method in a subclass to take specific actions at the start of
  183. * each Namespace prefix scope (such as storing the prefix mapping).</p>
  184. *
  185. * @param prefix The Namespace prefix being declared.
  186. * @param uri The Namespace URI mapped to the prefix.
  187. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  188. * wrapping another exception.
  189. * @see org.xml.sax.ContentHandler#startPrefixMapping
  190. */
  191. public void startPrefixMapping (String prefix, String uri)
  192. throws SAXException
  193. {
  194. // no op
  195. }
  196. /**
  197. * Receive notification of the end of a Namespace mapping.
  198. *
  199. * <p>By default, do nothing. Application writers may override this
  200. * method in a subclass to take specific actions at the end of
  201. * each prefix mapping.</p>
  202. *
  203. * @param prefix The Namespace prefix being declared.
  204. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  205. * wrapping another exception.
  206. * @see org.xml.sax.ContentHandler#endPrefixMapping
  207. */
  208. public void endPrefixMapping (String prefix)
  209. throws SAXException
  210. {
  211. // no op
  212. }
  213. /**
  214. * Receive notification of the start of an element.
  215. *
  216. * <p>By default, do nothing. Application writers may override this
  217. * method in a subclass to take specific actions at the start of
  218. * each element (such as allocating a new tree node or writing
  219. * output to a file).</p>
  220. *
  221. * @param name The element type name.
  222. * @param attributes The specified or defaulted attributes.
  223. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  224. * wrapping another exception.
  225. * @see org.xml.sax.ContentHandler#startElement
  226. */
  227. public void startElement (String uri, String localName,
  228. String qName, Attributes attributes)
  229. throws SAXException
  230. {
  231. // no op
  232. }
  233. /**
  234. * Receive notification of the end of an element.
  235. *
  236. * <p>By default, do nothing. Application writers may override this
  237. * method in a subclass to take specific actions at the end of
  238. * each element (such as finalising a tree node or writing
  239. * output to a file).</p>
  240. *
  241. * @param name The element type name.
  242. * @param attributes The specified or defaulted attributes.
  243. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  244. * wrapping another exception.
  245. * @see org.xml.sax.ContentHandler#endElement
  246. */
  247. public void endElement (String uri, String localName, String qName)
  248. throws SAXException
  249. {
  250. // no op
  251. }
  252. /**
  253. * Receive notification of character data inside an element.
  254. *
  255. * <p>By default, do nothing. Application writers may override this
  256. * method to take specific actions for each chunk of character data
  257. * (such as adding the data to a node or buffer, or printing it to
  258. * a file).</p>
  259. *
  260. * @param ch The characters.
  261. * @param start The start position in the character array.
  262. * @param length The number of characters to use from the
  263. * character array.
  264. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  265. * wrapping another exception.
  266. * @see org.xml.sax.ContentHandler#characters
  267. */
  268. public void characters (char ch[], int start, int length)
  269. throws SAXException
  270. {
  271. // no op
  272. }
  273. /**
  274. * Receive notification of ignorable whitespace in element content.
  275. *
  276. * <p>By default, do nothing. Application writers may override this
  277. * method to take specific actions for each chunk of ignorable
  278. * whitespace (such as adding data to a node or buffer, or printing
  279. * it to a file).</p>
  280. *
  281. * @param ch The whitespace characters.
  282. * @param start The start position in the character array.
  283. * @param length The number of characters to use from the
  284. * character array.
  285. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  286. * wrapping another exception.
  287. * @see org.xml.sax.ContentHandler#ignorableWhitespace
  288. */
  289. public void ignorableWhitespace (char ch[], int start, int length)
  290. throws SAXException
  291. {
  292. // no op
  293. }
  294. /**
  295. * Receive notification of a processing instruction.
  296. *
  297. * <p>By default, do nothing. Application writers may override this
  298. * method in a subclass to take specific actions for each
  299. * processing instruction, such as setting status variables or
  300. * invoking other methods.</p>
  301. *
  302. * @param target The processing instruction target.
  303. * @param data The processing instruction data, or null if
  304. * none is supplied.
  305. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  306. * wrapping another exception.
  307. * @see org.xml.sax.ContentHandler#processingInstruction
  308. */
  309. public void processingInstruction (String target, String data)
  310. throws SAXException
  311. {
  312. // no op
  313. }
  314. /**
  315. * Receive notification of a skipped entity.
  316. *
  317. * <p>By default, do nothing. Application writers may override this
  318. * method in a subclass to take specific actions for each
  319. * processing instruction, such as setting status variables or
  320. * invoking other methods.</p>
  321. *
  322. * @param name The name of the skipped entity.
  323. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  324. * wrapping another exception.
  325. * @see org.xml.sax.ContentHandler#processingInstruction
  326. */
  327. public void skippedEntity (String name)
  328. throws SAXException
  329. {
  330. // no op
  331. }
  332. ////////////////////////////////////////////////////////////////////
  333. // Default implementation of the ErrorHandler interface.
  334. ////////////////////////////////////////////////////////////////////
  335. /**
  336. * Receive notification of a parser warning.
  337. *
  338. * <p>The default implementation does nothing. Application writers
  339. * may override this method in a subclass to take specific actions
  340. * for each warning, such as inserting the message in a log file or
  341. * printing it to the console.</p>
  342. *
  343. * @param e The warning information encoded as an exception.
  344. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  345. * wrapping another exception.
  346. * @see org.xml.sax.ErrorHandler#warning
  347. * @see org.xml.sax.SAXParseException
  348. */
  349. public void warning (SAXParseException e)
  350. throws SAXException
  351. {
  352. // no op
  353. }
  354. /**
  355. * Receive notification of a recoverable parser error.
  356. *
  357. * <p>The default implementation does nothing. Application writers
  358. * may override this method in a subclass to take specific actions
  359. * for each error, such as inserting the message in a log file or
  360. * printing it to the console.</p>
  361. *
  362. * @param e The warning information encoded as an exception.
  363. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  364. * wrapping another exception.
  365. * @see org.xml.sax.ErrorHandler#warning
  366. * @see org.xml.sax.SAXParseException
  367. */
  368. public void error (SAXParseException e)
  369. throws SAXException
  370. {
  371. // no op
  372. }
  373. /**
  374. * Report a fatal XML parsing error.
  375. *
  376. * <p>The default implementation throws a SAXParseException.
  377. * Application writers may override this method in a subclass if
  378. * they need to take specific actions for each fatal error (such as
  379. * collecting all of the errors into a single report): in any case,
  380. * the application must stop all regular processing when this
  381. * method is invoked, since the document is no longer reliable, and
  382. * the parser may no longer report parsing events.</p>
  383. *
  384. * @param e The error information encoded as an exception.
  385. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  386. * wrapping another exception.
  387. * @see org.xml.sax.ErrorHandler#fatalError
  388. * @see org.xml.sax.SAXParseException
  389. */
  390. public void fatalError (SAXParseException e)
  391. throws SAXException
  392. {
  393. throw e;
  394. }
  395. }
  396. // end of DefaultHandler.java