1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.form.validator; |
16 |
|
|
17 |
|
import org.apache.tapestry.IMarkupWriter; |
18 |
|
import org.apache.tapestry.IRequestCycle; |
19 |
|
import org.apache.tapestry.form.FormComponentContributorContext; |
20 |
|
import org.apache.tapestry.form.IFormComponent; |
21 |
|
import org.apache.tapestry.form.ValidationMessages; |
22 |
|
import org.apache.tapestry.json.JSONLiteral; |
23 |
|
import org.apache.tapestry.json.JSONObject; |
24 |
|
import org.apache.tapestry.valid.ValidationConstants; |
25 |
|
import org.apache.tapestry.valid.ValidationConstraint; |
26 |
|
import org.apache.tapestry.valid.ValidationStrings; |
27 |
|
import org.apache.tapestry.valid.ValidatorException; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
public class Email extends BaseValidator |
37 |
|
{ |
38 |
|
public static final String TLD_PATTERN = "arpa|aero|biz|com|coop|edu|gov|info|int|mil|museum|name|net|" |
39 |
|
+ "org|pro|travel|xxx|jobs|mobi|post|" |
40 |
|
+ "ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|" |
41 |
|
+ "bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|" |
42 |
|
+ "ec|ee|eg|er|eu|es|et|fi|fj|fk|fm|fo|fr|ga|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|" |
43 |
|
+ "gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kr|kw|ky|kz|" |
44 |
|
+ "la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|" |
45 |
|
+ "my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|" |
46 |
|
+ "re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sk|sl|sm|sn|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|" |
47 |
|
+ "tn|to|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw"; |
48 |
|
public static final String DOMAIN_PATTERN = "([0-9a-z]([-0-9a-z]{0,61}[0-9a-z])?\\.)+" + "(" + TLD_PATTERN + ")"; |
49 |
|
public static final String USERNAME_PATTERN = "([-/!\\#$*?=_+&'\\da-z]+[.])*[-/!\\#$*?=_+&'\\da-z]+"; |
50 |
|
public static final String PATTERN = "^(?i)"+ USERNAME_PATTERN + "@" + "(" + DOMAIN_PATTERN + ")$"; |
51 |
|
|
52 |
1 |
private static final java.util.regex.Pattern PATTERN_COMPILED = java.util.regex.Pattern.compile(PATTERN); |
53 |
|
|
54 |
|
public Email() |
55 |
17 |
{ |
56 |
17 |
} |
57 |
|
|
58 |
|
public Email(String initializer) |
59 |
|
{ |
60 |
2 |
super(initializer); |
61 |
2 |
} |
62 |
|
|
63 |
|
public void validate(IFormComponent field, ValidationMessages messages, Object object) |
64 |
|
throws ValidatorException |
65 |
|
{ |
66 |
16 |
String input = (String) object; |
67 |
|
|
68 |
16 |
if ( !PATTERN_COMPILED.matcher(input).matches() ) |
69 |
8 |
throw new ValidatorException(buildMessage(messages, field), |
70 |
|
ValidationConstraint.EMAIL_FORMAT); |
71 |
8 |
} |
72 |
|
|
73 |
|
private String buildMessage(ValidationMessages messages, IFormComponent field) |
74 |
|
{ |
75 |
10 |
return messages.formatValidationMessage( |
76 |
|
getMessage(), |
77 |
|
ValidationStrings.INVALID_EMAIL, |
78 |
|
new Object[] |
79 |
|
{ field.getDisplayName() }); |
80 |
|
} |
81 |
|
|
82 |
|
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
83 |
|
FormComponentContributorContext context, IFormComponent field) |
84 |
|
{ |
85 |
2 |
JSONObject profile = context.getProfile(); |
86 |
|
|
87 |
2 |
if (!profile.has(ValidationConstants.CONSTRAINTS)) { |
88 |
2 |
profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); |
89 |
|
} |
90 |
2 |
JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); |
91 |
|
|
92 |
2 |
accumulateProperty(cons, field.getClientId(), |
93 |
|
new JSONLiteral("[tapestry.form.validation.isEmailAddress,false,true]")); |
94 |
|
|
95 |
2 |
accumulateProfileProperty(field, profile, |
96 |
|
ValidationConstants.CONSTRAINTS, buildMessage(context, field)); |
97 |
2 |
} |
98 |
|
} |