All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javax.xml.parsers.DocumentBuilder Maven / Gradle / Ivy

Go to download

Xerces2 is the next generation of high performance, fully compliant XML parsers in the Apache Xerces family. This new version of Xerces introduces the Xerces Native Interface (XNI), a complete framework for building parser components and configurations that is extremely modular and easy to program. The Apache Xerces2 parser is the reference implementation of XNI but other parser components, configurations, and parsers can be written using the Xerces Native Interface. For complete design and implementation documents, refer to the XNI Manual. Xerces2 is a fully conforming XML Schema 1.0 processor. A partial experimental implementation of the XML Schema 1.1 Structures and Datatypes Working Drafts (December 2009) and an experimental implementation of the XML Schema Definition Language (XSD): Component Designators (SCD) Candidate Recommendation (January 2010) are provided for evaluation. For more information, refer to the XML Schema page. Xerces2 also provides a complete implementation of the Document Object Model Level 3 Core and Load/Save W3C Recommendations and provides a complete implementation of the XML Inclusions (XInclude) W3C Recommendation. It also provides support for OASIS XML Catalogs v1.1. Xerces2 is able to parse documents written according to the XML 1.1 Recommendation, except that it does not yet provide an option to enable normalization checking as described in section 2.13 of this specification. It also handles namespaces according to the XML Namespaces 1.1 Recommendation, and will correctly serialize XML 1.1 documents if the DOM level 3 load/save APIs are in use.

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// $Id: DocumentBuilder.java 584483 2007-10-14 02:54:48Z mrglavas $

package javax.xml.parsers;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.validation.Schema;

import org.w3c.dom.Document;
import org.w3c.dom.DOMImplementation;

import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Defines the API to obtain DOM Document instances from an XML
 * document. Using this class, an application programmer can obtain a
 * {@link Document} from XML.

