FileDocCategorySizeDatePackage
Validator.javaAPI DocGlassfish v2 API12553Fri May 04 22:34:58 BST 2007com.sun.enterprise.tools.common.validation

Validator

public class Validator extends Object
Validator is an Object that knows how to validate its corresponding Object called {@link Validatee}

Every Validatee has a corresponding {@link Validator} object and this object knows how to validate its Validatee. Validator maintains a list of {@link Constraint}s that needs to be applied for each of the element of its Validatee. Constraint objects are built using an two file , Validation File and Constraints File. {@ValidationManager} is an object that constructs the Validator objects for all the Validatees. ValidationManager maintains a map of xpaths to Validators. It constructs this by reading two files - Validation File and Constraints File. Validation File specifies constraints to be applied to the elements. Constraints File provides information about the Constraints such as Constraints class name, number and type of Constraint's constructor arguments.

Validations are performed, recursively. Two types of Validations are perfomed, Structural validations and specified validations. Structural validations are expressed through {@link CardinalConstraint}. CardinalConstraint is an implicit constraint. Its always applied to each of the element; you dont need to specify it explicitly. Whereas , specified Constaints need to be explicitly specified for each element you wanted to apply this Constaint to. You can also define, your own custom Constraints and apply them to any elements.

see
Constraint
see
Validator
see
ValidationManager
see
CardianlConstraint
author
Rajeshwar Patil
version
%I%, %G%

Fields Summary
private static String
SIZE_PREFIX
private HashMap
elementToConstraints
A map that stores Constraint lists for the elements of the Validatee that this Object corresponds to.
private com.sun.enterprise.tools.common.validation.util.Utils
utils
Constructors Summary
Validator()
Creates a new instance of Validator


           
     
        elementToConstraints = new HashMap();
        utils = new Utils();
    
Methods Summary
voidaddElementConstraint(java.lang.String element, com.sun.enterprise.tools.common.validation.constraints.Constraint constraint)
Adds the given Constraint for the given element in the map maintained by this object. This maps maintains Constraint lists for each of the elements of the Corresponding Validatee.

param
element the element for which the given constraint to be added to its list
param
constraint the given Constraint to be added to the list of Constraints of the given element.

        ArrayList list = (ArrayList) elementToConstraints.get(element);
        if(null == list) {
            list = new ArrayList();
        }
        list.add(constraint);
        elementToConstraints.put(element, list);
    
java.util.Collectionvalidate(java.lang.String elementName, java.lang.String elementDtdName, com.sun.enterprise.tools.common.validation.Validatee validatee)
Validates the given element. The given element could be a single element or an array.

param
elementName the element to be validated
param
elementDtdName the dtd name of the element to be validated
param
validatee the Validatee object that this object corresponds to.
return
Collection the Collection of ConstraintFailure Objects. Collection is empty if there are no failures.

        ArrayList failures = new ArrayList();
        boolean isIndexed = validatee.isIndexed(elementName);

        if(isIndexed){
            failures.addAll(validateProperties(elementName,
                elementDtdName, validatee));
        } else {
            failures.addAll(validateProperty(elementName,
                elementDtdName, validatee));
        }
        return failures;
    
private java.util.Collectionvalidate(java.lang.String value, java.util.ArrayList constraints, java.lang.String name)
Applies the given set of constraints to the given string Returns set of constraints that the given string fails to satisfy.

param
value the value to be validated
param
constraints the given set of Constraints
param
name the name of the element being validated
return
Collection the Collection of ConstraintFailure Objects. Collection is empty if there are no failures.

        ArrayList failed_constrains = new ArrayList();
        int size = constraints.size();
        Constraint constraint =  null;
        for(int i=0; i<size; i++) {
            constraint = (Constraint)constraints.get(i);
            failed_constrains.addAll(constraint.match(value, name));
        }
        return failed_constrains;
    
