henning     2003/06/19 08:18:25

  Modified:    src/java/org/apache/turbine/services/intake/validator
                        IntegerValidator.java
  Added:       src/java/org/apache/turbine/services/intake/validator
                        BigDecimalValidator.java FloatValidator.java
                        LongValidator.java ShortValidator.java
  Log:
  The validators for the various number types.
  
  Patch donated by Colin Chalmers <[EMAIL PROTECTED]>
  
  Revision  Changes    Path
  1.7       +41 -29    
jakarta-turbine-2/src/java/org/apache/turbine/services/intake/validator/IntegerValidator.java
  
  Index: IntegerValidator.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/intake/validator/IntegerValidator.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- IntegerValidator.java     9 Mar 2003 03:06:28 -0000       1.6
  +++ IntegerValidator.java     19 Jun 2003 15:18:25 -0000      1.7
  @@ -57,8 +57,8 @@
   import java.util.Map;
   
   /**
  - * Validates numbers with the following constraints in addition to those
  - * listed in DefaultValidator.
  + * Validates Integers with the following constraints in addition to those
  + * listed in NumberValidator and DefaultValidator.
    *
    * <table>
    * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
  @@ -66,38 +66,53 @@
    * <td>&nbsp;</td></tr>
    * <tr><td>maxValue</td><td>less than Integer.MAX_VALUE</td>
    * <td>&nbsp;</td></tr>
  - * <tr><td>notANumberMessage</td><td>Some text</td>
  + * <tr><td>invalidNumberMessage</td><td>Some text</td>
    * <td>Entry was not a valid number</td></tr>
    * </table>
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">John McNally</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]">Colin Chalmers</a>
    * @version $Id$
    */
   public class IntegerValidator
           extends NumberValidator
   {
  -    private static String INVALID_NUMBER = "Entry was not a valid integer";
  +    /* Init the minValue to that for a Integer */
  +    private int minValue = Integer.MIN_VALUE;
   
  -    private int minValue;
  -    private int maxValue;
  +    /* Init the maxValue to that for a Integer */
  +    private int maxValue = Integer.MAX_VALUE;
   
  +    /**
  +     * Constructor to use when initialising Object
  +     *
  +     * @param paramMap
  +     * @throws InvalidMaskException
  +     */
       public IntegerValidator(Map paramMap)
               throws InvalidMaskException
       {
  -        this();
  +        invalidNumberMessage = "Entry was not a valid Integer";
           init(paramMap);
       }
   
  +    /**
  +     * Default Constructor
  +     */
       public IntegerValidator()
       {
  -        // sets the default invalid number message
  -        super();
       }
   
  -    protected void doInit(Map paramMap)
  +    /**
  +     * Method to initialise Object
  +     *
  +     * @param paramMap
  +     * @throws InvalidMaskException
  +     */
  +    public void init(Map paramMap)
  +            throws InvalidMaskException
       {
  -        minValue = Integer.MIN_VALUE;
  -        maxValue = Integer.MAX_VALUE;
  +        super.init(paramMap);
   
           Constraint constraint = (Constraint) paramMap.get("minValue");
           if (constraint != null)
  @@ -116,11 +131,6 @@
           }
       }
   
  -    protected String getDefaultInvalidNumberMessage()
  -    {
  -        return INVALID_NUMBER;
  -    }
  -
       /**
        * Determine whether a testValue meets the criteria specified
        * in the constraints defined for this validator
  @@ -129,9 +139,11 @@
        * @exception ValidationException containing an error message if the
        * testValue did not pass the validation tests.
        */
  -    protected void doAssertValidity(String testValue)
  +    public void assertValidity(String testValue)
               throws ValidationException
       {
  +        super.assertValidity(testValue);
  +
           int i = 0;
           try
           {
  @@ -139,18 +151,18 @@
           }
           catch (RuntimeException e)
           {
  -            message = invalidNumberMessage;
  +            errorMessage = invalidNumberMessage;
               throw new ValidationException(invalidNumberMessage);
           }
   
           if (i < minValue)
           {
  -            message = minValueMessage;
  -            throw new ValidationException(minValueMessage);
  +            errorMessage = minValueMessage;
  +
           }
           if (i > maxValue)
           {
  -            message = maxValueMessage;
  +            errorMessage = maxValueMessage;
               throw new ValidationException(maxValueMessage);
           }
       }
  @@ -173,11 +185,11 @@
       /**
        * Set the value of minValue.
        *
  -     * @param value  Value to assign to minValue.
  +     * @param minValue  Value to assign to minValue.
        */
  -    public void setMinValue(int value)
  +    public void setMinValue(int minValue)
       {
  -        this.minValue = value;
  +        this.minValue = minValue;
       }
   
       /**
  @@ -193,10 +205,10 @@
       /**
        * Set the value of maxValue.
        *
  -     * @param value  Value to assign to maxValue.
  +     * @param maxValue  Value to assign to maxValue.
        */
  -    public void setMaxValue(int value)
  +    public void setMaxValue(int maxValue)
       {
  -        this.maxValue = value;
  +        this.maxValue = maxValue;
       }
   }
  
  
  
  1.1                  
