|
|
|
|
org.eclipse.emf.validation
Class AbstractModelConstraint
java.lang.Object
org.eclipse.emf.validation.AbstractModelConstraint
-
public abstract class AbstractModelConstraint
- extends
Object
Abstract superclass of all constraint implementations provided via the
constraintProviders extension point in the plug-in manifest XML
whose language is "Java".
The same constraint implementation class may be supplied for multiple
constraints (distinguished by their IDs in the extension XML). In such
cases, the validation system will only create a single instance of the
AbstractModelConstraint , shared by all of the constraint IDs.
Therefore, this instance should not cache or otherwise retain any state
related to a particular constraint or validation operation. If it is
necessary to retain any state, then this information should be indexed by
the constraint ID provided by the
IValidationContext.getCurrentConstraintId() method of the validation
context.
Methods inherited from class java.lang.
Object
|
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
|
AbstractModelConstraint
public AbstractModelConstraint()
- Initializes me.
validate
public abstract
IStatus validate(
IValidationContext ctx)
-
Validates an object in the specified context. The
target of the validation operation
is available from the context object.
Note that it is best to use the
IValidationContext.createSuccessStatus() and
IValidationContext.createFailureStatus(Object...) methods of the context
object to create the status object returned from this method, to ensure
that the status object returned is correctly handled by the validation
system.
A single constraint implementation may check multiple conditions. In such
cases, it can return a
multi-status of
multiple results created by the overloaded variants of the
ConstraintStatus.createStatus(IValidationContext, java.util.Collection, String, Object[])
method. In these cases, also, each resulting status can store a distinct
result locus. For example:
public IStatus validate(IValidationContext ctx) {
List problems = new java.util.ArrayList();
// check the first condition. This method adds results to the
// ctx's result locus if it finds a problem
IStatus problem = checkFirstCondition(ctx);
if (problem != null) problems.add(problem);
// check another condition, involving different objects
problem = checkSecondCondition(ctx);
if (problem != null) problems.add(problem);
return problems.isEmpty()? ctx.createSuccessStatus() :
ConstraintStatus.createMultiStatus(ctx, problems);
}
private IStatus checkFirstCondition(IValidationContext ctx) {
EObject target = ctx.getTarget();
Collection problemElements = ...; // collect problem elements
boolean ok = ... ; // check the target and some related objects
return ok? null : ConstraintStatus.createStatus(
ctx,
problemElements,
"Problem with {0}",
new Object[] {problemElements});
}
private IStatus checkSecondCondition(IValidationContext ctx) ...
-
-
Parameters:
-
ctx - the validation context that provides access to the current
constraint evaluation environment. The framework will never
pass a null value
-
Returns:
- the status of validation of the target object. The
IStatus.getSeverity() of the record is either
IStatus.OK to indicate success,
or some other value to indicate that validation failed.
Must not return null
-
See Also:
-
IValidationContext.createSuccessStatus() ,
IValidationContext.createFailureStatus(Object...) ,
ConstraintStatus.createStatus(IValidationContext, java.util.Collection, String, Object[]) ,
ConstraintStatus.createMultiStatus(IValidationContext, Collection)
|
|
|