commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dgra...@apache.org
Subject cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator Validator.java
Date Sat, 24 May 2003 20:09:39 GMT
dgraham     2003/05/24 13:09:39

  Modified:    validator/src/share/org/apache/commons/validator
                        Validator.java
  Log:
  Refactored handling of indexed field retrieval as suggested in 
  PR# 18689.
  
  Revision  Changes    Path
  1.23      +61 -72    jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java
  
  Index: Validator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- Validator.java	24 May 2003 19:40:12 -0000	1.22
  +++ Validator.java	24 May 2003 20:09:39 -0000	1.23
  @@ -561,24 +561,12 @@
           int pos,
           List params,
           Object[] paramValue)
  -        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
  +        throws ValidatorException {
               
           int beanIndexPos = params.indexOf(BEAN_KEY);
           int fieldIndexPos = params.indexOf(FIELD_KEY);
           
  -        Object oIndexed =
  -            PropertyUtils.getProperty(
  -                this.getParameterValue(BEAN_KEY),
  -                field.getIndexedListProperty());
  -                
  -        Object indexedList[] = new Object[0];
  -        
  -        if (oIndexed instanceof Collection) {
  -            indexedList = ((Collection) oIndexed).toArray();
  -            
  -        } else if (oIndexed.getClass().isArray()) {
  -            indexedList = (Object[]) oIndexed;
  -        }
  +        Object indexedList[] = this.getIndexedProperty(field);
           
           // Set current iteration object to the parameter array
           paramValue[beanIndexPos] = indexedList[pos];
  @@ -591,7 +579,7 @@
                   indexedField.getKey(),
                   Field.TOKEN_INDEXED,
                   "[" + pos + "]"));
  -                
  +        
           paramValue[fieldIndexPos] = indexedField;
       }
   
  @@ -602,58 +590,17 @@
        */
       private void validateField(Field field, ValidatorResults allResults)
           throws ValidatorException {
  -            
  -        int length = 1; // default to non-indexed length of 1
  -        
  -        // this block only finds out how many times to run the validation
  -        if (field.isIndexed()) {
  -            Object oIndexed;
  -            try {
  -                oIndexed =
  -                    PropertyUtils.getProperty(
  -                        this.getParameterValue(BEAN_KEY),
  -                        field.getIndexedListProperty());
  -        
  -            } catch (Exception e) {
  -                log.error("in validateField", e);
  -                return;
  -            }
  -        
  -            Object indexedList[] = new Object[0];
  -        
  -            if (oIndexed instanceof Collection) {
  -                indexedList = ((Collection) oIndexed).toArray();
  -            } else if (oIndexed.getClass().isArray()) {
  -                indexedList = (Object[]) oIndexed;
  -            }
  -        
  -            length = indexedList.length;
  -        }
  -        
  -        this.validateList(field, allResults, length);
  -    }
   
  -    /**
  -     * Runs all validations on the field.
  -     * @param field
  -     * @param allResults
  -     * @param length 1 for non-indexed fields, the array length for indexed fields.
  -     * @throws ValidatorException
  -     */
  -    private void validateList(
  -        Field field,
  -        ValidatorResults allResults,
  -        int length)
  -        throws ValidatorException {
  -            
  -        Map actions = resources.getValidatorActions();
  -            
  -        for (int pos = 0; pos < length; pos++) {
  -            ValidatorResults results = new ValidatorResults();
  +        int numberOfFieldsToValidate =
  +            field.isIndexed() ? this.getIndexedProperty(field).length : 1;
  +
  +        Map actions = this.resources.getValidatorActions();
  +
  +        for (int fieldNumber = 0; fieldNumber < numberOfFieldsToValidate; fieldNumber++)
{
               StringTokenizer st = new StringTokenizer(field.getDepends(), ",");
               while (st.hasMoreTokens()) {
                   String depend = st.nextToken().trim();
  -        
  +
                   ValidatorAction action = (ValidatorAction) actions.get(depend);
                   if (action == null) {
                       log.error(
  @@ -661,19 +608,61 @@
                               + depend
                               + " found for field "
                               + field.getProperty());
  -                            
  +
                       return;
                   }
  -        
  -                boolean good = validateFieldForRule(field, action, results, actions, pos);
  -        
  +
  +                ValidatorResults results = new ValidatorResults();
  +                boolean good =
  +                    validateFieldForRule(field, action, results, actions, fieldNumber);
  +
                   allResults.merge(results);
  -        
  +
                   if (!good) {
                       return;
                   }
               }
           }
  +    }
  +    
  +    /**
  +     * Returns an indexed property from the object we're validating.
  +     * 
  +     * @param field This field.getIndexedListProperty() will be found in the object we're
  +     * currently validating
  +     * @throws ValidatorException If there's an error looking up the property or, the 
  +     * property found is not indexed.
  +     */
  +    private Object[] getIndexedProperty(Field field) throws ValidatorException {
  +        Object indexedProperty = null;
  +
  +        try {
  +            indexedProperty =
  +                PropertyUtils.getProperty(
  +                    this.getParameterValue(BEAN_KEY),
  +                    field.getIndexedListProperty());
  +                    
  +        } catch (IllegalAccessException e) {
  +            throw new ValidatorException(e.getMessage());
  +        } catch (InvocationTargetException e) {
  +            throw new ValidatorException(e.getMessage());
  +        } catch (NoSuchMethodException e) {
  +            throw new ValidatorException(e.getMessage());
  +        }
  +
  +        if (indexedProperty instanceof Collection) {
  +            return ((Collection) indexedProperty).toArray();
  +            
  +        } else if (indexedProperty.getClass().isArray()) {
  +            return (Object[]) indexedProperty;
  +            
  +        } else {
  +            throw new ValidatorException(
  +                "Non-collection, non-array indexed property "
  +                    + field.getKey()
  +                    + " found");
  +        }
  +
       }
   
       /**
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message