jakarta-turbine-2/src/java/org/apache/turbine/services/intake/validator/BigDecimalValidator.java
  
  Index: BigDecimalValidator.java
  ===================================================================
  package org.apache.turbine.services.intake.validator;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.math.BigDecimal;
  import java.util.Map;
  
  /**
   * Validates BigDecimals with the following constraints in addition to those
   * listed in NumberValidator and DefaultValidator.
   *
   * <table>
   * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
   * <tr><td>minValue</td><td>greater than BigDecimal minValue</td>
   * <td>&nbsp;</td></tr>
   * <tr><td>maxValue</td><td>less than BigDecimal maxValue</td>
   * <td>&nbsp;</td></tr>
   * <tr><td>invalidNumberMessage</td><td>Some text</td>
   * <td>Entry was not a valid number</td></tr>
   * </table>
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">John McNally</a>
   * @author <a href="mailto:[EMAIL PROTECTED]">Colin Chalmers</a>
   * @version $Id: BigDecimalValidator.java,v 1.1 2003/06/19 15:18:25 henning Exp $
   */
  public class BigDecimalValidator
          extends NumberValidator
  {
      private BigDecimal minValue = null;
      private BigDecimal maxValue = null;
  
      /**
       * Constructor to use when initialising Object
       *
       * @param paramMap
       * @throws InvalidMaskException
       */
      public BigDecimalValidator(Map paramMap)
              throws InvalidMaskException
      {
          invalidNumberMessage = "Entry was not a valid BigDecimal";
          init(paramMap);
      }
  
      /**
       * Default Constructor
       */
      public BigDecimalValidator()
      {
      }
  
      /**
       * Method to initialise Object
       *
       * @param paramMap
       * @throws InvalidMaskException
       */
      public void init(Map paramMap)
              throws InvalidMaskException
      {
          super.init(paramMap);
  
          Constraint constraint = (Constraint) paramMap.get("minValue");
          if (constraint != null)
          {
              String param = constraint.getValue();
              minValue = new BigDecimal(param);
              minValueMessage = constraint.getMessage();
          }
  
          constraint = (Constraint) paramMap.get("maxValue");
          if (constraint != null)
          {
              String param = constraint.getValue();
              maxValue = new BigDecimal(param);
              maxValueMessage = constraint.getMessage();
          }
      }
  
      /**
       * Determine whether a testValue meets the criteria specified
       * in the constraints defined for this validator
       *
       * @param testValue a <code>String</code> to be tested
       * @exception ValidationException containing an error message if the
       * testValue did not pass the validation tests.
       */
      public void assertValidity(String testValue)
              throws ValidationException
      {
          super.assertValidity(testValue);
  
          BigDecimal bd = null;
          try
          {
              bd = new BigDecimal(testValue);
          }
          catch (RuntimeException e)
          {
              errorMessage = invalidNumberMessage;
              throw new ValidationException(invalidNumberMessage);
          }
  
          if (minValue != null && bd.compareTo(minValue) < 0)
          {
              errorMessage = minValueMessage;
              throw new ValidationException(minValueMessage);
          }
          if (maxValue != null && bd.compareTo(maxValue) > 0)
          {
              errorMessage = maxValueMessage;
              throw new ValidationException(maxValueMessage);
          }
      }
  
  
      // ************************************************************
      // **                Bean accessor methods                   **
      // ************************************************************
  
      /**
       * Get the value of minValue.
       *
       * @return value of minValue.
       */
      public BigDecimal getMinValue()
      {
          return minValue;
      }
  
      /**
       * Set the value of minValue.
       *
       * @param minValue  Value to assign to minValue.
       */
      public void setMinValue(BigDecimal minValue)
      {
          this.minValue = minValue;
      }
  
      /**
       * Get the value of maxValue.
       *
       * @return value of maxValue.
       */
      public BigDecimal getMaxValue()
      {
          return maxValue;
      }
  
      /**
       * Set the value of maxValue.
       *
       * @param maxValue  Value to assign to maxValue.
       */
      public void setMaxValue(BigDecimal maxValue)
      {
          this.maxValue = maxValue;
      }
  }
  
  
  
  1.1                  
