Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/validation/ValidatorHandler.java
32285 views
/*1* Copyright (c) 2003, 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*/2425package javax.xml.validation;2627import org.w3c.dom.ls.LSResourceResolver;28import org.xml.sax.ContentHandler;29import org.xml.sax.ErrorHandler;30import org.xml.sax.SAXNotRecognizedException;31import org.xml.sax.SAXNotSupportedException;3233/**34* Streaming validator that works on SAX stream.35*36* <p>37* A {@link ValidatorHandler} object is not thread-safe and not reentrant.38* In other words, it is the application's responsibility to make39* sure that one {@link ValidatorHandler} object is not used from40* more than one thread at any given time.41*42* <p>43* {@link ValidatorHandler} checks if the SAX events follow44* the set of constraints described in the associated {@link Schema},45* and additionally it may modify the SAX events (for example46* by adding default values, etc.)47*48* <p>49* {@link ValidatorHandler} extends from {@link ContentHandler},50* but it refines the underlying {@link ContentHandler} in51* the following way:52* <ol>53* <li>startElement/endElement events must receive non-null String54* for <code>uri</code>, <code>localName</code>, and <code>qname</code>,55* even though SAX allows some of them to be null.56* Similarly, the user-specified {@link ContentHandler} will receive non-null57* Strings for all three parameters.58*59* <li>Applications must ensure that {@link ValidatorHandler}'s60* {@link ContentHandler#startPrefixMapping(String,String)} and61* {@link ContentHandler#endPrefixMapping(String)} are invoked62* properly. Similarly, the user-specified {@link ContentHandler}63* will receive startPrefixMapping/endPrefixMapping events.64* If the {@link ValidatorHandler} introduces additional namespace65* bindings, the user-specified {@link ContentHandler} will receive66* additional startPrefixMapping/endPrefixMapping events.67*68* <li>{@link org.xml.sax.Attributes} for the69* {@link ContentHandler#startElement(String,String,String,Attributes)} method70* may or may not include xmlns* attributes.71* </ol>72*73* <p>74* A {@link ValidatorHandler} is automatically reset every time75* the startDocument method is invoked.76*77* <h2>Recognized Properties and Features</h2>78* <p>79* This spec defines the following feature that must be recognized80* by all {@link ValidatorHandler} implementations.81*82* <h3><code>http://xml.org/sax/features/namespace-prefixes</code></h3>83* <p>84* This feature controls how a {@link ValidatorHandler} introduces85* namespace bindings that were not present in the original SAX event86* stream.87* When this feature is set to true, it must make88* sure that the user's {@link ContentHandler} will see89* the corresponding <code>xmlns*</code> attribute in90* the {@link org.xml.sax.Attributes} object of the91* {@link ContentHandler#startElement(String,String,String,Attributes)}92* callback. Otherwise, <code>xmlns*</code> attributes must not be93* added to {@link org.xml.sax.Attributes} that's passed to the94* user-specified {@link ContentHandler}.95* <p>96* (Note that regardless of this switch, namespace bindings are97* always notified to applications through98* {@link ContentHandler#startPrefixMapping(String,String)} and99* {@link ContentHandler#endPrefixMapping(String)} methods of the100* {@link ContentHandler} specified by the user.)101*102* <p>103* Note that this feature does <em>NOT</em> affect the way104* a {@link ValidatorHandler} receives SAX events. It merely105* changes the way it augments SAX events.106*107* <p>This feature is set to <code>false</code> by default.</p>108*109* @author <a href="mailto:[email protected]">Kohsuke Kawaguchi</a>110* @since 1.5111*/112public abstract class ValidatorHandler implements ContentHandler {113114/**115* <p>Constructor for derived classes.</p>116*117* <p>The constructor does nothing.</p>118*119* <p>Derived classes must create {@link ValidatorHandler} objects that have120* <code>null</code> {@link ErrorHandler} and121* <code>null</code> {@link LSResourceResolver}.</p>122*/123protected ValidatorHandler() {124}125126/**127* Sets the {@link ContentHandler} which receives128* the augmented validation result.129*130* <p>131* When a {@link ContentHandler} is specified, a132* {@link ValidatorHandler} will work as a filter133* and basically copy the incoming events to the134* specified {@link ContentHandler}.135*136* <p>137* In doing so, a {@link ValidatorHandler} may modify138* the events, for example by adding defaulted attributes.139*140* <p>141* A {@link ValidatorHandler} may buffer events to certain142* extent, but to allow {@link ValidatorHandler} to be used143* by a parser, the following requirement has to be met.144*145* <ol>146* <li>When147* {@link ContentHandler#startElement(String, String, String, Attributes)},148* {@link ContentHandler#endElement(String, String, String)},149* {@link ContentHandler#startDocument()}, or150* {@link ContentHandler#endDocument()}151* are invoked on a {@link ValidatorHandler},152* the same method on the user-specified {@link ContentHandler}153* must be invoked for the same event before the callback154* returns.155* <li>{@link ValidatorHandler} may not introduce new elements that156* were not present in the input.157*158* <li>{@link ValidatorHandler} may not remove attributes that were159* present in the input.160* </ol>161*162* <p>163* When a callback method on the specified {@link ContentHandler}164* throws an exception, the same exception object must be thrown165* from the {@link ValidatorHandler}. The {@link ErrorHandler}166* should not be notified of such an exception.167*168* <p>169* This method can be called even during a middle of a validation.170*171* @param receiver172* A {@link ContentHandler} or a null value.173*/174public abstract void setContentHandler(ContentHandler receiver);175176/**177* Gets the {@link ContentHandler} which receives the178* augmented validation result.179*180* @return181* This method returns the object that was last set through182* the {@link #getContentHandler()} method, or null183* if that method has never been called since this {@link ValidatorHandler}184* has created.185*186* @see #setContentHandler(ContentHandler)187*/188public abstract ContentHandler getContentHandler();189190/**191* Sets the {@link ErrorHandler} to receive errors encountered192* during the validation.193*194* <p>195* Error handler can be used to customize the error handling process196* during a validation. When an {@link ErrorHandler} is set,197* errors found during the validation will be first sent198* to the {@link ErrorHandler}.199*200* <p>201* The error handler can abort further validation immediately202* by throwing {@link org.xml.sax.SAXException} from the handler. Or for example203* it can print an error to the screen and try to continue the204* validation by returning normally from the {@link ErrorHandler}205*206* <p>207* If any {@link Throwable} is thrown from an {@link ErrorHandler},208* the same {@link Throwable} object will be thrown toward the209* root of the call stack.210*211* <p>212* {@link ValidatorHandler} is not allowed to213* throw {@link org.xml.sax.SAXException} without first reporting it to214* {@link ErrorHandler}.215*216* <p>217* When the {@link ErrorHandler} is null, the implementation will218* behave as if the following {@link ErrorHandler} is set:219* <pre>220* class DraconianErrorHandler implements {@link ErrorHandler} {221* public void fatalError( {@link org.xml.sax.SAXParseException} e ) throws {@link org.xml.sax.SAXException} {222* throw e;223* }224* public void error( {@link org.xml.sax.SAXParseException} e ) throws {@link org.xml.sax.SAXException} {225* throw e;226* }227* public void warning( {@link org.xml.sax.SAXParseException} e ) throws {@link org.xml.sax.SAXException} {228* // noop229* }230* }231* </pre>232*233* <p>234* When a new {@link ValidatorHandler} object is created, initially235* this field is set to null.236*237* @param errorHandler238* A new error handler to be set. This parameter can be null.239*/240public abstract void setErrorHandler(ErrorHandler errorHandler);241242/**243* Gets the current {@link ErrorHandler} set to this {@link ValidatorHandler}.244*245* @return246* This method returns the object that was last set through247* the {@link #setErrorHandler(ErrorHandler)} method, or null248* if that method has never been called since this {@link ValidatorHandler}249* has created.250*251* @see #setErrorHandler(ErrorHandler)252*/253public abstract ErrorHandler getErrorHandler();254255/**256* Sets the {@link LSResourceResolver} to customize257* resource resolution while in a validation episode.258*259* <p>260* {@link ValidatorHandler} uses a {@link LSResourceResolver}261* when it needs to locate external resources while a validation,262* although exactly what constitutes "locating external resources" is263* up to each schema language.264*265* <p>266* When the {@link LSResourceResolver} is null, the implementation will267* behave as if the following {@link LSResourceResolver} is set:268* <pre>269* class DumbLSResourceResolver implements {@link LSResourceResolver} {270* public {@link org.w3c.dom.ls.LSInput} resolveResource(271* String publicId, String systemId, String baseURI) {272*273* return null; // always return null274* }275* }276* </pre>277*278* <p>279* If a {@link LSResourceResolver} throws a {@link RuntimeException}280* (or instances of its derived classes),281* then the {@link ValidatorHandler} will abort the parsing and282* the caller of the <code>validate</code> method will receive283* the same {@link RuntimeException}.284*285* <p>286* When a new {@link ValidatorHandler} object is created, initially287* this field is set to null.288*289* @param resourceResolver290* A new resource resolver to be set. This parameter can be null.291*/292public abstract void setResourceResolver(LSResourceResolver resourceResolver);293294/**295* Gets the current {@link LSResourceResolver} set to this {@link ValidatorHandler}.296*297* @return298* This method returns the object that was last set through299* the {@link #setResourceResolver(LSResourceResolver)} method, or null300* if that method has never been called since this {@link ValidatorHandler}301* has created.302*303* @see #setErrorHandler(ErrorHandler)304*/305public abstract LSResourceResolver getResourceResolver();306307/**308* Obtains the {@link TypeInfoProvider} implementation of this309* {@link ValidatorHandler}.310*311* <p>312* The obtained {@link TypeInfoProvider} can be queried during a parse313* to access the type information determined by the validator.314*315* <p>316* Some schema languages do not define the notion of type,317* for those languages, this method may not be supported.318* However, to be compliant with this specification, implementations319* for W3C XML Schema 1.0 must support this operation.320*321* @return322* null if the validator / schema language does not support323* the notion of {@link org.w3c.dom.TypeInfo}.324* Otherwise a non-null valid {@link TypeInfoProvider}.325*/326public abstract TypeInfoProvider getTypeInfoProvider();327328329/**330* Look up the value of a feature flag.331*332* <p>The feature name is any fully-qualified URI. It is333* possible for a {@link ValidatorHandler} to recognize a feature name but334* temporarily be unable to return its value.335* Some feature values may be available only in specific336* contexts, such as before, during, or after a validation.337*338* <p>Implementors are free (and encouraged) to invent their own features,339* using names built on their own URIs.</p>340*341* @param name The feature name, which is a non-null fully-qualified URI.342*343* @return The current value of the feature (true or false).344*345* @throws SAXNotRecognizedException If the feature346* value can't be assigned or retrieved.347* @throws SAXNotSupportedException When the348* {@link ValidatorHandler} recognizes the feature name but349* cannot determine its value at this time.350* @throws NullPointerException When <code>name</code> is <code>null</code>.351*352* @see #setFeature(String, boolean)353*/354public boolean getFeature(String name)355throws SAXNotRecognizedException, SAXNotSupportedException {356357if (name == null) {358throw new NullPointerException();359}360361throw new SAXNotRecognizedException(name);362}363364/**365* <p>Set a feature for this <code>ValidatorHandler</code>.</p>366*367* <p>Feature can be used to control the way a368* {@link ValidatorHandler} parses schemas. The feature name is369* any fully-qualified URI. It is possible for a370* {@link SchemaFactory} to371* expose a feature value but to be unable to change the current372* value. Some feature values may be immutable or mutable only in373* specific contexts, such as before, during, or after a374* validation.</p>375*376* <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.377* When the feature is:</p>378* <ul>379* <li>380* <code>true</code>: the implementation will limit XML processing to conform to implementation limits.381* Examples include enity expansion limits and XML Schema constructs that would consume large amounts of resources.382* If XML processing is limited for security reasons, it will be reported via a call to the registered383* {@link ErrorHandler#fatalError(SAXParseException exception)}.384* See {@link #setErrorHandler(ErrorHandler errorHandler)}.385* </li>386* <li>387* <code>false</code>: the implementation will processing XML according to the XML specifications without388* regard to possible implementation limits.389* </li>390* </ul>391*392* @param name The feature name, which is a non-null fully-qualified URI.393* @param value The requested value of the feature (true or false).394*395* @throws SAXNotRecognizedException If the feature396* value can't be assigned or retrieved.397* @throws SAXNotSupportedException When the398* {@link ValidatorHandler} recognizes the feature name but399* cannot set the requested value.400* @throws NullPointerException When <code>name</code> is <code>null</code>.401*402* @see #getFeature(String)403*/404public void setFeature(String name, boolean value)405throws SAXNotRecognizedException, SAXNotSupportedException {406407if (name == null) {408throw new NullPointerException();409}410411throw new SAXNotRecognizedException(name);412}413414/**415* Set the value of a property.416*417* <p>The property name is any fully-qualified URI. It is418* possible for a {@link ValidatorHandler} to recognize a property name but419* to be unable to change the current value.420* Some property values may be immutable or mutable only421* in specific contexts, such as before, during, or after422* a validation.</p>423*424* <p>{@link ValidatorHandler}s are not required to recognize setting425* any specific property names.</p>426*427* @param name The property name, which is a non-null fully-qualified URI.428* @param object The requested value for the property.429*430* @throws SAXNotRecognizedException If the property431* value can't be assigned or retrieved.432* @throws SAXNotSupportedException When the433* {@link ValidatorHandler} recognizes the property name but434* cannot set the requested value.435* @throws NullPointerException When <code>name</code> is <code>null</code>.436*/437public void setProperty(String name, Object object)438throws SAXNotRecognizedException, SAXNotSupportedException {439440if (name == null) {441throw new NullPointerException();442}443444throw new SAXNotRecognizedException(name);445}446447/**448* Look up the value of a property.449*450* <p>The property name is any fully-qualified URI. It is451* possible for a {@link ValidatorHandler} to recognize a property name but452* temporarily be unable to return its value.453* Some property values may be available only in specific454* contexts, such as before, during, or after a validation.</p>455*456* <p>{@link ValidatorHandler}s are not required to recognize any specific457* property names.</p>458*459* <p>Implementors are free (and encouraged) to invent their own properties,460* using names built on their own URIs.</p>461*462* @param name The property name, which is a non-null fully-qualified URI.463*464* @return The current value of the property.465*466* @throws SAXNotRecognizedException If the property467* value can't be assigned or retrieved.468* @throws SAXNotSupportedException When the469* XMLReader recognizes the property name but470* cannot determine its value at this time.471* @throws NullPointerException When <code>name</code> is <code>null</code>.472*473* @see #setProperty(String, Object)474*/475public Object getProperty(String name)476throws SAXNotRecognizedException, SAXNotSupportedException {477478if (name == null) {479throw new NullPointerException();480}481482throw new SAXNotRecognizedException(name);483}484}485486487