|
|||||||||||||||||||
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 | |||||||||||||||
MaxLength.java | 100% | 100% | 83.3% | 95.7% |
|
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.form.validator; | |
16 | ||
17 | import org.apache.tapestry.IMarkupWriter; | |
18 | import org.apache.tapestry.IRequestCycle; | |
19 | import org.apache.tapestry.form.FormComponentContributorContext; | |
20 | import org.apache.tapestry.form.IFormComponent; | |
21 | import org.apache.tapestry.form.ValidationMessages; | |
22 | import org.apache.tapestry.valid.ValidationConstraint; | |
23 | import org.apache.tapestry.valid.ValidationStrings; | |
24 | import org.apache.tapestry.valid.ValidatorException; | |
25 | ||
26 | /** | |
27 | * Validator that ensures a string value does not exceed a maximum length. | |
28 | * | |
29 | * @author Howard Lewis Ship | |
30 | * @since 4.0 | |
31 | */ | |
32 | public class MaxLength extends BaseValidator | |
33 | { | |
34 | private int _maxLength; | |
35 | ||
36 | 0 | public MaxLength() |
37 | { | |
38 | ||
39 | } | |
40 | ||
41 | 4 | public MaxLength(String initializer) |
42 | { | |
43 | 4 | super(initializer); |
44 | } | |
45 | ||
46 | 4 | public void setMaxLength(int maxLength) |
47 | { | |
48 | 4 | _maxLength = maxLength; |
49 | } | |
50 | ||
51 | 3 | public void validate(IFormComponent field, ValidationMessages messages, Object object) |
52 | throws ValidatorException | |
53 | { | |
54 | 3 | String string = (String) object; |
55 | ||
56 | 3 | if (string.length() > _maxLength) |
57 | 2 | throw new ValidatorException(buildMessage(messages, field), |
58 | ValidationConstraint.MAXIMUM_WIDTH); | |
59 | } | |
60 | ||
61 | 3 | protected String buildMessage(ValidationMessages messages, IFormComponent field) |
62 | { | |
63 | 3 | return messages.formatValidationMessage( |
64 | getMessage(), | |
65 | ValidationStrings.VALUE_TOO_LONG, | |
66 | new Object[] | |
67 | { new Integer(_maxLength), field.getDisplayName() }); | |
68 | } | |
69 | ||
70 | 1 | public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
71 | FormComponentContributorContext context, IFormComponent field) | |
72 | { | |
73 | 1 | context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js"); |
74 | ||
75 | 1 | StringBuffer buffer = new StringBuffer("function(event) { validate_max_length(event, "); |
76 | 1 | buffer.append(context.getFieldDOM()); |
77 | 1 | buffer.append(", "); |
78 | 1 | buffer.append(_maxLength); |
79 | 1 | buffer.append(", '"); |
80 | 1 | buffer.append(buildMessage(context, field)); |
81 | 1 | buffer.append("'); }"); |
82 | ||
83 | 1 | context.addSubmitListener(buffer.toString()); |
84 | } | |
85 | ||
86 | } |
|