Source Code Cross Referenced for GenericValidator.java in  » Library » Apache-commons-validator-1.3.1-src » org » apache » commons » validator » 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 » Library » Apache commons validator 1.3.1 src » org.apache.commons.validator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.validator;
018:
019:        import java.io.Serializable;
020:        import java.util.Locale;
021:
022:        import org.apache.oro.text.perl.Perl5Util;
023:
024:        /**
025:         * This class contains basic methods for performing validations.
026:         *
027:         * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
028:         */
029:        public class GenericValidator implements  Serializable {
030:
031:            /**
032:             * UrlValidator used in wrapper method.
033:             */
034:            private static final UrlValidator URL_VALIDATOR = new UrlValidator();
035:
036:            /**
037:             * CreditCardValidator used in wrapper method.
038:             */
039:            private static final CreditCardValidator CREDIT_CARD_VALIDATOR = new CreditCardValidator();
040:
041:            /**
042:             * <p>Checks if the field isn't null and length of the field is greater 
043:             * than zero not including whitespace.</p>
044:             *
045:             * @param value The value validation is being performed on.
046:             * @return true if blank or null.
047:             */
048:            public static boolean isBlankOrNull(String value) {
049:                return ((value == null) || (value.trim().length() == 0));
050:            }
051:
052:            /**
053:             * <p>Checks if the value matches the regular expression.</p>
054:             *
055:             * @param value The value validation is being performed on.
056:             * @param regexp The regular expression.
057:             * @return true if matches the regular expression.
058:             */
059:            public static boolean matchRegexp(String value, String regexp) {
060:                if (regexp == null || regexp.length() <= 0) {
061:                    return false;
062:                }
063:
064:                Perl5Util matcher = new Perl5Util();
065:                return matcher.match("/" + regexp + "/", value);
066:            }
067:
068:            /**
069:             * <p>Checks if the value can safely be converted to a byte primitive.</p>
070:             *
071:             * @param value The value validation is being performed on.
072:             * @return true if the value can be converted to a Byte.
073:             */
074:            public static boolean isByte(String value) {
075:                return (GenericTypeValidator.formatByte(value) != null);
076:            }
077:
078:            /**
079:             * <p>Checks if the value can safely be converted to a short primitive.</p>
080:             *
081:             * @param value The value validation is being performed on.
082:             * @return true if the value can be converted to a Short.
083:             */
084:            public static boolean isShort(String value) {
085:                return (GenericTypeValidator.formatShort(value) != null);
086:            }
087:
088:            /**
089:             * <p>Checks if the value can safely be converted to a int primitive.</p>
090:             *
091:             * @param value The value validation is being performed on.
092:             * @return true if the value can be converted to an Integer.
093:             */
094:            public static boolean isInt(String value) {
095:                return (GenericTypeValidator.formatInt(value) != null);
096:            }
097:
098:            /**
099:             * <p>Checks if the value can safely be converted to a long primitive.</p>
100:             *
101:             * @param value The value validation is being performed on.
102:             * @return true if the value can be converted to a Long.
103:             */
104:            public static boolean isLong(String value) {
105:                return (GenericTypeValidator.formatLong(value) != null);
106:            }
107:
108:            /**
109:             * <p>Checks if the value can safely be converted to a float primitive.</p>
110:             *
111:             * @param value The value validation is being performed on.
112:             * @return true if the value can be converted to a Float.
113:             */
114:            public static boolean isFloat(String value) {
115:                return (GenericTypeValidator.formatFloat(value) != null);
116:            }
117:
118:            /**
119:             * <p>Checks if the value can safely be converted to a double primitive.</p>
120:             *
121:             * @param value The value validation is being performed on.
122:             * @return true if the value can be converted to a Double.
123:             */
124:            public static boolean isDouble(String value) {
125:                return (GenericTypeValidator.formatDouble(value) != null);
126:            }
127:
128:            /**
129:             * <p>Checks if the field is a valid date.  The <code>Locale</code> is
130:             * used with <code>java.text.DateFormat</code>.  The setLenient method
131:             * is set to <code>false</code> for all.</p>
132:             *
133:             * @param value The value validation is being performed on.
134:             * @param locale The locale to use for the date format, defaults to the 
135:             * system default if null.
136:             * @return true if the value can be converted to a Date.
137:             */
138:            public static boolean isDate(String value, Locale locale) {
139:                return DateValidator.getInstance().isValid(value, locale);
140:            }
141:
142:            /**
143:             * <p>Checks if the field is a valid date.  The pattern is used with
144:             * <code>java.text.SimpleDateFormat</code>.  If strict is true, then the
145:             * length will be checked so '2/12/1999' will not pass validation with
146:             * the format 'MM/dd/yyyy' because the month isn't two digits.
147:             * The setLenient method is set to <code>false</code> for all.</p>
148:             *
149:             * @param value The value validation is being performed on.
150:             * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
151:             * @param strict Whether or not to have an exact match of the datePattern.
152:             * @return true if the value can be converted to a Date.
153:             */
154:            public static boolean isDate(String value, String datePattern,
155:                    boolean strict) {
156:                return DateValidator.getInstance().isValid(value, datePattern,
157:                        strict);
158:            }
159:
160:            /**
161:             * <p>Checks if a value is within a range (min &amp; max specified
162:             * in the vars attribute).</p>
163:             *
164:             * @param value The value validation is being performed on.
165:             * @param min The minimum value of the range.
166:             * @param max The maximum value of the range.
167:             * @return true if the value is in the specified range.
168:             */
169:            public static boolean isInRange(byte value, byte min, byte max) {
170:                return ((value >= min) && (value <= max));
171:            }
172:
173:            /**
174:             * <p>Checks if a value is within a range (min &amp; max specified
175:             * in the vars attribute).</p>
176:             *
177:             * @param value The value validation is being performed on.
178:             * @param min The minimum value of the range.
179:             * @param max The maximum value of the range.
180:             * @return true if the value is in the specified range.
181:             */
182:            public static boolean isInRange(int value, int min, int max) {
183:                return ((value >= min) && (value <= max));
184:            }
185:
186:            /**
187:             * <p>Checks if a value is within a range (min &amp; max specified
188:             * in the vars attribute).</p>
189:             *
190:             * @param value The value validation is being performed on.
191:             * @param min The minimum value of the range.
192:             * @param max The maximum value of the range.
193:             * @return true if the value is in the specified range.
194:             */
195:            public static boolean isInRange(float value, float min, float max) {
196:                return ((value >= min) && (value <= max));
197:            }
198:
199:            /**
200:             * <p>Checks if a value is within a range (min &amp; max specified
201:             * in the vars attribute).</p>
202:             *
203:             * @param value The value validation is being performed on.
204:             * @param min The minimum value of the range.
205:             * @param max The maximum value of the range.
206:             * @return true if the value is in the specified range.
207:             */
208:            public static boolean isInRange(short value, short min, short max) {
209:                return ((value >= min) && (value <= max));
210:            }
211:
212:            /**
213:             * <p>Checks if a value is within a range (min &amp; max specified
214:             * in the vars attribute).</p>
215:             *
216:             * @param value The value validation is being performed on.
217:             * @param min The minimum value of the range.
218:             * @param max The maximum value of the range.
219:             * @return true if the value is in the specified range.
220:             */
221:            public static boolean isInRange(long value, long min, long max) {
222:                return ((value >= min) && (value <= max));
223:            }
224:
225:            /**
226:             * <p>Checks if a value is within a range (min &amp; max specified
227:             * in the vars attribute).</p>
228:             *
229:             * @param value The value validation is being performed on.
230:             * @param min The minimum value of the range.
231:             * @param max The maximum value of the range.
232:             * @return true if the value is in the specified range.
233:             */
234:            public static boolean isInRange(double value, double min, double max) {
235:                return ((value >= min) && (value <= max));
236:            }
237:
238:            /**
239:             * Checks if the field is a valid credit card number.
240:             * @param value The value validation is being performed on.
241:             * @return true if the value is valid Credit Card Number.
242:             */
243:            public static boolean isCreditCard(String value) {
244:                return CREDIT_CARD_VALIDATOR.isValid(value);
245:            }
246:
247:            /**
248:             * <p>Checks if a field has a valid e-mail address.</p>
249:             *
250:             * @param value The value validation is being performed on.
251:             * @return true if the value is valid Email Address.
252:             */
253:            public static boolean isEmail(String value) {
254:                return EmailValidator.getInstance().isValid(value);
255:            }
256:
257:            /**
258:             * <p>Checks if a field is a valid url address.</p>
259:             * If you need to modify what is considered valid then
260:             * consider using the UrlValidator directly.
261:             *
262:             * @param value The value validation is being performed on.
263:             * @return true if the value is valid Url.
264:             */
265:            public static boolean isUrl(String value) {
266:                return URL_VALIDATOR.isValid(value);
267:            }
268:
269:            /**
270:             * <p>Checks if the value's length is less than or equal to the max.</p>
271:             *
272:             * @param value The value validation is being performed on.
273:             * @param max The maximum length.
274:             * @return true if the value's length is less than the specified maximum.
275:             */
276:            public static boolean maxLength(String value, int max) {
277:                return (value.length() <= max);
278:            }
279:
280:            /**
281:             * <p>Checks if the value's adjusted length is less than or equal to the max.</p>
282:             *
283:             * @param value The value validation is being performed on.
284:             * @param max The maximum length.
285:             * @param lineEndLength The length to use for line endings.
286:             * @return true if the value's length is less than the specified maximum.
287:             */
288:            public static boolean maxLength(String value, int max,
289:                    int lineEndLength) {
290:                int adjustAmount = adjustForLineEnding(value, lineEndLength);
291:                return ((value.length() + adjustAmount) <= max);
292:            }
293:
294:            /**
295:             * <p>Checks if the value's length is greater than or equal to the min.</p>
296:             *
297:             * @param value The value validation is being performed on.
298:             * @param min The minimum length.
299:             * @return true if the value's length is more than the specified minimum.
300:             */
301:            public static boolean minLength(String value, int min) {
302:                return (value.length() >= min);
303:            }
304:
305:            /**
306:             * <p>Checks if the value's adjusted length is greater than or equal to the min.</p>
307:             *
308:             * @param value The value validation is being performed on.
309:             * @param min The minimum length.
310:             * @param lineEndLength The length to use for line endings.
311:             * @return true if the value's length is more than the specified minimum.
312:             */
313:            public static boolean minLength(String value, int min,
314:                    int lineEndLength) {
315:                int adjustAmount = adjustForLineEnding(value, lineEndLength);
316:                return ((value.length() + adjustAmount) >= min);
317:            }
318:
319:            /**
320:             * Calculate an adjustment amount for line endings.
321:             *
322:             * See Bug 37962 for the rational behind this.
323:             *
324:             * @param value The value validation is being performed on.
325:             * @param lineEndLength The length to use for line endings.
326:             * @return the adjustment amount.
327:             */
328:            private static int adjustForLineEnding(String value,
329:                    int lineEndLength) {
330:                int nCount = 0;
331:                int rCount = 0;
332:                for (int i = 0; i < value.length(); i++) {
333:                    if (value.charAt(i) == '\n') {
334:                        nCount++;
335:                    }
336:                    if (value.charAt(i) == '\r') {
337:                        rCount++;
338:                    }
339:                }
340:                return ((nCount * lineEndLength) - (rCount + nCount));
341:            }
342:
343:            // See http://issues.apache.org/bugzilla/show_bug.cgi?id=29015 WRT the "value" methods
344:
345:            /**
346:             * <p>Checks if the value is greater than or equal to the min.</p>
347:             *
348:             * @param value The value validation is being performed on.
349:             * @param min The minimum numeric value.
350:             * @return true if the value is &gt;= the specified minimum.
351:             */
352:            public static boolean minValue(int value, int min) {
353:                return (value >= min);
354:            }
355:
356:            /**
357:             * <p>Checks if the value is greater than or equal to the min.</p>
358:             *
359:             * @param value The value validation is being performed on.
360:             * @param min The minimum numeric value.
361:             * @return true if the value is &gt;= the specified minimum.
362:             */
363:            public static boolean minValue(long value, long min) {
364:                return (value >= min);
365:            }
366:
367:            /**
368:             * <p>Checks if the value is greater than or equal to the min.</p>
369:             *
370:             * @param value The value validation is being performed on.
371:             * @param min The minimum numeric value.
372:             * @return true if the value is &gt;= the specified minimum.
373:             */
374:            public static boolean minValue(double value, double min) {
375:                return (value >= min);
376:            }
377:
378:            /**
379:             * <p>Checks if the value is greater than or equal to the min.</p>
380:             *
381:             * @param value The value validation is being performed on.
382:             * @param min The minimum numeric value.
383:             * @return true if the value is &gt;= the specified minimum.
384:             */
385:            public static boolean minValue(float value, float min) {
386:                return (value >= min);
387:            }
388:
389:            /**
390:             * <p>Checks if the value is less than or equal to the max.</p>
391:             *
392:             * @param value The value validation is being performed on.
393:             * @param max The maximum numeric value.
394:             * @return true if the value is &lt;= the specified maximum.
395:             */
396:            public static boolean maxValue(int value, int max) {
397:                return (value <= max);
398:            }
399:
400:            /**
401:             * <p>Checks if the value is less than or equal to the max.</p>
402:             *
403:             * @param value The value validation is being performed on.
404:             * @param max The maximum numeric value.
405:             * @return true if the value is &lt;= the specified maximum.
406:             */
407:            public static boolean maxValue(long value, long max) {
408:                return (value <= max);
409:            }
410:
411:            /**
412:             * <p>Checks if the value is less than or equal to the max.</p>
413:             *
414:             * @param value The value validation is being performed on.
415:             * @param max The maximum numeric value.
416:             * @return true if the value is &lt;= the specified maximum.
417:             */
418:            public static boolean maxValue(double value, double max) {
419:                return (value <= max);
420:            }
421:
422:            /**
423:             * <p>Checks if the value is less than or equal to the max.</p>
424:             *
425:             * @param value The value validation is being performed on.
426:             * @param max The maximum numeric value.
427:             * @return true if the value is &lt;= the specified maximum.
428:             */
429:            public static boolean maxValue(float value, float max) {
430:                return (value <= max);
431:            }
432:
433:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.