001 package org.apache.fulcrum.intake.validator; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022 import java.text.NumberFormat; 023 import java.text.ParseException; 024 import java.util.Locale; 025 import java.util.Map; 026 027 import org.apache.commons.lang.StringUtils; 028 import org.apache.fulcrum.intake.model.Field; 029 030 /** 031 * Validates Floats with the following constraints in addition to those 032 * listed in NumberValidator and DefaultValidator. 033 * 034 * <table> 035 * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr> 036 * <tr><td>minValue</td><td>greater than Float.MIN_VALUE</td> 037 * <td> </td></tr> 038 * <tr><td>maxValue</td><td>less than Float.MAX_VALUE</td> 039 * <td> </td></tr> 040 * <tr><td>invalidNumberMessage</td><td>Some text</td> 041 * <td>Entry was not a valid number</td></tr> 042 * </table> 043 * 044 * @author <a href="mailto:jmcnally@collab.net">John McNally</a> 045 * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a> 046 * @author <a href="mailto:jh@byteaction.de">Jürgen Hoffmann</a> 047 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a> 048 * @version $Id: FloatValidator.java 812827 2009-09-09 08:46:27Z tv $ 049 */ 050 public class FloatValidator 051 extends NumberValidator 052 { 053 /* Init the minValue to that for a Float */ 054 private float minValue = Float.NEGATIVE_INFINITY; 055 056 /* Init the maxValue to that for a Float */ 057 private float maxValue = Float.POSITIVE_INFINITY; 058 059 /** 060 * Constructor to use when initialising Object 061 * 062 * @param paramMap 063 * @throws InvalidMaskException 064 */ 065 public FloatValidator(Map paramMap) 066 throws InvalidMaskException 067 { 068 this(); 069 init(paramMap); 070 } 071 072 /** 073 * Default Constructor 074 */ 075 public FloatValidator() 076 { 077 invalidNumberMessage = "Entry was not a valid Float"; 078 } 079 080 /** 081 * Method to initialise Object 082 * 083 * @param paramMap 084 * @throws InvalidMaskException 085 */ 086 public void init(Map paramMap) 087 throws InvalidMaskException 088 { 089 super.init(paramMap); 090 091 Constraint constraint = (Constraint) paramMap.get(MIN_VALUE_RULE_NAME); 092 if (constraint != null) 093 { 094 String param = constraint.getValue(); 095 minValue = Float.parseFloat(param); 096 minValueMessage = constraint.getMessage(); 097 } 098 099 constraint = (Constraint) paramMap.get(MAX_VALUE_RULE_NAME); 100 if (constraint != null) 101 { 102 String param = constraint.getValue(); 103 maxValue = Float.parseFloat(param); 104 maxValueMessage = constraint.getMessage(); 105 } 106 } 107 108 /** 109 * Determine whether a field meets the criteria specified 110 * in the constraints defined for this validator 111 * 112 * @param field a <code>Field</code> to be tested 113 * @exception ValidationException containing an error message if the 114 * testValue did not pass the validation tests. 115 */ 116 public void assertValidity(Field field) 117 throws ValidationException 118 { 119 Locale locale = field.getLocale(); 120 121 if (field.isMultiValued()) 122 { 123 String[] stringValues = (String[])field.getTestValue(); 124 125 for (int i = 0; i < stringValues.length; i++) 126 { 127 assertValidity(stringValues[i], locale); 128 } 129 } 130 else 131 { 132 assertValidity((String)field.getTestValue(), locale); 133 } 134 } 135 136 /** 137 * Determine whether a testValue meets the criteria specified 138 * in the constraints defined for this validator 139 * 140 * @param testValue a <code>String</code> to be tested 141 * @param locale the Locale of the associated field 142 * @exception ValidationException containing an error message if the 143 * testValue did not pass the validation tests. 144 */ 145 public void assertValidity(String testValue, Locale locale) 146 throws ValidationException 147 { 148 super.assertValidity(testValue); 149 150 if (required || StringUtils.isNotEmpty(testValue)) 151 { 152 float f = 0.0f; 153 NumberFormat nf = NumberFormat.getInstance(locale); 154 155 try 156 { 157 f = nf.parse(testValue).floatValue(); 158 } 159 catch (ParseException e) 160 { 161 errorMessage = invalidNumberMessage; 162 throw new ValidationException(invalidNumberMessage); 163 } 164 165 if (f < minValue) 166 { 167 errorMessage = minValueMessage; 168 throw new ValidationException(minValueMessage); 169 } 170 if (f > maxValue) 171 { 172 errorMessage = maxValueMessage; 173 throw new ValidationException(maxValueMessage); 174 } 175 } 176 } 177 178 179 // ************************************************************ 180 // ** Bean accessor methods ** 181 // ************************************************************ 182 183 /** 184 * Get the value of minValue. 185 * 186 * @return value of minValue. 187 */ 188 public float getMinValue() 189 { 190 return minValue; 191 } 192 193 /** 194 * Set the value of minValue. 195 * 196 * @param minValue Value to assign to minValue. 197 */ 198 public void setMinValue(float minValue) 199 { 200 this.minValue = minValue; 201 } 202 203 /** 204 * Get the value of maxValue. 205 * 206 * @return value of maxValue. 207 */ 208 public float getMaxValue() 209 { 210 return maxValue; 211 } 212 213 /** 214 * Set the value of maxValue. 215 * 216 * @param maxValue Value to assign to maxValue. 217 */ 218 public void setMaxValue(float maxValue) 219 { 220 this.maxValue = maxValue; 221 } 222 }