1. /*
  2. * @(#)SAXParser.java 1.10 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.xml.parsers;
  8. import java.io.InputStream;
  9. import java.io.IOException;
  10. import java.io.File;
  11. import org.xml.sax.Parser;
  12. import org.xml.sax.XMLReader;
  13. import org.xml.sax.HandlerBase;
  14. import org.xml.sax.helpers.DefaultHandler;
  15. import org.xml.sax.InputSource;
  16. import org.xml.sax.SAXException;
  17. import org.xml.sax.SAXNotRecognizedException;
  18. import org.xml.sax.SAXNotSupportedException;
  19. /**
  20. * Defines the API that wraps an {@link org.xml.sax.XMLReader}
  21. * implementation class. In JAXP 1.0, this class wrapped the
  22. * {@link org.xml.sax.Parser} interface, however this interface was
  23. * replaced by the {@link org.xml.sax.XMLReader}. For ease
  24. * of transition, this class continues to support the same name
  25. * and interface as well as supporting new methods.
  26. *
  27. * An instance of this class can be obtained from the
  28. * {@link javax.xml.parsers.SAXParserFactory#newSAXParser()} method.
  29. * Once an instance of this class is obtained, XML can be parsed from
  30. * a variety of input sources. These input sources are InputStreams,
  31. * Files, URLs, and SAX InputSources.<p>
  32. *
  33. *
  34. * As the content is parsed by the underlying parser, methods of the
  35. * given {@link org.xml.sax.HandlerBase} or the
  36. * {@link org.xml.sax.helpers.DefaultHandler} are called.<p>
  37. *
  38. * Implementors of this class which wrap an underlying implementation
  39. * can consider using the {@link org.xml.sax.helpers.ParserAdapter}
  40. * class to initially adapt their SAX1 impelemntation to work under
  41. * this revised class.<p>
  42. *
  43. * An implementation of <code>SAXParser</code> is <em>NOT</em>
  44. * guaranteed to behave as per the specification if it is used concurrently by
  45. * two or more threads. It is recommended to have one instance of the
  46. * <code>SAXParser</code> per thread or it is upto the application to
  47. * make sure about the use of <code>SAXParser</code> from more than one
  48. * thread.
  49. *
  50. * @since JAXP 1.0
  51. * @version 1.0
  52. */
  53. public abstract class SAXParser {
  54. protected SAXParser () {
  55. }
  56. /**
  57. * Parse the content of the given {@link java.io.InputStream}
  58. * instance as XML using the specified {@link org.xml.sax.HandlerBase}.
  59. * <i> Use of the DefaultHandler version of this method is recommended as
  60. * the HandlerBase class has been deprecated in SAX 2.0</i>
  61. *
  62. * @param is InputStream containing the content to be parsed.
  63. * @param hb The SAX HandlerBase to use.
  64. * @exception IOException If any IO errors occur.
  65. * @exception IllegalArgumentException If the given InputStream is null.
  66. * @exception SAXException If the underlying parser throws a
  67. * SAXException while parsing.
  68. * @see org.xml.sax.DocumentHandler
  69. */
  70. public void parse(InputStream is, HandlerBase hb)
  71. throws SAXException, IOException
  72. {
  73. if (is == null) {
  74. throw new IllegalArgumentException("InputStream cannot be null");
  75. }
  76. InputSource input = new InputSource(is);
  77. this.parse(input, hb);
  78. }
  79. /**
  80. * Parse the content of the given {@link java.io.InputStream}
  81. * instance as XML using the specified {@link org.xml.sax.HandlerBase}.
  82. * <i> Use of the DefaultHandler version of this method is recommended as
  83. * the HandlerBase class has been deprecated in SAX 2.0</i>
  84. *
  85. * @param is InputStream containing the content to be parsed.
  86. * @param hb The SAX HandlerBase to use.
  87. * @param systemId The systemId which is needed for resolving relative URIs.
  88. * @exception IOException If any IO errors occur.
  89. * @exception IllegalArgumentException If the given InputStream is null.
  90. * @exception SAXException If the underlying parser throws a
  91. * SAXException while parsing.
  92. * @see org.xml.sax.DocumentHandler
  93. * version of this method instead.
  94. */
  95. public void parse(InputStream is, HandlerBase hb, String systemId)
  96. throws SAXException, IOException
  97. {
  98. if (is == null) {
  99. throw new IllegalArgumentException("InputStream cannot be null");
  100. }
  101. InputSource input = new InputSource(is);
  102. input.setSystemId(systemId);
  103. this.parse(input, hb);
  104. }
  105. /**
  106. * Parse the content of the given {@link java.io.InputStream}
  107. * instance as XML using the specified
  108. * {@link org.xml.sax.helpers.DefaultHandler}.
  109. *
  110. * @param is InputStream containing the content to be parsed.
  111. * @param dh The SAX DefaultHandler to use.
  112. * @exception IOException If any IO errors occur.
  113. * @exception IllegalArgumentException If the given InputStream is null.
  114. * @exception SAXException If the underlying parser throws a
  115. * SAXException while parsing.
  116. * @see org.xml.sax.DocumentHandler
  117. */
  118. public void parse(InputStream is, DefaultHandler dh)
  119. throws SAXException, IOException
  120. {
  121. if (is == null) {
  122. throw new IllegalArgumentException("InputStream cannot be null");
  123. }
  124. InputSource input = new InputSource(is);
  125. this.parse(input, dh);
  126. }
  127. /**
  128. * Parse the content of the given {@link java.io.InputStream}
  129. * instance as XML using the specified
  130. * {@link org.xml.sax.helpers.DefaultHandler}.
  131. *
  132. * @param is InputStream containing the content to be parsed.
  133. * @param dh The SAX DefaultHandler to use.
  134. * @param systemId The systemId which is needed for resolving relative URIs.
  135. * @exception IOException If any IO errors occur.
  136. * @exception IllegalArgumentException If the given InputStream is null.
  137. * @exception SAXException If the underlying parser throws a
  138. * SAXException while parsing.
  139. * @see org.xml.sax.DocumentHandler
  140. * version of this method instead.
  141. */
  142. public void parse(InputStream is, DefaultHandler dh, String systemId)
  143. throws SAXException, IOException
  144. {
  145. if (is == null) {
  146. throw new IllegalArgumentException("InputStream cannot be null");
  147. }
  148. InputSource input = new InputSource(is);
  149. input.setSystemId(systemId);
  150. this.parse(input, dh);
  151. }
  152. /**
  153. * Parse the content described by the giving Uniform Resource
  154. * Identifier (URI) as XML using the specified
  155. * {@link org.xml.sax.HandlerBase}.
  156. * <i> Use of the DefaultHandler version of this method is recommended as
  157. * the <code>HandlerBase</code> class has been deprecated in SAX 2.0</i>
  158. *
  159. * @param uri The location of the content to be parsed.
  160. * @param hb The SAX HandlerBase to use.
  161. * @exception IOException If any IO errors occur.
  162. * @exception IllegalArgumentException If the uri is null.
  163. * @exception SAXException If the underlying parser throws a
  164. * SAXException while parsing.
  165. * @see org.xml.sax.DocumentHandler
  166. */
  167. public void parse(String uri, HandlerBase hb)
  168. throws SAXException, IOException
  169. {
  170. if (uri == null) {
  171. throw new IllegalArgumentException("uri cannot be null");
  172. }
  173. InputSource input = new InputSource(uri);
  174. this.parse(input, hb);
  175. }
  176. /**
  177. * Parse the content described by the giving Uniform Resource
  178. * Identifier (URI) as XML using the specified
  179. * {@link org.xml.sax.helpers.DefaultHandler}.
  180. *
  181. * @param uri The location of the content to be parsed.
  182. * @param dh The SAX DefaultHandler to use.
  183. * @exception IOException If any IO errors occur.
  184. * @exception IllegalArgumentException If the uri is null.
  185. * @exception SAXException If the underlying parser throws a
  186. * SAXException while parsing.
  187. * @see org.xml.sax.DocumentHandler
  188. */
  189. public void parse(String uri, DefaultHandler dh)
  190. throws SAXException, IOException
  191. {
  192. if (uri == null) {
  193. throw new IllegalArgumentException("uri cannot be null");
  194. }
  195. InputSource input = new InputSource(uri);
  196. this.parse(input, dh);
  197. }
  198. /**
  199. * Parse the content of the file specified as XML using the
  200. * specified {@link org.xml.sax.HandlerBase}.
  201. * <i> Use of the DefaultHandler version of this method is recommended as
  202. * the HandlerBase class has been deprecated in SAX 2.0</i>
  203. *
  204. * @param f The file containing the XML to parse
  205. * @param hb The SAX HandlerBase to use.
  206. * @exception IOException If any IO errors occur.
  207. * @exception IllegalArgumentException If the File object is null.
  208. * @see org.xml.sax.DocumentHandler
  209. * @exception SAXException If the underlying parser throws a
  210. * SAXException while parsing.
  211. */
  212. public void parse(File f, HandlerBase hb)
  213. throws SAXException, IOException
  214. {
  215. if (f == null) {
  216. throw new IllegalArgumentException("File cannot be null");
  217. }
  218. String uri = "file:" + f.getAbsolutePath();
  219. if (File.separatorChar == '\\') {
  220. uri = uri.replace('\\', '/');
  221. }
  222. InputSource input = new InputSource(uri);
  223. this.parse(input, hb);
  224. }
  225. /**
  226. * Parse the content of the file specified as XML using the
  227. * specified {@link org.xml.sax.helpers.DefaultHandler}.
  228. *
  229. * @param f The file containing the XML to parse
  230. * @param dh The SAX DefaultHandler to use.
  231. * @exception IOException If any IO errors occur.
  232. * @exception IllegalArgumentException If the File object is null.
  233. * @exception SAXException If the underlying parser throws a
  234. * SAXException while parsing.
  235. * @see org.xml.sax.DocumentHandler
  236. */
  237. public void parse(File f, DefaultHandler dh)
  238. throws SAXException, IOException
  239. {
  240. if (f == null) {
  241. throw new IllegalArgumentException("File cannot be null");
  242. }
  243. String uri = "file:" + f.getAbsolutePath();
  244. if (File.separatorChar == '\\') {
  245. uri = uri.replace('\\', '/');
  246. }
  247. InputSource input = new InputSource(uri);
  248. this.parse(input, dh);
  249. }
  250. /**
  251. * Parse the content given {@link org.xml.sax.InputSource}
  252. * as XML using the specified
  253. * {@link org.xml.sax.HandlerBase}.
  254. * <i> Use of the DefaultHandler version of this method is recommended as
  255. * the HandlerBase class has been deprecated in SAX 2.0</i>
  256. *
  257. * @param is The InputSource containing the content to be parsed.
  258. * @param hb The SAX HandlerBase to use.
  259. * @exception IOException If any IO errors occur.
  260. * @exception IllegalArgumentException If the InputSource is null.
  261. * @exception SAXException If the underlying parser throws a
  262. * SAXException while parsing.
  263. * @see org.xml.sax.DocumentHandler
  264. */
  265. public void parse(InputSource is, HandlerBase hb)
  266. throws SAXException, IOException
  267. {
  268. if (is == null) {
  269. throw new IllegalArgumentException("InputSource cannot be null");
  270. }
  271. Parser parser = this.getParser();
  272. if (hb != null) {
  273. parser.setDocumentHandler(hb);
  274. parser.setEntityResolver(hb);
  275. parser.setErrorHandler(hb);
  276. parser.setDTDHandler(hb);
  277. }
  278. parser.parse(is);
  279. }
  280. /**
  281. * Parse the content given {@link org.xml.sax.InputSource}
  282. * as XML using the specified
  283. * {@link org.xml.sax.helpers.DefaultHandler}.
  284. *
  285. * @param is The InputSource containing the content to be parsed.
  286. * @param dh The SAX DefaultHandler to use.
  287. * @exception IOException If any IO errors occur.
  288. * @exception IllegalArgumentException If the InputSource is null.
  289. * @exception SAXException If the underlying parser throws a
  290. * SAXException while parsing.
  291. * @see org.xml.sax.DocumentHandler
  292. */
  293. public void parse(InputSource is, DefaultHandler dh)
  294. throws SAXException, IOException
  295. {
  296. if (is == null) {
  297. throw new IllegalArgumentException("InputSource cannot be null");
  298. }
  299. XMLReader reader = this.getXMLReader();
  300. if (dh != null) {
  301. reader.setContentHandler(dh);
  302. reader.setEntityResolver(dh);
  303. reader.setErrorHandler(dh);
  304. reader.setDTDHandler(dh);
  305. }
  306. reader.parse(is);
  307. }
  308. /**
  309. * Returns the SAX parser that is encapsultated by the
  310. * implementation of this class.
  311. *
  312. * @return The SAX parser that is encapsultated by the
  313. * implementation of this class.
  314. */
  315. public abstract org.xml.sax.Parser getParser() throws SAXException;
  316. /**
  317. * Returns the {@link org.xml.sax.XMLReader} that is encapsulated by the
  318. * implementation of this class.
  319. *
  320. * @return The XMLReader that is encapsulated by the
  321. * implementation of this class.
  322. */
  323. public abstract org.xml.sax.XMLReader getXMLReader() throws SAXException;
  324. /**
  325. * Indicates whether or not this parser is configured to
  326. * understand namespaces.
  327. *
  328. * @return true if this parser is configured to
  329. * understand namespaces; false otherwise.
  330. */
  331. public abstract boolean isNamespaceAware();
  332. /**
  333. * Indicates whether or not this parser is configured to
  334. * validate XML documents.
  335. *
  336. * @return true if this parser is configured to
  337. * validate XML documents; false otherwise.
  338. */
  339. public abstract boolean isValidating();
  340. /**
  341. * Sets the particular property in the underlying implementation of
  342. * {@link org.xml.sax.XMLReader}.
  343. * A list of the core features and properties can be found at
  344. * <a href="http://www.megginson.com/SAX/Java/features.html"> http://www.megginson.com/SAX/Java/features.html </a>
  345. *
  346. * @param name The name of the property to be set.
  347. * @param value The value of the property to be set.
  348. * @exception SAXNotRecognizedException When the underlying XMLReader does
  349. * not recognize the property name.
  350. *
  351. * @exception SAXNotSupportedException When the underlying XMLReader
  352. * recognizes the property name but doesn't support the
  353. * property.
  354. *
  355. * @see org.xml.sax.XMLReader#setProperty
  356. */
  357. public abstract void setProperty(String name, Object value)
  358. throws SAXNotRecognizedException, SAXNotSupportedException;
  359. /**
  360. *
  361. * Returns the particular property requested for in the underlying
  362. * implementation of {@link org.xml.sax.XMLReader}.
  363. *
  364. * @param name The name of the property to be retrieved.
  365. * @return Value of the requested property.
  366. *
  367. * @exception SAXNotRecognizedException When the underlying XMLReader does
  368. * not recognize the property name.
  369. *
  370. * @exception SAXNotSupportedException When the underlying XMLReader
  371. * recognizes the property name but doesn't support the
  372. * property.
  373. *
  374. * @see org.xml.sax.XMLReader#getProperty
  375. */
  376. public abstract Object getProperty(String name)
  377. throws SAXNotRecognizedException, SAXNotSupportedException;
  378. }