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.form.FormComponentContributorContext;
020    import org.apache.tapestry.form.IFormComponent;
021    import org.apache.tapestry.form.ValidationMessages;
022    import org.apache.tapestry.json.JSONLiteral;
023    import org.apache.tapestry.json.JSONObject;
024    import org.apache.tapestry.valid.ValidationConstants;
025    import org.apache.tapestry.valid.ValidationConstraint;
026    import org.apache.tapestry.valid.ValidationStrings;
027    import org.apache.tapestry.valid.ValidatorException;
028    
029    /**
030     * Validator that ensures a string value does not exceed a maximum length.
031     * 
032     * @author Howard Lewis Ship
033     * @since 4.0
034     */
035    public class MaxLength extends BaseValidator
036    {
037        private int _maxLength;
038    
039        public MaxLength()
040        {
041    
042        }
043    
044        public MaxLength(String initializer)
045        {
046            super(initializer);
047        }
048    
049        public void setMaxLength(int maxLength)
050        {
051            _maxLength = maxLength;
052        }
053    
054        public int getMaxLength()
055        {
056            return _maxLength;
057        }
058        
059        public void validate(IFormComponent field, ValidationMessages messages, Object object)
060                throws ValidatorException
061        {
062            String string = (String) object;
063    
064            if (string.length() > _maxLength)
065                throw new ValidatorException(buildMessage(messages, field),
066                        ValidationConstraint.MAXIMUM_WIDTH);
067        }
068    
069        protected String buildMessage(ValidationMessages messages, IFormComponent field)
070        {
071            return messages.formatValidationMessage(
072                    getMessage(),
073                    ValidationStrings.VALUE_TOO_LONG,
074                    new Object[]
075                    { new Integer(_maxLength), field.getDisplayName() });
076        }
077    
078        public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
079                FormComponentContributorContext context, IFormComponent field)
080        {
081            JSONObject profile = context.getProfile();
082            
083            if (!profile.has(ValidationConstants.CONSTRAINTS)) {
084                profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
085            }
086            JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
087            
088            accumulateProperty(cons, field.getClientId(), 
089                    new JSONLiteral("[tapestry.form.validation.isText,{"
090                            + "maxlength:" + _maxLength + "}]"));
091            
092            accumulateProfileProperty(field, profile, 
093                    ValidationConstants.CONSTRAINTS, buildMessage(context, field));
094        }
095    
096    }