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.HandlerBase;
  11. import org.xml.sax.InputSource;
  12. import org.xml.sax.SAXException;
  13. import org.w3c.dom.Document;
  14. import org.w3c.dom.DOMImplementation;
  15. /**
  16. * Defines the API to obtain DOM Document instances from an XML
  17. * document. Using this class, an application programmer can obtain a
  18. * {@link org.w3c.dom.Document} from XML.<p>
  19. *
  20. * An instance of this class can be obtained from the
  21. * {@link javax.xml.parsers.DocumentBuilderFactory#newDocumentBuilder()
  22. * DocumentBuilderFactory.newDocumentBuilder} method. Once
  23. * an instance of this class is obtained, XML can be parsed from a
  24. * variety of input sources. These input sources are InputStreams,
  25. * Files, URLs, and SAX InputSources.<p>
  26. *
  27. * Note that this class reuses several classes from the SAX API. This
  28. * does not require that the implementor of the underlying DOM
  29. * implementation use a SAX parser to parse XML document into a
  30. * <code>Document</code>. It merely requires that the implementation
  31. * communicate with the application using these existing APIs. <p>
  32. *
  33. * An implementation of <code>DocumentBuilder</code> is <em>NOT</em>
  34. * guaranteed to behave as per the specification if it is used concurrently by
  35. * two or more threads. It is recommended to have one instance of the
  36. * <code>DocumentBuilder</code> per thread or it is upto the application to
  37. * make sure about the use of <code>DocumentBuilder</code> from more than one
  38. * thread.
  39. *
  40. * @since JAXP 1.0
  41. * @version 1.0
  42. */
  43. public abstract class DocumentBuilder {
  44. protected DocumentBuilder () {
  45. }
  46. /**
  47. * Parse the content of the given <code>InputStream</code> as an XML
  48. * document and return a new DOM {@link org.w3c.dom.Document} object.
  49. *
  50. * @param is InputStream containing the content to be parsed.
  51. * @exception IOException If any IO errors occur.
  52. * @exception SAXException If any parse errors occur.
  53. * @exception IllegalArgumentException If the InputStream is null
  54. * @see org.xml.sax.DocumentHandler
  55. */
  56. public Document parse(InputStream is)
  57. throws SAXException, IOException
  58. {
  59. if (is == null) {
  60. throw new IllegalArgumentException("InputStream cannot be null");
  61. }
  62. InputSource in = new InputSource(is);
  63. return parse(in);
  64. }
  65. /**
  66. * Parse the content of the given <code>InputStream</code> as an XML
  67. * document and return a new DOM {@link org.w3c.dom.Document} object.
  68. *
  69. * @param is InputStream containing the content to be parsed.
  70. * @param systemId Provide a base for resolving relative URIs.
  71. * @exception IOException If any IO errors occur.
  72. * @exception SAXException If any parse errors occur.
  73. * @exception IllegalArgumentException If the InputStream is null.
  74. * @see org.xml.sax.DocumentHandler
  75. * @return A new DOM Document object.
  76. */
  77. public Document parse(InputStream is, String systemId)
  78. throws SAXException, IOException
  79. {
  80. if (is == null) {
  81. throw new IllegalArgumentException("InputStream cannot be null");
  82. }
  83. InputSource in = new InputSource(is);
  84. in.setSystemId(systemId);
  85. return parse(in);
  86. }
  87. /**
  88. * Parse the content of the given URI as an XML document
  89. * and return a new DOM {@link org.w3c.dom.Document} object.
  90. *
  91. * @param uri The location of the content to be parsed.
  92. * @exception IOException If any IO errors occur.
  93. * @exception SAXException If any parse errors occur.
  94. * @exception IllegalArgumentException If the URI is null.
  95. * @see org.xml.sax.DocumentHandler
  96. * @return A new DOM Document object.
  97. */
  98. public Document parse(String uri)
  99. throws SAXException, IOException
  100. {
  101. if (uri == null) {
  102. throw new IllegalArgumentException("URI cannot be null");
  103. }
  104. InputSource in = new InputSource(uri);
  105. return parse(in);
  106. }
  107. /**
  108. * Parse the content of the given file as an XML document
  109. * and return a new DOM {@link org.w3c.dom.Document} object.
  110. *
  111. * @param f The file containing the XML to parse.
  112. * @exception IOException If any IO errors occur.
  113. * @exception SAXException If any parse errors occur.
  114. * @exception IllegalArgumentException If the file is null.
  115. * @see org.xml.sax.DocumentHandler
  116. * @return A new DOM Document object.
  117. */
  118. public Document parse(File f)
  119. throws SAXException, IOException
  120. {
  121. if (f == null) {
  122. throw new IllegalArgumentException("File cannot be null");
  123. }
  124. String uri = "file:" + f.getAbsolutePath();
  125. if (File.separatorChar == '\\') {
  126. uri = uri.replace('\\', '/');
  127. }
  128. InputSource in = new InputSource(uri);
  129. return parse(in);
  130. }
  131. /**
  132. * Parse the content of the given input source as an XML document
  133. * and return a new DOM {@link org.w3c.dom.Document} object.
  134. *
  135. * @param is InputSource containing the content to be parsed.
  136. * @exception IOException If any IO errors occur.
  137. * @exception SAXException If any parse errors occur.
  138. * @exception IllegalArgumentException If the InputSource is null.
  139. * @see org.xml.sax.DocumentHandler
  140. * @return A new DOM Document object.
  141. */
  142. public abstract Document parse(InputSource is)
  143. throws SAXException, IOException;
  144. /**
  145. * Indicates whether or not this parser is configured to
  146. * understand namespaces.
  147. *
  148. * @return true if this parser is configured to understand
  149. * namespaces; false otherwise.
  150. */
  151. public abstract boolean isNamespaceAware();
  152. /**
  153. * Indicates whether or not this parser is configured to
  154. * validate XML documents.
  155. *
  156. * @return true if this parser is configured to validate
  157. * XML documents; false otherwise.
  158. */
  159. public abstract boolean isValidating();
  160. /**
  161. * Specify the {@link org.xml.sax.EntityResolver} to be used to resolve
  162. * entities present in the XML document to be parsed. Setting
  163. * this to <code>null</code> will result in the underlying
  164. * implementation using it's own default implementation and
  165. * behavior.
  166. *
  167. * @param er The <code>EntityResolver</code> to be used to resolve entities
  168. * present in the XML document to be parsed.
  169. */
  170. public abstract void setEntityResolver(org.xml.sax.EntityResolver er);
  171. /**
  172. * Specify the {@link org.xml.sax.ErrorHandler} to be used to report
  173. * errors present in the XML document to be parsed. Setting
  174. * this to <code>null</code> will result in the underlying
  175. * implementation using it's own default implementation and
  176. * behavior.
  177. *
  178. * @param eh The <code>ErrorHandler</code> to be used to report errors
  179. * present in the XML document to be parsed.
  180. */
  181. public abstract void setErrorHandler(org.xml.sax.ErrorHandler eh);
  182. /**
  183. * Obtain a new instance of a DOM {@link org.w3c.dom.Document} object
  184. * to build a DOM tree with. An alternative way to create a DOM
  185. * Document object is to use the
  186. * {@link #getDOMImplementation() getDOMImplementation}
  187. * method to get a DOM Level 2 DOMImplementation object and then use
  188. * DOM Level 2 methods on that object to create a DOM Document object.
  189. *
  190. * @return A new instance of a DOM Document object.
  191. */
  192. public abstract Document newDocument();
  193. /**
  194. * Obtain an instance of a {@link org.w3c.dom.DOMImplementation} object.
  195. *
  196. * @return A new instance of a <code>DOMImplementation</code>.
  197. */
  198. public abstract DOMImplementation getDOMImplementation();
  199. }