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