001 // Copyright 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.form.validator;
016
017 import org.apache.tapestry.IMarkupWriter;
018 import org.apache.tapestry.IRequestCycle;
019 import org.apache.tapestry.TapestryUtils;
020 import org.apache.tapestry.form.FormComponentContributorContext;
021 import org.apache.tapestry.form.IFormComponent;
022 import org.apache.tapestry.form.ValidationMessages;
023 import org.apache.tapestry.util.RegexpMatcher;
024 import org.apache.tapestry.valid.ValidationConstraint;
025 import org.apache.tapestry.valid.ValidationStrings;
026 import org.apache.tapestry.valid.ValidatorException;
027
028 /**
029 * Validates that the user input, a string, is an email address (by checking it against a regular
030 * expression).
031 *
032 * @author Howard Lewis Ship
033 * @since 4.0
034 */
035 public class Email extends BaseValidator
036 {
037 static final String PATTERN = "^\\w[-._\\w]*\\w@\\w[-._\\w]*\\w\\.\\w{2,6}$";
038
039 // TODO: Possible thread safety issue if the validator
040 // is shared across threads, because the matcher
041 // will be too.
042 private RegexpMatcher _matcher = new RegexpMatcher();
043
044 public Email()
045 {
046 }
047
048 public Email(String initializer)
049 {
050 super(initializer);
051 }
052
053 public void validate(IFormComponent field, ValidationMessages messages, Object object)
054 throws ValidatorException
055 {
056 String input = (String) object;
057
058 if (!_matcher.matches(PATTERN, input))
059 throw new ValidatorException(buildMessage(messages, field),
060 ValidationConstraint.EMAIL_FORMAT);
061 }
062
063 private String buildMessage(ValidationMessages messages, IFormComponent field)
064 {
065 return messages.formatValidationMessage(
066 getMessage(),
067 ValidationStrings.INVALID_EMAIL,
068 new Object[]
069 { field.getDisplayName() });
070 }
071
072 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
073 FormComponentContributorContext context, IFormComponent field)
074 {
075 context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js");
076
077 String pattern = _matcher.getEscapedPatternString(PATTERN);
078 String message = TapestryUtils.enquote(buildMessage(context, field));
079
080 StringBuffer buffer = new StringBuffer("function(event) { Tapestry.validate_regex(event, '");
081 buffer.append(field.getClientId());
082 buffer.append("', '");
083 buffer.append(pattern);
084 buffer.append("', ");
085 buffer.append(message);
086 buffer.append("); }");
087
088 context.addSubmitHandler(buffer.toString());
089 }
090 }