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 | |
import org.apache.oro.text.regex.Perl5Compiler; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class Pattern extends BaseValidator |
38 | |
{ |
39 | |
|
40 | |
|
41 | |
|
42 | |
private String _pattern; |
43 | |
private String _quotedPattern; |
44 | |
private java.util.regex.Pattern _compiledPattern; |
45 | |
|
46 | |
public Pattern() |
47 | 0 | { |
48 | 0 | } |
49 | |
|
50 | |
public Pattern(String initializer) |
51 | |
{ |
52 | 0 | super(initializer); |
53 | 0 | } |
54 | |
|
55 | |
public void validate(IFormComponent field, ValidationMessages messages, Object object) |
56 | |
throws ValidatorException |
57 | |
{ |
58 | 0 | String input = object.toString(); |
59 | |
|
60 | 0 | if (! _compiledPattern.matcher(input).matches() ) |
61 | 0 | throw new ValidatorException(buildMessage(messages, field), |
62 | |
ValidationConstraint.PATTERN_MISMATCH); |
63 | 0 | } |
64 | |
|
65 | |
private String buildMessage(ValidationMessages messages, IFormComponent field) |
66 | |
{ |
67 | 0 | return messages.formatValidationMessage( |
68 | |
getMessage(), |
69 | |
ValidationStrings.PATTERN_MISMATCH, |
70 | |
new Object[] |
71 | |
{field.getDisplayName(), _pattern }); |
72 | |
} |
73 | |
|
74 | |
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
75 | |
FormComponentContributorContext context, IFormComponent field) |
76 | |
{ |
77 | 0 | JSONObject profile = context.getProfile(); |
78 | |
|
79 | 0 | if (!profile.has(ValidationConstants.CONSTRAINTS)) { |
80 | 0 | profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); |
81 | |
} |
82 | 0 | JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); |
83 | |
|
84 | 0 | accumulateProperty(cons, field.getClientId(), |
85 | |
new JSONLiteral("[tapestry.form.validation.isValidPattern,\"" |
86 | |
+ _quotedPattern + "\"]")); |
87 | |
|
88 | 0 | accumulateProfileProperty(field, profile, |
89 | |
ValidationConstants.CONSTRAINTS, buildMessage(context, field)); |
90 | 0 | } |
91 | |
|
92 | |
public void setPattern(String pattern) |
93 | |
{ |
94 | 0 | _pattern = pattern; |
95 | 0 | _compiledPattern = java.util.regex.Pattern.compile(pattern); |
96 | 0 | _quotedPattern = Perl5Compiler.quotemeta(_pattern); |
97 | 0 | } |
98 | |
|
99 | |
public String getPattern() |
100 | |
{ |
101 | 0 | return _pattern; |
102 | |
} |
103 | |
} |