Jump to: navigation, search

Howto: Register Annotation Validators

Revision as of 01:10, 22 April 2008 by Yuri.strot.xored.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

< 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.

  <extension
        point="org.eclipse.emf.validation.constraintProviders">
     <constraintProvider cache="true">
        <package namespaceUri="http:///org/eclipse/tigerstripe/annotation/example/person.ecore"/>
        <constraints categories="org.eclipse.tigerstripe.annotation.core.validation.category">
           <constraint
                 lang="Java"
                 class="org.eclipse.tigerstripe.annotation.example.person.validators.PersonValidator"
                 severity="ERROR"
                 mode="Live"
                 name="Valid Person Age"
                 id="org.eclipse.tigerstripe.annotation.example.person.validAge"
                 statusCode="1">
              <description>
                 Person age should be non-negative number and non greater than 100 years
              </description>
              <message>
                 Person age can't be {0}
              </message>
              <target class="Person">
                 <event name="Set">
                    <feature name="age"/>
                 </event>
              </target>
           </constraint>
        </constraints>
     </constraintProvider>
  </extension>

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.