1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.commons.validator;
20
21 import javax.faces.application.FacesMessage;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.validator.ValidatorException;
25
26 import org.apache.commons.validator.GenericValidator;
27 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
28 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidator;
29
30
31
32
33
34
35
36
37
38
39 @JSFValidator(
40 name = "mcv:validateRegExpr",
41 clazz = "org.apache.myfaces.commons.validator.RegExprValidator",
42 tagClass = "org.apache.myfaces.commons.validator.ValidateRegExprTag",
43 serialuidtag = "-449945949876262076L")
44 public abstract class AbstractRegExprValidator extends ValidatorBase {
45
46
47
48 public static final String VALIDATOR_ID = "org.apache.myfaces.commons.validator.RegExpr";
49
50
51
52
53
54 public static final String REGEXPR_MESSAGE_ID = "org.apache.myfaces.commons.validator.RegExpr.INVALID";
55
56 public AbstractRegExprValidator(){
57 }
58
59 public void validate(
60 FacesContext facesContext,
61 UIComponent uiComponent,
62 Object value)
63 throws ValidatorException {
64
65 if (facesContext == null) throw new NullPointerException("facesContext");
66 if (uiComponent == null) throw new NullPointerException("uiComponent");
67
68 if (value == null)
69 {
70 return;
71 }
72 Object[] args = {value.toString()};
73 String pattern = getPattern();
74 if (pattern == null)
75 {
76 pattern = getPatternExpression();
77 }
78 else if (pattern != null && pattern.length() <= 0)
79 {
80 pattern = getPatternExpression();
81 }
82 if(!GenericValidator.matchRegexp(value.toString(),"^"+pattern+"$")){
83 throw new ValidatorException(getFacesMessage(REGEXPR_MESSAGE_ID, args));
84 }
85 }
86
87
88
89
90
91
92
93
94
95 @JSFProperty(
96 literalOnly = true)
97 public abstract String getPattern();
98
99
100
101
102 public abstract void setPattern(String string);
103
104
105
106
107
108
109
110 @JSFProperty
111 public abstract String getPatternExpression();
112
113
114 }