KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > valid > IntValidator


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 JavaDoc;
18 import java.util.Map JavaDoc;
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 {@link org.apache.tapestry.valid.NumberValidator}.
26  *
27  * @author Howard M. Lewis Ship
28  */

29 public class IntValidator extends AbstractNumericValidator
30 {
31     private boolean _minimumSet;
32
33     private int _minimum;
34
35     private boolean _maximumSet;
36
37     private int _maximum;
38
39     public IntValidator()
40     {
41     }
42
43     public IntValidator(String JavaDoc initializer)
44     {
45         super(initializer);
46     }
47
48     public String JavaDoc toString(IFormComponent field, Object JavaDoc value)
49     {
50         if (value == null)
51             return null;
52
53         // Be generous; maybe it isn't quite an int, so
54
// treat it as a Number
55

56         Number JavaDoc number = (Number JavaDoc) value;
57
58         if (getZeroIsNull() && number.intValue() == 0)
59             return null;
60
61         return number.toString();
62     }
63
64     public Object JavaDoc toObject(IFormComponent field, String JavaDoc value) throws ValidatorException
65     {
66         if (checkRequired(field, value))
67             return null;
68
69         try
70         {
71             int intValue = Integer.parseInt(value);
72
73             if (_minimumSet && intValue < _minimum)
74                 throw new ValidatorException(buildNumberTooSmallMessage(
75                         field,
76                         new Integer JavaDoc(_minimum)), ValidationConstraint.TOO_SMALL);
77
78             if (_maximumSet && intValue > _maximum)
79                 throw new ValidatorException(buildNumberTooLargeMessage(
80                         field,
81                         new Integer JavaDoc(_maximum)), ValidationConstraint.TOO_LARGE);
82
83             return new Integer JavaDoc(intValue);
84         }
85         catch (NumberFormatException JavaDoc ex)
86         {
87             throw new ValidatorException(buildInvalidNumericFormatMessage(field),
88                     ValidationConstraint.NUMBER_FORMAT);
89         }
90     }
91
92     public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
93             IRequestCycle cycle)
94     {
95         if (!isClientScriptingEnabled())
96             return;
97
98         if (!(isRequired() || _minimumSet || _maximumSet))
99             return;
100
101         Map JavaDoc symbols = buildSymbols(field);
102
103         processValidatorScript(getScriptPath(), cycle, field, symbols);
104     }
105
106     Map JavaDoc buildSymbols(IFormComponent field)
107     {
108         Map JavaDoc symbols = new HashMap JavaDoc();
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 JavaDoc minimum = _minimumSet ? new Integer JavaDoc(_minimum) : null;
118             Number JavaDoc maximum = _maximumSet ? new Integer JavaDoc(_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 JavaDoc getDefaultScriptPath()
142     {
143         return "/org/apache/tapestry/valid/IntegerValidator.script";
144     }
145 }
Popular Tags