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.util.Map; 58 import org.apache.regexp.RE; 59 import org.apache.turbine.util.TurbineException; 60 61 /*** 62 * A validator that will compare a testValue against the following 63 * constraints: 64 * <table> 65 * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr> 66 * <tr><td>required</td><td>true|false</td><td>false</td></tr> 67 * <tr><td>mask</td><td>regexp</td><td> </td></tr> 68 * <tr><td>minLength</td><td>integer</td><td>0</td></tr> 69 * <tr><td>maxLength</td><td>integer</td><td> </td></tr> 70 * </table> 71 * 72 * This validator can serve as the base class for more specific validators 73 * 74 * @author <a href="mailto:jmcnally@collab.net">John McNally</a> 75 * @version $Id: DefaultValidator.java,v 1.2 2002/07/11 16:53:27 mpoeschl Exp $ 76 */ 77 public class DefaultValidator 78 implements Validator, InitableByConstraintMap 79 { 80 protected boolean required; 81 protected String requiredMessage; 82 protected RE mask; 83 protected String maskMessage; 84 protected int minLength; 85 protected String minLengthMessage; 86 protected int maxLength; 87 protected String maxLengthMessage; 88 89 protected String message; 90 91 public DefaultValidator(Map paramMap) 92 throws TurbineException 93 { 94 init(paramMap); 95 } 96 97 public DefaultValidator() 98 { 99 } 100 101 102 103 /*** 104 * Extract the relevant parameters from the constraints listed 105 * in <rule> tags within the intake.xml file. 106 * 107 * @param paramMap a <code>Map</code> of <code>Rule</code>'s 108 * containing constraints on the input. 109 * @exception TurbineException if an error occurs 110 */ 111 public void init(Map paramMap) 112 throws TurbineException 113 { 114 mask = null; 115 maskMessage = null; 116 minLength = 0; 117 minLengthMessage = null; 118 maxLength = 0; 119 maxLengthMessage = null; 120 121 Constraint constraint = (Constraint)paramMap.get("mask"); 122 if ( constraint != null ) 123 { 124 String param = constraint.getValue(); 125 setMask(param); 126 maskMessage = constraint.getMessage(); 127 } 128 129 constraint = (Constraint)paramMap.get("minLength"); 130 if ( constraint != null ) 131 { 132 String param = constraint.getValue(); 133 minLength = Integer.parseInt(param); 134 minLengthMessage = constraint.getMessage(); 135 } 136 137 constraint = (Constraint)paramMap.get("maxLength"); 138 if ( constraint != null ) 139 { 140 String param = constraint.getValue(); 141 maxLength = Integer.parseInt(param); 142 maxLengthMessage = constraint.getMessage(); 143 } 144 145 constraint = (Constraint)paramMap.get("required"); 146 if ( constraint == null ) 147 { 148 required = false; 149 } 150 else 151 { 152 String param = constraint.getValue(); 153 required = new Boolean(param).booleanValue(); 154 requiredMessage = constraint.getMessage(); 155 } 156 } 157 158 /*** 159 * Determine whether a testValue meets the criteria specified 160 * in the constraints defined for this validator 161 * 162 * @param testValue a <code>String</code> to be tested 163 * @return true if valid, false otherwise 164 */ 165 public boolean isValid(String testValue) 166 { 167 boolean valid = false; 168 try 169 { 170 assertValidity(testValue); 171 valid = true; 172 } 173 catch (ValidationException ve) 174 { 175 valid = false; 176 } 177 return valid; 178 } 179 180 /*** 181 * Determine whether a testValue meets the criteria specified 182 * in the constraints defined for this validator 183 * 184 * @param testValue a <code>String</code> to be tested 185 * @exception ValidationException containing an error message if the 186 * testValue did not pass the validation tests. 187 */ 188 public void assertValidity(String testValue) 189 throws ValidationException 190 { 191 message = null; 192 193 if ( (!required && minLength == 0) 194 && ( testValue == null || testValue.length() == 0) ) 195 { 196 return; 197 } 198 else if ( required 199 && ( testValue == null || testValue.length() == 0)) 200 { 201 message = requiredMessage; 202 throw new ValidationException(requiredMessage); 203 } 204 205 // allow subclasses first chance at validation 206 doAssertValidity(testValue); 207 208 if ( mask != null && !mask.match(testValue) ) 209 { 210 message = maskMessage; 211 throw new ValidationException(maskMessage); 212 } 213 if ( minLength > 0 && testValue.length() < minLength ) 214 { 215 message = minLengthMessage; 216 throw new ValidationException(minLengthMessage); 217 } 218 if ( maxLength > 0 && testValue.length() > maxLength ) 219 { 220 message = maxLengthMessage; 221 throw new ValidationException(maxLengthMessage); 222 } 223 } 224 225 /*** 226 * Get the last error message resulting from invalid input. 227 * 228 * @return a <code>String</code> message, or the empty String "". 229 */ 230 public String getMessage() 231 { 232 if ( message == null ) 233 { 234 return ""; 235 } 236 return message; 237 } 238 239 240 /*** 241 * Method to allow subclasses to add additional validation 242 */ 243 protected void doAssertValidity(String testValue) 244 throws ValidationException 245 { 246 } 247 248 // ************************************************************ 249 // ** Bean accessor methods ** 250 // ************************************************************ 251 252 /*** 253 * Get the value of required. 254 * @return value of required. 255 */ 256 public boolean isRequired() 257 { 258 return required; 259 } 260 261 /*** 262 * Set the value of required. 263 * @param v Value to assign to required. 264 */ 265 public void setRequired(boolean v) 266 { 267 this.required = v; 268 } 269 270 /*** 271 * Get the value of requiredMessage. 272 * @return value of requiredMessage. 273 */ 274 public String getRequiredMessage() 275 { 276 return requiredMessage; 277 } 278 279 /*** 280 * Set the value of requiredMessage. 281 * @param v Value to assign to requiredMessage. 282 */ 283 public void setRequiredMessage(String v) 284 { 285 this.requiredMessage = v; 286 } 287 288 /*** 289 * Get the value of mask. 290 * @return value of mask. 291 */ 292 public String getMask() 293 { 294 return mask.toString(); 295 } 296 297 /*** 298 * Set the value of mask. 299 * @param v Value to assign to mask. 300 */ 301 public void setMask(String v) 302 throws TurbineException 303 { 304 try 305 { 306 mask = new RE(v); 307 } 308 catch (org.apache.regexp.RESyntaxException e) 309 { 310 throw new TurbineException(e); 311 } 312 } 313 314 /*** 315 * Get the value of maskMessage. 316 * @return value of maskMessage. 317 */ 318 public String getMaskMessage() 319 { 320 return maskMessage; 321 } 322 323 /*** 324 * Set the value of maskMessage. 325 * @param v Value to assign to maskMessage. 326 */ 327 public void setMaskMessage(String v) 328 { 329 this.maskMessage = v; 330 } 331 332 /*** 333 * Get the value of minLength. 334 * @return value of minLength. 335 */ 336 public int getMinLength() 337 { 338 return minLength; 339 } 340 341 /*** 342 * Set the value of minLength. 343 * @param v Value to assign to minLength. 344 */ 345 public void setMinLength(int v) 346 { 347 this.minLength = v; 348 } 349 350 /*** 351 * Get the value of minLengthMessage. 352 * @return value of minLengthMessage. 353 */ 354 public String getMinLengthMessage() 355 { 356 return minLengthMessage; 357 } 358 359 /*** 360 * Set the value of minLengthMessage. 361 * @param v Value to assign to minLengthMessage. 362 */ 363 public void setMinLengthMessage(String v) 364 { 365 this.minLengthMessage = v; 366 } 367 368 /*** 369 * Get the value of maxLength. 370 * @return value of maxLength. 371 */ 372 public int getMaxLength() 373 { 374 return maxLength; 375 } 376 377 /*** 378 * Set the value of maxLength. 379 * @param v Value to assign to maxLength. 380 */ 381 public void setMaxLength(int v) 382 { 383 this.maxLength = v; 384 } 385 386 /*** 387 * Get the value of maxLengthMessage. 388 * @return value of maxLengthMessage. 389 */ 390 public String getMaxLengthMessage() 391 { 392 return maxLengthMessage; 393 } 394 395 /*** 396 * Set the value of maxLengthMessage. 397 * @param v Value to assign to maxLengthMessage. 398 */ 399 public void setMaxLengthMessage(String v) 400 { 401 this.maxLengthMessage = v; 402 } 403 }

This page was automatically generated by Maven