001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of jWebTk.
006: *
007: * jWebTk is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * jWebTk is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with jWebTk; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jwebtk.validator;
020:
021: import org.apache.commons.validator.GenericValidator;
022: import org.apache.commons.validator.ISBNValidator;
023:
024: import java.util.Locale;
025:
026: public class Validator {
027: /**
028: * Validates an International Standard Book Number.
029: *
030: * @param value to validate
031: * @return true if the value passes validation, false otherwise.
032: */
033: public static boolean isIsbn(String value) {
034: if (value == null || value.length() == 0)
035: return true;
036:
037: return new ISBNValidator().isValid(value);
038: }
039:
040: /**
041: * Validates a value to make sure it is not empty or null.
042: *
043: * @param value to validate
044: * @return true if the value passes validation, false otherwise.
045: */
046: public static boolean isEmptyOrNull(String value) {
047: return GenericValidator.isBlankOrNull(value);
048: }
049:
050: /**
051: * Validates a value to make sure it is not empty or null.
052: *
053: * @param value to validate
054: * @return true if the value passes validation, false otherwise.
055: */
056: public static boolean isNotEmptyOrNull(String value) {
057: return !GenericValidator.isBlankOrNull(value);
058: }
059:
060: /**
061: * Validates a byte value
062: *
063: * @param value to validate
064: * @return true if the value passes validation, false otherwise.
065: */
066: public static boolean isByte(String value) {
067: if (value == null || value.length() == 0)
068: return true;
069:
070: return GenericValidator.isByte(value);
071: }
072:
073: /**
074: * Validates a credit card number. Simply checks if it is properly formatted. Does not contact any other party for card validation.
075: *
076: * @param value to validate
077: * @return true if the value passes validation, false otherwise.
078: */
079: public static boolean isCreditCard(String value) {
080: if (value == null || value.length() == 0)
081: return true;
082:
083: return GenericValidator.isCreditCard(value);
084: }
085:
086: /**
087: * Validates a date value to make sure it has a valid format.
088: *
089: * @param value to validate
090: * @return true if the value passes validation, false otherwise.
091: */
092: public static boolean isDate(String value, Locale locale) {
093: if (value == null || value.length() == 0)
094: return true;
095:
096: return GenericValidator.isDate(value, locale);
097: }
098:
099: /**
100: * Validates a date value to make sure it has a valid format matching the given date pattern.
101: * Date pattern is defined in java.text.SimpleDateFormat.
102: *
103: * @param value to validate
104: * @return true if the value passes validation, false otherwise.
105: */
106: public static boolean isDate(String value, String datePattern,
107: boolean strict) {
108: if (value == null || value.length() == 0)
109: return true;
110:
111: return GenericValidator.isDate(value, datePattern, strict);
112: }
113:
114: /**
115: * Validates a double value.
116: * @param value to validate
117: * @return true if the value passes validation, false otherwise.
118: */
119: public static boolean isDouble(String value) {
120: if (value == null || value.length() == 0)
121: return true;
122:
123: return GenericValidator.isDouble(value);
124: }
125:
126: /**
127: * Validates as an email value.
128: * @param value to validate
129: * @return true if the value passes validation, false otherwise.
130: */
131: public static boolean isEmail(String value) {
132: if (value == null || value.length() == 0)
133: return true;
134:
135: return GenericValidator.isEmail(value);
136: }
137:
138: /**
139: * Validate that value compares equal to “str”.
140: * @param value to validate
141: * @param value2 string to compare
142: * @param ignore character case
143: * @return true if the value passes validation, false otherwise.
144: */
145: public static boolean isEqualTo(String value, String value2,
146: boolean ignore) {
147: if (value == null || value.length() == 0)
148: return true;
149:
150: if (ignore)
151: return value.equalsIgnoreCase(value2);
152:
153: return value.equals(value2);
154: }
155:
156: /**
157: * Validates as a float value.
158: * @param value to validate
159: * @return true if the value passes validation, false otherwise.
160: */
161: public static boolean isFloat(String value) {
162: if (value == null || value.length() == 0)
163: return true;
164:
165: return GenericValidator.isFloat(value);
166: }
167:
168: /**
169: * Validates that a value is in a given range, inclusive.
170: * @param value to validate
171: * @param min value is equal or greater
172: * @param max value is equal or less
173: * @return true if the value passes validation, false otherwise.
174: */
175: public static boolean isInRange(byte value, byte min, byte max) {
176: return GenericValidator.isInRange(value, min, max);
177: }
178:
179: /**
180: * Validates that a value is in a given range, inclusive.
181: * @param value to validate
182: * @param min value is equal or greater
183: * @param max value is equal or less
184: * @return true if the value passes validation, false otherwise.
185: */
186: public static boolean isInRange(double value, double min, double max) {
187: return GenericValidator.isInRange(value, min, max);
188: }
189:
190: /**
191: * Validates that a value is in a given range, inclusive.
192: * @param value to validate
193: * @param min value is equal or greater
194: * @param max value is equal or less
195: * @return true if the value passes validation, false otherwise.
196: */
197: public static boolean isInRange(float value, float min, float max) {
198: return GenericValidator.isInRange(value, min, max);
199: }
200:
201: /**
202: * Validates that a value is in a given range, inclusive.
203: * @param value to validate
204: * @param min value is equal or greater
205: * @param max value is equal or less
206: * @return true if the value passes validation, false otherwise.
207: */
208: public static boolean isInRange(int value, int min, int max) {
209: return GenericValidator.isInRange(value, min, max);
210: }
211:
212: /**
213: * Validates that a value is in a given range, inclusive.
214: * @param value to validate
215: * @param min value is equal or greater
216: * @param max value is equal or less
217: * @return true if the value passes validation, false otherwise.
218: */
219: public static boolean isInRange(long value, long min, long max) {
220: return GenericValidator.isInRange(value, min, max);
221: }
222:
223: /**
224: * Validates that a value is in a given range, inclusive.
225: * @param value to validate
226: * @param min value is equal or greater
227: * @param max value is equal or less
228: * @return true if the value passes validation, false otherwise.
229: */
230: public static boolean isInRange(short value, short min, short max) {
231: return GenericValidator.isInRange(value, min, max);
232: }
233:
234: /**
235: * Validates an integer value.
236: * @param value to validate
237: * @return true if the value passes validation, false otherwise.
238: */
239: public static boolean isInt(String value) {
240: if (value == null || value.length() == 0)
241: return true;
242:
243: return GenericValidator.isInt(value);
244: }
245:
246: /**
247: * Validates a long value.
248: * @param value to validate
249: * @return true if the value passes validation, false otherwise.
250: */
251: public static boolean isLong(String value) {
252: if (value == null || value.length() == 0)
253: return true;
254:
255: return GenericValidator.isLong(value);
256: }
257:
258: /**
259: * Validates a short value.
260: * @param value to validate
261: * @return true if the value passes validation, false otherwise.
262: */
263: public static boolean isShort(String value) {
264: if (value == null || value.length() == 0)
265: return true;
266:
267: return GenericValidator.isShort(value);
268: }
269:
270: /**
271: * Validates that the given value is a properly formatted URL.
272: * @param value to validate
273: * @return true if the value passes validation, false otherwise.
274: */
275: public static boolean isUrl(String value) {
276: if (value == null || value.length() == 0)
277: return true;
278:
279: return GenericValidator.isUrl(value);
280: }
281:
282: /**
283: * Validates that the given value is a substring of cmp_string.
284: * @param value to validate
285: * @param compareString the string to check
286: * @return true if the value passes validation, false otherwise.
287: */
288: public static boolean isSubstring(String value,
289: String compareString, boolean ignoreCase) {
290: if (ignoreCase)
291: return value.toLowerCase().indexOf(
292: compareString.toLowerCase()) != -1;
293:
294: return value.indexOf(compareString) != -1;
295: }
296:
297: /**
298: * Validates that the given value starts with the match string.
299: * @param value to validate
300: * @param matchString the string to check
301: * @return true if the value passes validation, false otherwise.
302: */
303: public static boolean startsWith(String value, String matchString,
304: boolean ignoreCase) {
305: if (ignoreCase)
306: return value.toLowerCase().startsWith(
307: matchString.toLowerCase());
308:
309: return value.startsWith(matchString);
310: }
311:
312: /**
313: * Validates that the given value ends with the match string.
314: * @param value to validate
315: * @param matchString the string to check
316: * @return true if the value passes validation, false otherwise.
317: */
318: public static boolean endsWith(String value, String matchString,
319: boolean ignoreCase) {
320: if (ignoreCase)
321: return value.toLowerCase().endsWith(
322: matchString.toLowerCase());
323:
324: return value.endsWith(matchString);
325: }
326:
327: /**
328: * Validates that the given value matches the provided regular expression.
329: * this is based on Java regular expression as found in String.matchRegexp().
330: * @param value to validate
331: * @param regexp the regular expression string to check
332: * @return true if the value passes validation, false otherwise.
333: */
334: public static boolean matchRegexp(String value, String regexp) {
335: if (value == null || value.length() == 0)
336: return true;
337:
338: return GenericValidator.matchRegexp(value, regexp);
339: }
340:
341: /**
342: * Validates that value is less than or equal to the maximum length.
343: * @param value to validate
344: * @param max the maximum length
345: * @return true if the value passes validation, false otherwise.
346: */
347: public static boolean maxLength(String value, int max) {
348: if (value == null || value.length() == 0)
349: return true;
350:
351: return GenericValidator.maxLength(value, max);
352: }
353:
354: /**
355: * Validates that value is greater than or equal to the minimum length.
356: * @param value to validate
357: * @param min the minimum length
358: * @return true if the value passes validation, false otherwise.
359: */
360: public static boolean minLength(String value, int min) {
361: if (value == null || value.length() == 0)
362: return true;
363:
364: return GenericValidator.minLength(value, min);
365: }
366:
367: /**
368: * Validates that value is less than or equal to the maximum.
369: * @param value to validate
370: * @param max the maximum length
371: * @return true if the value passes validation, false otherwise.
372: */
373: public static boolean maxValue(double value, double max) {
374: return GenericValidator.maxValue(value, max);
375: }
376:
377: /**
378: * Validates that value is less than or equal to the maximum.
379: * @param value to validate
380: * @param max the maximum length
381: * @return true if the value passes validation, false otherwise.
382: */
383: public static boolean maxValue(float value, float max) {
384: return GenericValidator.maxValue(value, max);
385: }
386:
387: /**
388: * Validates that value is less than or equal to the maximum.
389: * @param value to validate
390: * @param max the maximum length
391: * @return true if the value passes validation, false otherwise.
392: */
393: public static boolean maxValue(int value, int max) {
394: return GenericValidator.maxValue(value, max);
395: }
396:
397: /**
398: * Validates that value is less than or equal to the maximum.
399: * @param value to validate
400: * @param max the maximum length
401: * @return true if the value passes validation, false otherwise.
402: */
403: public static boolean maxValue(long value, long max) {
404: return GenericValidator.maxValue(value, max);
405: }
406:
407: /**
408: * Validates that value is less than or equal to the maximum.
409: * @param value to validate
410: * @param min the minimum length
411: * @return true if the value passes validation, false otherwise.
412: */
413: public static boolean minValue(double value, double min) {
414: return GenericValidator.minValue(value, min);
415: }
416:
417: /**
418: * Validates that value is greater than or equal to the minimum.
419: * @param value to validate
420: * @param min the minimum length
421: * @return true if the value passes validation, false otherwise.
422: */
423: public static boolean minValue(float value, float min) {
424: return GenericValidator.minValue(value, min);
425: }
426:
427: /**
428: * Validates that value is greater than or equal to the minimum.
429: * @param value to validate
430: * @param min the minimum length
431: * @return true if the value passes validation, false otherwise.
432: */
433: public static boolean minValue(int value, int min) {
434: return GenericValidator.minValue(value, min);
435: }
436:
437: /**
438: * Validates that value is greater than or equal to the minimum.
439: * @param value to validate
440: * @param min the minimum length
441: * @return true if the value passes validation, false otherwise.
442: */
443: public static boolean minValue(long value, long min) {
444: return GenericValidator.minValue(value, min);
445: }
446: }
|