0

With the Oval validation framework (http://oval.sourceforge.net/) it is possible to create custom annotation 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 can define all the constraints I want in the XML configuration file except for the custom constraint.

Annotation based I have the following code:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import net.sf.oval.exception.ConstraintsViolatedException;

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@net.sf.oval.configuration.annotation.Constraint(checkWith = UpperCaseCheck.class)
public @interface UpperCase {

    /**
     * Message to be used for the ConstraintsViolatedException
     * 
     * @see ConstraintsViolatedException
     */
    String message() default "must be upper case";
}


import net.sf.oval.Validator;
import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
import net.sf.oval.context.OValContext;

public class UpperCaseCheck extends AbstractAnnotationCheck<UpperCase> {

    private static final long serialVersionUID = 2715187850228144730L;

    public boolean isSatisfied(Object validatedObject, Object valueToValidate, OValContext context, Validator validator) {
        if (valueToValidate == null)
            return true;

        String val = valueToValidate.toString();

        return val.equals(val.toUpperCase());
    }

}

And the annotation check:

@NotNull
@UpperCase
private String p_name;

How do I define the custom constraint check (UpperCase) in my XML configuration?

1 Answers1

0

Simply reference your constraint by the fully qualified class name of the check implementation in the xml file, e.g. <mypackage.UpperCaseCheck />