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.valid;
016    
017    import java.util.HashMap;
018    import java.util.Map;
019    
020    import org.apache.tapestry.IMarkupWriter;
021    import org.apache.tapestry.IRequestCycle;
022    import org.apache.tapestry.form.IFormComponent;
023    
024    /**
025     * A type-specific replacement for {@link org.apache.tapestry.valid.NumberValidator}.
026     * 
027     * @author Howard M. Lewis Ship
028     */
029    public class IntValidator extends AbstractNumericValidator
030    {
031        private boolean _minimumSet;
032    
033        private int _minimum;
034    
035        private boolean _maximumSet;
036    
037        private int _maximum;
038    
039        public IntValidator()
040        {
041        }
042    
043        public IntValidator(String initializer)
044        {
045            super(initializer);
046        }
047    
048        public String toString(IFormComponent field, Object value)
049        {
050            if (value == null)
051                return null;
052    
053            // Be generous; maybe it isn't quite an int, so
054            // treat it as a Number
055    
056            Number number = (Number) value;
057    
058            if (getZeroIsNull() && number.intValue() == 0)
059                return null;
060    
061            return number.toString();
062        }
063    
064        public Object toObject(IFormComponent field, String value) throws ValidatorException
065        {
066            if (checkRequired(field, value))
067                return null;
068    
069            try
070            {
071                int intValue = Integer.parseInt(value);
072    
073                if (_minimumSet && intValue < _minimum)
074                    throw new ValidatorException(buildNumberTooSmallMessage(
075                            field,
076                            new Integer(_minimum)), ValidationConstraint.TOO_SMALL);
077    
078                if (_maximumSet && intValue > _maximum)
079                    throw new ValidatorException(buildNumberTooLargeMessage(
080                            field,
081                            new Integer(_maximum)), ValidationConstraint.TOO_LARGE);
082    
083                return new Integer(intValue);
084            }
085            catch (NumberFormatException ex)
086            {
087                throw new ValidatorException(buildInvalidNumericFormatMessage(field),
088                        ValidationConstraint.NUMBER_FORMAT);
089            }
090        }
091    
092        public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
093                IRequestCycle cycle)
094        {
095            if (!isClientScriptingEnabled())
096                return;
097    
098            if (!(isRequired() || _minimumSet || _maximumSet))
099                return;
100    
101            Map symbols = buildSymbols(field);
102    
103            processValidatorScript(getScriptPath(), cycle, field, symbols);
104        }
105    
106        Map buildSymbols(IFormComponent field)
107        {
108            Map symbols = new HashMap();
109    
110            if (isRequired())
111                symbols.put("requiredMessage", buildRequiredMessage(field));
112    
113            symbols.put("formatMessage", buildInvalidIntegerFormatMessage(field));
114    
115            if (_minimumSet || _maximumSet)
116            {
117                Number minimum = _minimumSet ? new Integer(_minimum) : null;
118                Number maximum = _maximumSet ? new Integer(_maximum) : null;
119    
120                symbols.put("minimum", minimum);
121                symbols.put("maximum", maximum);
122    
123                symbols.put("rangeMessage", buildRangeMessage(field, minimum, maximum));
124            }
125    
126            return symbols;
127        }
128    
129        public void setMaximum(int maximum)
130        {
131            _maximum = maximum;
132            _maximumSet = true;
133        }
134    
135        public void setMinimum(int minimum)
136        {
137            _minimum = minimum;
138            _minimumSet = true;
139        }
140    
141        protected String getDefaultScriptPath()
142        {
143            return "/org/apache/tapestry/valid/IntegerValidator.script";
144        }
145    }