dims        01/06/15 04:27:50

  Modified:    src/org/apache/cocoon Tag: cocoon_20_branch Constants.java
               src/org/apache/cocoon/acting Tag: cocoon_20_branch
                        AbstractValidatorAction.java
                        FormValidatorAction.java
                        SessionValidatorAction.java
               webapp   Tag: cocoon_20_branch cocoon.xconf
  Log:
  Contribution for Form Validation LogicSheet, Actions, Helpers
  from Christian Haul <[EMAIL PROTECTED]>
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.4.2.2   +12 -9     xml-cocoon2/src/org/apache/cocoon/Constants.java
  
  Index: Constants.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/Constants.java,v
  retrieving revision 1.4.2.1
  retrieving revision 1.4.2.2
  diff -u -r1.4.2.1 -r1.4.2.2
  --- Constants.java    2001/06/14 15:11:32     1.4.2.1
  +++ Constants.java    2001/06/15 11:27:45     1.4.2.2
  @@ -10,7 +10,7 @@
   
   /**
    * @author <a href="mailto:[EMAIL PROTECTED]";>Stefano Mazzocchi</a>
  - * @version CVS $Revision: 1.4.2.1 $ $Date: 2001/06/14 15:11:32 $
  + * @version CVS $Revision: 1.4.2.2 $ $Date: 2001/06/15 11:27:45 $
    */
   
   public interface Constants {
  @@ -35,14 +35,17 @@
       String PARSER_PROPERTY = "org.apache.cocoon.components.parser.Parser";
       String DEFAULT_PARSER  = "org.apache.cocoon.components.parser.JaxpParser";
   
  -    String XSP_PREFIX          = "xsp";
  -    String XSP_URI             = "http://apache.org/xsp";;
  -    String XSP_REQUEST_PREFIX  = "xsp-request";
  -    String XSP_REQUEST_URI     = XSP_URI + "/request/2.0";
  -    String XSP_RESPONSE_PREFIX = "xsp-response";
  -    String XSP_RESPONSE_URI    = XSP_URI + "/response/2.0";
  -    String XSP_COOKIE_PREFIX  = "xsp-cookie";
  -    String XSP_COOKIE_URI     = XSP_URI + "/cookie/2.0";
  +    String XSP_PREFIX                     = "xsp";
  +    String XSP_URI                        = "http://apache.org/xsp";;
  +    String XSP_REQUEST_PREFIX             = "xsp-request";
  +    String XSP_REQUEST_URI                = XSP_URI + "/request/2.0";
  +    String XSP_RESPONSE_PREFIX            = "xsp-response";
  +    String XSP_RESPONSE_URI               = XSP_URI + "/response/2.0";
  +    String XSP_COOKIE_PREFIX         = "xsp-cookie";
  +    String XSP_COOKIE_URI            = XSP_URI + "/cookie/2.0";
  +    String XSP_FORMVALIDATOR_PATH    = 
"org.apache.cocoon.acting.FormValidatorAction.results";
  +    String XSP_FORMVALIDATOR_PREFIX  = "xsp-formval";
  +    String XSP_FORMVALIDATOR_URI     = Constants.XSP_URI + "/form-validator/2.0";
   
       String XML_NAMESPACE_URI = "http://www.w3.org/XML/1998/namespace";;
   
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.4.2.1   +198 -218  
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.4
  retrieving revision 1.4.2.1
  diff -u -r1.4 -r1.4.2.1
  --- AbstractValidatorAction.java      2001/05/31 17:37:38     1.4
  +++ AbstractValidatorAction.java      2001/06/15 11:27:47     1.4.2.1
  @@ -1,4 +1,4 @@
  -// $Id: AbstractValidatorAction.java,v 1.4 2001/05/31 17:37:38 bloritsch Exp $
  +// $Id: AbstractValidatorAction.java,v 1.4.2.1 2001/06/15 11:27:47 dims Exp $
   package org.apache.cocoon.acting;
   
   import org.apache.avalon.framework.configuration.Configurable;
  @@ -11,6 +11,10 @@
   
   import java.util.Map;
   
  +import org.apache.cocoon.acting.ValidatorActionResult;
  +import org.apache.cocoon.acting.ValidatorActionHelper;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
  +
   /**
    * Abstract implementation of action that needs to perform validation of
    * parameters (from session, from request, etc.). All `validator' actions
  @@ -98,7 +102,7 @@
    * </tr>
    * </table>
    * @author Martin Man &lt;[EMAIL PROTECTED]&gt;
  - * @version CVS $Revision: 1.4 $ $Date: 2001/05/31 17:37:38 $
  + * @version CVS $Revision: 1.4.2.1 $ $Date: 2001/06/15 11:27:47 $
    */
   public abstract class AbstractValidatorAction
   extends AbstractComplementaryConfigurableAction
  @@ -116,12 +120,8 @@
        * (as taken from HTTP request for example) or wheteher it should be
        * regular instance of java.lang.Double, java.lang.Long, etc.
        * @return The validated parameter.
  -     * FIXME: should be reworked so that validation returns something
  -     * meaningfull in case the parameter is nullable and really is null (which
  -     * is o.k.) but returns null. Workaround for now is that empty string is
  -     * returned for nullable strings and -1 for numbers.
        */
  -    public Object validateParameter (String name, Configuration constraints, 
  +    public ValidatorActionHelper validateParameter (String name, Configuration 
constraints, 
               Configuration[] conf, Map params, boolean is_string) {
           String type = null;
           int i = 0;
  @@ -175,33 +175,37 @@
        * Validates nullability and default value for given parameter. If given
        * constraints are not null they are validated as well.
        */
  -    private Object validateString (String name, Configuration constraints,
  +    private ValidatorActionHelper validateString (String name, Configuration 
constraints,
               Configuration conf, Map params, boolean is_string) {
           Object param = params.get (name);
           String value = null;
  +     String dflt = getDefault (conf);
  +     boolean nullable = getNullable (conf);
  +
           getLogger().debug ("VALIDATOR: validating string parameter "
                   + name + " (encoded in a string: " + is_string + ")");
           value = getStringValue (name, conf, param, is_string);
           if (value == null) {
               getLogger().debug ("VALIDATOR: string parameter "
                       + name + " is null");
  -            return null;
  +         if ( !nullable ){
  +             ValidatorActionHelper vah = new ValidatorActionHelper ( value, 
ValidatorActionResult.ISNULL );
  +                 return new ValidatorActionHelper ( value, 
ValidatorActionResult.ISNULL );}
  +         else 
  +             value = dflt;
           }
           if (constraints != null) {
               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;
  +         regex = constraints.getAttribute ( "matches-regex", regex);
  +
  +         Long minlen = getAttributeAsLong (conf, "min-len", null);
  +         minlen = getAttributeAsLong (constraints, "min-len", minlen);
   
  -         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;
  +         Long maxlen = getAttributeAsLong (conf, "max-len", null);
  +         maxlen = getAttributeAsLong (constraints, "max-len", maxlen);
   
               // Validate whether param is equal to constant
               if (!"".equals (eq)) {
  @@ -209,7 +213,7 @@
                           + name + " should be equal to " + eq);
                   if (!value.equals (eq)) {
                       getLogger().debug ("VALIDATOR: and it is not");
  -                    return null;
  +                 return new ValidatorActionHelper ( value, 
ValidatorActionResult.NOMATCH);
                   }
               }
   
  @@ -221,116 +225,98 @@
                           + name + " should be equal to " + params.get (eqp));
                   if (!value.equals (params.get (eqp))) {
                       getLogger().debug ("VALIDATOR: and it is not");
  -                    return null;
  +                 return new ValidatorActionHelper ( value, 
ValidatorActionResult.NOMATCH);
                   }
               }
            
            // Validate whether param length is at least of minimum length
  -         if (!"".equals (minlen)) {
  +         if (minlen != null) {
                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;
  +             if ( value.length() < minlen.longValue() ) {
  +                 getLogger().debug ("VALIDATOR: and it is shorter (" +
  +                                    value.length() + ")" );
  +                 return new ValidatorActionHelper ( value, 
ValidatorActionResult.TOOSMALL);
                }
            }
   
            // Validate whether param length is at most of maximum length
  -         if (!"".equals (maxlen)) {
  +         if (maxlen != null) {
                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;
  +                                + name + " should be at most " + maxlen + " 
characters long");
  +
  +             if ( value.length() > maxlen.longValue() ) {
  +                 getLogger().debug ("VALIDATOR: and it is longer (" +
  +                                    value.length() + ")" );
  +                 return new ValidatorActionHelper ( value, 
ValidatorActionResult.TOOLARGE);
                }
            }
   
            // Validate wheter param matches regular expression
            if (!"".equals (regex)) {
                getLogger().debug ("VALIDATOR: string parameter " + name +
  -                     " should match regexp \"" + regex + "\"" );
  +                                " should match regexp \"" + regex + "\"" );
                try {
                    RE r = new RE ( regex );
                    if ( !r.match(value) ) {
                        getLogger().debug("VALIDATOR: and it does not match");
  -                     return null;
  +                     return new ValidatorActionHelper ( value, 
ValidatorActionResult.NOMATCH);
                    };
                } catch ( RESyntaxException rese ) {
                    getLogger().error ("VALIDATOR: string parameter " + name +
  -                         " regex error ", rese);
  -                 return null;
  +                                    " regex error ", rese);
  +                 return new ValidatorActionHelper ( value, 
ValidatorActionResult.NOMATCH);
                }
            }
   
           }
  -        return value;
  +        return new ValidatorActionHelper(value);
       }
   
       /**
        * Validates nullability and default value for given parameter. If given
        * constraints are not null they are validated as well.
        */
  -    private Object validateLong (String name, Configuration constraints,
  +    private ValidatorActionHelper validateLong (String name, Configuration 
constraints,
               Configuration conf, Map params, boolean is_string) {
           Object param = params.get (name);
  +     boolean nullable = getNullable(conf);
           Long value = null;
  +     Long dflt = null;
  +     {
  +         String tmp = getDefault(conf);
  +         if ( tmp != null ) dflt = Long.decode(tmp);
  +     }
  +
           getLogger().debug ("VALIDATOR: validating long parameter "
                   + name + " (encoded in a string: " + is_string + ")");
           value = getLongValue (name, conf, param, is_string);
           if (value == null) {
               getLogger().debug ("VALIDATOR: long parameter "
                       + name + " is null");
  -            return null;
  +         if (!nullable)
  +             return new ValidatorActionHelper ( value, 
ValidatorActionResult.ISNULL);
  +         else 
  +             value=dflt;
           }
           if (constraints != null) {
  -            String eq = constraints.getAttribute ("equals-to", "");
  +            Long eq = getAttributeAsLong (constraints, "equals-to", null);
               String eqp = constraints.getAttribute ("equals-to-param", "");
  +
  +         Long min = getAttributeAsLong (conf, "min", null);
  +         min = getAttributeAsLong ( constraints, "min", min);
   
  -         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;
  +         Long max = getAttributeAsLong (conf, "max",null);
  +         max = getAttributeAsLong (constraints, "max", max);
   
               // Validate whether param is equal to constant
  -            if (!"".equals (eq)) {
  +            if (eq != null) {
                   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;
  +
  +             if (!value.equals(eq)) {
  +                 getLogger().debug ("VALIDATOR: and it is not");
  +                 return new ValidatorActionHelper ( value, 
ValidatorActionResult.NOMATCH);
                }
               }
   
  @@ -340,31 +326,28 @@
               if (!"".equals (eqp)) {
                   getLogger().debug ("VALIDATOR: long parameter "
                           + name + " should be equal to " + params.get (eqp));
  +             // Request parameter is stored as string. 
  +             // Need to convert it beforehand.
                try {
  -                 Long _eqp = new Long ( (String) params.get(eqp) );
  +                 Long _eqp = new Long ( Long.parseLong((String) params.get(eqp)) );
                    if (!value.equals (_eqp)) {
                        getLogger().debug ("VALIDATOR: and it is not");
  -                     return null;
  +                     return new ValidatorActionHelper ( value, 
ValidatorActionResult.NOMATCH);
                    }
                } catch ( NumberFormatException nfe ) {
                    getLogger().debug("VALIDATOR: long parameter "+ name +": "+eqp+" 
is no long", nfe);
  -                 return null;
  +                 return new ValidatorActionHelper ( value, 
ValidatorActionResult.NOMATCH);
                }
               }
   
            // Validate wheter param is at least min
  -         if (!"".equals (min)) {
  +         if (min != null) {
                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;
  +
  +             if (min.compareTo(value)>0) {
  +                 getLogger().debug ("VALIDATOR: and it is not");
  +                 return new ValidatorActionHelper ( value, 
ValidatorActionResult.TOOSMALL);
                }
            }
   
  @@ -372,62 +355,59 @@
            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;
  +             if (max.compareTo(value)<0) {
  +                 getLogger().debug ("VALIDATOR: and it is not");
  +                 return new ValidatorActionHelper ( value, 
ValidatorActionResult.TOOLARGE);
                }
            }
           }
  -        return value;
  +        return new ValidatorActionHelper(value);
       }
   
       /**
        * Validates nullability and default value for given parameter. If given
        * constraints are not null they are validated as well.
        */
  -    private Object validateDouble (String name, Configuration constraints,
  +    private ValidatorActionHelper validateDouble (String name, Configuration 
constraints,
               Configuration conf, Map params, boolean is_string) {
           Object param = params.get (name);
  +     boolean nullable = getNullable(conf);
           Double value = null;
  +     Double dflt = null;
  +     { 
  +         String tmp = getDefault(conf); 
  +         if ( tmp!=null ) dflt = Double.valueOf(tmp);
  +     }
  +
           getLogger().debug ("VALIDATOR: validating double parameter "
                   + name + " (encoded in a string: " + is_string + ")");
           value = getDoubleValue (name, conf, param, is_string);
           if (value == null) {
               getLogger().debug ("VALIDATOR: double parameter "
                       + name + " is null");
  -            return null;
  +         if (!nullable)
  +             return new ValidatorActionHelper ( value, 
ValidatorActionResult.ISNULL);
  +         else
  +             value = dflt;
           }
           if (constraints != null) {
  -            String eq = constraints.getAttribute ("equals-to", "");
  +            Double eq = getAttributeAsDouble (constraints, "equals-to", null);
               String eqp = constraints.getAttribute ("equals-to-param", "");
  +
  +         Double min = getAttributeAsDouble (conf, "min", null);
  +         min = getAttributeAsDouble ( constraints, "min", min);
   
  -         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;
  +         Double max = getAttributeAsDouble (conf, "max", null);
  +         max = getAttributeAsDouble (constraints, "max", max);
   
               // Validate whether param is equal to constant
  -            if (!"".equals (eq)) {
  +            if (eq != null) {
                   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;
  +
  +             if (!value.equals (eq)) {
  +                 getLogger().debug ("VALIDATOR: and it is not");
  +                 return new ValidatorActionHelper ( value, 
ValidatorActionResult.NOMATCH);
                }
               }
   
  @@ -437,77 +417,52 @@
               if (!"".equals (eqp)) {
                   getLogger().debug ("VALIDATOR: Double parameter "
                           + name + " should be equal to " + params.get (eqp));
  +             // Request parameter is stored as string. 
  +             // Need to convert it beforehand.
                try {
  -                 Double _eqp = new Double ( (String) params.get(eqp) );
  +                 Double _eqp = new Double ( Double.parseDouble((String) 
params.get(eqp)));
                    if (!value.equals (_eqp)) {
                        getLogger().debug ("VALIDATOR: and it is not");
  -                     return null;
  +                     return new ValidatorActionHelper ( value, 
ValidatorActionResult.NOMATCH);
                    }
                } catch ( NumberFormatException nfe ) {
                    getLogger().debug("VALIDATOR: Double parameter "+ name +": "+eqp+" 
is no double", nfe);
  -                 return null;
  +                 return new ValidatorActionHelper ( value, 
ValidatorActionResult.NOMATCH);
                }
               }
   
            // Validate wheter param is at least min
  -         if (!"".equals (min)) {
  +         if (min != null) {
                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;
  +             if (0>value.compareTo(min)) {
  +                 getLogger().debug ("VALIDATOR: and it is not");
  +                 return new ValidatorActionHelper ( value, 
ValidatorActionResult.TOOSMALL);
                }
            }
   
            // Validate wheter param is at most max
  -         if (!"".equals (max)) {
  +         if (max != null) {
                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;
  +             if (0<value.compareTo(max)) {
  +                 getLogger().debug ("VALIDATOR: and it is not");
  +                 return new ValidatorActionHelper ( value, 
ValidatorActionResult.TOOLARGE);
                }
            }
           }
  -        return value;
  +        return new ValidatorActionHelper(value);
       }
   
       /**
  -     * Checks whether param is nullable, if so checks for default value and
  -     * returns it, otherwise it returns the parsed Double value.
  -     * If the parameter is nullable and really is null and no default value
  -     * was specified, return -1 so that the validation succeeds.
  +     * Returns the parsed Double value.
        *
        * FIXME: is the name parameter needed? It is not used
        */
       private Double getDoubleValue (String name,
               Configuration conf, Object param, boolean is_string) {
  -        boolean nullable = false;
  -        Double dflt = null;
           Double value = null;
  -        nullable = getNullable (conf);
  -        /* check for default value */
  -        try {
  -            String tmp = conf.getAttribute ("default", "");
  -            if ("".equals (tmp.trim ())) {
  -                tmp = null;
  -            }
  -            dflt = Double.valueOf (tmp);
  -        } catch (Exception e) {
  -            dflt = null;
  -        }
  +
           /* convert param to double */
           if (is_string) {
               String tmp = (String)param;
  @@ -525,40 +480,19 @@
               } catch (Exception e) {
                   value = null;
               }
  -        }
  -        /* return appropriate value */
  -        if (nullable && value == null) {
  -            return dflt != null ? dflt : new Double ("-1");
  -        } else {
  -            return value;
           }
  +     return value;
       }
   
       /**
  -     * Checks whether param is nullable, if so checks for default value and
  -     * returns it, otherwise it returns the parsed Double value.
  -     * If the parameter is nullable and really is null and no default value
  -     * was specified, return -1 so that the validation succeeds.
  +     * Returns the parsed Double value.
        *
        * FIXME:
  -     * Is the name parameter needed? it is not used.
  -     */
  +     * Is the name parameter needed? it is not used.  */
       private Long getLongValue (String name,
               Configuration conf, Object param, boolean is_string) {
  -        boolean nullable = false;
  -        Long dflt = null;
           Long value = null;
  -        nullable = getNullable (conf);
  -        /* check for default value */
  -        try {
  -            String tmp = conf.getAttribute ("default", "");
  -            if ("".equals (tmp.trim ())) {
  -                tmp = null;
  -            }
  -            dflt = Long.decode (tmp);
  -        } catch (Exception e) {
  -            dflt = null;
  -        }
  +
           /* convert param to long */
           if (is_string) {
               String tmp = (String)param;
  @@ -576,41 +510,22 @@
               } catch (Exception e) {
                   value = null;
               }
  -        }
  -        /* return appropriate value */
  -        if (nullable && value == null) {
  -            return dflt != null ? dflt : new Long ("-1");
  -        } else {
  -            return value;
           }
  +     return value;
       }
   
       /**
  -     * Checks whether param is nullable, if so checks for default value and
  -     * returns it, otherwise it returns the parsed Double value.
  -     * If the parameter is nullable and really is null and no default value
  -     * was specified, return "" so that the validation succeeds.
  +     * Checks whether param is nullable, and returns it, otherwise it
  +     * returns the parsed Double value. 
        *
        * FIXME:
        * Is the "is_string" parameter really needed?
        * Is the "name" parameter really needed?
  -     * Neither are used
  -     */
  +     * Neither are used */
       private String getStringValue (String name,
               Configuration conf, Object param, boolean is_string) {
  -        boolean nullable = false;
  -        String dflt = null;
           String value = null;
  -        nullable = getNullable (conf);
  -        /* check for default value */
  -        try {
  -            dflt = conf.getAttribute ("default", "");
  -            if ("".equals (dflt.trim ())) {
  -                dflt = null;
  -            }
  -        } catch (Exception e) {
  -            dflt = null;
  -        }
  +     
           /* convert param to string */
           try {
               value = (String)param;
  @@ -619,13 +534,8 @@
               }
           } catch (Exception e) {
               value = null;
  -        }
  -        /* return appropriate value */
  -        if (nullable && value == null) {
  -            return dflt != null ? dflt : "";
  -        } else {
  -            return value;
           }
  +     return value;
       }
   
       /**
  @@ -642,7 +552,77 @@
           }
           return false;
       }
  +
  +    /**
  +     * Returns the default value from given configuration.
  +     */
  +    private String getDefault (Configuration conf) {
  +     String dflt = null;
  +        try {
  +            dflt = conf.getAttribute ("default", "");
  +            if ("".equals (dflt.trim ())) {
  +                return null;
  +            }
  +        } catch (Exception e) {
  +            return null;
  +        }
  +     return dflt;
  +    }
  +
  +    /**
  +     * Replacement for Avalon's Configuration.getAttributeAsLong
  +     * because that one doesn't take <code>Long</code> but long and
  +     * thus won't take <code>null</code> as parameter value for
  +     * default.
  +     *
  +     * @param conf Configuration
  +     * @param name Parameter's name
  +     * @param dflt Default value
  +     * @return Parameter's value in <code>configuration</code> or
  +     * <code>dflt</code> if parameter is not set or couldn't be
  +     * converted to a <code>Long</code>
  +     * @see org.apache.avalon.Configuration.getParameterAsLong */
  +
  +    private Long getAttributeAsLong (Configuration conf, String name, Long dflt) {
  +     Long value = null;
  +     try {
  +         value = new Long(conf.getAttributeAsLong(name));
  +     } catch (ConfigurationException e) {
  +         value = dflt;
  +     }
  +     return value;
  +    }
  +
  +    /**
  +     * Addition to Avalon's Configuration.getAttributeAsFloat
  +     * because that one does only deal with <code>float</code>.
  +     *
  +     * @param conf Configuration
  +     * @param name Parameter's name
  +     * @param dflt Default value
  +     * @return Parameter's value in <code>configuration</code> or
  +     * <code>dflt</code> if parameter is not set or couldn't be
  +     * converted to a <code>Double</code>
  +     * @see org.apache.avalon.Configuration.getParameterAsFloat
  +     */
  +
  +    private Double getAttributeAsDouble (Configuration conf, String name, Double 
dflt) {
  +     Double value = null;
  +     String tmp = null;
  +     try {
  +         tmp = conf.getAttribute(name);
  +         try {
  +             value = new Double ( Double.parseDouble(tmp) );
  +         } catch ( NumberFormatException nfe ) {
  +             value = null;
  +         }
  +     } catch (ConfigurationException e) {
  +         value = dflt;
  +     }
  +     return value;
  +    }
  +
   }
   
  -// $Id: AbstractValidatorAction.java,v 1.4 2001/05/31 17:37:38 bloritsch Exp $
  +// $Id: AbstractValidatorAction.java,v 1.4.2.1 2001/06/15 11:27:47 dims Exp $
   // vim: set et ts=4 sw=4:
  
  
  
  1.4.2.1   +35 -14    
xml-cocoon2/src/org/apache/cocoon/acting/FormValidatorAction.java
  
  Index: FormValidatorAction.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/org/apache/cocoon/acting/FormValidatorAction.java,v
  retrieving revision 1.4
  retrieving revision 1.4.2.1
  diff -u -r1.4 -r1.4.2.1
  --- FormValidatorAction.java  2001/06/05 21:36:20     1.4
  +++ FormValidatorAction.java  2001/06/15 11:27:47     1.4.2.1
  @@ -1,4 +1,4 @@
  -// $Id: FormValidatorAction.java,v 1.4 2001/06/05 21:36:20 dims Exp $
  +// $Id: FormValidatorAction.java,v 1.4.2.1 2001/06/15 11:27:47 dims Exp $
   package org.apache.cocoon.acting;
   
   import java.util.Collections;
  @@ -14,6 +14,8 @@
   import org.apache.cocoon.util.Tokenizer;
   import org.apache.log.Logger;
   
  +import org.apache.cocoon.acting.ValidatorActionHelper;
  +
   /**
    * This is the action used to validate Request parameters.
    * The parameters are described via the external xml
  @@ -49,8 +51,13 @@
    * This action returns null when validation fails, otherwise it provides
    * all validated parameters to the sitemap via {name} expression.
    *
  + * In addition a request attribute 
<code>org.apache.cocoon.acting.FormValidatorAction.results</code>
  + * contains the validation results in both cases and make it available
  + * to XSPs. Mind you that redirections create new request objects and thus
  + * the result is not available for the target page.
  + *
    * @author Martin Man &lt;[EMAIL PROTECTED]&gt;
  - * @version CVS $Revision: 1.4 $ $Date: 2001/06/05 21:36:20 $
  + * @version CVS $Revision: 1.4.2.1 $ $Date: 2001/06/15 11:27:47 $
    */
   public class FormValidatorAction extends AbstractValidatorAction
   {
  @@ -76,6 +83,8 @@
               Configuration[] desc = conf.getChildren ("parameter");
               Configuration[] csets = conf.getChildren ("constraint-set");
               HashMap actionMap = new HashMap ();
  +            HashMap resultMap = new HashMap ();
  +         boolean allOK = true;
               /* 
                * old obsoleted method
                */
  @@ -86,7 +95,7 @@
                   String[] rparams = Tokenizer.tokenize (valstr, ",", false);
   
                   /* perform actuall validation */
  -                Object result = null;
  +                ValidatorActionHelper result = null;
                   String name = null;
                   HashMap params = new HashMap (rparams.length);
                   /* put required params into hash */
  @@ -104,12 +113,13 @@
                       name = rparams[i].trim ();
                       result = validateParameter (name, null, desc,
                               params, true);
  -                    if (result == null) {
  +                    if (!result.isOK()) {
                           getLogger().debug ("FORMVALIDATOR: "
                                   + "validation failed for parameter " + name);
  -                        return null;
  +                        allOK = false;
                       }
  -                    actionMap.put (name, result);
  +                    actionMap.put (name, result.getObject());
  +                    resultMap.put (name, result.getResult());
                   }
               }
               /* 
  @@ -140,7 +150,7 @@
                   Configuration[] set = cset.getChildren ("validate");
   
                   /* perform actuall validation */
  -                Object result = null;
  +                ValidatorActionHelper result = null;
                   String name = null;
                   HashMap params = new HashMap (set.length);
                   getLogger ().debug ("FORMVALIDATOR: given set "
  @@ -161,17 +171,28 @@
                       name = set[i].getAttribute ("name", null);
                       result = validateParameter (name, set[i], 
                               desc, params, true);
  -                    if (result == null) {
  +                    if (!result.isOK()) {
                           getLogger().debug ("FORMVALIDATOR: "
                                   + "validation failed for parameter " + name);
  -                        return null;
  +                        allOK = false;
                       }
  -                    actionMap.put (name, result);
  +                    actionMap.put (name, result.getObject());
  +                    resultMap.put (name, result.getResult());
                   }
               }
  -            getLogger().debug ("FORMVALIDATOR: all form "
  -                    + "params validated");
  -            return Collections.unmodifiableMap (actionMap);
  +         if (!allOK) {
  +             // if any validation failed return an empty map
  +             actionMap = null;
  +             getLogger().debug ("FORMVALIDATOR: all form "
  +                                + "params validated. An error occurred.");
  +         } else {
  +             getLogger().debug ("FORMVALIDATOR: all form "
  +                                + "params successfully validated");
  +         }
  +         // store validation results in request attribute
  +         req.setAttribute(Constants.XSP_FORMVALIDATOR_PATH, resultMap);
  +         //      return Collections.unmodifiableMap (actionMap);
  +         return actionMap;
           } catch (Exception e) {
               getLogger().debug ("exception: ", e);
           }
  @@ -179,5 +200,5 @@
       }
   }
   
  -// $Id: FormValidatorAction.java,v 1.4 2001/06/05 21:36:20 dims Exp $
  +// $Id: FormValidatorAction.java,v 1.4.2.1 2001/06/15 11:27:47 dims Exp $
   // vim: set et ts=4 sw=4:
  
  
  
  1.3.2.1   +12 -11    
xml-cocoon2/src/org/apache/cocoon/acting/SessionValidatorAction.java
  
  Index: SessionValidatorAction.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/org/apache/cocoon/acting/SessionValidatorAction.java,v
  retrieving revision 1.3
  retrieving revision 1.3.2.1
  diff -u -r1.3 -r1.3.2.1
  --- SessionValidatorAction.java       2001/06/05 21:36:21     1.3
  +++ SessionValidatorAction.java       2001/06/15 11:27:48     1.3.2.1
  @@ -1,4 +1,4 @@
  -// $Id: SessionValidatorAction.java,v 1.3 2001/06/05 21:36:21 dims Exp $
  +// $Id: SessionValidatorAction.java,v 1.3.2.1 2001/06/15 11:27:48 dims Exp $
   package org.apache.cocoon.acting;
   
   import java.util.Collections;
  @@ -7,6 +7,7 @@
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.logger.AbstractLoggable;
   import org.apache.avalon.framework.parameters.Parameters;
  +import org.apache.cocoon.acting.ValidatorActionHelper;
   import org.apache.cocoon.Constants;
   import org.apache.cocoon.environment.Redirector;
   import org.apache.cocoon.environment.Request;
  @@ -52,7 +53,7 @@
    * all validated parameters to the sitemap via {name} expression.
    *
    * @author Martin Man &lt;[EMAIL PROTECTED]&gt;
  - * @version CVS $Revision: 1.3 $ $Date: 2001/06/05 21:36:21 $
  + * @version CVS $Revision: 1.3.2.1 $ $Date: 2001/06/15 11:27:48 $
    */
   public class SessionValidatorAction extends AbstractValidatorAction
   {
  @@ -94,7 +95,7 @@
                   String[] rparams = Tokenizer.tokenize (valstr, ",", false);
   
                   /* perform actuall validation */
  -                Object result = null;
  +                ValidatorActionHelper result = null;
                   String name = null;
                   HashMap params = new HashMap (rparams.length);
                   /* put required params into hash */
  @@ -112,13 +113,13 @@
                       name = rparams[i].trim ();
                       result = validateParameter (name, null, desc,
                               params, false);
  -                    if (result == null) {
  +                    if (!result.isOK()) {
                           getLogger().debug ("SESSIONVALIDATOR: "
                                   + "validation failed for parameter " + name);
                           return null;
                       }
  -                    session.setAttribute (name, result);
  -                    actionMap.put (name, result);
  +                    session.setAttribute (name, result.getObject());
  +                    actionMap.put (name, result.getObject());
                   }
               }
               /* 
  @@ -149,7 +150,7 @@
                   Configuration[] set = cset.getChildren ("validate");
   
                   /* perform actuall validation */
  -                Object result = null;
  +                ValidatorActionHelper result = null;
                   String name = null;
                   HashMap params = new HashMap (set.length);
                   getLogger ().debug ("SESSIONVALIDATOR: given set "
  @@ -170,13 +171,13 @@
                       name = set[i].getAttribute ("name", null);
                       result = validateParameter (name, set[i], 
                               desc, params, false);
  -                    if (result == null) {
  +                    if (!result.isOK()) {
                           getLogger().debug ("SESSIONVALIDATOR: "
                                   + "validation failed for parameter " + name);
                           return null;
                       }
  -                    session.setAttribute (name, result);
  -                    actionMap.put (name, result);
  +                    session.setAttribute (name, result.getObject());
  +                    actionMap.put (name, result.getObject());
                   }
               }
               getLogger().debug ("SESSIONVALIDATOR: all session "
  @@ -189,5 +190,5 @@
       }
   }
   
  -// $Id: SessionValidatorAction.java,v 1.3 2001/06/05 21:36:21 dims Exp $
  +// $Id: SessionValidatorAction.java,v 1.3.2.1 2001/06/15 11:27:48 dims Exp $
   // vim: set et ts=4 sw=4:
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.7.2.1   +7 -0      xml-cocoon2/webapp/cocoon.xconf
  
  Index: cocoon.xconf
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/webapp/cocoon.xconf,v
  retrieving revision 1.7
  retrieving revision 1.7.2.1
  diff -u -r1.7 -r1.7.2.1
  --- cocoon.xconf      2001/05/23 12:32:19     1.7
  +++ cocoon.xconf      2001/06/15 11:27:49     1.7.2.1
  @@ -113,6 +113,13 @@
             <parameter name="uri" value="http://apache.org/xsp/util/2.0"/>
             <parameter name="href" 
value="resource://org/apache/cocoon/components/language/markup/xsp/java/util.xsl"/>
           </builtin-logicsheet>
  +
  +        <builtin-logicsheet>
  +          <parameter name="prefix" value="xsp-formval"/>
  +          <parameter name="uri" value="http://apache.org/xsp/form-validator/2.0"/>
  +          <parameter name="href" 
value="resource://org/apache/cocoon/components/language/markup/xsp/java/form-validator.xsl"/>
  +        </builtin-logicsheet>
  +
         </target-language>
       </component-instance>
   
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to