|
|||||||||||||||||||
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 | |||||||||||||||
StringValidator.java | 71.4% | 77.8% | 66.7% | 73.6% |
|
1 |
// Copyright 2004, 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 |
* Simple validation of strings, to enforce required, and minimum length (maximum length is enforced
|
|
26 |
* in the client browser, by setting a maximum input length on the text field).
|
|
27 |
*
|
|
28 |
* @author Howard Lewis Ship
|
|
29 |
* @since 1.0.8
|
|
30 |
*/
|
|
31 |
|
|
32 |
public class StringValidator extends BaseValidator |
|
33 |
{ |
|
34 |
private int _minimumLength; |
|
35 |
|
|
36 |
private String _minimumLengthMessage;
|
|
37 |
|
|
38 |
/** @since 2.2 * */
|
|
39 |
|
|
40 |
private String _scriptPath = "/org/apache/tapestry/valid/StringValidator.script"; |
|
41 |
|
|
42 | 10 |
public StringValidator()
|
43 |
{ |
|
44 |
} |
|
45 |
|
|
46 |
/**
|
|
47 |
* Initializes the StringValidator with properties defined by the initializer.
|
|
48 |
*
|
|
49 |
* @since 4.0
|
|
50 |
*/
|
|
51 |
|
|
52 | 0 |
public StringValidator(String initializer)
|
53 |
{ |
|
54 | 0 |
super(initializer);
|
55 |
} |
|
56 |
|
|
57 | 5 |
public String toString(IFormComponent field, Object value)
|
58 |
{ |
|
59 | 5 |
if (value == null) |
60 | 3 |
return null; |
61 |
|
|
62 | 2 |
return value.toString();
|
63 |
} |
|
64 |
|
|
65 | 11 |
public Object toObject(IFormComponent field, String input) throws ValidatorException |
66 |
{ |
|
67 | 11 |
if (checkRequired(field, input))
|
68 | 2 |
return null; |
69 |
|
|
70 | 6 |
if (_minimumLength > 0 && input.length() < _minimumLength)
|
71 | 1 |
throw new ValidatorException(buildMinimumLengthMessage(field), |
72 |
ValidationConstraint.MINIMUM_WIDTH); |
|
73 |
|
|
74 | 5 |
return input;
|
75 |
} |
|
76 |
|
|
77 | 8 |
public int getMinimumLength() |
78 |
{ |
|
79 | 8 |
return _minimumLength;
|
80 |
} |
|
81 |
|
|
82 | 5 |
public void setMinimumLength(int minimumLength) |
83 |
{ |
|
84 | 5 |
_minimumLength = minimumLength; |
85 |
} |
|
86 |
|
|
87 |
/**
|
|
88 |
* @since 2.2
|
|
89 |
*/
|
|
90 |
|
|
91 | 4 |
public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer, |
92 |
IRequestCycle cycle) |
|
93 |
{ |
|
94 | 4 |
if (!isClientScriptingEnabled())
|
95 | 0 |
return;
|
96 |
|
|
97 | 4 |
if (!(isRequired() || _minimumLength > 0))
|
98 | 0 |
return;
|
99 |
|
|
100 | 4 |
Map symbols = new HashMap();
|
101 |
|
|
102 | 4 |
if (isRequired())
|
103 | 4 |
symbols.put("requiredMessage", buildRequiredMessage(field));
|
104 |
|
|
105 | 4 |
if (_minimumLength > 0)
|
106 | 4 |
symbols.put("minimumLengthMessage", buildMinimumLengthMessage(field));
|
107 |
|
|
108 | 4 |
processValidatorScript(_scriptPath, cycle, field, symbols); |
109 |
} |
|
110 |
|
|
111 |
/**
|
|
112 |
* @since 2.2
|
|
113 |
*/
|
|
114 |
|
|
115 | 0 |
public String getScriptPath()
|
116 |
{ |
|
117 | 0 |
return _scriptPath;
|
118 |
} |
|
119 |
|
|
120 |
/**
|
|
121 |
* Allows a developer to use the existing validation logic with a different client-side script.
|
|
122 |
* This is often sufficient to allow application-specific error presentation (perhaps by using
|
|
123 |
* DHTML to update the content of a <span> tag, or to use a more sophisticated pop-up
|
|
124 |
* window than <code>window.alert()</code>).
|
|
125 |
*
|
|
126 |
* @since 2.2
|
|
127 |
*/
|
|
128 |
|
|
129 | 0 |
public void setScriptPath(String scriptPath) |
130 |
{ |
|
131 | 0 |
_scriptPath = scriptPath; |
132 |
} |
|
133 |
|
|
134 |
/** @since 3.0 */
|
|
135 | 0 |
public String getMinimumLengthMessage()
|
136 |
{ |
|
137 | 0 |
return _minimumLengthMessage;
|
138 |
} |
|
139 |
|
|
140 |
/**
|
|
141 |
* Overrides the <code>field-too-short</code> bundle key. Parameter {0} is the minimum length.
|
|
142 |
* Parameter {1} is the display name of the field.
|
|
143 |
*
|
|
144 |
* @since 3.0
|
|
145 |
*/
|
|
146 |
|
|
147 | 1 |
public void setMinimumLengthMessage(String string) |
148 |
{ |
|
149 | 1 |
_minimumLengthMessage = string; |
150 |
} |
|
151 |
|
|
152 |
/** @since 3.0 */
|
|
153 |
|
|
154 | 5 |
protected String buildMinimumLengthMessage(IFormComponent field)
|
155 |
{ |
|
156 | 5 |
String pattern = getPattern(_minimumLengthMessage, "field-too-short", field.getPage()
|
157 |
.getLocale()); |
|
158 |
|
|
159 | 5 |
return formatString(pattern, Integer.toString(_minimumLength), field.getDisplayName());
|
160 |
} |
|
161 |
|
|
162 |
} |
|