Path: blob/master/src/java.xml/share/classes/javax/xml/parsers/DocumentBuilder.java
40948 views
/*1* Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.parsers;2627import java.io.File;28import java.io.IOException;29import java.io.InputStream;3031import javax.xml.validation.Schema;3233import org.w3c.dom.Document;34import org.w3c.dom.DOMImplementation;3536import org.xml.sax.EntityResolver;37import org.xml.sax.ErrorHandler;38import org.xml.sax.InputSource;39import org.xml.sax.SAXException;4041/**42* Defines the API to obtain DOM Document instances from an XML43* document. Using this class, an application programmer can obtain a44* {@link Document} from XML.<p>45*46* An instance of this class can be obtained from the47* {@link DocumentBuilderFactory#newDocumentBuilder()} method. Once48* an instance of this class is obtained, XML can be parsed from a49* variety of input sources. These input sources are InputStreams,50* Files, URLs, and SAX InputSources.<p>51*52* Note that this class reuses several classes from the SAX API. This53* does not require that the implementor of the underlying DOM54* implementation use a SAX parser to parse XML document into a55* <code>Document</code>. It merely requires that the implementation56* communicate with the application using these existing APIs.57*58* @author Jeff Suttor59* @since 1.460*/6162public abstract class DocumentBuilder {636465/** Protected constructor */66protected DocumentBuilder () {67}6869/**70* <p>Reset this <code>DocumentBuilder</code> to its original configuration.</p>71*72* <p><code>DocumentBuilder</code> is reset to the same state as when it was created with73* {@link DocumentBuilderFactory#newDocumentBuilder()}.74* <code>reset()</code> is designed to allow the reuse of existing <code>DocumentBuilder</code>s75* thus saving resources associated with the creation of new <code>DocumentBuilder</code>s.</p>76*77* <p>The reset <code>DocumentBuilder</code> is not guaranteed to have the same {@link EntityResolver} or {@link ErrorHandler}78* <code>Object</code>s, e.g. {@link Object#equals(Object obj)}. It is guaranteed to have a functionally equal79* <code>EntityResolver</code> and <code>ErrorHandler</code>.</p>80*81* @throws UnsupportedOperationException When implementation does not82* override this method.83*84* @since 1.585*/86public void reset() {8788// implementors should override this method89throw new UnsupportedOperationException(90"This DocumentBuilder, \"" + this.getClass().getName() + "\", does not support the reset functionality."91+ " Specification \"" + this.getClass().getPackage().getSpecificationTitle() + "\""92+ " version \"" + this.getClass().getPackage().getSpecificationVersion() + "\""93);94}9596/**97* Parse the content of the given <code>InputStream</code> as an XML98* document and return a new DOM {@link Document} object.99* An <code>IllegalArgumentException</code> is thrown if the100* <code>InputStream</code> is null.101*102* @param is InputStream containing the content to be parsed.103*104* @return <code>Document</code> result of parsing the105* <code>InputStream</code>106*107* @throws IOException If any IO errors occur.108* @throws SAXException If any parse errors occur.109* @throws IllegalArgumentException When <code>is</code> is <code>null</code>110*111* @see org.xml.sax.DocumentHandler112*/113114public Document parse(InputStream is)115throws SAXException, IOException {116if (is == null) {117throw new IllegalArgumentException("InputStream cannot be null");118}119120InputSource in = new InputSource(is);121return parse(in);122}123124/**125* Parse the content of the given <code>InputStream</code> as an126* XML document and return a new DOM {@link Document} object.127* An <code>IllegalArgumentException</code> is thrown if the128* <code>InputStream</code> is null.129*130* @param is InputStream containing the content to be parsed.131* @param systemId Provide a base for resolving relative URIs.132*133* @return A new DOM Document object.134*135* @throws IOException If any IO errors occur.136* @throws SAXException If any parse errors occur.137* @throws IllegalArgumentException When <code>is</code> is <code>null</code>138*139* @see org.xml.sax.DocumentHandler140*/141142public Document parse(InputStream is, String systemId)143throws SAXException, IOException {144if (is == null) {145throw new IllegalArgumentException("InputStream cannot be null");146}147148InputSource in = new InputSource(is);149in.setSystemId(systemId);150return parse(in);151}152153/**154* Parse the content of the given URI as an XML document155* and return a new DOM {@link Document} object.156* An <code>IllegalArgumentException</code> is thrown if the157* URI is <code>null</code> null.158*159* @param uri The location of the content to be parsed.160*161* @return A new DOM Document object.162*163* @throws IOException If any IO errors occur.164* @throws SAXException If any parse errors occur.165* @throws IllegalArgumentException When <code>uri</code> is <code>null</code>166*167* @see org.xml.sax.DocumentHandler168*/169170public Document parse(String uri)171throws SAXException, IOException {172if (uri == null) {173throw new IllegalArgumentException("URI cannot be null");174}175176InputSource in = new InputSource(uri);177return parse(in);178}179180/**181* Parse the content of the given file as an XML document182* and return a new DOM {@link Document} object.183* An <code>IllegalArgumentException</code> is thrown if the184* <code>File</code> is <code>null</code> null.185*186* @param f The file containing the XML to parse.187*188* @throws IOException If any IO errors occur.189* @throws SAXException If any parse errors occur.190* @throws IllegalArgumentException When <code>f</code> is <code>null</code>191*192* @see org.xml.sax.DocumentHandler193* @return A new DOM Document object.194*/195196public Document parse(File f) throws SAXException, IOException {197if (f == null) {198throw new IllegalArgumentException("File cannot be null");199}200201//convert file to appropriate URI, f.toURI().toASCIIString()202//converts the URI to string as per rule specified in203//RFC 2396,204InputSource in = new InputSource(f.toURI().toASCIIString());205return parse(in);206}207208/**209* Parse the content of the given input source as an XML document210* and return a new DOM {@link Document} object.211* An <code>IllegalArgumentException</code> is thrown if the212* <code>InputSource</code> is <code>null</code> null.213*214* @param is InputSource containing the content to be parsed.215*216* @return A new DOM Document object.217*218* @throws IOException If any IO errors occur.219* @throws SAXException If any parse errors occur.220* @throws IllegalArgumentException When <code>is</code> is <code>null</code>221*222* @see org.xml.sax.DocumentHandler223*/224225public abstract Document parse(InputSource is)226throws SAXException, IOException;227228229/**230* Indicates whether or not this parser is configured to231* understand namespaces.232*233* @return true if this parser is configured to understand234* namespaces; false otherwise.235*/236237public abstract boolean isNamespaceAware();238239/**240* Indicates whether or not this parser is configured to241* validate XML documents.242*243* @return true if this parser is configured to validate244* XML documents; false otherwise.245*/246247public abstract boolean isValidating();248249/**250* Specify the {@link EntityResolver} to be used to resolve251* entities present in the XML document to be parsed. Setting252* this to <code>null</code> will result in the underlying253* implementation using it's own default implementation and254* behavior.255*256* @param er The <code>EntityResolver</code> to be used to resolve entities257* present in the XML document to be parsed.258*/259260public abstract void setEntityResolver(EntityResolver er);261262/**263* Specify the {@link ErrorHandler} to be used by the parser.264* Setting this to <code>null</code> will result in the underlying265* implementation using it's own default implementation and266* behavior.267*268* @param eh The <code>ErrorHandler</code> to be used by the parser.269*/270271public abstract void setErrorHandler(ErrorHandler eh);272273/**274* Obtain a new instance of a DOM {@link Document} object275* to build a DOM tree with.276*277* @return A new instance of a DOM Document object.278*/279280public abstract Document newDocument();281282/**283* Obtain an instance of a {@link DOMImplementation} object.284*285* @return A new instance of a <code>DOMImplementation</code>.286*/287288public abstract DOMImplementation getDOMImplementation();289290/** <p>Get current state of canonicalization.</p>291*292* @return current state canonicalization control293*/294/*295public boolean getCanonicalization() {296return canonicalState;297}298*/299300/** <p>Get a reference to the the {@link Schema} being used by301* the XML processor.</p>302*303* <p>If no schema is being used, <code>null</code> is returned.</p>304*305* @return {@link Schema} being used or <code>null</code>306* if none in use307*308* @throws UnsupportedOperationException When implementation does not309* override this method310*311* @since 1.5312*/313public Schema getSchema() {314throw new UnsupportedOperationException(315"This parser does not support specification \""316+ this.getClass().getPackage().getSpecificationTitle()317+ "\" version \""318+ this.getClass().getPackage().getSpecificationVersion()319+ "\""320);321}322323324/**325* <p>Get the XInclude processing mode for this parser.</p>326*327* @return328* the return value of329* the {@link DocumentBuilderFactory#isXIncludeAware()}330* when this parser was created from factory.331*332* @throws UnsupportedOperationException When implementation does not333* override this method334*335* @since 1.5336*337* @see DocumentBuilderFactory#setXIncludeAware(boolean)338*/339public boolean isXIncludeAware() {340throw new UnsupportedOperationException(341"This parser does not support specification \""342+ this.getClass().getPackage().getSpecificationTitle()343+ "\" version \""344+ this.getClass().getPackage().getSpecificationVersion()345+ "\""346);347}348}349350351