001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005//     http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.validator;
014
015import org.apache.tapestry5.Field;
016import org.apache.tapestry5.MarkupWriter;
017import org.apache.tapestry5.ValidationException;
018import org.apache.tapestry5.commons.MessageFormatter;
019import org.apache.tapestry5.services.FormSupport;
020import org.apache.tapestry5.services.Html5Support;
021import org.apache.tapestry5.services.javascript.DataConstants;
022import org.apache.tapestry5.services.javascript.JavaScriptSupport;
023
024import java.util.regex.Pattern;
025
026/**
027 * A validator that checks if a given string is well-formed email address. This validator is not configurable.
028 *
029 * Starting with release 5.4, this validator also performs client-side validation.
030 */
031public class Email extends AbstractValidator<Void, String>
032{
033
034    // The client-side uses a similar RE, but converts the input to lower case before applying the pattern.
035    // See validation.coffee.
036
037    private static final Pattern PATTERN = Pattern
038            .compile("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", Pattern.CASE_INSENSITIVE);
039    
040    final private Html5Support html5Support;
041
042    public Email(JavaScriptSupport javaScriptSupport, Html5Support html5Support)
043    {
044        super(null, String.class, "invalid-email", javaScriptSupport);
045        this.html5Support = html5Support;
046    }
047
048    public void render(Field field, Void constraintValue, MessageFormatter formatter, MarkupWriter writer,
049                       FormSupport formSupport)
050    {
051        if (formSupport.isClientValidationEnabled())
052        {
053            javaScriptSupport.require("t5/core/validation");
054
055            writer.attributes(
056                    DataConstants.VALIDATION_ATTRIBUTE, true,
057                    "data-validate-email", true,
058                    "data-email-message", formatter.toString());
059        }
060        
061        if (html5Support.isHtml5SupportEnabled()) {
062            writer.getElement().forceAttributes("type", "email");
063        }
064        
065    }
066
067    public void validate(Field field, Void constraintValue, MessageFormatter formatter, String value)
068            throws ValidationException
069    {
070        if (!PATTERN.matcher(value).matches()) throw new ValidationException(formatter.toString());
071    }
072}