Path: blob/master/src/java.xml/share/classes/javax/xml/validation/Validator.java
40948 views
/*1* Copyright (c) 2003, 2017, 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 java.io.IOException;2829import javax.xml.transform.Result;30import javax.xml.transform.Source;3132import org.w3c.dom.ls.LSResourceResolver;33import org.xml.sax.ErrorHandler;34import org.xml.sax.SAXException;35import org.xml.sax.SAXNotRecognizedException;36import org.xml.sax.SAXNotSupportedException;3738/**39* A processor that checks an XML document against {@link Schema}.40*41* <p>42* A validator object is not thread-safe and not reentrant.43* In other words, it is the application's responsibility to make44* sure that one {@link Validator} object is not used from45* more than one thread at any given time, and while the {@code validate}46* method is invoked, applications may not recursively call47* the {@code validate} method.48*49*50* @author Kohsuke Kawaguchi51* @since 1.552*/53public abstract class Validator {5455/**56* Constructor for derived classes.57*58* <p>The constructor does nothing.59*60* <p>Derived classes must create {@link Validator} objects that have61* {@code null} {@link ErrorHandler} and62* {@code null} {@link LSResourceResolver}.63*/64protected Validator() {65}6667/**68* Reset this {@code Validator} to its original configuration.69*70* <p>{@code Validator} is reset to the same state as when it was created with71* {@link Schema#newValidator()}.72* {@code reset()} is designed to allow the reuse of existing {@code Validator}s73* thus saving resources associated with the creation of new {@code Validator}s.74*75* <p>The reset {@code Validator} is not guaranteed to have76* the same {@link LSResourceResolver} or {@link ErrorHandler}77* {@code Object}s, e.g. {@link Object#equals(Object obj)}.78* It is guaranteed to have a functionally equal79* {@code LSResourceResolver} and {@code ErrorHandler}.80*/81public abstract void reset();8283/**84* Validates the specified input.85*86* <p>This is just a convenience method for87* {@link #validate(Source source, Result result)}88* with {@code result} of {@code null}.89*90* @param source91* XML to be validated. Must be an XML document or92* XML element and must not be null. For backwards compatibility,93* the results of attempting to validate anything other than94* a document or element are implementation-dependent.95* Implementations must either recognize and process the input96* or throw an IllegalArgumentException.97*98* @throws IllegalArgumentException99* If the {@code Source}100* is an XML artifact that the implementation cannot101* validate (for example, a processing instruction).102*103* @throws SAXException104* If the {@link ErrorHandler} throws a {@link SAXException} or105* if a fatal error is found and the {@link ErrorHandler} returns106* normally.107*108* @throws IOException109* If the validator is processing a110* {@link javax.xml.transform.sax.SAXSource} and the111* underlying {@link org.xml.sax.XMLReader} throws an112* {@link IOException}.113*114*115* @throws NullPointerException If {@code source} is116* {@code null}.117*118* @see #validate(Source source, Result result)119*/120public void validate(Source source)121throws SAXException, IOException {122123validate(source, null);124}125126/**127* Validates the specified input and send the augmented validation128* result to the specified output.129*130* <p>This method places the following restrictions on the types of131* the {@link Source}/{@link Result} accepted.132*133* <table class="plain">134* <caption>{@code Source} / {@code Result} Accepted</caption>135* <thead>136* <tr>137* <td></td>138* <th scope="col">{@link javax.xml.transform.stream.StreamSource}</th>139* <th scope="col">{@link javax.xml.transform.sax.SAXSource}</th>140* <th scope="col">{@link javax.xml.transform.dom.DOMSource}</th>141* <th scope="col">{@link javax.xml.transform.stax.StAXSource}</th>142* </tr>143* </thead>144* <tbody style="text-align:center">145* <tr>146* <th scope="row">{@code null}</th>147* <td>OK</td>148* <td>OK</td>149* <td>OK</td>150* <td>OK</td>151* </tr>152* <tr>153* <th scope="row">{@link javax.xml.transform.stream.StreamResult}</th>154* <td>OK</td>155* <td>{@code IllegalArgumentException}</td>156* <td>{@code IllegalArgumentException}</td>157* <td>{@code IllegalArgumentException}</td>158* </tr>159* <tr>160* <th scope="row">{@link javax.xml.transform.sax.SAXResult}</th>161* <td>{@code IllegalArgumentException}</td>162* <td>OK</td>163* <td>{@code IllegalArgumentException}</td>164* <td>{@code IllegalArgumentException}</td>165* </tr>166* <tr>167* <th scope="row">{@link javax.xml.transform.dom.DOMResult}</th>168* <td>{@code IllegalArgumentException}</td>169* <td>{@code IllegalArgumentException}</td>170* <td>OK</td>171* <td>{@code IllegalArgumentException}</td>172* </tr>173* <tr>174* <th scope="row">{@link javax.xml.transform.stax.StAXResult}</th>175* <td>{@code IllegalArgumentException}</td>176* <td>{@code IllegalArgumentException}</td>177* <td>{@code IllegalArgumentException}</td>178* <td>OK</td>179* </tr>180* </tbody>181* </table>182*183* <p>To validate one {@code Source} into another kind of184* {@code Result}, use the identity transformer (see185* {@link javax.xml.transform.TransformerFactory#newTransformer()}).186*187* <p>Errors found during the validation is sent to the specified188* {@link ErrorHandler}.189*190* <p>If a document is valid, or if a document contains some errors191* but none of them were fatal and the {@code ErrorHandler} didn't192* throw any exception, then the method returns normally.193*194* @param source195* XML to be validated. Must be an XML document or196* XML element and must not be null. For backwards compatibility,197* the results of attempting to validate anything other than198* a document or element are implementation-dependent.199* Implementations must either recognize and process the input200* or throw an IllegalArgumentException.201*202* @param result203* The {@code Result} object that receives (possibly augmented)204* XML. This parameter can be null if the caller is not interested205* in it.206*207* Note that when a {@code DOMResult} is used,208* a validator might just pass the same DOM node from209* {@code DOMSource} to {@code DOMResult}210* (in which case {@code source.getNode()==result.getNode()}),211* it might copy the entire DOM tree, or it might alter the212* node given by the source.213*214* @throws IllegalArgumentException215* If the {@code Result} type doesn't match the216* {@code Source} type or if the {@code Source}217* is an XML artifact that the implementation cannot218* validate (for example, a processing instruction).219* @throws SAXException220* If the {@code ErrorHandler} throws a221* {@code SAXException} or222* if a fatal error is found and the {@code ErrorHandler} returns223* normally.224* @throws IOException225* If the validator is processing a226* {@code SAXSource} and the227* underlying {@link org.xml.sax.XMLReader} throws an228* {@code IOException}.229* @throws NullPointerException230* If the {@code source} parameter is {@code null}.231*232* @see #validate(Source source)233*/234public abstract void validate(Source source, Result result)235throws SAXException, IOException;236237/**238* Sets the {@link ErrorHandler} to receive errors encountered239* during the {@code validate} method invocation.240*241* <p>242* Error handler can be used to customize the error handling process243* during a validation. When an {@link ErrorHandler} is set,244* errors found during the validation will be first sent245* to the {@link ErrorHandler}.246*247* <p>248* The error handler can abort further validation immediately249* by throwing {@link SAXException} from the handler. Or for example250* it can print an error to the screen and try to continue the251* validation by returning normally from the {@link ErrorHandler}252*253* <p>254* If any {@link Throwable} is thrown from an {@link ErrorHandler},255* the caller of the {@code validate} method will be thrown256* the same {@link Throwable} object.257*258* <p>259* {@link Validator} is not allowed to260* throw {@link SAXException} without first reporting it to261* {@link ErrorHandler}.262*263* <p>264* When the {@link ErrorHandler} is null, the implementation will265* behave as if the following {@link ErrorHandler} is set:266* <pre>267* class DraconianErrorHandler implements {@link ErrorHandler} {268* public void fatalError( {@link org.xml.sax.SAXParseException} e ) throws {@link SAXException} {269* throw e;270* }271* public void error( {@link org.xml.sax.SAXParseException} e ) throws {@link SAXException} {272* throw e;273* }274* public void warning( {@link org.xml.sax.SAXParseException} e ) throws {@link SAXException} {275* // noop276* }277* }278* </pre>279*280* <p>281* When a new {@link Validator} object is created, initially282* this field is set to null.283*284* @param errorHandler285* A new error handler to be set. This parameter can be null.286*/287public abstract void setErrorHandler(ErrorHandler errorHandler);288289/**290* Gets the current {@link ErrorHandler} set to this {@link Validator}.291*292* @return293* This method returns the object that was last set through294* the {@link #setErrorHandler(ErrorHandler)} method, or null295* if that method has never been called since this {@link Validator}296* has created.297*298* @see #setErrorHandler(ErrorHandler)299*/300public abstract ErrorHandler getErrorHandler();301302/**303* Sets the {@link LSResourceResolver} to customize304* resource resolution while in a validation episode.305*306* <p>307* {@link Validator} uses a {@link LSResourceResolver}308* when it needs to locate external resources while a validation,309* although exactly what constitutes "locating external resources" is310* up to each schema language.311*312* <p>313* When the {@link LSResourceResolver} is null, the implementation will314* behave as if the following {@link LSResourceResolver} is set:315* <pre>316* class DumbLSResourceResolver implements {@link LSResourceResolver} {317* public {@link org.w3c.dom.ls.LSInput} resolveResource(318* String publicId, String systemId, String baseURI) {319*320* return null; // always return null321* }322* }323* </pre>324*325* <p>326* If a {@link LSResourceResolver} throws a {@link RuntimeException}327* (or instances of its derived classes),328* then the {@link Validator} will abort the parsing and329* the caller of the {@code validate} method will receive330* the same {@link RuntimeException}.331*332* <p>333* When a new {@link Validator} object is created, initially334* this field is set to null.335*336* @param resourceResolver337* A new resource resolver to be set. This parameter can be null.338*/339public abstract void setResourceResolver(LSResourceResolver resourceResolver);340341/**342* Gets the current {@link LSResourceResolver} set to this {@link Validator}.343*344* @return345* This method returns the object that was last set through346* the {@link #setResourceResolver(LSResourceResolver)} method, or null347* if that method has never been called since this {@link Validator}348* has created.349*350* @see #setErrorHandler(ErrorHandler)351*/352public abstract LSResourceResolver getResourceResolver();353354355356/**357* Look up the value of a feature flag.358*359* <p>The feature name is any fully-qualified URI. It is360* possible for a {@link Validator} to recognize a feature name but361* temporarily be unable to return its value.362* Some feature values may be available only in specific363* contexts, such as before, during, or after a validation.364*365* <p>Implementors are free (and encouraged) to invent their own features,366* using names built on their own URIs.367*368* @param name The feature name, which is a non-null fully-qualified URI.369*370* @return The current value of the feature (true or false).371*372* @throws SAXNotRecognizedException If the feature373* value can't be assigned or retrieved.374* @throws SAXNotSupportedException When the375* {@link Validator} recognizes the feature name but376* cannot determine its value at this time.377* @throws NullPointerException378* When the name parameter is null.379*380* @see #setFeature(String, boolean)381*/382public boolean getFeature(String name)383throws SAXNotRecognizedException, SAXNotSupportedException {384385if (name == null) {386throw new NullPointerException("the name parameter is null");387}388389throw new SAXNotRecognizedException(name);390}391392/**393* Set the value of a feature flag.394*395* <p>396* Feature can be used to control the way a {@link Validator}397* parses schemas, although {@link Validator}s are not required398* to recognize any specific feature names.399*400* <p>The feature name is any fully-qualified URI. It is401* possible for a {@link Validator} to expose a feature value but402* to be unable to change the current value.403* Some feature values may be immutable or mutable only404* in specific contexts, such as before, during, or after405* a validation.406*407* @param name The feature name, which is a non-null fully-qualified URI.408* @param value The requested value of the feature (true or false).409*410* @throws SAXNotRecognizedException If the feature411* value can't be assigned or retrieved.412* @throws SAXNotSupportedException When the413* {@link Validator} recognizes the feature name but414* cannot set the requested value.415* @throws NullPointerException416* When the name parameter is null.417*418* @see #getFeature(String)419*/420public void setFeature(String name, boolean value)421throws SAXNotRecognizedException, SAXNotSupportedException {422423if (name == null) {424throw new NullPointerException("the name parameter is null");425}426427throw new SAXNotRecognizedException(name);428}429430/**431* Set the value of a property.432*433* <p>The property name is any fully-qualified URI. It is434* possible for a {@link Validator} to recognize a property name but435* to be unable to change the current value.436* Some property values may be immutable or mutable only437* in specific contexts, such as before, during, or after438* a validation.439*440* <p>441* All implementations that implement JAXP 1.5 or newer are required to442* support the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} and443* {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA} properties.444*445* <ul>446* <li>447* <p>Access to external DTDs in source or Schema file is restricted to448* the protocols specified by the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD}449* property. If access is denied during validation due to the restriction450* of this property, {@link org.xml.sax.SAXException} will be thrown by the451* {@link #validate(Source)} method.452*453* <p>Access to external reference set by the schemaLocation attribute is454* restricted to the protocols specified by the455* {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA} property.456* If access is denied during validation due to the restriction of this property,457* {@link org.xml.sax.SAXException} will be thrown by the458* {@link #validate(Source)} method.459* </li>460* </ul>461*462* @param name The property name, which is a non-null fully-qualified URI.463* @param object The requested value for the property.464*465* @throws SAXNotRecognizedException If the property466* value can't be assigned or retrieved.467* @throws SAXNotSupportedException When the468* {@link Validator} recognizes the property name but469* cannot set the requested value.470* @throws NullPointerException471* When the name parameter is null.472*/473public void setProperty(String name, Object object)474throws SAXNotRecognizedException, SAXNotSupportedException {475476if (name == null) {477throw new NullPointerException("the name parameter is null");478}479480throw new SAXNotRecognizedException(name);481}482483/**484* Look up the value of a property.485*486* <p>The property name is any fully-qualified URI. It is487* possible for a {@link Validator} to recognize a property name but488* temporarily be unable to return its value.489* Some property values may be available only in specific490* contexts, such as before, during, or after a validation.491*492* <p>{@link Validator}s are not required to recognize any specific493* property names.494*495* <p>Implementors are free (and encouraged) to invent their own properties,496* using names built on their own URIs.497*498* @param name The property name, which is a non-null fully-qualified URI.499*500* @return The current value of the property.501*502* @throws SAXNotRecognizedException If the property503* value can't be assigned or retrieved.504* @throws SAXNotSupportedException When the505* XMLReader recognizes the property name but506* cannot determine its value at this time.507* @throws NullPointerException508* When the name parameter is null.509*510* @see #setProperty(String, Object)511*/512public Object getProperty(String name)513throws SAXNotRecognizedException, SAXNotSupportedException {514515if (name == null) {516throw new NullPointerException("the name parameter is null");517}518519throw new SAXNotRecognizedException(name);520}521}522523524