|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
StringValidator.java | 42.9% | 44.4% | 50% | 45.3% |
|
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 | 9 | 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 | 2 | public String toString(IFormComponent field, Object value) |
58 | { | |
59 | 2 | if (value == null) |
60 | 1 | return null; |
61 | ||
62 | 1 | return value.toString(); |
63 | } | |
64 | ||
65 | 7 | public Object toObject(IFormComponent field, String input) throws ValidatorException |
66 | { | |
67 | 7 | if (checkRequired(field, input)) |
68 | 2 | return null; |
69 | ||
70 | 3 | if (_minimumLength > 0 && input.length() < _minimumLength) |
71 | 1 | throw new ValidatorException(buildMinimumLengthMessage(field), |
72 | ValidationConstraint.MINIMUM_WIDTH); | |
73 | ||
74 | 2 | return input; |
75 | } | |
76 | ||
77 | 0 | public int getMinimumLength() |
78 | { | |
79 | 0 | return _minimumLength; |
80 | } | |
81 | ||
82 | 4 | public void setMinimumLength(int minimumLength) |
83 | { | |
84 | 4 | _minimumLength = minimumLength; |
85 | } | |
86 | ||
87 | /** | |
88 | * @since 2.2 | |
89 | */ | |
90 | ||
91 | 0 | public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer, |
92 | IRequestCycle cycle) | |
93 | { | |
94 | 0 | if (!isClientScriptingEnabled()) |
95 | 0 | return; |
96 | ||
97 | 0 | if (!(isRequired() || _minimumLength > 0)) |
98 | 0 | return; |
99 | ||
100 | 0 | Map symbols = new HashMap(); |
101 | ||
102 | 0 | if (isRequired()) |
103 | 0 | symbols.put("requiredMessage", buildRequiredMessage(field)); |
104 | ||
105 | 0 | if (_minimumLength > 0) |
106 | 0 | symbols.put("minimumLengthMessage", buildMinimumLengthMessage(field)); |
107 | ||
108 | 0 | 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 | 1 | protected String buildMinimumLengthMessage(IFormComponent field) |
155 | { | |
156 | 1 | String pattern = getPattern(_minimumLengthMessage, "field-too-short", field.getPage() |
157 | .getLocale()); | |
158 | ||
159 | 1 | return formatString(pattern, Integer.toString(_minimumLength), field.getDisplayName()); |
160 | } | |
161 | ||
162 | } |
|