Hi,

the attached patch adds the following to AbstractValidatorAction.java

    * equals-to supported by Long & Double
    * equals-to-param supported by Long & Double
    * min as minimum value supported by Long & Double
    * max as maximum value supported by Long & Double
    * min-len as minimum string length supported by String
    * max-len as maximum string length supported by String
    * matches-regex as POSIX regular expression to match against String

    * all apart equals-to(-param)? can be overridden by redefinition
      in a constraint-set. Rationale: For a database applikation it is
      sensible to define e.g. a maximum length for an attribute
      globally. This might be as well valid for a regular
      expression. On some occasions (e.g. a search form) one might
      like the user to be less constrained.

It would be nice if the attached patch could be applied. I hope I
didn't break too many coding conventions.

Since I believe in client-side checking to improve usability I'd like
to use the same descriptor.xml file to generate appropriate UI
elements (e.g. set the MAXLEN attribute for a text input field or
set a trigger to a javascript routine). I wounder what the best way
would be. Somehow call the AbstractValidatorAction from a TagLib
through an interface class?

        Chris.

-- 
C h r i s t i a n       H a u l
[EMAIL PROTECTED]
    fingerprint: 99B0 1D9D 7919 644A 4837  7D73 FEF9 6856 335A 9E08
Index: src/org/apache/cocoon/acting/AbstractValidatorAction.java
===================================================================
RCS file: 
/home/cvspublic/xml-cocoon2/src/org/apache/cocoon/acting/AbstractValidatorAction.java,v
retrieving revision 1.2
diff -u -r1.2 AbstractValidatorAction.java
--- src/org/apache/cocoon/acting/AbstractValidatorAction.java   2001/05/11 08:50:23    
 1.2
+++ src/org/apache/cocoon/acting/AbstractValidatorAction.java   2001/05/28 13:04:33
@@ -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,11 +65,35 @@
  * 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>
@@ -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;
     }

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

Reply via email to