cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/validationruleimpl AbstractValidationRule.java RangeValidationRule.java
Date Tue, 30 Dec 2003 20:22:08 GMT
vgritsenko    2003/12/30 12:22:08

  Modified:    src/blocks/woody/java/org/apache/cocoon/woody/datatype/validationruleimpl
                        AbstractValidationRule.java
                        RangeValidationRule.java
  Log:
  Range is applicable not only to numbers, but to any comparables (example: dates).
  Reduce code duplication.
  Add evaluateComparable (increase code duplication).
  
  Revision  Changes    Path
  1.3       +34 -3     cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/validationruleimpl/AbstractValidationRule.java
  
  Index: AbstractValidationRule.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/validationruleimpl/AbstractValidationRule.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractValidationRule.java	13 Nov 2003 13:19:09 -0000	1.2
  +++ AbstractValidationRule.java	30 Dec 2003 20:22:08 -0000	1.3
  @@ -107,11 +107,42 @@
           } catch (CannotYetResolveWarning w) {
               return w;
           } catch (ExpressionException e) {
  -            return new ValidationError("Error evaluating \"" + exprName + "\" expression
on \"" + ruleName + "\" validation rule", false);
  +            return new ValidationError("Error evaluating \"" + exprName + "\" expression
on \"" +
  +                                       ruleName + "\" validation rule", false);
           }
  -        if (!(expressionResult instanceof BigDecimal))
  -            return new ValidationError("Got non-numeric result from \"" + exprName + "\"
expression on \"" + ruleName + "\" validation rule", false);
  +        
  +        if (!(expressionResult instanceof BigDecimal)) {
  +            return new ValidationError("Got non-numeric result from \"" + exprName + "\"
expression on \"" +
  +                                       ruleName + "\" validation rule", false);
  +        }
  +
           return expressionResult;
       }
   
  +    /**
  +     * Helper method for evaluating expressions whose result is comparable.
  +     *
  +     * @param exprName a name for the expression that's descriptive for the user, e.g.
the name of the attribute in which it was defined
  +     * @param ruleName a descriptive name for the validation rule, usually the rule's element
name
  +     * @return either a ValidationError (because expression evaluation failed) or a CannotYetResolveWarning
  +     * (because another, required field referenced in the expression has not yet a value),
or a BigDecimal.
  +     */
  +    protected Object evaluateComparable(Expression expression, ExpressionContext expressionContext,
String exprName, String ruleName) {
  +        Object expressionResult;
  +        try {
  +            expressionResult = expression.evaluate(expressionContext);
  +        } catch (CannotYetResolveWarning w) {
  +            return w;
  +        } catch (ExpressionException e) {
  +            return new ValidationError("Error evaluating \"" + exprName + "\" expression
on \"" +
  +                                       ruleName + "\" validation rule", false);
  +        }
  +        
  +        if (!(expressionResult instanceof Comparable)) {
  +            return new ValidationError("Got non-comparable result from \"" + exprName +
"\" expression on \"" +
  +                                       ruleName + "\" validation rule", false);
  +        }
  +
  +        return expressionResult;
  +    }
   }
  
  
  
  1.5       +52 -42    cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/validationruleimpl/RangeValidationRule.java
  
  Index: RangeValidationRule.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/validationruleimpl/RangeValidationRule.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- RangeValidationRule.java	13 Nov 2003 13:15:41 -0000	1.4
  +++ RangeValidationRule.java	30 Dec 2003 20:22:08 -0000	1.5
  @@ -50,14 +50,14 @@
   */
   package org.apache.cocoon.woody.datatype.validationruleimpl;
   
  +import java.math.BigDecimal;
  +
  +import org.apache.cocoon.woody.Constants;
   import org.apache.cocoon.woody.datatype.ValidationError;
   import org.apache.cocoon.woody.formmodel.CannotYetResolveWarning;
   import org.apache.cocoon.woody.util.I18nMessage;
  -import org.apache.cocoon.woody.Constants;
  -import org.outerj.expression.ExpressionContext;
   import org.outerj.expression.Expression;
  -
  -import java.math.BigDecimal;
  +import org.outerj.expression.ExpressionContext;
   
   /**
    * Checks numeric ranges. Works for Integer, Long and BigDecimal values.
  @@ -87,57 +87,67 @@
       }
   
       public ValidationError validate(Object value, ExpressionContext expressionContext)
{
  -        BigDecimal decimal = null;
  -        if (value instanceof Integer)
  +        Comparable decimal;
  +        if (value instanceof Integer) {
               decimal = new BigDecimal(((Integer) value).intValue());
  -		else if (value instanceof Long)
  -			decimal = new BigDecimal(((Long) value).longValue()); 
  -        else
  -            decimal = (BigDecimal) value;
  -
  -        if (minExpr != null && maxExpr != null) {
  -            Object result = evaluateNumeric(minExpr, expressionContext, MIN_ATTR, RANGE_ELEM);
  -            if (result instanceof ValidationError)
  +        } else if (value instanceof Long) {
  +            decimal = new BigDecimal(((Long) value).longValue()); 
  +        } else {
  +            decimal = (Comparable) value;
  +        }
  +
  +        Comparable min = null; 
  +        if (minExpr != null) {
  +            Object result = evaluateComparable(minExpr, expressionContext, MIN_ATTR, RANGE_ELEM);
  +            if (result instanceof ValidationError) {
                   return (ValidationError) result;
  -            else if (result instanceof CannotYetResolveWarning)
  +            } else if (result instanceof CannotYetResolveWarning) {
                   return null;
  -            BigDecimal min = (BigDecimal) result;
  -
  -            result = evaluateNumeric(maxExpr, expressionContext, MAX_ATTR, RANGE_ELEM);
  -            if (result instanceof ValidationError)
  +            }
  +            min = (Comparable) result;
  +        }
  +        
  +        Comparable max = null;
  +        if (maxExpr != null) {
  +            Object result = evaluateComparable(maxExpr, expressionContext, MAX_ATTR, RANGE_ELEM);
  +            if (result instanceof ValidationError) {
                   return (ValidationError) result;
  -            else if (result instanceof CannotYetResolveWarning)
  +            } else if (result instanceof CannotYetResolveWarning) {
                   return null;
  -            BigDecimal max = (BigDecimal) result;
  +            }
  +            max = (Comparable) result;
  +        }
  +        
  +        if (min != null && max != null) {
  +            if (decimal.compareTo(min) < 0 || decimal.compareTo(max) > 0) {
  +                return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.numeric.range",
  +                                                                                 new String[]{min.toString(),
max.toString()},
  +                                                                                 Constants.I18N_CATALOGUE));
  +            }
   
  -            if (decimal.compareTo(min) < 0 || decimal.compareTo(max) > 0)
  -                return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.numeric.range",
new String[]{min.toString(), max.toString()}, Constants.I18N_CATALOGUE));
               return null;
  -        } else if (minExpr != null) {
  -            Object result = evaluateNumeric(minExpr, expressionContext, MIN_ATTR, RANGE_ELEM);
  -            if (result instanceof ValidationError)
  -                return (ValidationError) result;
  -            else if (result instanceof CannotYetResolveWarning)
  -                return null;
  -            BigDecimal min = (BigDecimal) result;
  -            if (decimal.compareTo(min) < 0)
  -                return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.numeric.min",
new String[]{min.toString()}, Constants.I18N_CATALOGUE));
  +        } else if (min != null) {
  +            if (decimal.compareTo(min) < 0) {
  +                return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.numeric.min",
  +                                                                                 new String[]{min.toString()},
  +                                                                                 Constants.I18N_CATALOGUE));
  +            }
  +
               return null;
  -        } else if (maxExpr != null) {
  -            Object result = evaluateNumeric(maxExpr, expressionContext, MAX_ATTR, RANGE_ELEM);
  -            if (result instanceof ValidationError)
  -                return (ValidationError) result;
  -            else if (result instanceof CannotYetResolveWarning)
  -                return null;
  -            BigDecimal max = (BigDecimal) result;
  -            if (decimal.compareTo(max) > 0)
  -                return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.numeric.max",
new String[]{max.toString()}, Constants.I18N_CATALOGUE));
  +        } else if (max != null) {
  +            if (decimal.compareTo(max) > 0) {
  +                return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.numeric.max",
  +                                                                                 new String[]{max.toString()},
  +                                                                                 Constants.I18N_CATALOGUE));
  +            }
  +
               return null;
           }
  +
           return null;
       }
   
       public boolean supportsType(Class clazz, boolean arrayType) {
  -        return (clazz.isAssignableFrom(Integer.class) || clazz.isAssignableFrom(Long.class)
|| clazz.isAssignableFrom(BigDecimal.class)) && !arrayType;
  +        return Comparable.class.isAssignableFrom(clazz) && !arrayType;
       }
   }
  
  
  

Mime
View raw message