如何使用 OVal 验证框架为基于自定义 XML 的方法约束添加其他上下文/值
使用 OVal 验证框架 (http://oval.sourceforge.net/),可以创建自定义注释或基于 XML 的约束 (http://oval.sourceforge.net/userguide.html#d4e493)。
我的目的是根据一些约束定义生成 OVal XML 配置文件,这就是为什么我想使用 XML 配置执行完整的 OVal 约束定义 (http://oval.sourceforge.net/userguide.html#d4e551)。
我想验证类(Attend)的某个方法(getDomain)的返回值,并且我必须为自定义检查类的 isSatisfied 方法添加附加值(六个字符串)。
到目前为止,我的 XML 配置如下所示:
<class type="my.package.Attend"
overwrite="false" applyFieldConstraintsToSetter="true">
<field name="NAME">
<notNull />
<maxLength max="4" />
</field>
<method name="getDomain">
<returnValue>
<my.package.DomainCheck />
</returnValue>
</method>
</class>
我有一个检查器类 DomainCheck,它应该接收来自 getDomain 方法的返回值。 在 DomainCheck 的 isSatisfied 方法中,我必须使用一些必须在 XML 中以某种方式配置的附加参数来验证返回值。
我的第一个问题是,DomainCheck 的 isSatisfied 方法未被调用。 如果我删除方法约束,验证结果将无效,正如我从字段约束中期望的那样。但如果我添加方法约束,则不会调用 DomainCheck 并且验证结果有效(应该仍然无效)。我不明白为什么不调用自定义检查。我的方法约束定义一定有问题。 以下是我的自定义检查类和相应的接口:
package my.package;
import ...
public class DomainCheck extends AbstractAnnotationCheck<Domain> {
public boolean isSatisfied(Object validatedObject, Object valueToValidate, OValContext context, Validator validator) {
if (valueToValidate == null) {
return true;
}
List<?> domainMembers = (ArrayList<?>) valueToValidate;
for (Object domainMember : domainMembers) {
// do validation
}
return false
}
}
package my.package;
import ...
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@net.sf.oval.configuration.annotation.Constraint(checkWith = DomainCheck.class)
public @interface Domain {
String message() default "must be conform to ...";
}
如果这可行,我的第二个问题将是配置附加参数。 我想到了类似的东西:
<method name="getDomain">
<returnValue>
<my.package.DomainCheck />
</returnValue>
<parameter type="java.lang.String">OneName</parameter>
<parameter type="java.lang.String">AnotherName</parameter>
<parameter type="java.lang.String">0</parameter>
<parameter type="java.lang.String">*</parameter>
<parameter type="java.lang.String">5</parameter>
<parameter type="java.lang.String">100</parameter>
</method>
上面的语法是用于定义方法签名的约束,所以这显然不起作用。但我找不到任何可能的定义来实现我的目的。
那么,为什么我的自定义检查没有被调用,如果有解决方案,我如何在 XML 配置中为 isSatisfied 方法定义附加参数以及如何在 isSatisfied 方法中访问它们?
预先感谢您的任何建议! 干杯大卫
With the OVal validation framework (http://oval.sourceforge.net/) it is possible to create custom annotation or XML based constraints (http://oval.sourceforge.net/userguide.html#d4e493).
My intention is to generate an OVal XML configuration file out of some constraint definitions, that's why I would like to do the complete OVal constraint definition with the XML configuration (http://oval.sourceforge.net/userguide.html#d4e551).
I would like to validate the return value of a certain method (getDomain) of a class (Attend) and I have to add additional values (six strings) for the isSatisfied method of my custom check class.
My XML configuration so far looks like this:
<class type="my.package.Attend"
overwrite="false" applyFieldConstraintsToSetter="true">
<field name="NAME">
<notNull />
<maxLength max="4" />
</field>
<method name="getDomain">
<returnValue>
<my.package.DomainCheck />
</returnValue>
</method>
</class>
I have a checker class DomainCheck which should receive the return value from the getDomain method.
In the isSatisfied method of the DomainCheck I have to validate the return value with some additional parameters that I have to configure somehow in the XML.
My first problem is, that the isSatisfied method of the DomainCheck is not invoked.
If I delete the method constraint, the validation result is invalid as I expect it from the field constraint. But if I add the method constraint, the DomainCheck is not invoked and the validation result is valid (should still be invalid). I can not see why the custom check is not invoked. Something must be wrong with my method constraint definition.
Here are my custom check class and the appropriate interface:
package my.package;
import ...
public class DomainCheck extends AbstractAnnotationCheck<Domain> {
public boolean isSatisfied(Object validatedObject, Object valueToValidate, OValContext context, Validator validator) {
if (valueToValidate == null) {
return true;
}
List<?> domainMembers = (ArrayList<?>) valueToValidate;
for (Object domainMember : domainMembers) {
// do validation
}
return false
}
}
package my.package;
import ...
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@net.sf.oval.configuration.annotation.Constraint(checkWith = DomainCheck.class)
public @interface Domain {
String message() default "must be conform to ...";
}
If this would work, my second problem would be to configure the additional parameters.
I thought of something like:
<method name="getDomain">
<returnValue>
<my.package.DomainCheck />
</returnValue>
<parameter type="java.lang.String">OneName</parameter>
<parameter type="java.lang.String">AnotherName</parameter>
<parameter type="java.lang.String">0</parameter>
<parameter type="java.lang.String">*</parameter>
<parameter type="java.lang.String">5</parameter>
<parameter type="java.lang.String">100</parameter>
</method>
The syntax above is for defining constraints for a method signature, so this obviously does not work. But I can not find any possible definition for my purpose.
So, why is my custom check not invoked and if there is a solution for that, how can I define additional parameters for the isSatisfied method in the XML configuration and how do I access them in the isSatisfied method?
Thank you in advance for any advice!
Cheers David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试在此处使用按合同编程功能。为此,您需要执行一些额外的准备工作: http://oval.sourceforge.net /userguide.html#project-preparation
You are trying to use a programming by contract feature here. For this to work you need to perform some additonal preparations: http://oval.sourceforge.net/userguide.html#project-preparation