-
已过时。自JAXB 2.0起
public interface Validator从JAXB 2.0开始,此类已被弃用且可选。Validator类负责在运行时控制内容树的验证。- Unmarshal-Time Validation
-
This form of validation enables a client application to receive information about validation errors and warnings detected while unmarshalling XML data into a Java content tree and is completely orthogonal to the other types of validation. To enable or disable it, see the javadoc for
Unmarshaller.setValidating. All JAXB 1.0 Providers are required to support this operation. - On-Demand Validation
-
This form of validation enables a client application to receive information about validation errors and warnings detected in the Java content tree. At any point, client applications can call the
Validator.validatemethod on the Java content tree (or any sub-tree of it). All JAXB 1.0 Providers are required to support this operation. - Fail-Fast Validation
- This form of validation enables a client application to receive immediate feedback about modifications to the Java content tree that violate type constraints on Java Properties as defined in the specification. JAXB Providers are not required support this type of validation. Of the JAXB Providers that do support this type of validation, some may require you to decide at schema compile time whether or not a client application will be allowed to request fail-fast validation at runtime.
Validator课程负责管理按需验证。Unmarshaller课程负责在Unmarshaller操作期间管理Unmarshal-Time Validation。 虽然在元帅行动中没有能够进行验证的正式方法,但是Marshaller可能会检测到错误,这些错误将被报告到其ValidationEventHandler注册的ValidationEventHandler。If the client application does not set an event handler on their
Validator,Unmarshaller, orMarshallerprior to calling the validate, unmarshal, or marshal methods, then a default event handler will receive notification of any errors or warnings encountered. The default event handler will cause the current operation to halt after encountering the first error or fatal error (but will attempt to continue after receiving warnings).There are three ways to handle events encountered during the unmarshal, validate, and marshal operations:
- Use the default event handler
-
The default event handler will be used if you do not specify one via the
setEventHandlerAPI's onValidator,Unmarshaller, orMarshaller. - Implement and register a custom event handler
-
Client applications that require sophisticated event processing can implement the
ValidationEventHandlerinterface and register it with theUnmarshallerand/orValidator. -
Use the
ValidationEventCollectorutility -
For convenience, a specialized event handler is provided that simply collects any
ValidationEventobjects created during the unmarshal, validate, and marshal operations and returns them to the client application as ajava.util.Collection.
验证和成型
Validation events are handled differently depending on how the client application is configured to process them as described in the previous section. However, there are certain cases where a JAXB Provider indicates that it is no longer able to reliably detect and report errors. In these cases, the JAXB Provider will set the severity of the ValidationEvent to FATAL_ERROR to indicate that the unmarshal, validate, or marshal operations should be terminated. The default event handler and
ValidationEventCollectorutility class must terminate processing after being notified of a fatal error. Client applications that supply their ownValidationEventHandlershould also terminate processing after being notified of a fatal error. If not, unexpected behaviour may occur.There currently are not any properties required to be supported by all JAXB Providers on Validator. However, some providers may support their own set of provider specific properties.
- 从以下版本开始:
- 1.6,JAXB 1.0
- 另请参见:
-
JAXBContext,Unmarshaller,ValidationEventHandler,ValidationEvent,ValidationEventCollector
-
-
方法摘要
所有方法 接口方法 抽象方法 弃用的方法 Modifier and Type 方法 描述 ValidationEventHandlergetEventHandler()已过时。自JAXB2.0起ObjectgetProperty(String name)已过时。自JAXB2.0起voidsetEventHandler(ValidationEventHandler handler)已过时。自JAXB2.0起voidsetProperty(String name, Object value)已过时。自JAXB2.0起booleanvalidate(Object subrootObj)已过时。自JAXB2.0起booleanvalidateRoot(Object rootObj)已过时。自JAXB2.0起
-
-
-
方法详细信息
-
setEventHandler
void setEventHandler(ValidationEventHandler handler) throws JAXBException
已过时。 自JAXB2.0起允许应用程序注册验证事件处理程序。如果在调用
validate期间遇到任何验证错误,JAXB Provider将调用验证事件处理程序。 如果客户端应用程序在调用validate方法之前没有注册验证事件处理程序,则验证事件将由默认事件处理程序处理,该事件处理程序将在遇到第一个错误或致命错误后终止验证操作。使用null参数调用此方法将导致Validator恢复为默认的默认事件处理程序。
- 参数
-
handler- 验证事件处理程序 - 异常
-
JAXBException- 如果在设置事件处理程序时遇到错误
-
getEventHandler
ValidationEventHandler getEventHandler() throws JAXBException
已过时。 自JAXB2.0起返回当前事件处理程序或默认事件处理程序(如果尚未设置)。- 结果
- 当前的ValidationEventHandler或默认事件处理程序(如果尚未设置)
- 异常
-
JAXBException- 如果在获取当前事件处理程序时遇到错误
-
validate
boolean validate(Object subrootObj) throws JAXBException
已过时。 自JAXB2.0起验证从subrootObj开始的Java内容树。客户端应用程序可以使用此方法在运行时按需验证Java内容树。 该方法可用于验证Java内容树的任意任意子树。 全局约束检查不会作为此操作的一部分执行(即ID / IDREF约束)。
- 参数
-
subrootObj- obj开始验证 - 结果
-
如果根据
subrootObj的子树有效,subrootObjtrue,否则为false - 异常
-
JAXBException- 如果在验证期间发生意外问题 -
ValidationException- 如果ValidationEventHandler从其handleEvent方法返回false,或者Validator无法验证基于subrootObj的内容树 -
IllegalArgumentException- 如果subrootObj参数为空
-
validateRoot
boolean validateRoot(Object rootObj) throws JAXBException
已过时。 自JAXB2.0起验证基于rootObj的Java内容树。客户端应用程序可以使用此方法在运行时按需验证Java内容树。 该方法用于验证整个Java内容树。 全局约束检查将作为此操作的一部分执行(即ID / IDREF约束)。
- 参数
-
rootObj- 根目录开始验证 - 结果
-
如果根源于
rootObj的树是有效的,rootObjtrue,否则为false - 异常
-
JAXBException- 如果验证过程中出现意外问题 -
ValidationException- 如果ValidationEventHandler从其handleEvent方法返回false,或者Validator无法验证基于rootObj的内容树 -
IllegalArgumentException- 如果IllegalArgumentException参数为空
-
setProperty
void setProperty(String name, Object value) throws PropertyException
已过时。 自JAXB2.0起设置底层实现中的特定属性为Validator。 此方法只能用于设置上述标准JAXB定义属性之一或提供者特定属性。 尝试设置未定义的属性将导致抛出PropertyException。 见Supported Properties 。- 参数
-
name- 要设置的属性的名称。 可以使用常数字段或用户提供的字符串指定此值。 -
value- 要设置的属性的值 - 异常
-
PropertyException- 处理给定属性或值时出错 -
IllegalArgumentException- 如果name参数为null
-
getProperty
Object getProperty(String name) throws PropertyException
已过时。 自JAXB2.0起获取底层实现中的特定属性Validator。 此方法只能用于获取上述标准JAXB定义的属性之一或提供者特定属性。 尝试获取未定义的属性将导致抛出PropertyException。 见Supported Properties 。- 参数
-
name- 要检索的属性的名称 - 结果
- 请求的属性的值
- 异常
-
PropertyException- 检索给定属性或值属性名称时出现错误 -
IllegalArgumentException- 如果name参数为空
-
-