java.util.CollectionvalidateIndividualProperty(java.lang.String property, java.lang.String absoluteDtdName, java.lang.String fieldName)
Validates the given Element.

param
property the Element to be validated
param
absoluteDtdName the complete dtd name of property
param
fieldName the name of the GUI field, associated with property
return
Collection the Collection of ConstraintFailure Objects. Collection is empty if there are no failures.

        //This method  --fetches the constraint list for the specified element
        //             --applies the constraints to the specified element

        ArrayList failures = new ArrayList();
        String elementDtdName = utils.getName(absoluteDtdName,
                Constants.XPATH_DELIMITER_CHAR);

        ArrayList constraintList =
            (ArrayList)elementToConstraints.get(elementDtdName);

        if(constraintList != null) {
            failures.addAll(validate(property, constraintList, fieldName));
        } else {
            ///String format = 
            ///    BundleReader.getValue("MSG_No_definition_for");      //NOI18N
            ///    Object[] arguments = 
            ///        new Object[]{"Constraints", absoluteDtdName};    //NOI18N
            ///System.out.println(MessageFormat.format(format, arguments));
        }
        return failures;
    
private java.util.CollectionvalidateProperties(java.lang.String elementName, java.lang.String elementDtdName, com.sun.enterprise.tools.common.validation.Validatee validatee)
Validates the given element. The given element is an array element.

param
elementName the element to be validated
param
elementDtdName the dtd name of the element to be validated
param
validatee the Validatee object that this object corresponds to.
return
Collection the Collection of ConstraintFailure Objects. Collection is empty if there are no failures.

        ArrayList failures = new ArrayList();
        int noOfElements = 0;
        String sizeMethodName =  utils.methodNameFromBeanName(
            elementName, SIZE_PREFIX);
        Method sizeMethod = validatee.getMethod(sizeMethodName);
        noOfElements = ((Integer)validatee.invoke(sizeMethod)).intValue();
        
        ArrayList constraintList =
            (ArrayList)elementToConstraints.get(elementDtdName);
        if(constraintList != null) {
            for(int i=0; i<noOfElements; i++){
                String property = (String)validatee.getElement(elementName,
                    i);
                String name = validatee.getXPath() + Constants.XPATH_DELIMITER +
                    utils.getIndexedName(elementDtdName, i);
                failures.addAll(validate(property, constraintList, name));
            }
        } else {
            ///String format = 
            ///    BundleReader.getValue("MSG_No_definition_for");      //NOI18N
            ///    Object[] arguments = 
            ///        new Object[]{"Constraints", elementName};        //NOI18N
            ///System.out.println(MessageFormat.format(format, arguments));
        }
        return failures;
    
private java.util.CollectionvalidateProperty(java.lang.String elementName, java.lang.String elementDtdName, com.sun.enterprise.tools.common.validation.Validatee validatee)
Validates the given element. The given element is a scalar element.

param
elementName the element to be validated
param
elementDtdName the dtd name of the element to be validated
param
validatee the Validatee object that this object corresponds to.
return
Collection the Collection of ConstraintFailure Objects. Collection is empty if there are no failures.

        //This method  --fetches the value of the specified element
        //             --fetches the constraint list for the specified element
        //             --applies the constraints to the specified element
        
        ArrayList failures = new ArrayList();
        ArrayList constraintList =
            (ArrayList)elementToConstraints.get(elementDtdName);
        String name = validatee.getXPath() + Constants.XPATH_DELIMITER +
            elementDtdName;
        if(constraintList != null) {
            String property = (String)validatee.getElement(elementName);
            failures.addAll(validate(property, constraintList, name));
        } else {
            ///String format = 
            ///    BundleReader.getValue("MSG_No_definition_for");      //NOI18N
            ///    Object[] arguments = new Object[]{"Constraints", name}; //NOI18N
            ///System.out.println(MessageFormat.format(format, arguments));
        }
        return failures;