Coverage Report - org.apache.tapestry.valid.IntValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
IntValidator
0%
0/40
0%
0/34
4
 
 1  
 // Copyright 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.valid;
 16  
 
 17  
 import java.util.HashMap;
 18  
 import java.util.Map;
 19  
 
 20  
 import org.apache.tapestry.IMarkupWriter;
 21  
 import org.apache.tapestry.IRequestCycle;
 22  
 import org.apache.tapestry.form.IFormComponent;
 23  
 
 24  
 /**
 25  
  * A type-specific replacement for
 26  
  * {@link org.apache.tapestry.valid.NumberValidator}.
 27  
  * 
 28  
  * @author Howard M. Lewis Ship
 29  
  */
 30  
 public class IntValidator extends AbstractNumericValidator
 31  
 {
 32  
 
 33  
     private boolean _minimumSet;
 34  
 
 35  
     private int _minimum;
 36  
 
 37  
     private boolean _maximumSet;
 38  
 
 39  
     private int _maximum;
 40  
 
 41  
     public IntValidator()
 42  0
     {
 43  0
     }
 44  
 
 45  
     public IntValidator(String initializer)
 46  
     {
 47  0
         super(initializer);
 48  0
     }
 49  
 
 50  
     public String toString(IFormComponent field, Object value)
 51  
     {
 52  0
         if (value == null) return null;
 53  
 
 54  
         // Be generous; maybe it isn't quite an int, so
 55  
         // treat it as a Number
 56  
 
 57  0
         Number number = (Number) value;
 58  
 
 59  0
         if (getZeroIsNull() && number.intValue() == 0) return null;
 60  
 
 61  0
         return number.toString();
 62  
     }
 63  
 
 64  
     public Object toObject(IFormComponent field, String value)
 65  
         throws ValidatorException
 66  
     {
 67  0
         if (checkRequired(field, value)) return null;
 68  
 
 69  
         try
 70  
         {
 71  0
             int intValue = Integer.parseInt(value);
 72  
 
 73  0
             if (_minimumSet && intValue < _minimum)
 74  0
                 throw new ValidatorException(buildNumberTooSmallMessage(field,
 75  
                         new Integer(_minimum)), ValidationConstraint.TOO_SMALL);
 76  
 
 77  0
             if (_maximumSet && intValue > _maximum)
 78  0
                 throw new ValidatorException(buildNumberTooLargeMessage(field,
 79  
                         new Integer(_maximum)), ValidationConstraint.TOO_LARGE);
 80  
 
 81  0
             return new Integer(intValue);
 82  
         }
 83  0
         catch (NumberFormatException ex)
 84  
         {
 85  0
             throw new ValidatorException(
 86  
                     buildInvalidNumericFormatMessage(field),
 87  
                     ValidationConstraint.NUMBER_FORMAT);
 88  
         }
 89  
     }
 90  
 
 91  
     public void renderValidatorContribution(IFormComponent field,
 92  
             IMarkupWriter writer, IRequestCycle cycle)
 93  
     {
 94  0
         if (!isClientScriptingEnabled()) return;
 95  
 
 96  0
         if (!(isRequired() || _minimumSet || _maximumSet)) return;
 97  
 
 98  0
         Map symbols = buildSymbols(field);
 99  
 
 100  0
         processValidatorScript(getScriptPath(), cycle, field, symbols);
 101  0
     }
 102  
 
 103  
     Map buildSymbols(IFormComponent field)
 104  
     {
 105  0
         Map symbols = new HashMap();
 106  
 
 107  0
         if (isRequired())
 108  0
             symbols.put("requiredMessage", buildRequiredMessage(field));
 109  
 
 110  0
         symbols.put("formatMessage", buildInvalidIntegerFormatMessage(field));
 111  
 
 112  0
         if (_minimumSet || _maximumSet)
 113  
         {
 114  0
             Number minimum = _minimumSet ? new Integer(_minimum) : null;
 115  0
             Number maximum = _maximumSet ? new Integer(_maximum) : null;
 116  
 
 117  0
             symbols.put("minimum", minimum);
 118  0
             symbols.put("maximum", maximum);
 119  
 
 120  0
             symbols.put("rangeMessage", buildRangeMessage(field, minimum,
 121  
                     maximum));
 122  
         }
 123  
 
 124  0
         return symbols;
 125  
     }
 126  
 
 127  
     public void setMaximum(int maximum)
 128  
     {
 129  0
         _maximum = maximum;
 130  0
         _maximumSet = true;
 131  0
     }
 132  
 
 133  
     public void setMinimum(int minimum)
 134  
     {
 135  0
         _minimum = minimum;
 136  0
         _minimumSet = true;
 137  0
     }
 138  
 
 139  
     protected String getDefaultScriptPath()
 140  
     {
 141  0
         return "/org/apache/tapestry/valid/IntegerValidator.script";
 142  
     }
 143  
 }