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 |
|
|
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 |
7 |
{ |
43 |
7 |
} |
44 |
|
|
45 |
|
public IntValidator(String initializer) |
46 |
|
{ |
47 |
7 |
super(initializer); |
48 |
7 |
} |
49 |
|
|
50 |
|
public String toString(IFormComponent field, Object value) |
51 |
|
{ |
52 |
4 |
if (value == null) return null; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
3 |
Number number = (Number) value; |
58 |
|
|
59 |
3 |
if (getZeroIsNull() && number.intValue() == 0) return null; |
60 |
|
|
61 |
2 |
return number.toString(); |
62 |
|
} |
63 |
|
|
64 |
|
public Object toObject(IFormComponent field, String value) |
65 |
|
throws ValidatorException |
66 |
|
{ |
67 |
5 |
if (checkRequired(field, value)) return null; |
68 |
|
|
69 |
|
try |
70 |
|
{ |
71 |
4 |
int intValue = Integer.parseInt(value); |
72 |
|
|
73 |
3 |
if (_minimumSet && intValue < _minimum) |
74 |
1 |
throw new ValidatorException(buildNumberTooSmallMessage(field, |
75 |
|
new Integer(_minimum)), ValidationConstraint.TOO_SMALL); |
76 |
|
|
77 |
2 |
if (_maximumSet && intValue > _maximum) |
78 |
1 |
throw new ValidatorException(buildNumberTooLargeMessage(field, |
79 |
|
new Integer(_maximum)), ValidationConstraint.TOO_LARGE); |
80 |
|
|
81 |
1 |
return new Integer(intValue); |
82 |
|
} |
83 |
1 |
catch (NumberFormatException ex) |
84 |
|
{ |
85 |
1 |
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 |
5 |
Map symbols = new HashMap(); |
106 |
|
|
107 |
5 |
if (isRequired()) |
108 |
1 |
symbols.put("requiredMessage", buildRequiredMessage(field)); |
109 |
|
|
110 |
5 |
symbols.put("formatMessage", buildInvalidIntegerFormatMessage(field)); |
111 |
|
|
112 |
5 |
if (_minimumSet || _maximumSet) |
113 |
|
{ |
114 |
3 |
Number minimum = _minimumSet ? new Integer(_minimum) : null; |
115 |
3 |
Number maximum = _maximumSet ? new Integer(_maximum) : null; |
116 |
|
|
117 |
3 |
symbols.put("minimum", minimum); |
118 |
3 |
symbols.put("maximum", maximum); |
119 |
|
|
120 |
3 |
symbols.put("rangeMessage", buildRangeMessage(field, minimum, |
121 |
|
maximum)); |
122 |
|
} |
123 |
|
|
124 |
5 |
return symbols; |
125 |
|
} |
126 |
|
|
127 |
|
public void setMaximum(int maximum) |
128 |
|
{ |
129 |
3 |
_maximum = maximum; |
130 |
3 |
_maximumSet = true; |
131 |
3 |
} |
132 |
|
|
133 |
|
public void setMinimum(int minimum) |
134 |
|
{ |
135 |
3 |
_minimum = minimum; |
136 |
3 |
_minimumSet = true; |
137 |
3 |
} |
138 |
|
|
139 |
|
protected String getDefaultScriptPath() |
140 |
|
{ |
141 |
14 |
return "/org/apache/tapestry/valid/IntegerValidator.script"; |
142 |
|
} |
143 |
|
} |