KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > valid > StringValidator


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 JavaDoc;
18 import java.util.Map JavaDoc;
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 JavaDoc _minimumLengthMessage;
37
38     /** @since 2.2 * */
39
40     private String JavaDoc _scriptPath = "/org/apache/tapestry/valid/StringValidator.script";
41
42     public StringValidator()
43     {
44     }
45
46     /**
47      * Initializes the StringValidator with properties defined by the initializer.
48      *
49      * @since 4.0
50      */

51
52     public StringValidator(String JavaDoc initializer)
53     {
54         super(initializer);
55     }
56
57     public String JavaDoc toString(IFormComponent field, Object JavaDoc value)
58     {
59         if (value == null)
60             return null;
61
62         return value.toString();
63     }
64
65     public Object JavaDoc toObject(IFormComponent field, String JavaDoc input) throws ValidatorException
66     {
67         if (checkRequired(field, input))
68             return null;
69
70         if (_minimumLength > 0 && input.length() < _minimumLength)
71             throw new ValidatorException(buildMinimumLengthMessage(field),
72                     ValidationConstraint.MINIMUM_WIDTH);
73
74         return input;
75     }
76
77     public int getMinimumLength()
78     {
79         return _minimumLength;
80     }
81
82     public void setMinimumLength(int minimumLength)
83     {
84         _minimumLength = minimumLength;
85     }
86
87     /**
88      * @since 2.2
89      */

90
91     public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
92             IRequestCycle cycle)
93     {
94         if (!isClientScriptingEnabled())
95             return;
96
97         if (!(isRequired() || _minimumLength > 0))
98             return;
99
100         Map JavaDoc symbols = new HashMap JavaDoc();
101
102         if (isRequired())
103             symbols.put("requiredMessage", buildRequiredMessage(field));
104
105         if (_minimumLength > 0)
106             symbols.put("minimumLengthMessage", buildMinimumLengthMessage(field));
107
108         processValidatorScript(_scriptPath, cycle, field, symbols);
109     }
110
111     /**
112      * @since 2.2
113      */

114
115     public String JavaDoc getScriptPath()
116     {
117         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 &lt;span&gt; tag, or to use a more sophisticated pop-up
124      * window than <code>window.alert()</code>).
125      *
126      * @since 2.2
127      */

128
129     public void setScriptPath(String JavaDoc scriptPath)
130     {
131         _scriptPath = scriptPath;
132     }
133
134     /** @since 3.0 */
135     public String JavaDoc getMinimumLengthMessage()
136     {
137         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     public void setMinimumLengthMessage(String JavaDoc string)
148     {
149         _minimumLengthMessage = string;
150     }
151
152     /** @since 3.0 */
153
154     protected String JavaDoc buildMinimumLengthMessage(IFormComponent field)
155     {
156         String JavaDoc pattern = getPattern(_minimumLengthMessage, "field-too-short", field.getPage()
157                 .getLocale());
158
159         return formatString(pattern, Integer.toString(_minimumLength), field.getDisplayName());
160     }
161
162 }
Popular Tags