public interface Validator
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课程负责在解散操作期间管理Unmarshal-Time Validation。 虽然在组织操作中没有正式的启用验证的方法,但是Marshaller可能会检测到错误,这些错误将被报告到其上注册的 ValidationEventHandler。
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.
JAXBContext
, Unmarshaller
, ValidationEventHandler
, ValidationEvent
, ValidationEventCollector
Modifier and Type | Method and Description |
---|---|
ValidationEventHandler |
getEventHandler()
已弃用
自JAXB2.0起
|
Object |
getProperty(String name)
已弃用
自JAXB2.0起
|
void |
setEventHandler(ValidationEventHandler handler)
已弃用
自JAXB2.0起
|
void |
setProperty(String name, Object value)
已弃用
自JAXB2.0起
|
boolean |
validate(Object subrootObj)
已弃用
自JAXB2.0起
|
boolean |
validateRoot(Object rootObj)
已弃用
自JAXB2.0起
|
void setEventHandler(ValidationEventHandler handler) throws JAXBException
验证事件处理程序将由JAXB提供者,如果在通话过程中遇到任何验证错误被称为validate
。 如果客户端应用程序在调用validate方法之前没有注册验证事件处理程序,则验证事件将由默认事件处理程序处理,该事件处理程序将在遇到第一个错误或致命错误后终止验证操作。
使用null参数调用此方法将导致Validator恢复为默认的默认事件处理程序。
handler
- 验证事件处理程序
JAXBException
- 如果在设置事件处理程序时遇到错误
ValidationEventHandler getEventHandler() throws JAXBException
JAXBException
- 如果在获取当前事件处理程序时遇到错误
boolean validate(Object subrootObj) throws JAXBException
客户端应用程序可以使用此方法在运行时按需验证Java内容树。 该方法可用于验证Java内容树的任意任意子树。 全局约束检查不会作为此操作的一部分执行(即ID / IDREF约束)。
subrootObj
- obj开始验证
JAXBException
- 验证过程中是否发生意外问题
ValidationException
- 如果ValidationEventHandler
从其handleEvent方法返回false,或者Validator无法验证基于subrootObj的内容树
IllegalArgumentException
- 如果subrootObj参数为空
boolean validateRoot(Object rootObj) throws JAXBException
客户端应用程序可以使用此方法在运行时按需验证Java内容树。 该方法用于验证整个Java内容树。 全局约束检查将作为此操作的一部分执行(即ID / IDREF约束)。
rootObj
- 根目录开始验证
JAXBException
- 如果在验证期间发生意外问题
ValidationException
- 如果ValidationEventHandler
从其handleEvent方法返回false,或者Validator无法验证基于rootObj的内容树
IllegalArgumentException
- 如果rootObj参数为空
void setProperty(String name, Object value) throws PropertyException
name
- 要设置的属性的名称。
可以使用常量字段或用户提供的字符串指定此值。
value
- 要设置的属性的值
PropertyException
- 处理给定属性或值时出错
IllegalArgumentException
- 如果name参数为null
Object getProperty(String name) throws PropertyException
name
- 要检索的属性的名称
PropertyException
- 检索给定属性或值属性名称时出现错误
IllegalArgumentException
- 如果name参数为空
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.