|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
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 |
| |
|
26 |
| |
|
27 |
| |
|
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 |
21
| public IntValidator()
|
|
40 |
| { |
|
41 |
| } |
|
42 |
| |
|
43 |
21
| public IntValidator(String initializer)
|
|
44 |
| { |
|
45 |
21
| super(initializer);
|
|
46 |
| } |
|
47 |
| |
|
48 |
12
| public String toString(IFormComponent field, Object value)
|
|
49 |
| { |
|
50 |
12
| if (value == null)
|
|
51 |
3
| return null;
|
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
9
| Number number = (Number) value;
|
|
57 |
| |
|
58 |
9
| if (getZeroIsNull() && number.intValue() == 0)
|
|
59 |
3
| return null;
|
|
60 |
| |
|
61 |
6
| return number.toString();
|
|
62 |
| } |
|
63 |
| |
|
64 |
15
| public Object toObject(IFormComponent field, String value) throws ValidatorException
|
|
65 |
| { |
|
66 |
15
| if (checkRequired(field, value))
|
|
67 |
3
| return null;
|
|
68 |
| |
|
69 |
12
| try
|
|
70 |
| { |
|
71 |
12
| int intValue = Integer.parseInt(value);
|
|
72 |
| |
|
73 |
9
| if (_minimumSet && intValue < _minimum)
|
|
74 |
3
| throw new ValidatorException(buildNumberTooSmallMessage(
|
|
75 |
| field, |
|
76 |
| new Integer(_minimum)), ValidationConstraint.TOO_SMALL); |
|
77 |
| |
|
78 |
6
| if (_maximumSet && intValue > _maximum)
|
|
79 |
3
| throw new ValidatorException(buildNumberTooLargeMessage(
|
|
80 |
| field, |
|
81 |
| new Integer(_maximum)), ValidationConstraint.TOO_LARGE); |
|
82 |
| |
|
83 |
3
| return new Integer(intValue);
|
|
84 |
| } |
|
85 |
| catch (NumberFormatException ex) |
|
86 |
| { |
|
87 |
3
| throw new ValidatorException(buildInvalidNumericFormatMessage(field),
|
|
88 |
| ValidationConstraint.NUMBER_FORMAT); |
|
89 |
| } |
|
90 |
| } |
|
91 |
| |
|
92 |
0
| public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
|
|
93 |
| IRequestCycle cycle) |
|
94 |
| { |
|
95 |
0
| if (!isClientScriptingEnabled())
|
|
96 |
0
| return;
|
|
97 |
| |
|
98 |
0
| if (!(isRequired() || _minimumSet || _maximumSet))
|
|
99 |
0
| return;
|
|
100 |
| |
|
101 |
0
| Map symbols = buildSymbols(field);
|
|
102 |
| |
|
103 |
0
| processValidatorScript(getScriptPath(), cycle, field, symbols);
|
|
104 |
| } |
|
105 |
| |
|
106 |
15
| Map buildSymbols(IFormComponent field)
|
|
107 |
| { |
|
108 |
15
| Map symbols = new HashMap();
|
|
109 |
| |
|
110 |
15
| if (isRequired())
|
|
111 |
3
| symbols.put("requiredMessage", buildRequiredMessage(field));
|
|
112 |
| |
|
113 |
15
| symbols.put("formatMessage", buildInvalidIntegerFormatMessage(field));
|
|
114 |
| |
|
115 |
15
| if (_minimumSet || _maximumSet)
|
|
116 |
| { |
|
117 |
9
| Number minimum = _minimumSet ? new Integer(_minimum) : null;
|
|
118 |
9
| Number maximum = _maximumSet ? new Integer(_maximum) : null;
|
|
119 |
| |
|
120 |
9
| symbols.put("minimum", minimum);
|
|
121 |
9
| symbols.put("maximum", maximum);
|
|
122 |
| |
|
123 |
9
| symbols.put("rangeMessage", buildRangeMessage(field, minimum, maximum));
|
|
124 |
| } |
|
125 |
| |
|
126 |
15
| return symbols;
|
|
127 |
| } |
|
128 |
| |
|
129 |
9
| public void setMaximum(int maximum)
|
|
130 |
| { |
|
131 |
9
| _maximum = maximum;
|
|
132 |
9
| _maximumSet = true;
|
|
133 |
| } |
|
134 |
| |
|
135 |
9
| public void setMinimum(int minimum)
|
|
136 |
| { |
|
137 |
9
| _minimum = minimum;
|
|
138 |
9
| _minimumSet = true;
|
|
139 |
| } |
|
140 |
| |
|
141 |
42
| protected String getDefaultScriptPath()
|
|
142 |
| { |
|
143 |
42
| return "/org/apache/tapestry/valid/IntegerValidator.script";
|
|
144 |
| } |
|
145 |
| } |