Module  java.xml.bind
软件包  javax.xml.bind

Interface Validator


  • 已过时。
    自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.validate method 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

    使用默认EventHandler

    If the client application does not set an event handler on their Validator, Unmarshaller, or Marshaller prior 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 setEventHandler API's on Validator, Unmarshaller, or Marshaller.
    Implement and register a custom event handler
    Client applications that require sophisticated event processing can implement the ValidationEventHandler interface and register it with the Unmarshaller and/or Validator.
    Use the ValidationEventCollector utility
    For convenience, a specialized event handler is provided that simply collects any ValidationEvent objects created during the unmarshal, validate, and marshal operations and returns them to the client application as a java.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 ValidationEventCollector utility class must terminate processing after being notified of a fatal error. Client applications that supply their own ValidationEventHandler should 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
    另请参见:
    JAXBContextUnmarshallerValidationEventHandlerValidationEventValidationEventCollector
    • 方法详细信息

      • 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的子树有效, subrootObj true,否则为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的树是有效的, rootObj true,否则为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参数为空