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

This page was automatically generated by Maven