1 4 5 42 43 package org.apache.cocoon.faces.samples.carstore; 44 45 import javax.faces.application.FacesMessage; 46 import javax.faces.component.StateHolder; 47 import javax.faces.component.UIComponent; 48 import javax.faces.component.UIOutput; 49 import javax.faces.context.FacesContext; 50 import javax.faces.validator.Validator; 51 import javax.faces.validator.ValidatorException; 52 53 import java.util.ArrayList ; 54 import java.util.Iterator ; 55 import java.util.StringTokenizer ; 56 57 58 86 87 public class FormatValidator implements Validator, StateHolder { 88 89 90 92 98 public static final String FORMAT_INVALID_MESSAGE_ID = 99 "carstore.Format_Invalid"; 100 101 private ArrayList formatPatternsList = null; 102 103 104 public FormatValidator() { 108 super(); 109 } 110 111 112 119 public FormatValidator(String formatPatterns) { 120 super(); 121 this.formatPatterns = formatPatterns; 122 parseFormatPatterns(); 123 } 124 125 132 private String formatPatterns = null; 133 134 135 138 public String getFormatPatterns() { 139 140 return (this.formatPatterns); 141 142 } 143 144 145 151 public void setFormatPatterns(String formatPatterns) { 152 153 this.formatPatterns = formatPatterns; 154 parseFormatPatterns(); 155 } 156 157 158 162 public void parseFormatPatterns() { 163 if (formatPatterns == null || formatPatterns.length() == 0) { 164 return; 165 } 166 if (formatPatternsList != null) { 167 return; 169 } else { 170 formatPatternsList = new ArrayList (); 171 } 172 StringTokenizer st = new StringTokenizer (formatPatterns, "|"); 173 while (st.hasMoreTokens()) { 174 String token = st.nextToken(); 175 formatPatternsList.add(token); 176 } 177 } 178 179 180 public void validate(FacesContext context, UIComponent component, 184 Object toValidate) { 185 boolean valid = false; 186 String value = null; 187 if ((context == null) || (component == null)) { 188 throw new NullPointerException (); 189 } 190 if (!(component instanceof UIOutput)) { 191 return; 192 } 193 194 if (null == formatPatternsList || null == toValidate) { 195 return; 196 } 197 198 value = toValidate.toString(); 199 Iterator patternIt = formatPatternsList.iterator(); 201 while (patternIt.hasNext()) { 202 valid = isFormatValid(((String ) patternIt.next()), value); 203 if (valid) { 204 break; 205 } 206 } 207 if (!valid) { 208 FacesMessage errMsg = MessageFactory.getMessage(context, 209 FORMAT_INVALID_MESSAGE_ID, 210 (new Object []{ 211 formatPatterns 212 })); 213 throw new ValidatorException(errMsg); 214 } 215 } 216 217 218 221 protected boolean isFormatValid(String pattern, String value) { 222 boolean valid = true; 223 if (pattern == null || pattern.length() == 0) { 225 return true; 226 } 227 if (value == null || value.length() == 0) { 229 return false; 230 } 231 if (value.length() != pattern.length()) { 234 return false; 235 } 236 value = value.trim(); 237 char[] input = value.toCharArray(); 246 char[] fmtpattern = pattern.toCharArray(); 247 for (int i = 0; i < fmtpattern.length; ++i) { 248 if (fmtpattern[i] == 'A') { 249 if (!(Character.isLetter(input[i]))) { 250 valid = false; 251 } 252 } else if (fmtpattern[i] == '9') { 253 if (!(Character.isDigit(input[i]))) { 254 valid = false; 255 } 256 } else if (fmtpattern[i] == '#') { 257 if ((!(Character.isDigit(input[i]))) && 258 (!(Character.isLetter(input[i])))) { 259 valid = false; 260 } 261 } else { 262 if (!(fmtpattern[i] == input[i])) { 263 valid = false; 264 } 265 } 266 } 267 return valid; 268 269 } 270 271 272 public Object saveState(FacesContext context) { 273 Object values[] = new Object [2]; 274 values[0] = formatPatterns; 275 values[1] = formatPatternsList; 276 return (values); 277 } 278 279 280 public void restoreState(FacesContext context, Object state) { 281 Object values[] = (Object []) state; 282 formatPatterns = (String ) values[0]; 283 formatPatternsList = (ArrayList ) values[1]; 284 } 285 286 287 private boolean transientValue = false; 288 289 290 public boolean isTransient() { 291 return (this.transientValue); 292 } 293 294 295 public void setTransient(boolean transientValue) { 296 this.transientValue = transientValue; 297 } 298 } 299
| Popular Tags
|