Jump to: navigation, search

Howto: Register Annotation Validators

< To: Tigerstripe_APIs

To validate annotation you can use standard EMF Validation Framework. The EMF validation framework provides a means to evaluate and ensure the well-formedness of EMF models. More information about Validation Framework features can be found here: http://help.eclipse.org/help33/nav/25.

Validation comes in two forms: batch and live. While batch validation allows a client to explicitly evaluate a group of EObjects and their contents, live validation can be used by clients to listen on change notifications to EObjects to immediately verify that the change does not violate the well-formedness of the model. Annotation Framework automatically support live validation for all annotation object, i.e. every annotation object listen on change notifications and verify it using registered validators.

Validation Framework provide several ways to register EMF constraints. All of them can be found in the EMF Validation Framework Developer Guide. One of them represented in the org.eclipse.tigerstripe.annotation.example.person.validators plug-in. Constraint can be registered using constraint provider extension point and constraint class.

     <constraintProvider cache="true">
        <package namespaceUri="http:///org/eclipse/tigerstripe/annotation/example/person.ecore"/>
        <constraints categories="org.eclipse.tigerstripe.annotation.core.validation.category">
                 name="Valid Person Age"
                 Person age should be non-negative number and non greater than 100 years
                 Person age can't be {0}
              <target class="Person">
                 <event name="Set">
                    <feature name="age"/>

This constraint provider use "org.eclipse.tigerstripe.annotation.core.validation.category", which contributed by Annotation Framework. This category bound to the specific client context, allow to check validation status every time, when annotation object would be changed. But client can use own category if needed. Note than we obviously point to live mode (mode="Live"). If this mode would be "Batch", it wouldn't be automatically checked with an Annotation Framework. This extension point contribute person age constraint which do not allow to set age to negative number or set it more than 100 years old. The last thing is add constraint class:

 public class PersonValidator extends AbstractModelConstraint {
     public IStatus validate(IValidationContext ctx) {
     	  Person person = (Person)ctx.getTarget();
     	  int age = person.getAge();
     	  if (age < 0)
     	      return ctx.createFailureStatus(new Object[] { "negative" });
     	  if (age >= 100)
     	      return ctx.createFailureStatus(new Object[] { "greater than 100 years" });
     	  return ctx.createSuccessStatus();

Annotation Framework automatically add live adapter to the Person object, when it will be used as an annotation object. After this things we wouldn't change age to negative and more than 100 values.