Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/org/xml/sax/XMLReader.java
48534 views
/*1* Copyright (c) 2000, 2005, 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*/2425// XMLReader.java - read an XML document.26// http://www.saxproject.org27// Written by David Megginson28// NO WARRANTY! This class is in the Public Domain.29// $Id: XMLReader.java,v 1.3 2004/11/03 22:55:32 jsuttor Exp $3031package org.xml.sax;3233import java.io.IOException;343536/**37* Interface for reading an XML document using callbacks.38*39* <blockquote>40* <em>This module, both source code and documentation, is in the41* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>42* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>43* for further information.44* </blockquote>45*46* <p><strong>Note:</strong> despite its name, this interface does47* <em>not</em> extend the standard Java {@link java.io.Reader Reader}48* interface, because reading XML is a fundamentally different activity49* than reading character data.</p>50*51* <p>XMLReader is the interface that an XML parser's SAX2 driver must52* implement. This interface allows an application to set and53* query features and properties in the parser, to register54* event handlers for document processing, and to initiate55* a document parse.</p>56*57* <p>All SAX interfaces are assumed to be synchronous: the58* {@link #parse parse} methods must not return until parsing59* is complete, and readers must wait for an event-handler callback60* to return before reporting the next event.</p>61*62* <p>This interface replaces the (now deprecated) SAX 1.0 {@link63* org.xml.sax.Parser Parser} interface. The XMLReader interface64* contains two important enhancements over the old Parser65* interface (as well as some minor ones):</p>66*67* <ol>68* <li>it adds a standard way to query and set features and69* properties; and</li>70* <li>it adds Namespace support, which is required for many71* higher-level XML standards.</li>72* </ol>73*74* <p>There are adapters available to convert a SAX1 Parser to75* a SAX2 XMLReader and vice-versa.</p>76*77* @since SAX 2.078* @author David Megginson79* @see org.xml.sax.XMLFilter80* @see org.xml.sax.helpers.ParserAdapter81* @see org.xml.sax.helpers.XMLReaderAdapter82*/83public interface XMLReader84{858687////////////////////////////////////////////////////////////////////88// Configuration.89////////////////////////////////////////////////////////////////////909192/**93* Look up the value of a feature flag.94*95* <p>The feature name is any fully-qualified URI. It is96* possible for an XMLReader to recognize a feature name but97* temporarily be unable to return its value.98* Some feature values may be available only in specific99* contexts, such as before, during, or after a parse.100* Also, some feature values may not be programmatically accessible.101* (In the case of an adapter for SAX1 {@link Parser}, there is no102* implementation-independent way to expose whether the underlying103* parser is performing validation, expanding external entities,104* and so forth.) </p>105*106* <p>All XMLReaders are required to recognize the107* http://xml.org/sax/features/namespaces and the108* http://xml.org/sax/features/namespace-prefixes feature names.</p>109*110* <p>Typical usage is something like this:</p>111*112* <pre>113* XMLReader r = new MySAXDriver();114*115* // try to activate validation116* try {117* r.setFeature("http://xml.org/sax/features/validation", true);118* } catch (SAXException e) {119* System.err.println("Cannot activate validation.");120* }121*122* // register event handlers123* r.setContentHandler(new MyContentHandler());124* r.setErrorHandler(new MyErrorHandler());125*126* // parse the first document127* try {128* r.parse("http://www.foo.com/mydoc.xml");129* } catch (IOException e) {130* System.err.println("I/O exception reading XML document");131* } catch (SAXException e) {132* System.err.println("XML exception reading document.");133* }134* </pre>135*136* <p>Implementors are free (and encouraged) to invent their own features,137* using names built on their own URIs.</p>138*139* @param name The feature name, which is a fully-qualified URI.140* @return The current value of the feature (true or false).141* @exception org.xml.sax.SAXNotRecognizedException If the feature142* value can't be assigned or retrieved.143* @exception org.xml.sax.SAXNotSupportedException When the144* XMLReader recognizes the feature name but145* cannot determine its value at this time.146* @see #setFeature147*/148public boolean getFeature (String name)149throws SAXNotRecognizedException, SAXNotSupportedException;150151152/**153* Set the value of a feature flag.154*155* <p>The feature name is any fully-qualified URI. It is156* possible for an XMLReader to expose a feature value but157* to be unable to change the current value.158* Some feature values may be immutable or mutable only159* in specific contexts, such as before, during, or after160* a parse.</p>161*162* <p>All XMLReaders are required to support setting163* http://xml.org/sax/features/namespaces to true and164* http://xml.org/sax/features/namespace-prefixes to false.</p>165*166* @param name The feature name, which is a fully-qualified URI.167* @param value The requested value of the feature (true or false).168* @exception org.xml.sax.SAXNotRecognizedException If the feature169* value can't be assigned or retrieved.170* @exception org.xml.sax.SAXNotSupportedException When the171* XMLReader recognizes the feature name but172* cannot set the requested value.173* @see #getFeature174*/175public void setFeature (String name, boolean value)176throws SAXNotRecognizedException, SAXNotSupportedException;177178179/**180* Look up the value of a property.181*182* <p>The property name is any fully-qualified URI. It is183* possible for an XMLReader to recognize a property name but184* temporarily be unable to return its value.185* Some property values may be available only in specific186* contexts, such as before, during, or after a parse.</p>187*188* <p>XMLReaders are not required to recognize any specific189* property names, though an initial core set is documented for190* SAX2.</p>191*192* <p>Implementors are free (and encouraged) to invent their own properties,193* using names built on their own URIs.</p>194*195* @param name The property name, which is a fully-qualified URI.196* @return The current value of the property.197* @exception org.xml.sax.SAXNotRecognizedException If the property198* value can't be assigned or retrieved.199* @exception org.xml.sax.SAXNotSupportedException When the200* XMLReader recognizes the property name but201* cannot determine its value at this time.202* @see #setProperty203*/204public Object getProperty (String name)205throws SAXNotRecognizedException, SAXNotSupportedException;206207208/**209* Set the value of a property.210*211* <p>The property name is any fully-qualified URI. It is212* possible for an XMLReader to recognize a property name but213* to be unable to change the current value.214* Some property values may be immutable or mutable only215* in specific contexts, such as before, during, or after216* a parse.</p>217*218* <p>XMLReaders are not required to recognize setting219* any specific property names, though a core set is defined by220* SAX2.</p>221*222* <p>This method is also the standard mechanism for setting223* extended handlers.</p>224*225* @param name The property name, which is a fully-qualified URI.226* @param value The requested value for the property.227* @exception org.xml.sax.SAXNotRecognizedException If the property228* value can't be assigned or retrieved.229* @exception org.xml.sax.SAXNotSupportedException When the230* XMLReader recognizes the property name but231* cannot set the requested value.232*/233public void setProperty (String name, Object value)234throws SAXNotRecognizedException, SAXNotSupportedException;235236237238////////////////////////////////////////////////////////////////////239// Event handlers.240////////////////////////////////////////////////////////////////////241242243/**244* Allow an application to register an entity resolver.245*246* <p>If the application does not register an entity resolver,247* the XMLReader will perform its own default resolution.</p>248*249* <p>Applications may register a new or different resolver in the250* middle of a parse, and the SAX parser must begin using the new251* resolver immediately.</p>252*253* @param resolver The entity resolver.254* @see #getEntityResolver255*/256public void setEntityResolver (EntityResolver resolver);257258259/**260* Return the current entity resolver.261*262* @return The current entity resolver, or null if none263* has been registered.264* @see #setEntityResolver265*/266public EntityResolver getEntityResolver ();267268269/**270* Allow an application to register a DTD event handler.271*272* <p>If the application does not register a DTD handler, all DTD273* events reported by the SAX parser will be silently ignored.</p>274*275* <p>Applications may register a new or different handler in the276* middle of a parse, and the SAX parser must begin using the new277* handler immediately.</p>278*279* @param handler The DTD handler.280* @see #getDTDHandler281*/282public void setDTDHandler (DTDHandler handler);283284285/**286* Return the current DTD handler.287*288* @return The current DTD handler, or null if none289* has been registered.290* @see #setDTDHandler291*/292public DTDHandler getDTDHandler ();293294295/**296* Allow an application to register a content event handler.297*298* <p>If the application does not register a content handler, all299* content events reported by the SAX parser will be silently300* ignored.</p>301*302* <p>Applications may register a new or different handler in the303* middle of a parse, and the SAX parser must begin using the new304* handler immediately.</p>305*306* @param handler The content handler.307* @see #getContentHandler308*/309public void setContentHandler (ContentHandler handler);310311312/**313* Return the current content handler.314*315* @return The current content handler, or null if none316* has been registered.317* @see #setContentHandler318*/319public ContentHandler getContentHandler ();320321322/**323* Allow an application to register an error event handler.324*325* <p>If the application does not register an error handler, all326* error events reported by the SAX parser will be silently327* ignored; however, normal processing may not continue. It is328* highly recommended that all SAX applications implement an329* error handler to avoid unexpected bugs.</p>330*331* <p>Applications may register a new or different handler in the332* middle of a parse, and the SAX parser must begin using the new333* handler immediately.</p>334*335* @param handler The error handler.336* @see #getErrorHandler337*/338public void setErrorHandler (ErrorHandler handler);339340341/**342* Return the current error handler.343*344* @return The current error handler, or null if none345* has been registered.346* @see #setErrorHandler347*/348public ErrorHandler getErrorHandler ();349350351352////////////////////////////////////////////////////////////////////353// Parsing.354////////////////////////////////////////////////////////////////////355356/**357* Parse an XML document.358*359* <p>The application can use this method to instruct the XML360* reader to begin parsing an XML document from any valid input361* source (a character stream, a byte stream, or a URI).</p>362*363* <p>Applications may not invoke this method while a parse is in364* progress (they should create a new XMLReader instead for each365* nested XML document). Once a parse is complete, an366* application may reuse the same XMLReader object, possibly with a367* different input source.368* Configuration of the XMLReader object (such as handler bindings and369* values established for feature flags and properties) is unchanged370* by completion of a parse, unless the definition of that aspect of371* the configuration explicitly specifies other behavior.372* (For example, feature flags or properties exposing373* characteristics of the document being parsed.)374* </p>375*376* <p>During the parse, the XMLReader will provide information377* about the XML document through the registered event378* handlers.</p>379*380* <p>This method is synchronous: it will not return until parsing381* has ended. If a client application wants to terminate382* parsing early, it should throw an exception.</p>383*384* @param input The input source for the top-level of the385* XML document.386* @exception org.xml.sax.SAXException Any SAX exception, possibly387* wrapping another exception.388* @exception java.io.IOException An IO exception from the parser,389* possibly from a byte stream or character stream390* supplied by the application.391* @see org.xml.sax.InputSource392* @see #parse(java.lang.String)393* @see #setEntityResolver394* @see #setDTDHandler395* @see #setContentHandler396* @see #setErrorHandler397*/398public void parse (InputSource input)399throws IOException, SAXException;400401402/**403* Parse an XML document from a system identifier (URI).404*405* <p>This method is a shortcut for the common case of reading a406* document from a system identifier. It is the exact407* equivalent of the following:</p>408*409* <pre>410* parse(new InputSource(systemId));411* </pre>412*413* <p>If the system identifier is a URL, it must be fully resolved414* by the application before it is passed to the parser.</p>415*416* @param systemId The system identifier (URI).417* @exception org.xml.sax.SAXException Any SAX exception, possibly418* wrapping another exception.419* @exception java.io.IOException An IO exception from the parser,420* possibly from a byte stream or character stream421* supplied by the application.422* @see #parse(org.xml.sax.InputSource)423*/424public void parse (String systemId)425throws IOException, SAXException;426427}428429430