You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dw...@apache.org on 2002/03/30 05:28:36 UTC

cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator Validator.java ValidatorResult.java ValidatorResults.java

dwinterfeldt    02/03/29 20:28:36

  Modified:    validator/src/share/org/apache/commons/validator
                        Validator.java ValidatorResult.java
                        ValidatorResults.java
  Log:
  Added the ability for ValidatorResults and ValidatorResult to have an object returned from a validation method be associated with the result status.
  
  Revision  Changes    Path
  1.7       +7 -1      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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Validator.java	17 Mar 2002 18:30:55 -0000	1.6
  +++ Validator.java	30 Mar 2002 04:28:35 -0000	1.7
  @@ -1,4 +1,9 @@
   /*
  + * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java,v 1.7 2002/03/30 04:28:35 dwinterfeldt Exp $
  + * $Revision: 1.7 $
  + * $Date: 2002/03/30 04:28:35 $
  + *
  + * ====================================================================
    *
    * The Apache Software License, Version 1.1
    *
  @@ -82,6 +87,7 @@
    * and the validation rules for a JavaBean.</p>
    *
    * @author David Winterfeldt
  + * @version $Revision: 1.7 $ $Date: 2002/03/30 04:28:35 $
   */
   public class Validator implements Serializable {
   
  @@ -474,7 +480,7 @@
                              iErrorCount++;
                           }
                            
  -                        results.add(field, va.getName(), isValid(result));
  +                        results.add(field, va.getName(), isValid(result), result);
         	       } catch (Exception e) {
         	          bErrors = true;
         	          log.error("reflection: " + e.getMessage(), e);
  
  
  
  1.2       +66 -2     jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResult.java
  
  Index: ValidatorResult.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResult.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ValidatorResult.java	17 Mar 2002 00:15:53 -0000	1.1
  +++ ValidatorResult.java	30 Mar 2002 04:28:35 -0000	1.2
  @@ -1,4 +1,9 @@
   /*
  + * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResult.java,v 1.2 2002/03/30 04:28:35 dwinterfeldt Exp $
  + * $Revision: 1.2 $
  + * $Date: 2002/03/30 04:28:35 $
  + *
  + * ====================================================================
    *
    * The Apache Software License, Version 1.1
    *
  @@ -58,6 +63,7 @@
   package org.apache.commons.validator;
   
   import java.io.Serializable;
  +import java.util.Collections;
   import java.util.HashMap;
   import java.util.Map;
   
  @@ -67,6 +73,7 @@
    * validation rules processed on JavaBean.</p>
    *
    * @author David Winterfeldt
  + * @version $Revision: 1.2 $ $Date: 2002/03/30 04:28:35 $
   */
   public class ValidatorResult implements Serializable {
   
  @@ -95,7 +102,14 @@
       * Add the result of a validator action.
      */
      public void add(String validatorName, boolean result) {
  -      hAction.put(validatorName, new Boolean(result));
  +      add(validatorName, result, null);
  +   }
  +
  +   /**
  +    * Add the result of a validator action.
  +   */
  +   public void add(String validatorName, boolean result, Object value) {
  +      hAction.put(validatorName, new ResultStatus(result, value));
      }
   
      public boolean containsAction(String validatorName) {
  @@ -105,7 +119,57 @@
      public boolean isValid(String validatorName) {
         Object o = hAction.get(validatorName);	
         
  -      return ((o != null) ? ((Boolean)o).booleanValue() : false);
  +      return ((o != null) ? ((ResultStatus)o).getValid() : false);
  +   }
  +
  +   public Map getActionMap() {
  +      return Collections.unmodifiableMap(hAction);
  +   }
  +      
  +   /**
  +    * Contains the status of the validation.
  +   */
  +   protected class ResultStatus {
  +      private boolean valid = false;
  +      private Object result = null;
  +      
  +      public ResultStatus(boolean valid, Object result) {
  +         this.valid = valid;
  +         this.result = result;
  +      }
  +      
  +      /**
  +       * Gets whether or not the validation passed.
  +      */
  +      public boolean getValid() {
  +         return valid;
  +      }
  +
  +      /**
  +       * Sets whether or not the validation passed.
  +      */
  +      public void setValid(boolean valid) {
  +         this.valid = valid;
  +      }
  +
  +      /**
  +       * Gets the result returned by a validation method.  
  +       * This can be used to retrieve to the correctly 
  +       * typed value of a date validation for example.
  +      */
  +      public Object getResult() {
  +         return result;
  +      }
  +
  +      /**
  +       * Sets the result returned by a validation method.  
  +       * This can be used to retrieve to the correctly 
  +       * typed value of a date validation for example.
  +      */
  +      public void setResult(Object result) {
  +         this.result = result;
  +      }
  +
      }
      
   }
  
  
  
  1.2       +45 -3     jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResults.java
  
  Index: ValidatorResults.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResults.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ValidatorResults.java	17 Mar 2002 00:15:53 -0000	1.1
  +++ ValidatorResults.java	30 Mar 2002 04:28:35 -0000	1.2
  @@ -1,4 +1,9 @@
   /*
  + * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResults.java,v 1.2 2002/03/30 04:28:35 dwinterfeldt Exp $
  + * $Revision: 1.2 $
  + * $Date: 2002/03/30 04:28:35 $
  + *
  + * ====================================================================
    *
    * The Apache Software License, Version 1.1
    *
  @@ -69,6 +74,7 @@
    * validation rules processed on JavaBean.</p>
    *
    * @author David Winterfeldt
  + * @version $Revision: 1.2 $ $Date: 2002/03/30 04:28:35 $
   */
   public class ValidatorResults implements Serializable {
   
  @@ -81,6 +87,13 @@
       * Add a the result of a validator action.
      */
      public void add(Field field, String validatorName, boolean bResult) {
  +      add(field, validatorName, bResult, null);
  +   }
  +
  +   /**
  +    * Add a the result of a validator action.
  +   */
  +   public void add(Field field, String validatorName, boolean bResult, Object value) {
         ValidatorResult result = null;
         
         if (hResults.containsKey(field.getKey())) {
  @@ -89,7 +102,7 @@
            result = new ValidatorResult(field);
         }
         
  -      result.add(validatorName, bResult);
  +      result.add(validatorName, bResult, value);
         
         hResults.put(field.getKey(), result);
      }
  @@ -125,7 +138,7 @@
       * Return the set of all recorded messages, without distinction
       * by which property the messages are associated with.  If there are
       * no messages recorded, an empty enumeration is returned.
  -    */
  +   */
      public Iterator get() {
         if (hResults.size() == 0) {
            return (Collections.EMPTY_LIST.iterator());
  @@ -175,5 +188,34 @@
      public Iterator properties() {
         return (hResults.keySet().iterator());
      }
  -
  +   
  +   /**
  +    * Get a <code>Map</code> of any <code>Object</code>s 
  +    * returned from validation routines.
  +   */
  +   public Map getResultValueMap() {
  +      Map results = new HashMap();
  +      
  +      for (Iterator i = hResults.keySet().iterator(); i.hasNext(); ) {
  +         String propertyKey = (String)i.next();
  +         ValidatorResult vr = (ValidatorResult)hResults.get(propertyKey);
  +         
  +         Map hActions = vr.getActionMap();
  +         for (Iterator x = hActions.keySet().iterator(); x.hasNext(); ) {
  +            String actionKey = (String)x.next();   
  +            ValidatorResult.ResultStatus rs = (ValidatorResult.ResultStatus)hActions.get(actionKey);
  +
  +            if (rs != null) {
  +               Object result = rs.getResult();
  +
  +               if (result != null && !(result instanceof Boolean)) {
  +                  results.put(propertyKey, result);
  +               }
  +            }
  +         }   
  +      }
  +      
  +      return results;
  +   }
  +   
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>