Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package javax.xml.bind; |
aoqi@0 | 27 | |
aoqi@0 | 28 | /** |
aoqi@0 | 29 | * As of JAXB 2.0, this class is deprecated and optional. |
aoqi@0 | 30 | * <p> |
aoqi@0 | 31 | * The <tt>Validator</tt> class is responsible for controlling the validation |
aoqi@0 | 32 | * of content trees during runtime. |
aoqi@0 | 33 | * |
aoqi@0 | 34 | * <p> |
aoqi@0 | 35 | * <a name="validationtypes"></a> |
aoqi@0 | 36 | * <b>Three Forms of Validation</b><br> |
aoqi@0 | 37 | * <blockquote> |
aoqi@0 | 38 | * <dl> |
aoqi@0 | 39 | * <dt><b>Unmarshal-Time Validation</b></dt> |
aoqi@0 | 40 | * <dd>This form of validation enables a client application to receive |
aoqi@0 | 41 | * information about validation errors and warnings detected while |
aoqi@0 | 42 | * unmarshalling XML data into a Java content tree and is completely |
aoqi@0 | 43 | * orthogonal to the other types of validation. To enable or disable |
aoqi@0 | 44 | * it, see the javadoc for |
aoqi@0 | 45 | * {@link Unmarshaller#setValidating(boolean) Unmarshaller.setValidating}. |
aoqi@0 | 46 | * All JAXB 1.0 Providers are required to support this operation. |
aoqi@0 | 47 | * </dd> |
aoqi@0 | 48 | * |
aoqi@0 | 49 | * <dt><b>On-Demand Validation</b></dt> |
aoqi@0 | 50 | * <dd> This form of validation enables a client application to receive |
aoqi@0 | 51 | * information about validation errors and warnings detected in the |
aoqi@0 | 52 | * Java content tree. At any point, client applications can call |
aoqi@0 | 53 | * the {@link Validator#validate(Object) Validator.validate} method |
aoqi@0 | 54 | * on the Java content tree (or any sub-tree of it). All JAXB 1.0 |
aoqi@0 | 55 | * Providers are required to support this operation. |
aoqi@0 | 56 | * </dd> |
aoqi@0 | 57 | * |
aoqi@0 | 58 | * <dt><b>Fail-Fast Validation</b></dt> |
aoqi@0 | 59 | * <dd> This form of validation enables a client application to receive |
aoqi@0 | 60 | * immediate feedback about modifications to the Java content tree |
aoqi@0 | 61 | * that violate type constraints on Java Properties as defined in |
aoqi@0 | 62 | * the specification. JAXB Providers are not required support |
aoqi@0 | 63 | * this type of validation. Of the JAXB Providers that do support |
aoqi@0 | 64 | * this type of validation, some may require you to decide at schema |
aoqi@0 | 65 | * compile time whether or not a client application will be allowed |
aoqi@0 | 66 | * to request fail-fast validation at runtime. |
aoqi@0 | 67 | * </dd> |
aoqi@0 | 68 | * </dl> |
aoqi@0 | 69 | * </blockquote> |
aoqi@0 | 70 | * |
aoqi@0 | 71 | * <p> |
aoqi@0 | 72 | * The <tt>Validator</tt> class is responsible for managing On-Demand Validation. |
aoqi@0 | 73 | * The <tt>Unmarshaller</tt> class is responsible for managing Unmarshal-Time |
aoqi@0 | 74 | * Validation during the unmarshal operations. Although there is no formal |
aoqi@0 | 75 | * method of enabling validation during the marshal operations, the |
aoqi@0 | 76 | * <tt>Marshaller</tt> may detect errors, which will be reported to the |
aoqi@0 | 77 | * <tt>ValidationEventHandler</tt> registered on it. |
aoqi@0 | 78 | * |
aoqi@0 | 79 | * <p> |
aoqi@0 | 80 | * <a name="defaulthandler"></a> |
aoqi@0 | 81 | * <b>Using the Default EventHandler</b><br> |
aoqi@0 | 82 | * <blockquote> |
aoqi@0 | 83 | * If the client application does not set an event handler on their |
aoqi@0 | 84 | * <tt>Validator</tt>, <tt>Unmarshaller</tt>, or <tt>Marshaller</tt> prior to |
aoqi@0 | 85 | * calling the validate, unmarshal, or marshal methods, then a default event |
aoqi@0 | 86 | * handler will receive notification of any errors or warnings encountered. |
aoqi@0 | 87 | * The default event handler will cause the current operation to halt after |
aoqi@0 | 88 | * encountering the first error or fatal error (but will attempt to continue |
aoqi@0 | 89 | * after receiving warnings). |
aoqi@0 | 90 | * </blockquote> |
aoqi@0 | 91 | * |
aoqi@0 | 92 | * <p> |
aoqi@0 | 93 | * <a name="handlingevents"></a> |
aoqi@0 | 94 | * <b>Handling Validation Events</b><br> |
aoqi@0 | 95 | * <blockquote> |
aoqi@0 | 96 | * There are three ways to handle events encountered during the unmarshal, |
aoqi@0 | 97 | * validate, and marshal operations: |
aoqi@0 | 98 | * <dl> |
aoqi@0 | 99 | * <dt>Use the default event handler</dt> |
aoqi@0 | 100 | * <dd>The default event handler will be used if you do not specify one |
aoqi@0 | 101 | * via the <tt>setEventHandler</tt> API's on <tt>Validator</tt>, |
aoqi@0 | 102 | * <tt>Unmarshaller</tt>, or <tt>Marshaller</tt>. |
aoqi@0 | 103 | * </dd> |
aoqi@0 | 104 | * |
aoqi@0 | 105 | * <dt>Implement and register a custom event handler</dt> |
aoqi@0 | 106 | * <dd>Client applications that require sophisticated event processing |
aoqi@0 | 107 | * can implement the <tt>ValidationEventHandler</tt> interface and |
aoqi@0 | 108 | * register it with the <tt>Unmarshaller</tt> and/or |
aoqi@0 | 109 | * <tt>Validator</tt>. |
aoqi@0 | 110 | * </dd> |
aoqi@0 | 111 | * |
aoqi@0 | 112 | * <dt>Use the {@link javax.xml.bind.util.ValidationEventCollector ValidationEventCollector} |
aoqi@0 | 113 | * utility</dt> |
aoqi@0 | 114 | * <dd>For convenience, a specialized event handler is provided that |
aoqi@0 | 115 | * simply collects any <tt>ValidationEvent</tt> objects created |
aoqi@0 | 116 | * during the unmarshal, validate, and marshal operations and |
aoqi@0 | 117 | * returns them to the client application as a |
aoqi@0 | 118 | * <tt>java.util.Collection</tt>. |
aoqi@0 | 119 | * </dd> |
aoqi@0 | 120 | * </dl> |
aoqi@0 | 121 | * </blockquote> |
aoqi@0 | 122 | * |
aoqi@0 | 123 | * <p> |
aoqi@0 | 124 | * <b>Validation and Well-Formedness</b><br> |
aoqi@0 | 125 | * <blockquote> |
aoqi@0 | 126 | * <p> |
aoqi@0 | 127 | * Validation events are handled differently depending on how the client |
aoqi@0 | 128 | * application is configured to process them as described in the previous |
aoqi@0 | 129 | * section. However, there are certain cases where a JAXB Provider indicates |
aoqi@0 | 130 | * that it is no longer able to reliably detect and report errors. In these |
aoqi@0 | 131 | * cases, the JAXB Provider will set the severity of the ValidationEvent to |
aoqi@0 | 132 | * FATAL_ERROR to indicate that the unmarshal, validate, or marshal operations |
aoqi@0 | 133 | * should be terminated. The default event handler and |
aoqi@0 | 134 | * <tt>ValidationEventCollector</tt> utility class must terminate processing |
aoqi@0 | 135 | * after being notified of a fatal error. Client applications that supply their |
aoqi@0 | 136 | * own <tt>ValidationEventHandler</tt> should also terminate processing after |
aoqi@0 | 137 | * being notified of a fatal error. If not, unexpected behaviour may occur. |
aoqi@0 | 138 | * </blockquote> |
aoqi@0 | 139 | * |
aoqi@0 | 140 | * <p> |
aoqi@0 | 141 | * <a name="supportedProps"></a> |
aoqi@0 | 142 | * <b>Supported Properties</b><br> |
aoqi@0 | 143 | * <blockquote> |
aoqi@0 | 144 | * <p> |
aoqi@0 | 145 | * There currently are not any properties required to be supported by all |
aoqi@0 | 146 | * JAXB Providers on Validator. However, some providers may support |
aoqi@0 | 147 | * their own set of provider specific properties. |
aoqi@0 | 148 | * </blockquote> |
aoqi@0 | 149 | * |
aoqi@0 | 150 | * |
aoqi@0 | 151 | * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul> |
aoqi@0 | 152 | * @see JAXBContext |
aoqi@0 | 153 | * @see Unmarshaller |
aoqi@0 | 154 | * @see ValidationEventHandler |
aoqi@0 | 155 | * @see ValidationEvent |
aoqi@0 | 156 | * @see javax.xml.bind.util.ValidationEventCollector |
aoqi@0 | 157 | * @since JAXB1.0 |
aoqi@0 | 158 | * @deprecated since JAXB 2.0 |
aoqi@0 | 159 | */ |
aoqi@0 | 160 | public interface Validator { |
aoqi@0 | 161 | |
aoqi@0 | 162 | /** |
aoqi@0 | 163 | * Allow an application to register a validation event handler. |
aoqi@0 | 164 | * <p> |
aoqi@0 | 165 | * The validation event handler will be called by the JAXB Provider if any |
aoqi@0 | 166 | * validation errors are encountered during calls to |
aoqi@0 | 167 | * {@link #validate(Object) validate}. If the client application does not |
aoqi@0 | 168 | * register a validation event handler before invoking the validate method, |
aoqi@0 | 169 | * then validation events will be handled by the default event handler which |
aoqi@0 | 170 | * will terminate the validate operation after the first error or fatal error |
aoqi@0 | 171 | * is encountered. |
aoqi@0 | 172 | * <p> |
aoqi@0 | 173 | * Calling this method with a null parameter will cause the Validator |
aoqi@0 | 174 | * to revert back to the default default event handler. |
aoqi@0 | 175 | * |
aoqi@0 | 176 | * @param handler the validation event handler |
aoqi@0 | 177 | * @throws JAXBException if an error was encountered while setting the |
aoqi@0 | 178 | * event handler |
aoqi@0 | 179 | * @deprecated since JAXB2.0 |
aoqi@0 | 180 | */ |
aoqi@0 | 181 | public void setEventHandler( ValidationEventHandler handler ) |
aoqi@0 | 182 | throws JAXBException; |
aoqi@0 | 183 | |
aoqi@0 | 184 | /** |
aoqi@0 | 185 | * Return the current event handler or the default event handler if one |
aoqi@0 | 186 | * hasn't been set. |
aoqi@0 | 187 | * |
aoqi@0 | 188 | * @return the current ValidationEventHandler or the default event handler |
aoqi@0 | 189 | * if it hasn't been set |
aoqi@0 | 190 | * @throws JAXBException if an error was encountered while getting the |
aoqi@0 | 191 | * current event handler |
aoqi@0 | 192 | * @deprecated since JAXB2.0 |
aoqi@0 | 193 | */ |
aoqi@0 | 194 | public ValidationEventHandler getEventHandler() |
aoqi@0 | 195 | throws JAXBException; |
aoqi@0 | 196 | |
aoqi@0 | 197 | /** |
aoqi@0 | 198 | * Validate the Java content tree starting at <tt>subrootObj</tt>. |
aoqi@0 | 199 | * <p> |
aoqi@0 | 200 | * Client applications can use this method to validate Java content trees |
aoqi@0 | 201 | * on-demand at runtime. This method can be used to validate any arbitrary |
aoqi@0 | 202 | * subtree of the Java content tree. Global constraint checking <b>will not |
aoqi@0 | 203 | * </b> be performed as part of this operation (i.e. ID/IDREF constraints). |
aoqi@0 | 204 | * |
aoqi@0 | 205 | * @param subrootObj the obj to begin validation at |
aoqi@0 | 206 | * @throws JAXBException if any unexpected problem occurs during validation |
aoqi@0 | 207 | * @throws ValidationException |
aoqi@0 | 208 | * If the {@link ValidationEventHandler ValidationEventHandler} |
aoqi@0 | 209 | * returns false from its <tt>handleEvent</tt> method or the |
aoqi@0 | 210 | * <tt>Validator</tt> is unable to validate the content tree rooted |
aoqi@0 | 211 | * at <tt>subrootObj</tt> |
aoqi@0 | 212 | * @throws IllegalArgumentException |
aoqi@0 | 213 | * If the subrootObj parameter is null |
aoqi@0 | 214 | * @return true if the subtree rooted at <tt>subrootObj</tt> is valid, false |
aoqi@0 | 215 | * otherwise |
aoqi@0 | 216 | * @deprecated since JAXB2.0 |
aoqi@0 | 217 | */ |
aoqi@0 | 218 | public boolean validate( Object subrootObj ) throws JAXBException; |
aoqi@0 | 219 | |
aoqi@0 | 220 | /** |
aoqi@0 | 221 | * Validate the Java content tree rooted at <tt>rootObj</tt>. |
aoqi@0 | 222 | * <p> |
aoqi@0 | 223 | * Client applications can use this method to validate Java content trees |
aoqi@0 | 224 | * on-demand at runtime. This method is used to validate an entire Java |
aoqi@0 | 225 | * content tree. Global constraint checking <b>will</b> be performed as |
aoqi@0 | 226 | * part of this operation (i.e. ID/IDREF constraints). |
aoqi@0 | 227 | * |
aoqi@0 | 228 | * @param rootObj the root obj to begin validation at |
aoqi@0 | 229 | * @throws JAXBException if any unexpected problem occurs during validation |
aoqi@0 | 230 | * @throws ValidationException |
aoqi@0 | 231 | * If the {@link ValidationEventHandler ValidationEventHandler} |
aoqi@0 | 232 | * returns false from its <tt>handleEvent</tt> method or the |
aoqi@0 | 233 | * <tt>Validator</tt> is unable to validate the content tree rooted |
aoqi@0 | 234 | * at <tt>rootObj</tt> |
aoqi@0 | 235 | * @throws IllegalArgumentException |
aoqi@0 | 236 | * If the rootObj parameter is null |
aoqi@0 | 237 | * @return true if the tree rooted at <tt>rootObj</tt> is valid, false |
aoqi@0 | 238 | * otherwise |
aoqi@0 | 239 | * @deprecated since JAXB2.0 |
aoqi@0 | 240 | */ |
aoqi@0 | 241 | public boolean validateRoot( Object rootObj ) throws JAXBException; |
aoqi@0 | 242 | |
aoqi@0 | 243 | /** |
aoqi@0 | 244 | * Set the particular property in the underlying implementation of |
aoqi@0 | 245 | * <tt>Validator</tt>. This method can only be used to set one of |
aoqi@0 | 246 | * the standard JAXB defined properties above or a provider specific |
aoqi@0 | 247 | * property. Attempting to set an undefined property will result in |
aoqi@0 | 248 | * a PropertyException being thrown. See <a href="#supportedProps"> |
aoqi@0 | 249 | * Supported Properties</a>. |
aoqi@0 | 250 | * |
aoqi@0 | 251 | * @param name the name of the property to be set. This value can either |
aoqi@0 | 252 | * be specified using one of the constant fields or a user |
aoqi@0 | 253 | * supplied string. |
aoqi@0 | 254 | * @param value the value of the property to be set |
aoqi@0 | 255 | * |
aoqi@0 | 256 | * @throws PropertyException when there is an error processing the given |
aoqi@0 | 257 | * property or value |
aoqi@0 | 258 | * @throws IllegalArgumentException |
aoqi@0 | 259 | * If the name parameter is null |
aoqi@0 | 260 | * @deprecated since JAXB2.0 |
aoqi@0 | 261 | */ |
aoqi@0 | 262 | public void setProperty( String name, Object value ) |
aoqi@0 | 263 | throws PropertyException; |
aoqi@0 | 264 | |
aoqi@0 | 265 | /** |
aoqi@0 | 266 | * Get the particular property in the underlying implementation of |
aoqi@0 | 267 | * <tt>Validator</tt>. This method can only be used to get one of |
aoqi@0 | 268 | * the standard JAXB defined properties above or a provider specific |
aoqi@0 | 269 | * property. Attempting to get an undefined property will result in |
aoqi@0 | 270 | * a PropertyException being thrown. See <a href="#supportedProps"> |
aoqi@0 | 271 | * Supported Properties</a>. |
aoqi@0 | 272 | * |
aoqi@0 | 273 | * @param name the name of the property to retrieve |
aoqi@0 | 274 | * @return the value of the requested property |
aoqi@0 | 275 | * |
aoqi@0 | 276 | * @throws PropertyException |
aoqi@0 | 277 | * when there is an error retrieving the given property or value |
aoqi@0 | 278 | * property name |
aoqi@0 | 279 | * @throws IllegalArgumentException |
aoqi@0 | 280 | * If the name parameter is null |
aoqi@0 | 281 | * @deprecated since JAXB2.0 |
aoqi@0 | 282 | */ |
aoqi@0 | 283 | public Object getProperty( String name ) throws PropertyException; |
aoqi@0 | 284 | |
aoqi@0 | 285 | } |