KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > valid > EmailValidator


1 // Copyright 2004, 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.valid;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.Locale JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.tapestry.IMarkupWriter;
22 import org.apache.tapestry.IRequestCycle;
23 import org.apache.tapestry.form.IFormComponent;
24
25 /**
26  * Simple validation of email strings, to enforce required, and minimum length (maximum length is
27  * enforced in the client browser, by setting a maximum input length on the text field).
28  *
29  * @author Malcolm Edgar
30  * @since 2.3
31  */

32
33 public class EmailValidator extends BaseValidator
34 {
35     private int _minimumLength;
36
37     private String JavaDoc _minimumLengthMessage;
38
39     private String JavaDoc _invalidEmailFormatMessage;
40
41     private String JavaDoc _scriptPath = "/org/apache/tapestry/valid/EmailValidator.script";
42
43     public EmailValidator()
44     {
45     }
46
47     /**
48      * Initializes the EmailValidator with properties defined by the initializer.
49      *
50      * @since 4.0
51      */

52
53     public EmailValidator(String JavaDoc initializer)
54     {
55         super(initializer);
56     }
57
58     public String JavaDoc toString(IFormComponent field, Object JavaDoc value)
59     {
60         if (value == null)
61             return null;
62
63         return value.toString();
64     }
65
66     public Object JavaDoc toObject(IFormComponent field, String JavaDoc input) throws ValidatorException
67     {
68         if (checkRequired(field, input))
69             return null;
70
71         if (_minimumLength > 0 && input.length() < _minimumLength)
72             throw new ValidatorException(buildMinimumLengthMessage(field),
73                     ValidationConstraint.MINIMUM_WIDTH);
74
75         if (!isValidEmail(input))
76             throw new ValidatorException(buildInvalidEmailFormatMessage(field),
77                     ValidationConstraint.EMAIL_FORMAT);
78
79         return input;
80     }
81
82     public int getMinimumLength()
83     {
84         return _minimumLength;
85     }
86
87     public void setMinimumLength(int minimumLength)
88     {
89         _minimumLength = minimumLength;
90     }
91
92     public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
93             IRequestCycle cycle)
94     {
95         if (!isClientScriptingEnabled())
96             return;
97
98         Map JavaDoc symbols = new HashMap JavaDoc();
99
100         Locale JavaDoc locale = field.getPage().getLocale();
101         String JavaDoc displayName = field.getDisplayName();
102
103         if (isRequired())
104             symbols.put("requiredMessage", buildRequiredMessage(field));
105
106         if (_minimumLength > 0)
107             symbols.put("minimumLengthMessage", buildMinimumLengthMessage(field));
108
109         String JavaDoc pattern = getPattern(getInvalidEmailFormatMessage(), "invalid-email-format", locale);
110
111         symbols.put("emailFormatMessage", formatString(pattern, displayName));
112
113         processValidatorScript(_scriptPath, cycle, field, symbols);
114     }
115
116     public String JavaDoc getScriptPath()
117     {
118         return _scriptPath;
119     }
120
121     /**
122      * Allows a developer to use the existing validation logic with a different client-side script.
123      * This is often sufficient to allow application-specific error presentation (perhaps by using
124      * DHTML to update the content of a &lt;span&gt; tag, or to use a more sophisticated pop-up
125      * window than <code>window.alert()</code>).
126      */

127
128     public void setScriptPath(String JavaDoc scriptPath)
129     {
130         _scriptPath = scriptPath;
131     }
132
133     /**
134      * Return true if the email format is valid.
135      *
136      * @param email
137      * the email string to validate
138      * @return true if the email format is valid
139      */

140
141     protected boolean isValidEmail(String JavaDoc email)
142     {
143         int atIndex = email.indexOf('@');
144
145         return !((atIndex <= 0) || (atIndex == email.length() - 1));
146     }
147
148     /** @since 3.0 */
149
150     public String JavaDoc getInvalidEmailFormatMessage()
151     {
152         return _invalidEmailFormatMessage;
153     }
154
155     /** @since 3.0 */
156
157     public String JavaDoc getMinimumLengthMessage()
158     {
159         return _minimumLengthMessage;
160     }
161
162     /**
163      * Overrides the <code>invalid-email-format</code> bundle key. Parameter {0} is the display
164      * name of the field.
165      *
166      * @since 3.0
167      */

168
169     public void setInvalidEmailFormatMessage(String JavaDoc string)
170     {
171         _invalidEmailFormatMessage = string;
172     }
173
174     /**
175      * Overrides the <code>field-too-short</code> bundle key. Parameter {0} is the minimum length.
176      * Parameter {1} is the display name of the field.
177      *
178      * @since 3.0
179      */

180     public void setMinimumLengthMessage(String JavaDoc string)
181     {
182         _minimumLengthMessage = string;
183     }
184
185     /** @since 3.0 */
186
187     protected String JavaDoc buildMinimumLengthMessage(IFormComponent field)
188     {
189         String JavaDoc pattern = getPattern(_minimumLengthMessage, "field-too-short", field.getPage()
190                 .getLocale());
191
192         return formatString(pattern, Integer.toString(_minimumLength), field.getDisplayName());
193     }
194
195     /** @since 3.0 */
196
197     protected String JavaDoc buildInvalidEmailFormatMessage(IFormComponent field)
198     {
199         String JavaDoc pattern = getPattern(_invalidEmailFormatMessage, "invalid-email-format", field
200                 .getPage().getLocale());
201
202         return formatString(pattern, field.getDisplayName());
203     }
204 }
Popular Tags