|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Email.java | 100% | 100% | 100% | 100% |
|
1 | // Copyright 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
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.util.RegexpMatcher; | |
23 | import org.apache.tapestry.valid.ValidationConstraint; | |
24 | import org.apache.tapestry.valid.ValidationStrings; | |
25 | import org.apache.tapestry.valid.ValidatorException; | |
26 | ||
27 | /** | |
28 | * Validates that the user input, a string, is an email address (by checking it against a regular | |
29 | * expression). | |
30 | * | |
31 | * @author Howard Lewis Ship | |
32 | * @since 4.0 | |
33 | */ | |
34 | public class Email extends BaseValidator | |
35 | { | |
36 | static final String PATTERN = "^\\w[-._\\w]*\\w@\\w[-._\\w]*\\w\\.\\w{2,3}$"; | |
37 | ||
38 | // TODO: Possible thread safety issue if the validator | |
39 | // is shared across threads, because the matcher | |
40 | // will be too. | |
41 | private RegexpMatcher _matcher = new RegexpMatcher(); | |
42 | ||
43 | 4 | public Email() |
44 | { | |
45 | } | |
46 | ||
47 | 2 | public Email(String initializer) |
48 | { | |
49 | 2 | super(initializer); |
50 | } | |
51 | ||
52 | 3 | public void validate(IFormComponent field, ValidationMessages messages, Object object) |
53 | throws ValidatorException | |
54 | { | |
55 | 3 | String input = (String) object; |
56 | ||
57 | 3 | if (!_matcher.matches(PATTERN, input)) |
58 | 2 | throw new ValidatorException(buildMessage(messages, field), |
59 | ValidationConstraint.EMAIL_FORMAT); | |
60 | } | |
61 | ||
62 | 4 | private String buildMessage(ValidationMessages messages, IFormComponent field) |
63 | { | |
64 | 4 | return messages.formatValidationMessage( |
65 | getMessage(), | |
66 | ValidationStrings.INVALID_EMAIL, | |
67 | new Object[] | |
68 | { field.getDisplayName() }); | |
69 | } | |
70 | ||
71 | 2 | public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
72 | FormComponentContributorContext context, IFormComponent field) | |
73 | { | |
74 | 2 | context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js"); |
75 | ||
76 | 2 | String pattern = _matcher.getEscapedPatternString(PATTERN); |
77 | 2 | String message = buildMessage(context, field); |
78 | ||
79 | 2 | StringBuffer buffer = new StringBuffer("function(event) { validate_regexp(event, "); |
80 | 2 | buffer.append(context.getFieldDOM()); |
81 | 2 | buffer.append(", '"); |
82 | 2 | buffer.append(pattern); |
83 | 2 | buffer.append("', '"); |
84 | 2 | buffer.append(message); |
85 | 2 | buffer.append("'); }"); |
86 | ||
87 | 2 | context.addSubmitListener(buffer.toString()); |
88 | } | |
89 | } |
|