-
已过时。自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
。If the client application does not set an event handler on their
Validator
,Unmarshaller
, orMarshaller
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 onValidator
,Unmarshaller
, orMarshaller
. - Implement and register a custom event handler
-
Client applications that require sophisticated event processing can implement the
ValidationEventHandler
interface and register it with theUnmarshaller
and/orValidator
. -
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 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
ValidationEventCollector
utility class must terminate processing after being notified of a fatal error. Client applications that supply their ownValidationEventHandler
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
- 另请参见:
-
JAXBContext
,Unmarshaller
,ValidationEventHandler
,ValidationEvent
,ValidationEventCollector
-
-
方法摘要
所有方法 接口方法 抽象方法 弃用的方法 Modifier and Type 方法 描述 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起
-
-
-
方法详细信息
-
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参数为空
-
-