You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by he...@apache.org on 2003/06/19 17:24:49 UTC

cvs commit: jakarta-turbine-2/src/java/org/apache/turbine/services/intake/validator FileValidator.java

henning     2003/06/19 08:24:49

  Modified:    src/java/org/apache/turbine/services/intake/validator
                        FileValidator.java
  Log:
  Extend the FileValidator from the Default Validator. Remove all code which
  is also in the Default Validator.
  
  Patch donated by Colin Chalmers <co...@maxware.nl>
  
  Revision  Changes    Path
  1.8       +18 -273   jakarta-turbine-2/src/java/org/apache/turbine/services/intake/validator/FileValidator.java
  
  Index: FileValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/intake/validator/FileValidator.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- FileValidator.java	7 Apr 2003 15:30:50 -0000	1.7
  +++ FileValidator.java	19 Jun 2003 15:24:49 -0000	1.8
  @@ -61,309 +61,54 @@
   import org.apache.turbine.services.intake.IntakeException;
   
   /**
  - * A validator that will compare a testValue against the following
  - * constraints:
  - * <table>
  - * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
  - * <tr><td>required</td><td>true|false</td><td>false</td></tr>
  - * <tr><td>minLength</td><td>integer</td><td>0</td></tr>
  - * <tr><td>maxLength</td><td>integer</td><td>&nbsp;</td></tr>
  - * </table>
  + * A validator that will compare a FileItem testValue against the following
  + * constraints in addition to those listed in DefaultValidator.
  + *
  + *
    *
    * This validator can serve as the base class for more specific validators
    *
    * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
    * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
  + * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
    * @version $Id$
    */
   public class FileValidator
  -        implements Validator, InitableByConstraintMap
  +        extends DefaultValidator
   {
  -    protected boolean required;
  -    protected String requiredMessage;
  -    protected int minLength;
  -    protected String minLengthMessage;
  -    protected int maxLength;
  -    protected String maxLengthMessage;
  -
  -    protected String message;
  -
  -    public FileValidator(Map paramMap)
  -            throws IntakeException
  -    {
  -        init(paramMap);
  -    }
  -
  -    public FileValidator()
  -    {
  -    }
   
       /**
  -     * Extract the relevant parameters from the constraints listed
  -     * in <rule> tags within the intake.xml file.
        *
  -     * @param paramMap a <code>Map</code> of <code>Rule</code>'s
  -     * containing constraints on the input.
  -     */
  -    public void init(Map paramMap)
  -    {
  -        // Init minLength stuff
  -        minLength = 0;
  -        minLengthMessage = null;
  -
  -        // Init maxLength stuff
  -        maxLength = 0;
  -        maxLengthMessage = null;
  -
  -        Constraint constraint = (Constraint) paramMap.get("minLength");
  -        if (constraint != null)
  -        {
  -            String param = constraint.getValue();
  -            minLength = Integer.parseInt(param);
  -            minLengthMessage = constraint.getMessage();
  -        }
  -
  -        constraint = (Constraint) paramMap.get("maxLength");
  -        if (constraint != null)
  -        {
  -            String param = constraint.getValue();
  -            maxLength = Integer.parseInt(param);
  -            maxLengthMessage = constraint.getMessage();
  -        }
  -
  -        constraint = (Constraint) paramMap.get("required");
  -        if (constraint == null)
  -        {
  -            required = false;
  -        }
  -        else
  -        {
  -            String param = constraint.getValue();
  -            required = new Boolean(param).booleanValue();
  -            requiredMessage = constraint.getMessage();
  -        }
  -    }
  -
  -    /**
  -     * Determine whether a testValue meets the criteria specified
  -     * in the constraints defined for this validator
  +     * Constructor
        *
  -     * @param testValue a <code>String</code> to be tested
  -     * @return true if valid, false otherwise
  +     * @param paramMap a <code>Map</code> of <code>rule</code>'s
  +     * containing constraints on the input.
  +     * @exception InvalidMaskException an invalid mask was specified
        */
  -    public boolean isValid(String testValue)
  +    public FileValidator(Map paramMap)
  +            throws IntakeException
       {
  -        boolean valid = false;
  -        try
  -        {
  -            assertValidity(testValue);
  -            valid = true;
  -        }
  -        catch (ValidationException ve)
  -        {
  -            valid = false;
  -        }
  -        return valid;
  +        init(paramMap);
       }
   
       /**
  -     * This method is not implemented, but is provided for the Validator
  -     * interface.
  -     *
  -     * @param testValue a <code>String</code> to be tested
  -     * @exception ValidationException always thrown.
  +     * Default constructor
        */
  -    public void assertValidity(String testValue)
  -            throws ValidationException
  +    public FileValidator()
       {
  -        throw new ValidationException("this validation is not implemented");
       }
   
       /**
        * Determine whether a testValue meets the criteria specified
        * in the constraints defined for this validator
        *
  -     * @param testValue a <code>String</code> to be tested
  +     * @param testValue a <code>FileItem</code> to be tested
        * @exception ValidationException containing an error message if the
        * testValue did not pass the validation tests.
        */
       public void assertValidity(FileItem testValue)
               throws ValidationException
       {
  -        message = null;
  -
  -        if ((!required && minLength == 0)
  -                && (testValue == null || testValue.getSize() == 0))
  -        {
  -            return;
  -        }
  -        else if (required
  -                && (testValue == null || testValue.getSize() == 0))
  -        {
  -            message = requiredMessage;
  -            throw new ValidationException(requiredMessage);
  -        }
  -
  -        // allow subclasses first chance at validation
  -        doAssertValidity(testValue);
  -
  -        if (minLength > 0 && testValue.getSize() < minLength)
  -        {
  -            message = minLengthMessage;
  -            throw new ValidationException(minLengthMessage);
  -        }
  -        if (maxLength > 0 && testValue.getSize() > maxLength)
  -        {
  -            message = maxLengthMessage;
  -            throw new ValidationException(maxLengthMessage);
  -        }
  -    }
  -
  -    /**
  -     * Get the last error message resulting from invalid input.
  -     *
  -     * @return a <code>String</code> message, or the empty String "".
  -     */
  -    public String getMessage()
  -    {
  -        if (message == null)
  -        {
  -            return "";
  -        }
  -        return message;
  -    }
  -
  -    /**
  -     * Method to allow subclasses to add additional validation
  -     *
  -     * @param testValue <code>FileItem</code> to validate
  -     * @throws ValidationException validation failed
  -     */
  -    protected void doAssertValidity(FileItem testValue)
  -            throws ValidationException
  -    {
  -    }
  -
  -    // ************************************************************
  -    // **                Bean accessor methods                   **
  -    // ************************************************************
  -
  -    /**
  -     * Get the value of required.
  -     *
  -     * @return value of required.
  -     */
  -    public boolean isRequired()
  -    {
  -        return required;
  -    }
  -
  -    /**
  -     * Set the value of required.
  -     *
  -     * @param required  Value to assign to required.
  -     */
  -    public void setRequired(boolean required)
  -    {
  -        this.required = required;
  -    }
  -
  -    /**
  -     * Get the value of requiredMessage.
  -     * @return value of requiredMessage.
  -     */
  -    public String getRequiredMessage()
  -    {
  -        return requiredMessage;
  -    }
  -
  -    /**
  -     * Set the value of requiredMessage.
  -     *
  -     * @param message  Value to assign to requiredMessage.
  -     */
  -    public void setRequiredMessage(String message)
  -    {
  -        this.requiredMessage = message;
  -    }
  -
  -    /**
  -     * Get the value of minLength.
  -     *
  -     * @return value of minLength.
  -     */
  -    public int getMinLength()
  -    {
  -        return minLength;
  -    }
  -
  -    /**
  -     * Set the value of minLength.
  -     *
  -     * @param length  Value to assign to minLength.
  -     */
  -    public void setMinLength(int length)
  -    {
  -        this.minLength = length;
  -    }
  -
  -    /**
  -     * Get the value of minLengthMessage.
  -     *
  -     * @return value of minLengthMessage.
  -     */
  -    public String getMinLengthMessage()
  -    {
  -        return minLengthMessage;
  -    }
  -
  -    /**
  -     * Set the value of minLengthMessage.
  -     *
  -     * @param message  Value to assign to minLengthMessage.
  -     */
  -    public void setMinLengthMessage(String message)
  -    {
  -        this.minLengthMessage = message;
  -    }
  -
  -    /**
  -     * Get the value of maxLength.
  -     *
  -     * @return value of maxLength.
  -     */
  -    public int getMaxLength()
  -    {
  -        return maxLength;
  -    }
  -
  -    /**
  -     * Set the value of maxLength.
  -     *
  -     * @param length  Value to assign to maxLength.
  -     */
  -    public void setMaxLength(int length)
  -    {
  -        this.maxLength = length;
  -    }
  -
  -    /**
  -     * Get the value of maxLengthMessage.
  -     *
  -     * @return value of maxLengthMessage.
  -     */
  -    public String getMaxLengthMessage()
  -    {
  -        return maxLengthMessage;
  -    }
  -
  -    /**
  -     * Set the value of maxLengthMessage.
  -     *
  -     * @param message  Value to assign to maxLengthMessage.
  -     */
  -    public void setMaxLengthMessage(String message)
  -    {
  -        this.maxLengthMessage = message;
  +        super.assertValidity(testValue.getString());
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org