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