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