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