1 3 4 package com.sun.j2ee.blueprints.bpcatalog.validator; 5 6 import java.util.*; 7 import java.util.regex.*; 8 9 import javax.faces.FactoryFinder; 10 import javax.faces.component.UIComponent; 11 import javax.faces.component.UIInput; 12 import javax.faces.context.FacesContext; 13 import javax.faces.application.FacesMessage; 14 import javax.faces.validator.Validator; 15 import javax.faces.validator.ValidatorException; 16 import javax.faces.component.StateHolder; 17 18 import javax.faces.application.ApplicationFactory; 19 import javax.faces.application.Application; 20 21 22 23 26 27 public class DateValidator implements Validator, StateHolder { 28 29 30 public static final String DATE_INVALID_MESSAGE_ID = 31 "DateInvalid"; 32 33 private String datePattern = null; 34 private boolean transientValue = false; 35 36 public DateValidator() { 37 super(); 38 } 39 40 41 public DateValidator(String datePattern) { 42 super(); 43 this.datePattern = datePattern; 44 } 45 46 47 public String getDatePatern() { 48 49 return datePattern; 50 } 51 52 53 public void setDatePattern(String datePattern) { 54 this.datePattern = datePattern; 55 } 56 57 public void validate(FacesContext context, UIComponent component, 58 Object inputValue) { 59 60 boolean valid = false; 61 String value = (String )inputValue; 62 try { 63 valid = Pattern.matches(datePattern, value); 64 } catch(PatternSyntaxException pse) { 65 System.out.println("Date:Validator: There is a problem with the regular expression!"); 66 } 67 if (!valid) { 68 FacesMessage error = new FacesMessage("Date " + value + 69 " is invalid. Please enter a date conforming to " + 70 datePattern); 71 throw new ValidatorException(error); 72 } 73 } 74 75 76 public Object saveState(FacesContext context) { 77 Object values[] = new Object [1]; 78 values[0] = datePattern; 79 return (values); 80 } 81 82 83 public void restoreState(FacesContext context, Object state) { 84 Object values[] = (Object []) state; 85 datePattern = (String ) values[0]; 86 } 87 88 89 public boolean isTransient() { 90 return (this.transientValue); 91 } 92 93 94 public void setTransient(boolean transientValue) { 95 this.transientValue = transientValue; 96 } 97 } 98 | Popular Tags |