|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
IntValidator.java | 86.4% | 89.7% | 100% | 90% |
|
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 {@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 | 8 |
public IntValidator()
|
40 |
{ |
|
41 |
} |
|
42 |
|
|
43 | 7 |
public IntValidator(String initializer)
|
44 |
{ |
|
45 | 7 |
super(initializer);
|
46 |
} |
|
47 |
|
|
48 | 6 |
public String toString(IFormComponent field, Object value)
|
49 |
{ |
|
50 | 6 |
if (value == null) |
51 | 1 |
return null; |
52 |
|
|
53 |
// Be generous; maybe it isn't quite an int, so
|
|
54 |
// treat it as a Number
|
|
55 |
|
|
56 | 5 |
Number number = (Number) value; |
57 |
|
|
58 | 5 |
if (getZeroIsNull() && number.intValue() == 0)
|
59 | 1 |
return null; |
60 |
|
|
61 | 4 |
return number.toString();
|
62 |
} |
|
63 |
|
|
64 | 6 |
public Object toObject(IFormComponent field, String value) throws ValidatorException |
65 |
{ |
|
66 | 6 |
if (checkRequired(field, value))
|
67 | 1 |
return null; |
68 |
|
|
69 | 5 |
try
|
70 |
{ |
|
71 | 5 |
int intValue = Integer.parseInt(value);
|
72 |
|
|
73 | 4 |
if (_minimumSet && intValue < _minimum)
|
74 | 1 |
throw new ValidatorException(buildNumberTooSmallMessage( |
75 |
field, |
|
76 |
new Integer(_minimum)), ValidationConstraint.TOO_SMALL);
|
|
77 |
|
|
78 | 3 |
if (_maximumSet && intValue > _maximum)
|
79 | 1 |
throw new ValidatorException(buildNumberTooLargeMessage( |
80 |
field, |
|
81 |
new Integer(_maximum)), ValidationConstraint.TOO_LARGE);
|
|
82 |
|
|
83 | 2 |
return new Integer(intValue); |
84 |
} |
|
85 |
catch (NumberFormatException ex)
|
|
86 |
{ |
|
87 | 1 |
throw new ValidatorException(buildInvalidNumericFormatMessage(field), |
88 |
ValidationConstraint.NUMBER_FORMAT); |
|
89 |
} |
|
90 |
} |
|
91 |
|
|
92 | 2 |
public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer, |
93 |
IRequestCycle cycle) |
|
94 |
{ |
|
95 | 2 |
if (!isClientScriptingEnabled())
|
96 | 2 |
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 | 5 |
Map buildSymbols(IFormComponent field) |
107 |
{ |
|
108 | 5 |
Map symbols = new HashMap();
|
109 |
|
|
110 | 5 |
if (isRequired())
|
111 | 1 |
symbols.put("requiredMessage", buildRequiredMessage(field));
|
112 |
|
|
113 | 5 |
symbols.put("formatMessage", buildInvalidIntegerFormatMessage(field));
|
114 |
|
|
115 | 5 |
if (_minimumSet || _maximumSet)
|
116 |
{ |
|
117 | 3 |
Number minimum = _minimumSet ? new Integer(_minimum) : null; |
118 | 3 |
Number maximum = _maximumSet ? new Integer(_maximum) : null; |
119 |
|
|
120 | 3 |
symbols.put("minimum", minimum);
|
121 | 3 |
symbols.put("maximum", maximum);
|
122 |
|
|
123 | 3 |
symbols.put("rangeMessage", buildRangeMessage(field, minimum, maximum));
|
124 |
} |
|
125 |
|
|
126 | 5 |
return symbols;
|
127 |
} |
|
128 |
|
|
129 | 3 |
public void setMaximum(int maximum) |
130 |
{ |
|
131 | 3 |
_maximum = maximum; |
132 | 3 |
_maximumSet = true;
|
133 |
} |
|
134 |
|
|
135 | 3 |
public void setMinimum(int minimum) |
136 |
{ |
|
137 | 3 |
_minimum = minimum; |
138 | 3 |
_minimumSet = true;
|
139 |
} |
|
140 |
|
|
141 | 15 |
protected String getDefaultScriptPath()
|
142 |
{ |
|
143 | 15 |
return "/org/apache/tapestry/valid/IntegerValidator.script"; |
144 |
} |
|
145 |
} |
|