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