Source Code Cross Referenced for StringValidator.java in  » J2EE » wicket » wicket » markup » html » form » validation » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » wicket » wicket.markup.html.form.validation 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: StringValidator.java 461311 2006-07-08 21:00:10Z ivaynberg $ $Revision:
003:         * 1.5 $ $Date: 2006-07-08 23:00:10 +0200 (Sat, 08 Jul 2006) $
004:         * 
005:         * ==============================================================================
006:         * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007:         * use this file except in compliance with the License. You may obtain a copy of
008:         * the License at
009:         * 
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing, software
013:         * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014:         * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015:         * License for the specific language governing permissions and limitations under
016:         * the License.
017:         */
018:        package wicket.markup.html.form.validation;
019:
020:        import java.util.Map;
021:
022:        import wicket.markup.html.form.FormComponent;
023:        import wicket.util.string.Strings;
024:
025:        /**
026:         * A validator for strings that can be used for subclassing or use one of the
027:         * static factory methods to get the default string validators as range, maximum
028:         * or minimum.
029:         * 
030:         * @author Jonathan Locke
031:         * @author Johan Compagner
032:         * @author Igor Vaynberg (ivaynberg)
033:         */
034:        public abstract class StringValidator extends AbstractValidator {
035:
036:            /**
037:             * Gets a String range validator to check if a string length is between min
038:             * and max.
039:             * 
040:             * If that is not the case then an error message will be generated with the
041:             * key "StringValidator.range" and the messages keys that can be used are:
042:             * <ul>
043:             * <li>${minimum}: the minimum length</li>
044:             * <li>${maximum}: the maximum length</li>
045:             * <li>${length}: the length of the user input</li>
046:             * <li>${input}: the input the user did give</li>
047:             * <li>${name}: the name of the component that failed</li>
048:             * <li>${label}: the label of the component - either comes from
049:             * FormComponent.labelModel or resource key [form-id].[form-component-id] in
050:             * that order</li>
051:             * </ul>
052:             * 
053:             * @param minimum
054:             *            The minimum length of the string.
055:             * @param maximum
056:             *            The maximum length of the string.
057:             * 
058:             * @return The StringValidator
059:             */
060:            public static StringValidator lengthBetween(int minimum, int maximum) {
061:                return new LengthBetweenValidator(minimum, maximum);
062:            }
063:
064:            /**
065:             * Gets a String minimum validator to check if a string length is greater
066:             * then the given minimum value.
067:             * 
068:             * If that is not the case then an error message will be generated with the
069:             * key "StringValidator.minimum" and the messages keys that can be used are:
070:             * <ul>
071:             * <li>${minimum}: the minimum length</li>
072:             * <li>${length}: the length of the user input</li>
073:             * <li>${input}: the input the user did give</li>
074:             * <li>${name}: the name of the component that failed</li>
075:             * <li>${label}: the label of the component - either comes from
076:             * FormComponent.labelModel or resource key [form-id].[form-component-id] in
077:             * that order</li>
078:             * </ul>
079:             * 
080:             * @param minimum
081:             *            The minimum length of the string.
082:             * 
083:             * @return The StringValidator
084:             */
085:            public static StringValidator minimumLength(int minimum) {
086:                return new MinimumLengthValidator(minimum);
087:            }
088:
089:            /**
090:             * Gets a String maximum validator to check if a string length is smaller
091:             * then the given maximum value.
092:             * 
093:             * If that is not the case then an error message will be generated with the
094:             * key "StringValidator.maximum" and the messages keys that can be used are:
095:             * <ul>
096:             * <li>${maximum}: the maximum length</li>
097:             * <li>${length}: the length of the user input</li>
098:             * <li>${input}: the input the user did give</li>
099:             * <li>${name}: the name of the component that failed</li>
100:             * <li>${label}: the label of the component - either comes from
101:             * FormComponent.labelModel or resource key [form-id].[form-component-id] in
102:             * that order</li>
103:             * </ul>
104:             * 
105:             * @param maximum
106:             *            The maximum length of the string.
107:             * 
108:             * @return The StringValidator
109:             */
110:            public static StringValidator maximumLength(int maximum) {
111:                return new MaximumLengthValidator(maximum);
112:            }
113:
114:            /**
115:             * Gets a String exact length validator to check if a string length is exactly the same as the given value
116:             * 
117:             * If that is not the case then an error message will be generated with the
118:             * key "StringValidator.exact" and the messages keys that can be used are:
119:             * <ul>
120:             * <li>${exact}: the maximum length</li>
121:             * <li>${length}: the length of the user input</li>
122:             * <li>${input}: the input the user did give</li>
123:             * <li>${name}: the name of the component that failed</li>
124:             * <li>${label}: the label of the component - either comes from
125:             * FormComponent.labelModel or resource key [form-id].[form-component-id] in
126:             * that order</li>
127:             * </ul>
128:             * 
129:             * @param length
130:             *            The required length of the string.
131:             * 
132:             * @return The StringValidator
133:             */
134:            public static StringValidator exactLength(int length) {
135:                return new ExactLengthValidator(length);
136:            }
137:
138:            /**
139:             * @see wicket.markup.html.form.validation.IValidator#validate(wicket.markup.html.form.FormComponent)
140:             */
141:            public void validate(final FormComponent formComponent) {
142:                onValidate(formComponent, (String) formComponent
143:                        .getConvertedInput());
144:            }
145:
146:            /**
147:             * Subclasses should override this method to validate the string value for a
148:             * component.
149:             * 
150:             * @param formComponent
151:             *            form component
152:             * @param value
153:             *            The string value to validate
154:             */
155:            public abstract void onValidate(FormComponent formComponent,
156:                    String value);
157:
158:            private static class LengthBetweenValidator extends StringValidator {
159:                private static final long serialVersionUID = 1L;
160:                private final int minimum;
161:                private final int maximum;
162:
163:                private LengthBetweenValidator(int minimum, int maximum) {
164:                    this .minimum = minimum;
165:                    this .maximum = maximum;
166:
167:                }
168:
169:                /**
170:                 * @see wicket.markup.html.form.validation.StringValidator#onValidate(wicket.markup.html.form.FormComponent,
171:                 *      java.lang.String)
172:                 */
173:                public void onValidate(FormComponent formComponent, String value) {
174:                    if (!Strings.isEmpty(value)) {
175:                        if (value.length() < minimum
176:                                || value.length() > maximum) {
177:                            error(formComponent);
178:                        }
179:                    }
180:                }
181:
182:                protected Map messageModel(FormComponent formComponent) {
183:                    final Map map = super .messageModel(formComponent);
184:                    map.put("minimum", new Integer(minimum));
185:                    map.put("maximum", new Integer(maximum));
186:                    map.put("length", new Integer(((String) formComponent
187:                            .getConvertedInput()).length()));
188:                    return map;
189:                }
190:
191:                /**
192:                 * @see wicket.markup.html.form.validation.AbstractValidator#resourceKey(wicket.markup.html.form.FormComponent)
193:                 */
194:                protected String resourceKey(FormComponent formComponent) {
195:                    return "StringValidator.range";
196:                }
197:
198:            }
199:
200:            private static class MinimumLengthValidator extends StringValidator {
201:                private static final long serialVersionUID = 1L;
202:                private final int minimum;
203:
204:                private MinimumLengthValidator(int minimum) {
205:                    this .minimum = minimum;
206:                }
207:
208:                /**
209:                 * @see wicket.markup.html.form.validation.StringValidator#onValidate(wicket.markup.html.form.FormComponent,
210:                 *      java.lang.String)
211:                 */
212:                public void onValidate(FormComponent formComponent, String value) {
213:                    if (!Strings.isEmpty(value)) {
214:                        if (value.length() < minimum) {
215:                            error(formComponent);
216:                        }
217:                    }
218:                }
219:
220:                protected Map messageModel(FormComponent formComponent) {
221:                    final Map map = super .messageModel(formComponent);
222:                    map.put("minimum", new Integer(minimum));
223:                    map.put("length", new Integer(((String) formComponent
224:                            .getConvertedInput()).length()));
225:                    return map;
226:                }
227:
228:                /**
229:                 * @see wicket.markup.html.form.validation.AbstractValidator#resourceKey(wicket.markup.html.form.FormComponent)
230:                 */
231:                protected String resourceKey(FormComponent formComponent) {
232:                    return "StringValidator.minimum";
233:                }
234:
235:            }
236:
237:            private static class ExactLengthValidator extends StringValidator {
238:                private static final long serialVersionUID = 1L;
239:                private final int length;
240:
241:                private ExactLengthValidator(int length) {
242:                    this .length = length;
243:                }
244:
245:                /**
246:                 * @see wicket.markup.html.form.validation.StringValidator#onValidate(wicket.markup.html.form.FormComponent,
247:                 *      java.lang.String)
248:                 */
249:                public void onValidate(FormComponent formComponent, String value) {
250:                    if (!Strings.isEmpty(value)) {
251:                        if (value.length() != length) {
252:                            error(formComponent);
253:                        }
254:                    }
255:                }
256:
257:                protected Map messageModel(FormComponent formComponent) {
258:                    final Map map = super .messageModel(formComponent);
259:                    map.put("length", new Integer(((String) formComponent
260:                            .getConvertedInput()).length()));
261:                    map.put("exact", "" + this .length);
262:                    return map;
263:                }
264:
265:                /**
266:                 * @see wicket.markup.html.form.validation.AbstractValidator#resourceKey(wicket.markup.html.form.FormComponent)
267:                 */
268:                protected String resourceKey(FormComponent formComponent) {
269:                    return "StringValidator.exact";
270:                }
271:
272:            }
273:
274:            private static class MaximumLengthValidator extends StringValidator {
275:                private static final long serialVersionUID = 1L;
276:                private final int maximum;
277:
278:                private MaximumLengthValidator(int maximum) {
279:                    this .maximum = maximum;
280:                }
281:
282:                /**
283:                 * @see wicket.markup.html.form.validation.StringValidator#onValidate(wicket.markup.html.form.FormComponent,
284:                 *      java.lang.String)
285:                 */
286:                public void onValidate(FormComponent formComponent, String value) {
287:                    if (!Strings.isEmpty(value)) {
288:                        if (value.length() > maximum) {
289:                            error(formComponent);
290:                        }
291:                    }
292:                }
293:
294:                protected Map messageModel(FormComponent formComponent) {
295:                    final Map map = super .messageModel(formComponent);
296:                    map.put("maximum", new Integer(maximum));
297:                    map.put("length", new Integer(((String) formComponent
298:                            .getConvertedInput()).length()));
299:                    return map;
300:                }
301:
302:                /**
303:                 * @see wicket.markup.html.form.validation.AbstractValidator#resourceKey(wicket.markup.html.form.FormComponent)
304:                 */
305:                protected String resourceKey(FormComponent formComponent) {
306:                    return "StringValidator.maximum";
307:                }
308:            }
309:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.