View Javadoc
1 package org.apache.turbine.services.intake.validator; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.math.BigDecimal; 58 import java.util.Map; 59 import org.apache.turbine.util.TurbineException; 60 61 /*** 62 * Validates numbers with the following constraints in addition to those 63 * listed in DefaultValidator. 64 * 65 * <table> 66 * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr> 67 * <tr><td>minValue</td><td>greater than BigDecimal.MIN_VALUE</td> 68 * <td> </td></tr> 69 * <tr><td>maxValue</td><td>less than BigDecimal.MAX_VALUE</td> 70 * <td> </td></tr> 71 * <tr><td>notANumberMessage</td><td>Some text</td> 72 * <td>Entry was not a valid number</td></tr> 73 * </table> 74 * 75 * @author <a href="mailto:jmcnally@collab.net>;John McNally</a> 76 * @version $Id: NumberValidator.java,v 1.4 2002/07/16 16:10:22 henning Exp $ 77 */ 78 public class NumberValidator 79 extends DefaultValidator 80 { 81 private static String INVALID_NUMBER = "Entry was not a valid number"; 82 83 private BigDecimal minValue; 84 protected String minValueMessage; 85 private BigDecimal maxValue; 86 protected String maxValueMessage; 87 protected String invalidNumberMessage; 88 89 public NumberValidator(Map paramMap) 90 throws TurbineException 91 { 92 this(); 93 init(paramMap); 94 } 95 96 public NumberValidator() 97 { 98 invalidNumberMessage = getDefaultInvalidNumberMessage(); 99 } 100 101 /*** 102 * Extract the relevant parameters from the constraints listed 103 * in <input-param> tags within the intake.xml file. 104 * 105 * @param inputParameters a <code>Map</code> of <code>InputParam</code>'s 106 * containing constraints on the input. 107 * @exception TurbineException if an error occurs 108 */ 109 public void init(Map paramMap) 110 throws TurbineException 111 { 112 super.init(paramMap); 113 114 minValueMessage = null; 115 maxValueMessage = null; 116 117 doInit(paramMap); 118 119 Constraint constraint = (Constraint)paramMap.get("notANumberMessage"); 120 if ( constraint != null ) 121 { 122 String param = constraint.getValue(); 123 if ( param != null && param.length() != 0 ) 124 { 125 invalidNumberMessage = param; 126 } 127 else if ( constraint.getMessage().length() != 0 ) 128 { 129 invalidNumberMessage = constraint.getMessage(); 130 } 131 } 132 } 133 134 protected void doInit(Map paramMap) 135 { 136 minValue = null; 137 maxValue = null; 138 139 Constraint constraint = (Constraint)paramMap.get("minValue"); 140 if ( constraint != null ) 141 { 142 String param = constraint.getValue(); 143 minValue = new BigDecimal(param); 144 minValueMessage = constraint.getMessage(); 145 } 146 147 constraint = (Constraint)paramMap.get("maxValue"); 148 if ( constraint != null ) 149 { 150 String param = constraint.getValue(); 151 maxValue = new BigDecimal(param); 152 maxValueMessage = constraint.getMessage(); 153 } 154 } 155 156 protected String getDefaultInvalidNumberMessage() 157 { 158 return INVALID_NUMBER; 159 } 160 161 /*** 162 * Determine whether a testValue meets the criteria specified 163 * in the constraints defined for this validator 164 * 165 * @param testValue a <code>String</code> to be tested 166 * @exception ValidationException containing an error message if the 167 * testValue did not pass the validation tests. 168 */ 169 protected void doAssertValidity(String testValue) 170 throws ValidationException 171 { 172 BigDecimal bd = null; 173 try 174 { 175 bd = new BigDecimal(testValue); 176 } 177 catch (RuntimeException e) 178 { 179 message = invalidNumberMessage; 180 throw new ValidationException(invalidNumberMessage); 181 } 182 183 if ( minValue != null && bd.compareTo(minValue) < 0 ) 184 { 185 message = minValueMessage; 186 throw new ValidationException(minValueMessage); 187 } 188 if ( maxValue != null && bd.compareTo(maxValue) > 0 ) 189 { 190 message = maxValueMessage; 191 throw new ValidationException(maxValueMessage); 192 } 193 } 194 195 // ************************************************************ 196 // ** Bean accessor methods ** 197 // ************************************************************ 198 199 /*** 200 * Get the value of minValue. 201 * @return value of minValue. 202 */ 203 public BigDecimal getMinValueAsBigDecimal() 204 { 205 return minValue; 206 } 207 208 /*** 209 * Set the value of minValue. 210 * @param v Value to assign to minValue. 211 */ 212 public void setMinValue(BigDecimal v) 213 { 214 this.minValue = v; 215 } 216 217 /*** 218 * Get the value of minValueMessage. 219 * @return value of minValueMessage. 220 */ 221 public String getMinValueMessage() 222 { 223 return minValueMessage; 224 } 225 226 /*** 227 * Set the value of minValueMessage. 228 * @param v Value to assign to minValueMessage. 229 */ 230 public void setMinValueMessage(String v) 231 { 232 this.minValueMessage = v; 233 } 234 235 /*** 236 * Get the value of maxValue. 237 * @return value of maxValue. 238 */ 239 public BigDecimal getMaxValueAsBigDecimal() 240 { 241 return maxValue; 242 } 243 244 /*** 245 * Set the value of maxValue. 246 * @param v Value to assign to maxValue. 247 */ 248 public void setMaxValue(BigDecimal v) 249 { 250 this.maxValue = v; 251 } 252 253 /*** 254 * Get the value of maxValueMessage. 255 * @return value of maxValueMessage. 256 */ 257 public String getMaxValueMessage() 258 { 259 return maxValueMessage; 260 } 261 262 /*** 263 * Set the value of maxValueMessage. 264 * @param v Value to assign to maxValueMessage. 265 */ 266 public void setMaxValueMessage(String v) 267 { 268 this.maxValueMessage = v; 269 } 270 271 /*** 272 * Get the value of invalidNumberMessage. 273 * @return value of invalidNumberMessage. 274 */ 275 public String getInvalidNumberMessage() 276 { 277 return invalidNumberMessage; 278 } 279 280 /*** 281 * Set the value of invalidNumberMessage. 282 * @param v Value to assign to invalidNumberMessage. 283 */ 284 public void setInvalidNumberMessage(String v) 285 { 286 this.invalidNumberMessage = v; 287 } 288 289 }

This page was automatically generated by Maven