jakarta-turbine-2/src/java/org/apache/turbine/services/intake/validator/FloatValidator.java
  
  Index: FloatValidator.java
  ===================================================================
  package org.apache.turbine.services.intake.validator;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.util.Map;
  
  /**
   * Validates Floats with the following constraints in addition to those
   * listed in NumberValidator and DefaultValidator.
   *
   * <table>
   * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
   * <tr><td>minValue</td><td>greater than Float.MIN_VALUE</td>
   * <td>&nbsp;</td></tr>
   * <tr><td>maxValue</td><td>less than Float.MAX_VALUE</td>
   * <td>&nbsp;</td></tr>
   * <tr><td>invalidNumberMessage</td><td>Some text</td>
   * <td>Entry was not a valid number</td></tr>
   * </table>
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">John McNally</a>
   * @author <a href="mailto:[EMAIL PROTECTED]">Colin Chalmers</a>
   * @version $Id: FloatValidator.java,v 1.1 2003/06/19 15:18:25 henning Exp $
   */
  public class FloatValidator
          extends NumberValidator
  {
      /* Init the minValue to that for a Float */
      private float minValue = Float.MIN_VALUE;
  
      /* Init the maxValue to that for a Float */
      private float maxValue = Float.MAX_VALUE;
  
      /**
       * Constructor to use when initialising Object
       *
       * @param paramMap
       * @throws InvalidMaskException
       */
      public FloatValidator(Map paramMap)
              throws InvalidMaskException
      {
          invalidNumberMessage = "Entry was not a valid Float";
          init(paramMap);
      }
  
      /**
       * Default Constructor
       */
      public FloatValidator()
      {
      }
  
      /**
       * Method to initialise Object
       *
       * @param paramMap
       * @throws InvalidMaskException
       */
      public void init(Map paramMap)
              throws InvalidMaskException
      {
          super.init(paramMap);
  
          Constraint constraint = (Constraint) paramMap.get("minValue");
          if (constraint != null)
          {
              String param = constraint.getValue();
              minValue = Float.parseFloat(param);
              minValueMessage = constraint.getMessage();
          }
  
          constraint = (Constraint) paramMap.get("maxValue");
          if (constraint != null)
          {
              String param = constraint.getValue();
              maxValue = Float.parseFloat(param);
              maxValueMessage = constraint.getMessage();
          }
      }
  
      /**
       * Determine whether a testValue meets the criteria specified
       * in the constraints defined for this validator
       *
       * @param testValue a <code>String</code> to be tested
       * @exception ValidationException containing an error message if the
       * testValue did not pass the validation tests.
       */
      public void assertValidity(String testValue)
              throws ValidationException
      {
          super.assertValidity(testValue);
  
          float f = 0.0f;
          try
          {
              f = Float.parseFloat(testValue);
          }
          catch (RuntimeException e)
          {
              errorMessage = invalidNumberMessage;
              throw new ValidationException(invalidNumberMessage);
          }
  
          if (f < minValue)
          {
              errorMessage = minValueMessage;
              throw new ValidationException(minValueMessage);
          }
          if (f > maxValue)
          {
              errorMessage = maxValueMessage;
              throw new ValidationException(maxValueMessage);
          }
      }
  
  
      // ************************************************************
      // **                Bean accessor methods                   **
      // ************************************************************
  
      /**
       * Get the value of minValue.
       *
       * @return value of minValue.
       */
      public float getMinValue()
      {
          return minValue;
      }
  
      /**
       * Set the value of minValue.
       *
       * @param minValue  Value to assign to minValue.
       */
      public void setMinValue(float minValue)
      {
          this.minValue = minValue;
      }
  
      /**
       * Get the value of maxValue.
       *
       * @return value of maxValue.
       */
      public float getMaxValue()
      {
          return maxValue;
      }
  
      /**
       * Set the value of maxValue.
       *
       * @param maxValue  Value to assign to maxValue.
       */
      public void setMaxValue(float maxValue)
      {
          this.maxValue = maxValue;
      }
  }
  
  
  
  1.1                  