* * An instance of this class can be obtained from the * {@link DocumentBuilderFactory#newDocumentBuilder()} method. Once * an instance of this class is obtained, XML can be parsed from a * variety of input sources. These input sources are InputStreams, * Files, URLs, and SAX InputSources.

* * Note that this class reuses several classes from the SAX API. This * does not require that the implementor of the underlying DOM * implementation use a SAX parser to parse XML document into a * Document. It merely requires that the implementation * communicate with the application using these existing APIs. * * @author Jeff Suttor * @version $Revision: 584483 $, $Date: 2007-10-13 22:54:48 -0400 (Sat, 13 Oct 2007) $ */ public abstract class DocumentBuilder { private static final boolean DEBUG = false; /** Protected constructor */ protected DocumentBuilder () { } /** *

Reset this DocumentBuilder to its original configuration.

* *

DocumentBuilder is reset to the same state as when it was created with * {@link DocumentBuilderFactory#newDocumentBuilder()}. * reset() is designed to allow the reuse of existing DocumentBuilders * thus saving resources associated with the creation of new DocumentBuilders.

* *

The reset DocumentBuilder is not guaranteed to have the same {@link EntityResolver} or {@link ErrorHandler} * Objects, e.g. {@link Object#equals(Object obj)}. It is guaranteed to have a functionally equal * EntityResolver and ErrorHandler.

* * @since 1.5 */ public void reset() { // implementors should override this method throw new UnsupportedOperationException( "This DocumentBuilder, \"" + this.getClass().getName() + "\", does not support the reset functionality." + " Specification \"" + this.getClass().getPackage().getSpecificationTitle() + "\"" + " version \"" + this.getClass().getPackage().getSpecificationVersion() + "\"" ); } /** * Parse the content of the given InputStream as an XML * document and return a new DOM {@link Document} object. * An IllegalArgumentException is thrown if the * InputStream is null. * * @param is InputStream containing the content to be parsed. * @return Document result of parsing the * InputStream * @exception IOException If any IO errors occur. * @exception SAXException If any parse errors occur. * @see org.xml.sax.DocumentHandler */ public Document parse(InputStream is) throws SAXException, IOException { if (is == null) { throw new IllegalArgumentException("InputStream cannot be null"); } InputSource in = new InputSource(is); return parse(in); } /** * Parse the content of the given InputStream as an * XML document and return a new DOM {@link Document} object. * An IllegalArgumentException is thrown if the * InputStream is null. * * @param is InputStream containing the content to be parsed. * @param systemId Provide a base for resolving relative URIs. * @return A new DOM Document object. * @exception IOException If any IO errors occur. * @exception SAXException If any parse errors occur. * @see org.xml.sax.DocumentHandler */ public Document parse(InputStream is, String systemId) throws SAXException, IOException { if (is == null) { throw new IllegalArgumentException("InputStream cannot be null"); } InputSource in = new InputSource(is); in.setSystemId(systemId); return parse(in); } /** * Parse the content of the given URI as an XML document * and return a new DOM {@link Document} object. * An IllegalArgumentException is thrown if the * URI is null null. * * @param uri The location of the content to be parsed. * @return A new DOM Document object. * @exception IOException If any IO errors occur. * @exception SAXException If any parse errors occur. * @see org.xml.sax.DocumentHandler */ public Document parse(String uri) throws SAXException, IOException { if (uri == null) { throw new IllegalArgumentException("URI cannot be null"); } InputSource in = new InputSource(uri); return parse(in); } /** * Parse the content of the given file as an XML document * and return a new DOM {@link Document} object. * An IllegalArgumentException is thrown if the * File is null null. * * @param f The file containing the XML to parse. * @exception IOException If any IO errors occur. * @exception SAXException If any parse errors occur. * @see org.xml.sax.DocumentHandler * @return A new DOM Document object. */ public Document parse(File f) throws SAXException, IOException { if (f == null) { throw new IllegalArgumentException("File cannot be null"); } String escapedURI = FilePathToURI.filepath2URI(f.getAbsolutePath()); if (DEBUG) { System.out.println("Escaped URI = " + escapedURI); } InputSource in = new InputSource(escapedURI); return parse(in); } /** * Parse the content of the given input source as an XML document * and return a new DOM {@link Document} object. * An IllegalArgumentException is thrown if the * InputSource is null null. * * @param is InputSource containing the content to be parsed. * @exception IOException If any IO errors occur. * @exception SAXException If any parse errors occur. * @see org.xml.sax.DocumentHandler * @return A new DOM Document object. */ public abstract Document parse(InputSource is) throws SAXException, IOException; /** * Indicates whether or not this parser is configured to * understand namespaces. * * @return true if this parser is configured to understand * namespaces; false otherwise. */ public abstract boolean isNamespaceAware(); /** * Indicates whether or not this parser is configured to * validate XML documents. * * @return true if this parser is configured to validate * XML documents; false otherwise. */ public abstract boolean isValidating(); /** * Specify the {@link EntityResolver} to be used to resolve * entities present in the XML document to be parsed. Setting * this to null will result in the underlying * implementation using it's own default implementation and * behavior. * * @param er The EntityResolver to be used to resolve entities * present in the XML document to be parsed. */ public abstract void setEntityResolver(EntityResolver er); /** * Specify the {@link ErrorHandler} to be used by the parser. * Setting this to null will result in the underlying * implementation using it's own default implementation and * behavior. * * @param eh The ErrorHandler to be used by the parser. */ public abstract void setErrorHandler(ErrorHandler eh); /** * Obtain a new instance of a DOM {@link Document} object * to build a DOM tree with. * * @return A new instance of a DOM Document object. */ public abstract Document newDocument(); /** * Obtain an instance of a {@link DOMImplementation} object. * * @return A new instance of a DOMImplementation. */ public abstract DOMImplementation getDOMImplementation(); /**

Get a reference to the the {@link Schema} being used by * the XML processor.

* *

If no schema is being used, null is returned.

* * @return {@link Schema} being used or null * if none in use * * @throws UnsupportedOperationException * For backward compatibility, when implementations for * earlier versions of JAXP is used, this exception will be * thrown. * * @since 1.5 */ public Schema getSchema() { throw new UnsupportedOperationException( "This parser does not support specification \"" + this.getClass().getPackage().getSpecificationTitle() + "\" version \"" + this.getClass().getPackage().getSpecificationVersion() + "\"" ); } /** *

Get the XInclude processing mode for this parser.

* * @return * the return value of * the {@link DocumentBuilderFactory#isXIncludeAware()} * when this parser was created from factory. * * @throws UnsupportedOperationException * For backward compatibility, when implementations for * earlier versions of JAXP is used, this exception will be * thrown. * * @since 1.5 * * @see DocumentBuilderFactory#setXIncludeAware(boolean) */ public boolean isXIncludeAware() { throw new UnsupportedOperationException( "This parser does not support specification \"" + this.getClass().getPackage().getSpecificationTitle() + "\" version \"" + this.getClass().getPackage().getSpecificationVersion() + "\"" ); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy