Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/validation/Validator.java
32285 views
/*1* Copyright (c) 2003, 2013, 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* <p>A processor that checks an XML document against {@link Schema}.</p>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</code>46* method is invoked, applications may not recursively call47* the <code>validate</code> method.48* <p>49*50*51* @author <a href="mailto:[email protected]">Kohsuke Kawaguchi</a>52* @since 1.553*/54public abstract class Validator {5556/**57* Constructor for derived classes.58*59* <p>The constructor does nothing.</p>60*61* <p>Derived classes must create {@link Validator} objects that have62* <code>null</code> {@link ErrorHandler} and63* <code>null</code> {@link LSResourceResolver}.64* </p>65*/66protected Validator() {67}6869/**70* <p>Reset this <code>Validator</code> to its original configuration.</p>71*72* <p><code>Validator</code> is reset to the same state as when it was created with73* {@link Schema#newValidator()}.74* <code>reset()</code> is designed to allow the reuse of existing <code>Validator</code>s75* thus saving resources associated with the creation of new <code>Validator</code>s.</p>76*77* <p>The reset <code>Validator</code> is not guaranteed to have the same {@link LSResourceResolver} or {@link ErrorHandler}78* <code>Object</code>s, e.g. {@link Object#equals(Object obj)}. It is guaranteed to have a functionally equal79* <code>LSResourceResolver</code> and <code>ErrorHandler</code>.</p>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</code> of <code>null</code>.</p>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</code>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</code> is116* <code>null</code>.117*118* @see #validate(Source source, Result result)119*/120public void validate(Source source)121throws SAXException, IOException {122123validate(source, null);124}125126/**127* <p>Validates the specified input and send the augmented validation128* result to the specified output.</p>129*130* <p>This method places the following restrictions on the types of131* the {@link Source}/{@link Result} accepted.</p>132*133* <table border=1>134* <thead>135* <tr>136* <th colspan="5"><code>Source</code> / <code>Result</code> Accepted</th>137* </tr>138* <tr>139* <th></th>140* <th>{@link javax.xml.transform.stream.StreamSource}</th>141* <th>{@link javax.xml.transform.sax.SAXSource}</th>142* <th>{@link javax.xml.transform.dom.DOMSource}</th>143* <th>{@link javax.xml.transform.stax.StAXSource}</th>144* </tr>145* </thead>146* <tbody align="center">147* <tr>148* <td><code>null</code></td>149* <td>OK</td>150* <td>OK</td>151* <td>OK</td>152* <td>OK</td>153* </tr>154* <tr>155* <th>{@link javax.xml.transform.stream.StreamResult}</th>156* <td>OK</td>157* <td><code>IllegalArgumentException</code></td>158* <td><code>IllegalArgumentException</code></td>159* <td><code>IllegalArgumentException</code></td>160* </tr>161* <tr>162* <th>{@link javax.xml.transform.sax.SAXResult}</th>163* <td><code>IllegalArgumentException</code></td>164* <td>OK</td>165* <td><code>IllegalArgumentException</code></td>166* <td><code>IllegalArgumentException</code></td>167* </tr>168* <tr>169* <th>{@link javax.xml.transform.dom.DOMResult}</th>170* <td><code>IllegalArgumentException</code></td>171* <td><code>IllegalArgumentException</code></td>172* <td>OK</td>173* <td><code>IllegalArgumentException</code></td>174* </tr>175* <tr>176* <th>{@link javax.xml.transform.stax.StAXResult}</th>177* <td><code>IllegalArgumentException</code></td>178* <td><code>IllegalArgumentException</code></td>179* <td><code>IllegalArgumentException</code></td>180* <td>OK</td>181* </tr>182* </tbody>183* </table>184*185* <p>To validate one <code>Source</code> into another kind of186* <code>Result</code>, use the identity transformer (see187* {@link javax.xml.transform.TransformerFactory#newTransformer()}).</p>188*189* <p>Errors found during the validation is sent to the specified190* {@link ErrorHandler}.</p>191*192* <p>If a document is valid, or if a document contains some errors193* but none of them were fatal and the <code>ErrorHandler</code> didn't194* throw any exception, then the method returns normally.</p>195*196* @param source197* XML to be validated. Must be an XML document or198* XML element and must not be null. For backwards compatibility,199* the results of attempting to validate anything other than200* a document or element are implementation-dependent.201* Implementations must either recognize and process the input202* or throw an IllegalArgumentException.203*204* @param result205* The <code>Result</code> object that receives (possibly augmented)206* XML. This parameter can be null if the caller is not interested207* in it.208*209* Note that when a <code>DOMResult</code> is used,210* a validator might just pass the same DOM node from211* <code>DOMSource</code> to <code>DOMResult</code>212* (in which case <code>source.getNode()==result.getNode()</code>),213* it might copy the entire DOM tree, or it might alter the214* node given by the source.215*216* @throws IllegalArgumentException217* If the <code>Result</code> type doesn't match the218* <code>Source</code> type of if the <code>Source</code>219* is an XML artifact that the implementation cannot220* validate (for example, a processing instruction).221* @throws SAXException222* If the <code>ErrorHandler</code> throws a223* <code>SAXException</code> or224* if a fatal error is found and the <code>ErrorHandler</code> returns225* normally.226* @throws IOException227* If the validator is processing a228* <code>SAXSource</code> and the229* underlying {@link org.xml.sax.XMLReader} throws an230* <code>IOException</code>.231* @throws NullPointerException232* If the <code>source</code> parameter is <code>null</code>.233*234* @see #validate(Source source)235*/236public abstract void validate(Source source, Result result)237throws SAXException, IOException;238239/**240* Sets the {@link ErrorHandler} to receive errors encountered241* during the <code>validate</code> method invocation.242*243* <p>244* Error handler can be used to customize the error handling process245* during a validation. When an {@link ErrorHandler} is set,246* errors found during the validation will be first sent247* to the {@link ErrorHandler}.248*249* <p>250* The error handler can abort further validation immediately251* by throwing {@link SAXException} from the handler. Or for example252* it can print an error to the screen and try to continue the253* validation by returning normally from the {@link ErrorHandler}254*255* <p>256* If any {@link Throwable} is thrown from an {@link ErrorHandler},257* the caller of the <code>validate</code> method will be thrown258* the same {@link Throwable} object.259*260* <p>261* {@link Validator} is not allowed to262* throw {@link SAXException} without first reporting it to263* {@link ErrorHandler}.264*265* <p>266* When the {@link ErrorHandler} is null, the implementation will267* behave as if the following {@link ErrorHandler} is set:268* <pre>269* class DraconianErrorHandler implements {@link ErrorHandler} {270* public void fatalError( {@link org.xml.sax.SAXParseException} e ) throws {@link SAXException} {271* throw e;272* }273* public void error( {@link org.xml.sax.SAXParseException} e ) throws {@link SAXException} {274* throw e;275* }276* public void warning( {@link org.xml.sax.SAXParseException} e ) throws {@link SAXException} {277* // noop278* }279* }280* </pre>281*282* <p>283* When a new {@link Validator} object is created, initially284* this field is set to null.285*286* @param errorHandler287* A new error handler to be set. This parameter can be null.288*/289public abstract void setErrorHandler(ErrorHandler errorHandler);290291/**292* Gets the current {@link ErrorHandler} set to this {@link Validator}.293*294* @return295* This method returns the object that was last set through296* the {@link #setErrorHandler(ErrorHandler)} method, or null297* if that method has never been called since this {@link Validator}298* has created.299*300* @see #setErrorHandler(ErrorHandler)301*/302public abstract ErrorHandler getErrorHandler();303304/**305* Sets the {@link LSResourceResolver} to customize306* resource resolution while in a validation episode.307*308* <p>309* {@link Validator} uses a {@link LSResourceResolver}310* when it needs to locate external resources while a validation,311* although exactly what constitutes "locating external resources" is312* up to each schema language.313*314* <p>315* When the {@link LSResourceResolver} is null, the implementation will316* behave as if the following {@link LSResourceResolver} is set:317* <pre>318* class DumbLSResourceResolver implements {@link LSResourceResolver} {319* public {@link org.w3c.dom.ls.LSInput} resolveResource(320* String publicId, String systemId, String baseURI) {321*322* return null; // always return null323* }324* }325* </pre>326*327* <p>328* If a {@link LSResourceResolver} throws a {@link RuntimeException}329* (or instances of its derived classes),330* then the {@link Validator} will abort the parsing and331* the caller of the <code>validate</code> method will receive332* the same {@link RuntimeException}.333*334* <p>335* When a new {@link Validator} object is created, initially336* this field is set to null.337*338* @param resourceResolver339* A new resource resolver to be set. This parameter can be null.340*/341public abstract void setResourceResolver(LSResourceResolver resourceResolver);342343/**344* Gets the current {@link LSResourceResolver} set to this {@link Validator}.345*346* @return347* This method returns the object that was last set through348* the {@link #setResourceResolver(LSResourceResolver)} method, or null349* if that method has never been called since this {@link Validator}350* has created.351*352* @see #setErrorHandler(ErrorHandler)353*/354public abstract LSResourceResolver getResourceResolver();355356357358/**359* Look up the value of a feature flag.360*361* <p>The feature name is any fully-qualified URI. It is362* possible for a {@link Validator} to recognize a feature name but363* temporarily be unable to return its value.364* Some feature values may be available only in specific365* contexts, such as before, during, or after a validation.366*367* <p>Implementors are free (and encouraged) to invent their own features,368* using names built on their own URIs.</p>369*370* @param name The feature name, which is a non-null fully-qualified URI.371*372* @return The current value of the feature (true or false).373*374* @throws SAXNotRecognizedException If the feature375* value can't be assigned or retrieved.376* @throws SAXNotSupportedException When the377* {@link Validator} recognizes the feature name but378* cannot determine its value at this time.379* @throws NullPointerException380* When the name parameter is null.381*382* @see #setFeature(String, boolean)383*/384public boolean getFeature(String name)385throws SAXNotRecognizedException, SAXNotSupportedException {386387if (name == null) {388throw new NullPointerException("the name parameter is null");389}390391throw new SAXNotRecognizedException(name);392}393394/**395* Set the value of a feature flag.396*397* <p>398* Feature can be used to control the way a {@link Validator}399* parses schemas, although {@link Validator}s are not required400* to recognize any specific feature names.</p>401*402* <p>The feature name is any fully-qualified URI. It is403* possible for a {@link Validator} to expose a feature value but404* to be unable to change the current value.405* Some feature values may be immutable or mutable only406* in specific contexts, such as before, during, or after407* a validation.</p>408*409* @param name The feature name, which is a non-null fully-qualified URI.410* @param value The requested value of the feature (true or false).411*412* @throws SAXNotRecognizedException If the feature413* value can't be assigned or retrieved.414* @throws SAXNotSupportedException When the415* {@link Validator} recognizes the feature name but416* cannot set the requested value.417* @throws NullPointerException418* When the name parameter is null.419*420* @see #getFeature(String)421*/422public void setFeature(String name, boolean value)423throws SAXNotRecognizedException, SAXNotSupportedException {424425if (name == null) {426throw new NullPointerException("the name parameter is null");427}428429throw new SAXNotRecognizedException(name);430}431432/**433* Set the value of a property.434*435* <p>The property name is any fully-qualified URI. It is436* possible for a {@link Validator} to recognize a property name but437* to be unable to change the current value.438* Some property values may be immutable or mutable only439* in specific contexts, such as before, during, or after440* a validation.</p>441*442* <p>443* All implementations that implement JAXP 1.5 or newer are required to444* support the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} and445* {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA} properties.446* </p>447* <ul>448* <li>449* <p>Access to external DTDs in source or Schema file is restricted to450* the protocols specified by the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD}451* property. If access is denied during validation due to the restriction452* of this property, {@link org.xml.sax.SAXException} will be thrown by the453* {@link #validate(Source)} method.</p>454*455* <p>Access to external reference set by the schemaLocation attribute is456* restricted to the protocols specified by the457* {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA} property.458* If access is denied during validation due to the restriction of this property,459* {@link org.xml.sax.SAXException} will be thrown by the460* {@link #validate(Source)} method.</p>461* </li>462* </ul>463*464* @param name The property name, which is a non-null fully-qualified URI.465* @param object The requested value for the property.466*467* @throws SAXNotRecognizedException If the property468* value can't be assigned or retrieved.469* @throws SAXNotSupportedException When the470* {@link Validator} recognizes the property name but471* cannot set the requested value.472* @throws NullPointerException473* When the name parameter is null.474*/475public void setProperty(String name, Object object)476throws SAXNotRecognizedException, SAXNotSupportedException {477478if (name == null) {479throw new NullPointerException("the name parameter is null");480}481482throw new SAXNotRecognizedException(name);483}484485/**486* Look up the value of a property.487*488* <p>The property name is any fully-qualified URI. It is489* possible for a {@link Validator} to recognize a property name but490* temporarily be unable to return its value.491* Some property values may be available only in specific492* contexts, such as before, during, or after a validation.</p>493*494* <p>{@link Validator}s are not required to recognize any specific495* property names.</p>496*497* <p>Implementors are free (and encouraged) to invent their own properties,498* using names built on their own URIs.</p>499*500* @param name The property name, which is a non-null fully-qualified URI.501*502* @return The current value of the property.503*504* @throws SAXNotRecognizedException If the property505* value can't be assigned or retrieved.506* @throws SAXNotSupportedException When the507* XMLReader recognizes the property name but508* cannot determine its value at this time.509* @throws NullPointerException510* When the name parameter is null.511*512* @see #setProperty(String, Object)513*/514public Object getProperty(String name)515throws SAXNotRecognizedException, SAXNotSupportedException {516517if (name == null) {518throw new NullPointerException("the name parameter is null");519}520521throw new SAXNotRecognizedException(name);522}523}524525526