jakarta-turbine-2/src/java/org/apache/turbine/services/intake/validator/LongValidator.java
  
  Index: LongValidator.java
  ===================================================================
  package org.apache.turbine.services.intake.validator;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.util.Map;
  
  /**
   * Validates Longs with the following constraints in addition to those
   * listed in NumberValidator and DefaultValidator.
   *
   * <table>
   * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
   * <tr><td>minValue</td><td>greater than Long.MIN_VALUE</td>
   * <td>&nbsp;</td></tr>
   * <tr><td>maxValue</td><td>less than Long.MAX_VALUE</td>
   * <td>&nbsp;</td></tr>
   * <tr><td>invalidNumberMessage</td><td>Some text</td>
   * <td>Entry was not a valid number</td></tr>
   * </table>
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">John McNally</a>
   * @author <a href="mailto:[EMAIL PROTECTED]">Colin Chalmers</a>
   * @version $Id: LongValidator.java,v 1.1 2003/06/19 15:18:25 henning Exp $
   */
  public class LongValidator
          extends NumberValidator
  {
      /* Init the minValue to that for a Long */
      private long minValue = Long.MIN_VALUE;
  
      /* Init the maxValue to that for a Long */
      private long maxValue = Long.MAX_VALUE;
  
      /**
       * Constructor to use when initialising Object
       *
       * @param paramMap
       * @throws InvalidMaskException
       */
      public LongValidator(Map paramMap)
              throws InvalidMaskException
      {
          invalidNumberMessage = "Entry was not a valid Long";
          init(paramMap);
      }
  
      /**
       * Default Constructor
       */
      public LongValidator()
      {
      }
  
      /**
       * Method to initialise Object
       *
       * @param paramMap
       * @throws InvalidMaskException
       */
      public void init(Map paramMap)
              throws InvalidMaskException
      {
          super.init(paramMap);
  
          Constraint constraint = (Constraint) paramMap.get("minValue");
          if (constraint != null)
          {
              String param = constraint.getValue();
              minValue = Long.parseLong(param);
              minValueMessage = constraint.getMessage();
          }
  
          constraint = (Constraint) paramMap.get("maxValue");
          if (constraint != null)
          {
              String param = constraint.getValue();
              maxValue = Long.parseLong(param);
              maxValueMessage = constraint.getMessage();
          }
      }
  
      /**
       * Determine whether a testValue meets the criteria specified
       * in the constraints defined for this validator
       *
       * @param testValue a <code>String</code> to be tested
       * @exception ValidationException containing an error message if the
       * testValue did not pass the validation tests.
       */
      public void assertValidity(String testValue)
              throws ValidationException
      {
          super.assertValidity(testValue);
  
          long l = 0L;
          try
          {
              l = Long.parseLong(testValue);
          }
          catch (RuntimeException e)
          {
              errorMessage = invalidNumberMessage;
              throw new ValidationException(invalidNumberMessage);
          }
  
          if (l < minValue)
          {
              errorMessage = minValueMessage;
              throw new ValidationException(minValueMessage);
          }
          if (l > maxValue)
          {
              errorMessage = maxValueMessage;
              throw new ValidationException(maxValueMessage);
          }
      }
  
  
      // ************************************************************
      // **                Bean accessor methods                   **
      // ************************************************************
  
      /**
       * Get the value of minValue.
       *
       * @return value of minValue.
       */
      public long getMinValue()
      {
          return minValue;
      }
  
      /**
       * Set the value of minValue.
       *
       * @param minValue  Value to assign to minValue.
       */
      public void setMinValue(long minValue)
      {
          this.minValue = minValue;
      }
  
      /**
       * Get the value of maxValue.
       *
       * @return value of maxValue.
       */
      public long getMaxValue()
      {
          return maxValue;
      }
  
      /**
       * Set the value of maxValue.
       *
       * @param maxValue  Value to assign to maxValue.
       */
      public void setMaxValue(long maxValue)
      {
          this.maxValue = maxValue;
      }
  }
  
  
  
  1.1                  
