You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ba...@apache.org on 2001/05/28 23:25:22 UTC

cvs commit: xml-cocoon2/src/org/apache/cocoon/acting AbstractValidatorAction.java

balld       01/05/28 14:25:22

  Modified:    src/org/apache/cocoon/acting AbstractValidatorAction.java
  Log:
  patchfrom christian haul to add functionality to the AbstractValidationAction
  
  Revision  Changes    Path
  1.3       +259 -8    xml-cocoon2/src/org/apache/cocoon/acting/AbstractValidatorAction.java
  
  Index: AbstractValidatorAction.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/acting/AbstractValidatorAction.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractValidatorAction.java	2001/05/11 08:50:23	1.2
  +++ AbstractValidatorAction.java	2001/05/28 21:25:22	1.3
  @@ -1,4 +1,4 @@
  -// $Id: AbstractValidatorAction.java,v 1.2 2001/05/11 08:50:23 mman Exp $
  +// $Id: AbstractValidatorAction.java,v 1.3 2001/05/28 21:25:22 balld Exp $
   package org.apache.cocoon.acting;
   
   import org.apache.avalon.framework.configuration.Configurable;
  @@ -6,6 +6,9 @@
   import org.apache.avalon.framework.logger.AbstractLoggable;
   import org.apache.log.Logger;
   
  +import org.apache.regexp.RE;
  +import org.apache.regexp.RESyntaxException;
  +
   import java.util.Map;
   
   /**
  @@ -62,16 +65,40 @@
    * really is null or empty. Long numbers may be specified in decimal, hex or
    * octal values as accepted by java.Lang.decode (String s).
    *
  + * <h3>Constraints</h3>
  + * <table border="1">
  + * <tr>
  + *     <td>matches-regex</td><td>POSIX regular expression</td>
  + * </tr>
  + * <tr>
  + *     <td>min-len</td><td>positive integer</td>
  + * </tr>
  + * <tr>
  + *     <td>max-len</td><td>positive integer</td>
  + * </tr>
  + * <tr>
  + *     <td>min</td><td>Double / Long</td>
  + * </tr>
  + * <tr>
  + *     <td>max</td><td>Double / Long</td>
  + * </tr>
  + * </table>
  + * Constraints can be defined globally for a parameter and can be overridden
  + * by redefinition in a constraint-set. Thus if e.g. a database field can take
  + * at maximum 200 character, this property can be set globally.
  + * 
  + *
    * <h3>The attributes recognized in "constraint-set"</h3>
  - * <strong>FIXME: this works only with strings for now</strong>
    * <table>
    * <tr>
    *     <td>equals-to-param</td><td>parameter name</td>
  + * </tr>
  + * <tr>
    *     <td>equals-to</td><td>string constant</td>
    * </tr>
    * </table>
    * @author Martin Man &lt;Martin.Man@seznam.cz&gt;
  - * @version CVS $Revision: 1.2 $ $Date: 2001/05/11 08:50:23 $
  + * @version CVS $Revision: 1.3 $ $Date: 2001/05/28 21:25:22 $
    */
   public abstract class AbstractValidatorAction
   extends AbstractComplementaryConfigurableAction
  @@ -164,10 +191,22 @@
               String eq = constraints.getAttribute ("equals-to", "");
               String eqp = constraints.getAttribute ("equals-to-param", "");
   
  +	    String regex = conf.getAttribute ("matches-regex", "");
  +	    String tmp = constraints.getAttribute ( "matches-regex", null);
  +	    if ( tmp != null ) regex = tmp;
  +
  +	    String minlen = conf.getAttribute ("min-len","");
  +	    tmp = constraints.getAttribute ("min-len",null);
  +	    if ( tmp != null ) minlen = tmp;
  +
  +	    String maxlen = conf.getAttribute ("max-len","");
  +	    tmp = constraints.getAttribute ("max-len",null);
  +	    if ( tmp != null ) maxlen = tmp;
  +
               // Validate whether param is equal to constant
               if (!"".equals (eq)) {
                   getLogger().debug ("VALIDATOR: string parameter "
  -                        + name + "should be equal to " + eq);
  +                        + name + " should be equal to " + eq);
                   if (!value.equals (eq)) {
                       getLogger().debug ("VALIDATOR: and it is not");
                       return null;
  @@ -179,12 +218,74 @@
               // account?
               if (!"".equals (eqp)) {
                   getLogger().debug ("VALIDATOR: string parameter "
  -                        + name + "should be equal to " + params.get (eqp));
  +                        + name + " should be equal to " + params.get (eqp));
                   if (!value.equals (params.get (eqp))) {
                       getLogger().debug ("VALIDATOR: and it is not");
                       return null;
                   }
               }
  +	    
  +	    // Validate whether param length is at least of minimum length
  +	    if (!"".equals (minlen)) {
  +		getLogger().debug ("VALIDATOR: string parameter "
  +			+ name + " should be at least " + minlen + " characters long");
  +		try {
  +		    int min_len = java.lang.Integer.parseInt(minlen);
  +		    if ( min_len < 0 ) {
  +			getLogger().error("VALIDATOR: minimum length for parameter " 
  +			        + name + " is no positive integer");
  +		    }
  +		    if ( value.length() < min_len ) {
  +			getLogger().debug ("VALIDATOR: and it is shorter (" +
  +				value.length() + ")" );
  +			return null;
  +		    }
  +		} catch (NumberFormatException nfe) {
  +		    getLogger().error("VALIDATOR: minimum length for parameter " 
  +			    + name + "is no integer", nfe);
  +		    return null;
  +		}
  +	    }
  +
  +	    // Validate whether param length is at most of maximum length
  +	    if (!"".equals (maxlen)) {
  +		getLogger().debug ("VALIDATOR: string parameter "
  +			+ name + " should be at most " + maxlen + " characters long");
  +		try {
  +		    int max_len = java.lang.Integer.parseInt(maxlen);
  +		    if ( max_len < 0 ) {
  +			getLogger().error("VALIDATOR: maximum length for parameter " 
  +			        + name + " is no positive integer");
  +		    }
  +		    if ( value.length() > max_len ) {
  +			getLogger().debug ("VALIDATOR: and it is longer (" +
  +				value.length() + ")" );
  +			return null;
  +		    }
  +		} catch (NumberFormatException nfe) {
  +		    getLogger().error("VALIDATOR: maximum length for parameter " 
  +			    + name + " is no integer", nfe);
  +		    return null;
  +		}
  +	    }
  +
  +	    // Validate wheter param matches regular expression
  +	    if (!"".equals (regex)) {
  +		getLogger().debug ("VALIDATOR: string parameter " + name +
  +			" should match regexp \"" + regex + "\"" );
  +		try {
  +		    RE r = new RE ( regex );
  +		    if ( !r.match(value) ) {
  +			getLogger().debug("VALIDATOR: and it does not match");
  +			return null;
  +		    };
  +		} catch ( RESyntaxException rese ) {
  +		    getLogger().error ("VALIDATOR: string parameter " + name +
  +			    " regex error ", rese);
  +		    return null;
  +		}
  +	    }
  +
           }
           return value;
       }
  @@ -206,7 +307,82 @@
               return null;
           }
           if (constraints != null) {
  -            /* FIXME: add other checks as-well */
  +            String eq = constraints.getAttribute ("equals-to", "");
  +            String eqp = constraints.getAttribute ("equals-to-param", "");
  +
  +	    String min = conf.getAttribute ("min", "");
  +	    String tmp = constraints.getAttribute ( "min", null);
  +	    if ( tmp != null ) min = tmp;
  +
  +	    String max = conf.getAttribute ("max","");
  +	    tmp = constraints.getAttribute ("max",null);
  +	    if ( tmp != null ) max = tmp;
  +
  +            // Validate whether param is equal to constant
  +            if (!"".equals (eq)) {
  +                getLogger().debug ("VALIDATOR: long parameter "
  +                        + name + " should be equal to " + eq);
  +		try {
  +		    Long _eq = new Long(eq);
  +		    if (!value.equals (_eq)) {
  +			getLogger().debug ("VALIDATOR: and it is not");
  +			return null;
  +		    }
  +		} catch ( NumberFormatException nfe ) {
  +		    getLogger().error("VALIDATOR: "+eq+" is no long", nfe);
  +		    return null;
  +		}
  +            }
  +
  +            // Validate whether param is equal to another param
  +            // FIXME: take default value of param being compared with into
  +            // account?
  +            if (!"".equals (eqp)) {
  +                getLogger().debug ("VALIDATOR: long parameter "
  +                        + name + " should be equal to " + params.get (eqp));
  +		try {
  +		    Long _eqp = new Long ( (String) params.get(eqp) );
  +		    if (!value.equals (_eqp)) {
  +			getLogger().debug ("VALIDATOR: and it is not");
  +			return null;
  +		    }
  +		} catch ( NumberFormatException nfe ) {
  +		    getLogger().debug("VALIDATOR: long parameter "+ name +": "+eqp+" is no long", nfe);
  +		    return null;
  +		}
  +            }
  +
  +	    // Validate wheter param is at least min
  +	    if (!"".equals (min)) {
  +		getLogger().debug ("VALIDATOR: long parameter "
  +				   + name + " should be at least " + min);
  +		try {
  +		    Long _min = new Long ( min );
  +		    if (0>value.compareTo(_min)) {
  +			getLogger().debug ("VALIDATOR: and it is not");
  +			return null;
  +		    }
  +		} catch ( NumberFormatException nfe ) {
  +		    getLogger().error("VALIDATOR: long parameter "+ name +": min "+min+" is no long", nfe);
  +		    return null;
  +		}
  +	    }
  +
  +	    // Validate wheter param is at most max
  +	    if (!"".equals (max)) {
  +		getLogger().debug ("VALIDATOR: long parameter "
  +				   + name + " should be at most " + max);
  +		try {
  +		    Long _max = new Long ( max );
  +		    if (0<value.compareTo(_max)) {
  +			getLogger().debug ("VALIDATOR: and it is not");
  +			return null;
  +		    }
  +		} catch ( NumberFormatException nfe ) {
  +		    getLogger().error("VALIDATOR: long parameter "+ name +": max "+max+" is no long", nfe);
  +		    return null;
  +		}
  +	    }
           }
           return value;
       }
  @@ -228,7 +404,82 @@
               return null;
           }
           if (constraints != null) {
  -            /* FIXME: add other checks as-well */
  +            String eq = constraints.getAttribute ("equals-to", "");
  +            String eqp = constraints.getAttribute ("equals-to-param", "");
  +
  +	    String min = conf.getAttribute ("min", "");
  +	    String tmp = constraints.getAttribute ( "min", null);
  +	    if ( tmp != null ) min = tmp;
  +
  +	    String max = conf.getAttribute ("max","");
  +	    tmp = constraints.getAttribute ("max",null);
  +	    if ( tmp != null ) max = tmp;
  +
  +            // Validate whether param is equal to constant
  +            if (!"".equals (eq)) {
  +                getLogger().debug ("VALIDATOR: Double parameter "
  +                        + name + " should be equal to " + eq);
  +		try {
  +		    Double _eq = new Double(eq);
  +		    if (!value.equals (_eq)) {
  +			getLogger().debug ("VALIDATOR: and it is not");
  +			return null;
  +		    }
  +		} catch ( NumberFormatException nfe ) {
  +		    getLogger().error("VALIDATOR: "+eq+" is no double", nfe);
  +		    return null;
  +		}
  +            }
  +
  +            // Validate whether param is equal to another param
  +            // FIXME: take default value of param being compared with into
  +            // account?
  +            if (!"".equals (eqp)) {
  +                getLogger().debug ("VALIDATOR: Double parameter "
  +                        + name + " should be equal to " + params.get (eqp));
  +		try {
  +		    Double _eqp = new Double ( (String) params.get(eqp) );
  +		    if (!value.equals (_eqp)) {
  +			getLogger().debug ("VALIDATOR: and it is not");
  +			return null;
  +		    }
  +		} catch ( NumberFormatException nfe ) {
  +		    getLogger().debug("VALIDATOR: Double parameter "+ name +": "+eqp+" is no double", nfe);
  +		    return null;
  +		}
  +            }
  +
  +	    // Validate wheter param is at least min
  +	    if (!"".equals (min)) {
  +		getLogger().debug ("VALIDATOR: Double parameter "
  +				   + name + " should be at least " + min);
  +		try {
  +		    Double _min = new Double ( min );
  +		    if (0>value.compareTo(_min)) {
  +			getLogger().debug ("VALIDATOR: and it is not");
  +			return null;
  +		    }
  +		} catch ( NumberFormatException nfe ) {
  +		    getLogger().error("VALIDATOR: Double parameter "+ name +": min "+min+" is no double", nfe);
  +		    return null;
  +		}
  +	    }
  +
  +	    // Validate wheter param is at most max
  +	    if (!"".equals (max)) {
  +		getLogger().debug ("VALIDATOR: Double parameter "
  +				   + name + " should be at most " + max);
  +		try {
  +		    Double _max = new Double ( max );
  +		    if (0<value.compareTo(_max)) {
  +			getLogger().debug ("VALIDATOR: and it is not");
  +			return null;
  +		    }
  +		} catch ( NumberFormatException nfe ) {
  +		    getLogger().error("VALIDATOR: Double parameter "+ name +": max "+max+" is no double", nfe);
  +		    return null;
  +		}
  +	    }
           }
           return value;
       }
  @@ -383,5 +634,5 @@
       }
   }
   
  -// $Id: AbstractValidatorAction.java,v 1.2 2001/05/11 08:50:23 mman Exp $
  +// $Id: AbstractValidatorAction.java,v 1.3 2001/05/28 21:25:22 balld Exp $
   // vim: set et ts=4 sw=4:
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org