Coverage Report - org.apache.tapestry.valid.AbstractNumericValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractNumericValidator
0%
0/43
0%
0/6
1.208
 
 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 org.apache.tapestry.form.IFormComponent;
 18  
 
 19  
 /**
 20  
  * Base class for a number of implementations of {@link org.apache.tapestry.valid.IValidator},
 21  
  * meant to replace the awkward {@link org.apache.tapestry.valid.NumberValidator}.
 22  
  * 
 23  
  * @author Howard M. Lewis Ship
 24  
  */
 25  
 public abstract class AbstractNumericValidator extends BaseValidator
 26  
 {
 27  
     private boolean _zeroIsNull;
 28  
 
 29  0
     private String _scriptPath = getDefaultScriptPath();
 30  
     
 31  
     private String _invalidNumericFormatMessage;
 32  
 
 33  
     private String _invalidIntegerFormatMessage;
 34  
 
 35  
     private String _numberTooSmallMessage;
 36  
 
 37  
     private String _numberTooLargeMessage;
 38  
 
 39  
     private String _numberRangeMessage;
 40  
     
 41  
     public AbstractNumericValidator()
 42  
     {
 43  0
         super();
 44  0
     }
 45  
 
 46  
     public AbstractNumericValidator(String initializer)
 47  
     {
 48  0
         super(initializer);
 49  0
     }
 50  
 
 51  
     public AbstractNumericValidator(boolean required)
 52  
     {
 53  0
         super(required);
 54  0
     }
 55  
 
 56  
     /**
 57  
      * If true, then when rendering, a zero is treated as a non-value, and null is returned. If
 58  
      * false, the default, then zero is rendered as zero.
 59  
      */
 60  
 
 61  
     public boolean getZeroIsNull()
 62  
     {
 63  0
         return _zeroIsNull;
 64  
     }
 65  
 
 66  
     public void setZeroIsNull(boolean zeroIsNull)
 67  
     {
 68  0
         _zeroIsNull = zeroIsNull;
 69  0
     }
 70  
 
 71  
     /**
 72  
      * @since 2.2
 73  
      */
 74  
     public String getScriptPath()
 75  
     {
 76  0
         return _scriptPath;
 77  
     }
 78  
 
 79  
     /**
 80  
      * Allows a developer to use the existing validation logic with a different client-side script.
 81  
      * This is often sufficient to allow application-specific error presentation (perhaps by using
 82  
      * DHTML to update the content of a <span> tag, or to use a more sophisticated pop-up
 83  
      * window than <code>window.alert()</code>).
 84  
      * 
 85  
      * @since 2.2
 86  
      */
 87  
     public void setScriptPath(String scriptPath)
 88  
     {
 89  0
         _scriptPath = scriptPath;
 90  0
     }
 91  
 
 92  
     /** @since 3.0 */
 93  
     public String getInvalidNumericFormatMessage()
 94  
     {
 95  0
         return _invalidNumericFormatMessage;
 96  
     }
 97  
 
 98  
     /** @since 3.0 */
 99  
     public String getInvalidIntegerFormatMessage()
 100  
     {
 101  0
         return _invalidIntegerFormatMessage;
 102  
     }
 103  
 
 104  
     /** @since 3.0 */
 105  
     public String getNumberRangeMessage()
 106  
     {
 107  0
         return _numberRangeMessage;
 108  
     }
 109  
 
 110  
     /** @since 3.0 */
 111  
     public String getNumberTooLargeMessage()
 112  
     {
 113  0
         return _numberTooLargeMessage;
 114  
     }
 115  
 
 116  
     /** @since 3.0 */
 117  
     public String getNumberTooSmallMessage()
 118  
     {
 119  0
         return _numberTooSmallMessage;
 120  
     }
 121  
 
 122  
     /**
 123  
      * Overrides the <code>invalid-numeric-format</code> bundle key. Parameter {0} is the display
 124  
      * name of the field.
 125  
      * 
 126  
      * @since 3.0
 127  
      */
 128  
     public void setInvalidNumericFormatMessage(String string)
 129  
     {
 130  0
         _invalidNumericFormatMessage = string;
 131  0
     }
 132  
 
 133  
     /**
 134  
      * Overrides the <code>invalid-int-format</code> bundle key. Parameter {0} is the display name
 135  
      * of the field.
 136  
      * 
 137  
      * @since 3.0
 138  
      */
 139  
     public void setInvalidIntegerFormatMessage(String string)
 140  
     {
 141  0
         _invalidIntegerFormatMessage = string;
 142  0
     }
 143  
 
 144  
     /**
 145  
      * Overrides the <code>number-range</code> bundle key. Parameter [0} is the display name of
 146  
      * the field. Parameter {1} is the minimum value. Parameter {2} is the maximum value.
 147  
      * 
 148  
      * @since 3.0
 149  
      */
 150  
     public void setNumberRangeMessage(String string)
 151  
     {
 152  0
         _numberRangeMessage = string;
 153  0
     }
 154  
 
 155  
     /**
 156  
      * Overrides the <code>number-too-large</code> bundle key. Parameter {0} is the display name
 157  
      * of the field. Parameter {1} is the maximum allowed value.
 158  
      * 
 159  
      * @since 3.0
 160  
      */
 161  
     public void setNumberTooLargeMessage(String string)
 162  
     {
 163  0
         _numberTooLargeMessage = string;
 164  0
     }
 165  
 
 166  
     /**
 167  
      * Overrides the <code>number-too-small</code> bundle key. Parameter {0} is the display name
 168  
      * of the field. Parameter {1} is the minimum allowed value.
 169  
      * 
 170  
      * @since 3.0
 171  
      */
 172  
     public void setNumberTooSmallMessage(String string)
 173  
     {
 174  0
         _numberTooSmallMessage = string;
 175  0
     }
 176  
 
 177  
     /** @since 3.0 */
 178  
     protected String buildInvalidNumericFormatMessage(IFormComponent field)
 179  
     {
 180  0
         String pattern = getPattern(
 181  
                 getInvalidNumericFormatMessage(),
 182  
                 "invalid-numeric-format",
 183  
                 field.getPage().getLocale());
 184  
 
 185  0
         return formatString(pattern, field.getDisplayName());
 186  
     }
 187  
 
 188  
     protected String buildNumberTooSmallMessage(IFormComponent field, Number minimum)
 189  
     {
 190  0
         String pattern = getPattern(getNumberTooSmallMessage(), "number-too-small", field.getPage()
 191  
                 .getLocale());
 192  
 
 193  0
         return formatString(pattern, field.getDisplayName(), minimum);
 194  
     }
 195  
 
 196  
     /** @since 3.0 */
 197  
     protected String buildInvalidIntegerFormatMessage(IFormComponent field)
 198  
     {
 199  0
         String pattern = getPattern(getInvalidIntegerFormatMessage(), "invalid-int-format", field
 200  
                 .getPage().getLocale());
 201  
     
 202  0
         return formatString(pattern, field.getDisplayName());
 203  
     }
 204  
 
 205  
     /**
 206  
      * @since 3.0
 207  
      */
 208  
     protected String buildNumberTooLargeMessage(IFormComponent field, Number maximum)
 209  
     {
 210  0
         String pattern = getPattern(getNumberTooLargeMessage(), "number-too-large", field.getPage()
 211  
                 .getLocale());
 212  
     
 213  0
         return formatString(pattern, field.getDisplayName(), maximum);
 214  
     }
 215  
 
 216  
     protected String buildNumberRangeMessage(IFormComponent field, Number mininum, Number maximum)
 217  
     {
 218  0
         String pattern = getPattern(getNumberRangeMessage(), "number-range", field.getPage()
 219  
                 .getLocale());
 220  
     
 221  0
         return formatString(pattern, new Object[]
 222  
         { field.getDisplayName(), mininum, maximum });
 223  
     }
 224  
 
 225  
     protected String buildRangeMessage(IFormComponent field, Number minimum, Number maximum)
 226  
     {
 227  0
         if (minimum != null && maximum != null)
 228  0
             return buildNumberRangeMessage(field, minimum, maximum);
 229  
     
 230  0
         if (maximum != null)
 231  0
             return buildNumberTooLargeMessage(field, maximum);
 232  
     
 233  0
         return buildNumberTooSmallMessage(field, minimum);
 234  
     }
 235  
     
 236  
     protected abstract String getDefaultScriptPath();
 237  
 }