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 a user input string against a regular expression pattern.
030 *
031 * @author Howard Lewis Ship
032 * @since 4.0
033 * @see org.apache.tapestry.util.RegexpMatcher
034 */
035 public class Pattern extends BaseValidator
036 {
037 // It is expectd that each Pattern instance will be used by a single component instance,
038 // and therefore be restricted to a single thread.
039
040 private RegexpMatcher _matcher = new RegexpMatcher();
041
042 private String _pattern;
043
044 public Pattern()
045 {
046 }
047
048 public Pattern(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.PATTERN_MISMATCH);
061 }
062
063 private String buildMessage(ValidationMessages messages, IFormComponent field)
064 {
065 return messages.formatValidationMessage(
066 getMessage(),
067 ValidationStrings.REGEX_MISMATCH,
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 = 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(TapestryUtils.enquote(message));
086 buffer.append("); }");
087
088 context.addSubmitHandler(buffer.toString());
089 }
090
091 public void setPattern(String pattern)
092 {
093 _pattern = pattern;
094 }
095
096 }