1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.valid; |
16 | |
|
17 | |
import java.util.HashMap; |
18 | |
import java.util.Locale; |
19 | |
import java.util.Map; |
20 | |
|
21 | |
import org.apache.tapestry.IMarkupWriter; |
22 | |
import org.apache.tapestry.IRequestCycle; |
23 | |
import org.apache.tapestry.form.IFormComponent; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class EmailValidator extends BaseValidator |
35 | |
{ |
36 | |
|
37 | |
private int _minimumLength; |
38 | |
|
39 | |
private String _minimumLengthMessage; |
40 | |
|
41 | |
private String _invalidEmailFormatMessage; |
42 | |
|
43 | 0 | private String _scriptPath = "/org/apache/tapestry/valid/EmailValidator.script"; |
44 | |
|
45 | |
public EmailValidator() |
46 | 0 | { |
47 | 0 | } |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public EmailValidator(String initializer) |
57 | |
{ |
58 | 0 | super(initializer); |
59 | 0 | } |
60 | |
|
61 | |
public String toString(IFormComponent field, Object value) |
62 | |
{ |
63 | 0 | if (value == null) return null; |
64 | |
|
65 | 0 | return value.toString(); |
66 | |
} |
67 | |
|
68 | |
public Object toObject(IFormComponent field, String input) |
69 | |
throws ValidatorException |
70 | |
{ |
71 | 0 | if (checkRequired(field, input)) return null; |
72 | |
|
73 | 0 | if (_minimumLength > 0 && input.length() < _minimumLength) |
74 | 0 | throw new ValidatorException(buildMinimumLengthMessage(field), |
75 | |
ValidationConstraint.MINIMUM_WIDTH); |
76 | |
|
77 | 0 | if (!isValidEmail(input)) |
78 | 0 | throw new ValidatorException(buildInvalidEmailFormatMessage(field), |
79 | |
ValidationConstraint.EMAIL_FORMAT); |
80 | |
|
81 | 0 | return input; |
82 | |
} |
83 | |
|
84 | |
public int getMinimumLength() |
85 | |
{ |
86 | 0 | return _minimumLength; |
87 | |
} |
88 | |
|
89 | |
public void setMinimumLength(int minimumLength) |
90 | |
{ |
91 | 0 | _minimumLength = minimumLength; |
92 | 0 | } |
93 | |
|
94 | |
public void renderValidatorContribution(IFormComponent field, |
95 | |
IMarkupWriter writer, IRequestCycle cycle) |
96 | |
{ |
97 | 0 | if (!isClientScriptingEnabled()) return; |
98 | |
|
99 | 0 | Map symbols = new HashMap(); |
100 | |
|
101 | 0 | Locale locale = field.getPage().getLocale(); |
102 | 0 | String displayName = field.getDisplayName(); |
103 | |
|
104 | 0 | if (isRequired()) |
105 | 0 | symbols.put("requiredMessage", buildRequiredMessage(field)); |
106 | |
|
107 | 0 | if (_minimumLength > 0) |
108 | 0 | symbols.put("minimumLengthMessage", |
109 | |
buildMinimumLengthMessage(field)); |
110 | |
|
111 | 0 | String pattern = getPattern(getInvalidEmailFormatMessage(), |
112 | |
"invalid-email-format", locale); |
113 | |
|
114 | 0 | symbols.put("emailFormatMessage", formatString(pattern, displayName)); |
115 | |
|
116 | 0 | processValidatorScript(_scriptPath, cycle, field, symbols); |
117 | 0 | } |
118 | |
|
119 | |
public String getScriptPath() |
120 | |
{ |
121 | 0 | return _scriptPath; |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public void setScriptPath(String scriptPath) |
133 | |
{ |
134 | 0 | _scriptPath = scriptPath; |
135 | 0 | } |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
protected boolean isValidEmail(String email) |
146 | |
{ |
147 | 0 | int atIndex = email.indexOf('@'); |
148 | |
|
149 | 0 | return !((atIndex <= 0) || (atIndex == email.length() - 1)); |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
public String getInvalidEmailFormatMessage() |
155 | |
{ |
156 | 0 | return _invalidEmailFormatMessage; |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
public String getMinimumLengthMessage() |
162 | |
{ |
163 | 0 | return _minimumLengthMessage; |
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public void setInvalidEmailFormatMessage(String string) |
174 | |
{ |
175 | 0 | _invalidEmailFormatMessage = string; |
176 | 0 | } |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
public void setMinimumLengthMessage(String string) |
185 | |
{ |
186 | 0 | _minimumLengthMessage = string; |
187 | 0 | } |
188 | |
|
189 | |
|
190 | |
|
191 | |
protected String buildMinimumLengthMessage(IFormComponent field) |
192 | |
{ |
193 | 0 | String pattern = getPattern(_minimumLengthMessage, "field-too-short", |
194 | |
field.getPage().getLocale()); |
195 | |
|
196 | 0 | return formatString(pattern, Integer.toString(_minimumLength), field |
197 | |
.getDisplayName()); |
198 | |
} |
199 | |
|
200 | |
|
201 | |
|
202 | |
protected String buildInvalidEmailFormatMessage(IFormComponent field) |
203 | |
{ |
204 | 0 | String pattern = getPattern(_invalidEmailFormatMessage, |
205 | |
"invalid-email-format", field.getPage().getLocale()); |
206 | |
|
207 | 0 | return formatString(pattern, field.getDisplayName()); |
208 | |
} |
209 | |
} |