1
22
23 package com.liferay.portal.kernel.util;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30
31
38 public class Validator {
39
40 public static boolean equals(String s1, String s2) {
41 if ((s1 == null) && (s2 == null)) {
42 return true;
43 }
44 else if ((s1 == null) || (s2 == null)) {
45 return false;
46 }
47 else {
48 return s1.equals(s2);
49 }
50 }
51
52 public static boolean isAddress(String address) {
53 if (isNull(address)) {
54 return false;
55 }
56
57 String[] tokens = address.split(StringPool.AT);
58
59 if (tokens.length != 2) {
60 return false;
61 }
62
63 for (String token : tokens) {
64 for (char c : token.toCharArray()) {
65 if (Character.isWhitespace(c)) {
66 return false;
67 }
68 }
69 }
70
71 return true;
72 }
73
74 public static boolean isAscii(char c) {
75 int i = (int)c;
76
77 if ((i >= 32) && (i <= 126)) {
78 return true;
79 }
80 else {
81 return false;
82 }
83 }
84
85
91 public static boolean isChar(char c) {
92 int x = c;
93
94 if ((x >= _CHAR_BEGIN) && (x <= _CHAR_END)) {
95 return true;
96 }
97
98 return false;
99 }
100
101
108 public static boolean isChar(String s) {
109 if (isNull(s)) {
110 return false;
111 }
112
113 for (char c : s.toCharArray()) {
114 if (!isChar(c)) {
115 return false;
116 }
117 }
118
119 return true;
120 }
121
122 public static boolean isDate(int month, int day, int year) {
123 return isGregorianDate(month, day, year);
124 }
125
126
132 public static boolean isDigit(char c) {
133 int x = c;
134
135 if ((x >= _DIGIT_BEGIN) && (x <= _DIGIT_END)) {
136 return true;
137 }
138
139 return false;
140 }
141
142
148 public static boolean isDigit(String s) {
149 if (isNull(s)) {
150 return false;
151 }
152
153 for (char c : s.toCharArray()) {
154 if (!isDigit(c)) {
155 return false;
156 }
157 }
158
159 return true;
160 }
161
162 public static boolean isDomain(String domainName) {
163
164
167 if (isNull(domainName)) {
168 return false;
169 }
170
171 if (domainName.length() > 255) {
172 return false;
173 }
174
175 String[] domainNameArray = StringUtil.split(
176 domainName, StringPool.PERIOD);
177
178 for (String domainNamePart : domainNameArray) {
179 char[] domainNamePartCharArray = domainNamePart.toCharArray();
180
181 for (int i = 0; i < domainNamePartCharArray.length; i++) {
182 char c = domainNamePartCharArray[i];
183
184 if ((i == 0) && (c == CharPool.DASH)) {
185 return false;
186 }
187 else if ((i == (domainNamePartCharArray.length - 1)) &&
188 (c == CharPool.DASH)) {
189
190 return false;
191 }
192 else if ((!isChar(c)) && (!isDigit(c)) &&
193 (c != CharPool.DASH)) {
194
195 return false;
196 }
197 }
198 }
199
200 return true;
201 }
202
203 public static boolean isEmailAddress(String emailAddress) {
204 Boolean valid = null;
205
206 try {
207 valid = (Boolean)PortalClassInvoker.invoke(
208 "com.liferay.util.mail.InternetAddressUtil", "isValid",
209 emailAddress);
210 }
211 catch (Exception e) {
212 if (_log.isWarnEnabled()) {
213 _log.warn(e);
214 }
215 }
216
217 if (valid == null) {
218 return false;
219 }
220 else {
221 return valid.booleanValue();
222 }
223 }
224
225 public static boolean isEmailAddressSpecialChar(char c) {
226
227
229 for (int i = 0; i < _EMAIL_ADDRESS_SPECIAL_CHAR.length; i++) {
230 if (c == _EMAIL_ADDRESS_SPECIAL_CHAR[i]) {
231 return true;
232 }
233 }
234
235 return false;
236 }
237
238 public static boolean isGregorianDate(int month, int day, int year) {
239 if ((month < 0) || (month > 11)) {
240 return false;
241 }
242
243 int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
244
245 if (month == 1) {
246 int febMax = 28;
247
248 if (((year % 4) == 0) && ((year % 100) != 0) ||
249 ((year % 400) == 0)) {
250
251 febMax = 29;
252 }
253
254 if ((day < 1) || (day > febMax)) {
255 return false;
256 }
257 }
258 else if ((day < 1) || (day > months[month])) {
259 return false;
260 }
261
262 return true;
263 }
264
265 public static boolean isHex(String s) {
266 if (isNull(s)) {
267 return false;
268 }
269
270 return true;
271 }
272
273 public static boolean isHTML(String s) {
274 if (isNull(s)) {
275 return false;
276 }
277
278 if (((s.indexOf("<html>") != -1) || (s.indexOf("<HTML>") != -1)) &&
279 ((s.indexOf("</html>") != -1) || (s.indexOf("</HTML>") != -1))) {
280
281 return true;
282 }
283
284 return false;
285 }
286
287 public static boolean isIPAddress(String ipAddress){
288 Matcher matcher = _ipAddressPattern.matcher(ipAddress);
289
290 return matcher.matches();
291 }
292
293 public static boolean isJulianDate(int month, int day, int year) {
294 if ((month < 0) || (month > 11)) {
295 return false;
296 }
297
298 int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
299
300 if (month == 1) {
301 int febMax = 28;
302
303 if ((year % 4) == 0) {
304 febMax = 29;
305 }
306
307 if ((day < 1) || (day > febMax)) {
308 return false;
309 }
310 }
311 else if ((day < 1) || (day > months[month])) {
312 return false;
313 }
314
315 return true;
316 }
317
318 public static boolean isLUHN(String number) {
319 if (number == null) {
320 return false;
321 }
322
323 number = StringUtil.reverse(number);
324
325 int total = 0;
326
327 for (int i = 0; i < number.length(); i++) {
328 int x = 0;
329
330 if (((i + 1) % 2) == 0) {
331 x = Integer.parseInt(number.substring(i, i + 1)) * 2;
332
333 if (x >= 10) {
334 String s = String.valueOf(x);
335
336 x = Integer.parseInt(s.substring(0, 1)) +
337 Integer.parseInt(s.substring(1, 2));
338 }
339 }
340 else {
341 x = Integer.parseInt(number.substring(i, i + 1));
342 }
343
344 total = total + x;
345 }
346
347 if ((total % 10) == 0) {
348 return true;
349 }
350 else {
351 return false;
352 }
353 }
354
355 public static boolean isName(String name) {
356 if (isNull(name)) {
357 return false;
358 }
359
360 for (char c : name.trim().toCharArray()) {
361 if (((!isChar(c)) &&
362 (!Character.isWhitespace(c))) || (c == CharPool.COMMA)) {
363
364 return false;
365 }
366 }
367
368 return true;
369 }
370
371 public static boolean isNotNull(Object obj) {
372 return !isNull(obj);
373 }
374
375 public static boolean isNotNull(Long l) {
376 return !isNull(l);
377 }
378
379 public static boolean isNotNull(String s) {
380 return !isNull(s);
381 }
382
383 public static boolean isNotNull(Object[] array) {
384 return !isNull(array);
385 }
386
387 public static boolean isNull(Object obj) {
388 if (obj instanceof Long) {
389 return isNull((Long)obj);
390 }
391 else if (obj instanceof String) {
392 return isNull((String)obj);
393 }
394 else if (obj == null) {
395 return true;
396 }
397 else {
398 return false;
399 }
400 }
401
402 public static boolean isNull(Long l) {
403 if ((l == null) || l.longValue() == 0) {
404 return true;
405 }
406 else {
407 return false;
408 }
409 }
410
411 public static boolean isNull(String s) {
412 if (s == null) {
413 return true;
414 }
415
416 int counter = 0;
417
418 for (int i = 0; i < s.length(); i++) {
419 char c = s.charAt(i);
420
421 if (c == CharPool.SPACE) {
422 continue;
423 }
424 else if (counter > 3) {
425 return false;
426 }
427
428 if (counter == 0) {
429 if (c != CharPool.LOWER_CASE_N) {
430 return false;
431 }
432 }
433 else if (counter == 1) {
434 if (c != CharPool.LOWER_CASE_U) {
435 return false;
436 }
437 }
438 else if ((counter == 2) || (counter == 3)) {
439 if (c != CharPool.LOWER_CASE_L) {
440 return false;
441 }
442 }
443
444 counter++;
445 }
446
447 return true;
448 }
449
450 public static boolean isNull(Object[] array) {
451 if ((array == null) || (array.length == 0)) {
452 return true;
453 }
454 else {
455 return false;
456 }
457 }
458
459 public static boolean isNumber(String number) {
460 if (isNull(number)) {
461 return false;
462 }
463
464 for (char c : number.toCharArray()) {
465 if (!isDigit(c)) {
466 return false;
467 }
468 }
469
470 return true;
471 }
472
473 public static boolean isPassword(String password) {
474 if (isNull(password)) {
475 return false;
476 }
477
478 if (password.length() < 4) {
479 return false;
480 }
481
482 for (char c : password.toCharArray()) {
483 if (!isChar(c) && !isDigit(c)) {
484 return false;
485 }
486 }
487
488 return true;
489 }
490
491 public static boolean isPhoneNumber(String phoneNumber) {
492 return isNumber(StringUtil.extractDigits(phoneNumber));
493 }
494
495 public static boolean isVariableTerm(String s) {
496 if (s.startsWith(_VARIABLE_TERM_BEGIN) &&
497 s.endsWith(_VARIABLE_TERM_END)) {
498
499 return true;
500 }
501 else {
502 return false;
503 }
504 }
505
506 public static boolean isWhitespace(char c) {
507 int i = (int)c;
508
509 if ((i == 0) || Character.isWhitespace(c)) {
510 return true;
511 }
512 else {
513 return false;
514 }
515 }
516
517 private static final int _CHAR_BEGIN = 65;
518
519 private static final int _CHAR_END = 122;
520
521 private static final int _DIGIT_BEGIN = 48;
522
523 private static final int _DIGIT_END = 57;
524
525 private static final char[] _EMAIL_ADDRESS_SPECIAL_CHAR = new char[] {
526 '.', '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^',
527 '_', '`', '{', '|', '}', '~'
528 };
529
530 private static final String _VARIABLE_TERM_BEGIN = "[$";
531
532 private static final String _VARIABLE_TERM_END = "$]";
533
534 private static Log _log = LogFactoryUtil.getLog(Validator.class);
535
536 private static Pattern _ipAddressPattern = Pattern.compile(
537 "\\b" +
538 "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
539 "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
540 "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
541 "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])" +
542 "\\b");
543
544 }