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