1. // $Id: DocumentBuilder.java,v 1.25.14.1.2.1 2004/06/28 18:23:44 ndw Exp $
  2. /*
  3. * @(#)DocumentBuilder.java 1.19 04/07/26
  4. *
  5. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  6. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  7. */
  8. package javax.xml.parsers;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import javax.xml.validation.Schema;
  13. import org.w3c.dom.Document;
  14. import org.w3c.dom.DOMImplementation;
  15. import org.xml.sax.EntityResolver;
  16. import org.xml.sax.ErrorHandler;
  17. import org.xml.sax.InputSource;
  18. import org.xml.sax.SAXException;
  19. /**
  20. * Defines the API to obtain DOM Document instances from an XML
  21. * document. Using this class, an application programmer can obtain a
  22. * {@link Document} from XML.<p>
  23. *
  24. * An instance of this class can be obtained from the
  25. * {@link DocumentBuilderFactory#newDocumentBuilder()} method. Once
  26. * an instance of this class is obtained, XML can be parsed from a
  27. * variety of input sources. These input sources are InputStreams,
  28. * Files, URLs, and SAX InputSources.<p>
  29. *
  30. * Note that this class reuses several classes from the SAX API. This
  31. * does not require that the implementor of the underlying DOM
  32. * implementation use a SAX parser to parse XML document into a
  33. * <code>Document</code>. It merely requires that the implementation
  34. * communicate with the application using these existing APIs.
  35. *
  36. * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
  37. * @version $Revision: 1.25.14.1.2.1 $, $Date: 2004/06/28 18:23:44 $
  38. */
  39. public abstract class DocumentBuilder {
  40. /** Protected constructor */
  41. protected DocumentBuilder () {
  42. }
  43. /**
  44. * <p>Reset this <code>DocumentBuilder</code> to its original configuration.</p>
  45. *
  46. * <p><code>DocumentBuilder</code> is reset to the same state as when it was created with
  47. * {@link DocumentBuilderFactory#newDocumentBuilder()}.
  48. * <code>reset()</code> is designed to allow the reuse of existing <code>DocumentBuilder</code>s
  49. * thus saving resources associated with the creation of new <code>DocumentBuilder</code>s.</p>
  50. *
  51. * <p>The reset <code>DocumentBuilder</code> is not guaranteed to have the same {@link EntityResolver} or {@link ErrorHandler}
  52. * <code>Object</code>s, e.g. {@link Object#equals(Object obj)}. It is guaranteed to have a functionally equal
  53. * <code>EntityResolver</code> and <code>ErrorHandler</code>.</p>
  54. *
  55. * @since 1.5
  56. */
  57. public void reset() {
  58. // implementors should override this method
  59. throw new UnsupportedOperationException(
  60. "This DocumentBuilder, \"" + this.getClass().getName() + "\", does not support the reset functionality."
  61. + " Specification \"" + this.getClass().getPackage().getSpecificationTitle() + "\""
  62. + " version \"" + this.getClass().getPackage().getSpecificationVersion() + "\""
  63. );
  64. }
  65. /**
  66. * Parse the content of the given <code>InputStream</code> as an XML
  67. * document and return a new DOM {@link Document} object.
  68. * An <code>IllegalArgumentException</code> is thrown if the
  69. * <code>InputStream</code> is null.
  70. *
  71. * @param is InputStream containing the content to be parsed.
  72. * @return <code>Document</code> result of parsing the
  73. * <code>InputStream</code>
  74. * @exception IOException If any IO errors occur.
  75. * @exception SAXException If any parse errors occur.
  76. * @see org.xml.sax.DocumentHandler
  77. */
  78. public Document parse(InputStream is)
  79. throws SAXException, IOException {
  80. if (is == null) {
  81. throw new IllegalArgumentException("InputStream cannot be null");
  82. }
  83. InputSource in = new InputSource(is);
  84. return parse(in);
  85. }
  86. /**
  87. * Parse the content of the given <code>InputStream</code> as an
  88. * XML document and return a new DOM {@link Document} object.
  89. * An <code>IllegalArgumentException</code> is thrown if the
  90. * <code>InputStream</code> is null.
  91. *
  92. * @param is InputStream containing the content to be parsed.
  93. * @param systemId Provide a base for resolving relative URIs.
  94. * @return A new DOM Document object.
  95. * @exception IOException If any IO errors occur.
  96. * @exception SAXException If any parse errors occur.
  97. * @see org.xml.sax.DocumentHandler
  98. */
  99. public Document parse(InputStream is, String systemId)
  100. throws SAXException, IOException {
  101. if (is == null) {
  102. throw new IllegalArgumentException("InputStream cannot be null");
  103. }
  104. InputSource in = new InputSource(is);
  105. in.setSystemId(systemId);
  106. return parse(in);
  107. }
  108. /**
  109. * Parse the content of the given URI as an XML document
  110. * and return a new DOM {@link Document} object.
  111. * An <code>IllegalArgumentException</code> is thrown if the
  112. * URI is <code>null</code> null.
  113. *
  114. * @param uri The location of the content to be parsed.
  115. * @return A new DOM Document object.
  116. * @exception IOException If any IO errors occur.
  117. * @exception SAXException If any parse errors occur.
  118. * @see org.xml.sax.DocumentHandler
  119. */
  120. public Document parse(String uri)
  121. throws SAXException, IOException {
  122. if (uri == null) {
  123. throw new IllegalArgumentException("URI cannot be null");
  124. }
  125. InputSource in = new InputSource(uri);
  126. return parse(in);
  127. }
  128. /**
  129. * Parse the content of the given file as an XML document
  130. * and return a new DOM {@link Document} object.
  131. * An <code>IllegalArgumentException</code> is thrown if the
  132. * <code>File</code> is <code>null</code> null.
  133. *
  134. * @param f The file containing the XML to parse.
  135. * @exception IOException If any IO errors occur.
  136. * @exception SAXException If any parse errors occur.
  137. * @see org.xml.sax.DocumentHandler
  138. * @return A new DOM Document object.
  139. */
  140. public Document parse(File f) throws SAXException, IOException {
  141. if (f == null) {
  142. throw new IllegalArgumentException("File cannot be null");
  143. }
  144. String uri = "file:" + f.getAbsolutePath();
  145. if (File.separatorChar == '\\') {
  146. uri = uri.replace('\\', '/');
  147. }
  148. InputSource in = new InputSource(uri);
  149. return parse(in);
  150. }
  151. /**
  152. * Parse the content of the given input source as an XML document
  153. * and return a new DOM {@link Document} object.
  154. * An <code>IllegalArgumentException</code> is thrown if the
  155. * <code>InputSource</code> is <code>null</code> null.
  156. *
  157. * @param is InputSource containing the content to be parsed.
  158. * @exception IOException If any IO errors occur.
  159. * @exception SAXException If any parse errors occur.
  160. * @see org.xml.sax.DocumentHandler
  161. * @return A new DOM Document object.
  162. */
  163. public abstract Document parse(InputSource is)
  164. throws SAXException, IOException;
  165. /**
  166. * Indicates whether or not this parser is configured to
  167. * understand namespaces.
  168. *
  169. * @return true if this parser is configured to understand
  170. * namespaces; false otherwise.
  171. */
  172. public abstract boolean isNamespaceAware();
  173. /**
  174. * Indicates whether or not this parser is configured to
  175. * validate XML documents.
  176. *
  177. * @return true if this parser is configured to validate
  178. * XML documents; false otherwise.
  179. */
  180. public abstract boolean isValidating();
  181. /**
  182. * Specify the {@link EntityResolver} to be used to resolve
  183. * entities present in the XML document to be parsed. Setting
  184. * this to <code>null</code> will result in the underlying
  185. * implementation using it's own default implementation and
  186. * behavior.
  187. *
  188. * @param er The <code>EntityResolver</code> to be used to resolve entities
  189. * present in the XML document to be parsed.
  190. */
  191. public abstract void setEntityResolver(EntityResolver er);
  192. /**
  193. * Specify the {@link ErrorHandler} to be used by the parser.
  194. * Setting this to <code>null</code> will result in the underlying
  195. * implementation using it's own default implementation and
  196. * behavior.
  197. *
  198. * @param eh The <code>ErrorHandler</code> to be used by the parser.
  199. */
  200. public abstract void setErrorHandler(ErrorHandler eh);
  201. /**
  202. * Obtain a new instance of a DOM {@link Document} object
  203. * to build a DOM tree with.
  204. *
  205. * @return A new instance of a DOM Document object.
  206. */
  207. public abstract Document newDocument();
  208. /**
  209. * Obtain an instance of a {@link DOMImplementation} object.
  210. *
  211. * @return A new instance of a <code>DOMImplementation</code>.
  212. */
  213. public abstract DOMImplementation getDOMImplementation();
  214. /** <p>Get current state of canonicalization.</p>
  215. *
  216. * @return current state canonicalization control
  217. */
  218. /*
  219. public boolean getCanonicalization() {
  220. return canonicalState;
  221. }
  222. */
  223. /** <p>Get a reference to the the {@link Schema} being used by
  224. * the XML processor.</p>
  225. *
  226. * <p>If no schema is being used, <code>null</code> is returned.</p>
  227. *
  228. * @return {@link Schema} being used or <code>null</code>
  229. * if none in use
  230. *
  231. * @throws UnsupportedOperationException
  232. * For backward compatibility, when implementations for
  233. * earlier versions of JAXP is used, this exception will be
  234. * thrown.
  235. *
  236. * @since 1.5
  237. */
  238. public Schema getSchema() {
  239. throw new UnsupportedOperationException(
  240. "This parser does not support specification \""
  241. + this.getClass().getPackage().getSpecificationTitle()
  242. + "\" version \""
  243. + this.getClass().getPackage().getSpecificationVersion()
  244. + "\""
  245. );
  246. }
  247. /**
  248. * <p>Get the XInclude processing mode for this parser.</p>
  249. *
  250. * @return
  251. * the return value of
  252. * the {@link DocumentBuilderFactory#isXIncludeAware()}
  253. * when this parser was created from factory.
  254. *
  255. * @throws UnsupportedOperationException
  256. * For backward compatibility, when implementations for
  257. * earlier versions of JAXP is used, this exception will be
  258. * thrown.
  259. *
  260. * @since 1.5
  261. *
  262. * @see DocumentBuilderFactory#setXIncludeAware(boolean)
  263. */
  264. public boolean isXIncludeAware() {
  265. throw new UnsupportedOperationException(
  266. "This parser does not support specification \""
  267. + this.getClass().getPackage().getSpecificationTitle()
  268. + "\" version \""
  269. + this.getClass().getPackage().getSpecificationVersion()
  270. + "\""
  271. );
  272. }
  273. }