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