1 package org.apache.fulcrum.intake.validator; 2 3 56 57 import java.util.Map ; 58 import org.apache.regexp.RE; 59 import org.apache.fulcrum.ServiceException; 60 import org.apache.commons.fileupload.FileItem; 61 62 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 ServiceException 94 { 95 init(paramMap); 96 } 97 98 public FileValidator() 99 { 100 } 101 102 103 111 public void init(Map paramMap) 112 throws ServiceException 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 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 177 public void assertValidity(String testValue) 178 throws ValidationException 179 { 180 throw new ValidationException("this validation is not implemented"); 181 } 182 183 184 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 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 229 public String getMessage() 230 { 231 if ( message == null ) 232 { 233 return ""; 234 } 235 return message; 236 } 237 238 239 242 protected void doAssertValidity(FileItem testValue) 243 throws ValidationException 244 { 245 } 246 247 251 255 public boolean isRequired() 256 { 257 return required; 258 } 259 260 264 public void setRequired(boolean v) 265 { 266 this.required = v; 267 } 268 269 273 public String getRequiredMessage() 274 { 275 return requiredMessage; 276 } 277 278 282 public void setRequiredMessage(String v) 283 { 284 this.requiredMessage = v; 285 } 286 287 291 public int getMinLength() 292 { 293 return minLength; 294 } 295 296 300 public void setMinLength(int v) 301 { 302 this.minLength = v; 303 } 304 305 309 public String getMinLengthMessage() 310 { 311 return minLengthMessage; 312 } 313 314 318 public void setMinLengthMessage(String v) 319 { 320 this.minLengthMessage = v; 321 } 322 323 327 public int getMaxLength() 328 { 329 return maxLength; 330 } 331 332 336 public void setMaxLength(int v) 337 { 338 this.maxLength = v; 339 } 340 341 345 public String getMaxLengthMessage() 346 { 347 return maxLengthMessage; 348 } 349 350 354 public void setMaxLengthMessage(String v) 355 { 356 this.maxLengthMessage = v; 357 } 358 } 359 | Popular Tags |