jakarta-turbine-2/src/java/org/apache/turbine/services/intake/validator/ShortValidator.java
  
  Index: ShortValidator.java
  ===================================================================
  package org.apache.turbine.services.intake.validator;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.util.Map;
  
  /**
   * Validates Shorts with the following constraints in addition to those
   * listed in NumberValidator and DefaultValidator.
   *
   * <table>
   * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
   * <tr><td>minValue</td><td>greater than Short.MIN_VALUE</td>
   * <td>&nbsp;</td></tr>
   * <tr><td>maxValue</td><td>less than Short.MAX_VALUE</td>
   * <td>&nbsp;</td></tr>
   * <tr><td>invalidNumberMessage</td><td>Some text</td>
   * <td>Entry was not a valid Short</td></tr>
   * </table>
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">John McNally</a>
   * @author <a href="mailto:[EMAIL PROTECTED]">Colin Chalmers</a>
   * @version $Id: ShortValidator.java,v 1.1 2003/06/19 15:18:25 henning Exp $
   */
  public class ShortValidator
          extends NumberValidator
  {
      /* Init the minValue to that for a Short */
      private short minValue = Short.MIN_VALUE;
  
      /* Init the maxValue to that for a Short */
      private short maxValue = Short.MAX_VALUE;
  
      /**
       * Constructor to use when initialising Object
       *
       * @param paramMap
       * @throws InvalidMaskException
       */
      public ShortValidator(Map paramMap)
              throws InvalidMaskException
      {
          invalidNumberMessage = "Entry was not a valid Short";
          init(paramMap);
      }
  
      /**
       * Default Constructor
       */
      public ShortValidator()
      {
      }
  
      /**
       * Method to initialise Object
       *
       * @param paramMap
       * @throws InvalidMaskException
       */
      public void init(Map paramMap)
              throws InvalidMaskException
      {
          super.init(paramMap);
  
          Constraint constraint = (Constraint) paramMap.get("minValue");
          if (constraint != null)
          {
              String param = constraint.getValue();
              minValue = Short.parseShort(param);
              minValueMessage = constraint.getMessage();
          }
  
          constraint = (Constraint) paramMap.get("maxValue");
          if (constraint != null)
          {
              String param = constraint.getValue();
              maxValue = Short.parseShort(param);
              maxValueMessage = constraint.getMessage();
          }
      }
  
      /**
       * Determine whether a testValue meets the criteria specified
       * in the constraints defined for this validator
       *
       * @param testValue a <code>String</code> to be tested
       * @exception ValidationException containing an error message if the
       * testValue did not pass the validation tests.
       */
      public void assertValidity(String testValue)
              throws ValidationException
      {
          super.assertValidity(testValue);
  
          short s = 0;
          try
          {
              s = Short.parseShort(testValue);
          }
          catch (RuntimeException e)
          {
              errorMessage = invalidNumberMessage;
              throw new ValidationException(invalidNumberMessage);
          }
  
          if (s < minValue)
          {
              errorMessage = minValueMessage;
              throw new ValidationException(minValueMessage);
          }
          if (s > maxValue)
          {
              errorMessage = maxValueMessage;
              throw new ValidationException(maxValueMessage);
          }
      }
  
  
      // ************************************************************
      // **                Bean accessor methods                   **
      // ************************************************************
  
      /**
       * Get the value of minValue.
       *
       * @return value of minValue.
       */
      public short getMinValue()
      {
          return minValue;
      }
  
      /**
       * Set the value of minValue.
       *
       * @param minValue  Value to assign to minValue.
       */
      public void setMinValue(short minValue)
      {
          this.minValue = minValue;
      }
  
      /**
       * Get the value of maxValue.
       *
       * @return value of maxValue.
       */
      public short getMaxValue()
      {
          return maxValue;
      }
  
      /**
       * Set the value of maxValue.
       *
       * @param maxValue  Value to assign to maxValue.
       */
      public void setMaxValue(short maxValue)
      {
          this.maxValue = maxValue;
      }
  }
  
  
  

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

Reply via email to