001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.net.MalformedURLException;
021    import java.net.URL;
022    
023    import java.util.regex.Matcher;
024    import java.util.regex.Pattern;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author Alysa Carver
029     */
030    public class Validator {
031    
032            public static boolean equals(boolean boolean1, boolean boolean2) {
033                    if (boolean1 == boolean2) {
034                            return true;
035                    }
036                    else {
037                            return false;
038                    }
039            }
040    
041            public static boolean equals(byte byte1, byte byte2) {
042                    if (byte1 == byte2) {
043                            return true;
044                    }
045                    else {
046                            return false;
047                    }
048            }
049    
050            public static boolean equals(char char1, char char2) {
051                    if (char1 == char2) {
052                            return true;
053                    }
054                    else {
055                            return false;
056                    }
057            }
058    
059            public static boolean equals(double double1, double double2) {
060                    if (Double.compare(double1, double2) == 0) {
061                            return true;
062                    }
063                    else {
064                            return false;
065                    }
066            }
067    
068            public static boolean equals(float float1, float float2) {
069                    if (Float.compare(float1, float2) == 0) {
070                            return true;
071                    }
072                    else {
073                            return false;
074                    }
075            }
076    
077            public static boolean equals(int int1, int int2) {
078                    if (int1 == int2) {
079                            return true;
080                    }
081                    else {
082                            return false;
083                    }
084            }
085    
086            public static boolean equals(long long1, long long2) {
087                    if (long1 == long2) {
088                            return true;
089                    }
090                    else {
091                            return false;
092                    }
093            }
094    
095            public static boolean equals(Object obj1, Object obj2) {
096                    if ((obj1 == null) && (obj2 == null)) {
097                            return true;
098                    }
099                    else if ((obj1 == null) || (obj2 == null)) {
100                            return false;
101                    }
102                    else {
103                            return obj1.equals(obj2);
104                    }
105            }
106    
107            public static boolean equals(short short1, short short2) {
108                    if (short1 == short2) {
109                            return true;
110                    }
111                    else {
112                            return false;
113                    }
114            }
115    
116            public static boolean isAddress(String address) {
117                    if (isNull(address)) {
118                            return false;
119                    }
120    
121                    String[] tokens = address.split(StringPool.AT);
122    
123                    if (tokens.length != 2) {
124                            return false;
125                    }
126    
127                    for (String token : tokens) {
128                            for (char c : token.toCharArray()) {
129                                    if (Character.isWhitespace(c)) {
130                                            return false;
131                                    }
132                            }
133                    }
134    
135                    return true;
136            }
137    
138            public static boolean isAscii(char c) {
139                    int i = c;
140    
141                    if ((i >= 32) && (i <= 126)) {
142                            return true;
143                    }
144                    else {
145                            return false;
146                    }
147            }
148    
149            /**
150             * Returns <code>true</code> if c is a letter between a-z and A-Z.
151             *
152             * @return <code>true</code> if c is a letter between a-z and A-Z
153             */
154            public static boolean isChar(char c) {
155                    int x = c;
156    
157                    if ((x >= _CHAR_BEGIN) && (x <= _CHAR_END)) {
158                            return true;
159                    }
160    
161                    return false;
162            }
163    
164            /**
165             * Returns <code>true</code> if s is a string of letters that are between
166             * a-z and A-Z.
167             *
168             * @return <code>true</code> if s is a string of letters that are between
169             *                 a-z and A-Z
170             */
171            public static boolean isChar(String s) {
172                    if (isNull(s)) {
173                            return false;
174                    }
175    
176                    for (char c : s.toCharArray()) {
177                            if (!isChar(c)) {
178                                    return false;
179                            }
180                    }
181    
182                    return true;
183            }
184    
185            public static boolean isDate(int month, int day, int year) {
186                    return isGregorianDate(month, day, year);
187            }
188    
189            /**
190             * Returns <code>true</code> if c is a digit between 0 and 9.
191             *
192             * @return <code>true</code> if c is a digit between 0 and 9
193             */
194            public static boolean isDigit(char c) {
195                    int x = c;
196    
197                    if ((x >= _DIGIT_BEGIN) && (x <= _DIGIT_END)) {
198                            return true;
199                    }
200    
201                    return false;
202            }
203    
204            /**
205             * Returns <code>true</code> if s is a string of letters that are between 0
206             * and 9.
207             *
208             * @return <code>true</code> if s is a string of letters that are between 0
209             *                 and 9
210             */
211            public static boolean isDigit(String s) {
212                    if (isNull(s)) {
213                            return false;
214                    }
215    
216                    for (char c : s.toCharArray()) {
217                            if (!isDigit(c)) {
218                                    return false;
219                            }
220                    }
221    
222                    return true;
223            }
224    
225            public static boolean isDomain(String domainName) {
226    
227                    // See RFC-1034 (section 3), RFC-1123 (section 2.1), and RFC-952
228                    // (section B. Lexical grammar)
229    
230                    if (isNull(domainName)) {
231                            return false;
232                    }
233    
234                    if (domainName.length() > 255) {
235                            return false;
236                    }
237    
238                    String[] domainNameArray = StringUtil.split(
239                            domainName, StringPool.PERIOD);
240    
241                    for (String domainNamePart : domainNameArray) {
242                            char[] domainNamePartCharArray = domainNamePart.toCharArray();
243    
244                            for (int i = 0; i < domainNamePartCharArray.length; i++) {
245                                    char c = domainNamePartCharArray[i];
246    
247                                    if ((i == 0) && (c == CharPool.DASH)) {
248                                            return false;
249                                    }
250                                    else if ((i == (domainNamePartCharArray.length - 1)) &&
251                                                     (c == CharPool.DASH)) {
252    
253                                            return false;
254                                    }
255                                    else if ((!isChar(c)) && (!isDigit(c)) &&
256                                                     (c != CharPool.DASH)) {
257    
258                                            return false;
259                                    }
260                            }
261                    }
262    
263                    return true;
264            }
265    
266            public static boolean isEmailAddress(String emailAddress) {
267                    Boolean valid = null;
268    
269                    try {
270                            valid = (Boolean)PortalClassInvoker.invoke(
271                                    "com.liferay.util.mail.InternetAddressUtil", "isValid",
272                                    emailAddress);
273                    }
274                    catch (Exception e) {
275                            if (_log.isWarnEnabled()) {
276                                    _log.warn(e);
277                            }
278                    }
279    
280                    if (valid == null) {
281                            return false;
282                    }
283                    else {
284                            return valid.booleanValue();
285                    }
286            }
287    
288            public static boolean isEmailAddressSpecialChar(char c) {
289    
290                    // LEP-1445
291    
292                    for (int i = 0; i < _EMAIL_ADDRESS_SPECIAL_CHAR.length; i++) {
293                            if (c == _EMAIL_ADDRESS_SPECIAL_CHAR[i]) {
294                                    return true;
295                            }
296                    }
297    
298                    return false;
299            }
300    
301            public static boolean isGregorianDate(int month, int day, int year) {
302                    if ((month < 0) || (month > 11)) {
303                            return false;
304                    }
305    
306                    int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
307    
308                    if (month == 1) {
309                            int febMax = 28;
310    
311                            if (((year % 4) == 0) && ((year % 100) != 0) ||
312                                    ((year % 400) == 0)) {
313    
314                                    febMax = 29;
315                            }
316    
317                            if ((day < 1) || (day > febMax)) {
318                                    return false;
319                            }
320                    }
321                    else if ((day < 1) || (day > months[month])) {
322                            return false;
323                    }
324    
325                    return true;
326            }
327    
328            public static boolean isHex(String s) {
329                    if (isNull(s)) {
330                            return false;
331                    }
332    
333                    return true;
334            }
335    
336            public static boolean isHTML(String s) {
337                    if (isNull(s)) {
338                            return false;
339                    }
340    
341                    if (((s.indexOf("<html>") != -1) || (s.indexOf("<HTML>") != -1)) &&
342                            ((s.indexOf("</html>") != -1) || (s.indexOf("</HTML>") != -1))) {
343    
344                            return true;
345                    }
346    
347                    return false;
348            }
349    
350            public static boolean isIPAddress(String ipAddress) {
351                    Matcher matcher = _ipAddressPattern.matcher(ipAddress);
352    
353                    return matcher.matches();
354            }
355    
356            public static boolean isJulianDate(int month, int day, int year) {
357                    if ((month < 0) || (month > 11)) {
358                            return false;
359                    }
360    
361                    int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
362    
363                    if (month == 1) {
364                            int febMax = 28;
365    
366                            if ((year % 4) == 0) {
367                                    febMax = 29;
368                            }
369    
370                            if ((day < 1) || (day > febMax)) {
371                                    return false;
372                            }
373                    }
374                    else if ((day < 1) || (day > months[month])) {
375                            return false;
376                    }
377    
378                    return true;
379            }
380    
381            public static boolean isLUHN(String number) {
382                    if (number == null) {
383                            return false;
384                    }
385    
386                    number = StringUtil.reverse(number);
387    
388                    int total = 0;
389    
390                    for (int i = 0; i < number.length(); i++) {
391                            int x = 0;
392    
393                            if (((i + 1) % 2) == 0) {
394                                    x = Integer.parseInt(number.substring(i, i + 1)) * 2;
395    
396                                    if (x >= 10) {
397                                            String s = String.valueOf(x);
398    
399                                            x = Integer.parseInt(s.substring(0, 1)) +
400                                                    Integer.parseInt(s.substring(1, 2));
401                                    }
402                            }
403                            else {
404                                    x = Integer.parseInt(number.substring(i, i + 1));
405                            }
406    
407                            total = total + x;
408                    }
409    
410                    if ((total % 10) == 0) {
411                            return true;
412                    }
413                    else {
414                            return false;
415                    }
416            }
417    
418            public static boolean isName(String name) {
419                    if (isNull(name)) {
420                            return false;
421                    }
422    
423                    for (char c : name.trim().toCharArray()) {
424                            if (((!isChar(c)) &&
425                                    (!Character.isWhitespace(c))) || (c == CharPool.COMMA)) {
426    
427                                    return false;
428                            }
429                    }
430    
431                    return true;
432            }
433    
434            public static boolean isNotNull(Long l) {
435                    return !isNull(l);
436            }
437    
438            public static boolean isNotNull(Object obj) {
439                    return !isNull(obj);
440            }
441    
442            public static boolean isNotNull(Object[] array) {
443                    return !isNull(array);
444            }
445    
446            public static boolean isNotNull(String s) {
447                    return !isNull(s);
448            }
449    
450            public static boolean isNull(Long l) {
451                    if ((l == null) || (l.longValue() == 0)) {
452                            return true;
453                    }
454                    else {
455                            return false;
456                    }
457            }
458    
459            public static boolean isNull(Object obj) {
460                    if (obj instanceof Long) {
461                            return isNull((Long)obj);
462                    }
463                    else if (obj instanceof String) {
464                            return isNull((String)obj);
465                    }
466                    else if (obj == null) {
467                            return true;
468                    }
469                    else {
470                            return false;
471                    }
472            }
473    
474            public static boolean isNull(Object[] array) {
475                    if ((array == null) || (array.length == 0)) {
476                            return true;
477                    }
478                    else {
479                            return false;
480                    }
481            }
482    
483            public static boolean isNull(String s) {
484                    if (s == null) {
485                            return true;
486                    }
487    
488                    int counter = 0;
489    
490                    for (int i = 0; i < s.length(); i++) {
491                            char c = s.charAt(i);
492    
493                            if (c == CharPool.SPACE) {
494                                    continue;
495                            }
496                            else if (counter > 3) {
497                                    return false;
498                            }
499    
500                            if (counter == 0) {
501                                    if (c != CharPool.LOWER_CASE_N) {
502                                            return false;
503                                    }
504                            }
505                            else if (counter == 1) {
506                                    if (c != CharPool.LOWER_CASE_U) {
507                                            return false;
508                                    }
509                            }
510                            else if ((counter == 2) || (counter == 3)) {
511                                    if (c != CharPool.LOWER_CASE_L) {
512                                            return false;
513                                    }
514                            }
515    
516                            counter++;
517                    }
518    
519                    if ((counter == 0) || (counter == 4)) {
520                            return true;
521                    }
522    
523                    return false;
524            }
525    
526            public static boolean isNumber(String number) {
527                    if (isNull(number)) {
528                            return false;
529                    }
530    
531                    for (char c : number.toCharArray()) {
532                            if (!isDigit(c)) {
533                                    return false;
534                            }
535                    }
536    
537                    return true;
538            }
539    
540            public static boolean isPassword(String password) {
541                    if (isNull(password)) {
542                            return false;
543                    }
544    
545                    if (password.length() < 4) {
546                            return false;
547                    }
548    
549                    for (char c : password.toCharArray()) {
550                            if (!isChar(c) && !isDigit(c)) {
551                                    return false;
552                            }
553                    }
554    
555                    return true;
556            }
557    
558            public static boolean isPhoneNumber(String phoneNumber) {
559                    return isNumber(StringUtil.extractDigits(phoneNumber));
560            }
561    
562            public static boolean isUrl(String url) {
563                    if (Validator.isNotNull(url)) {
564                            try {
565                                    new URL(url);
566    
567                                    return true;
568                            }
569                            catch (MalformedURLException murle) {
570                            }
571                    }
572    
573                    return false;
574            }
575    
576            public static boolean isVariableName(String variableName) {
577                    if (isNull(variableName)) {
578                            return false;
579                    }
580    
581                    Matcher matcher = _variableNamePattern.matcher(variableName);
582    
583                    if (matcher.matches()) {
584                            return true;
585                    }
586                    else {
587                            return false;
588                    }
589            }
590    
591            public static boolean isVariableTerm(String s) {
592                    if (s.startsWith(_VARIABLE_TERM_BEGIN) &&
593                            s.endsWith(_VARIABLE_TERM_END)) {
594    
595                            return true;
596                    }
597                    else {
598                            return false;
599                    }
600            }
601    
602            public static boolean isWhitespace(char c) {
603                    int i = c;
604    
605                    if ((i == 0) || Character.isWhitespace(c)) {
606                            return true;
607                    }
608                    else {
609                            return false;
610                    }
611            }
612    
613            public static boolean isXml(String s) {
614                    if (s.startsWith(_XML_BEGIN) || s.startsWith(_XML_EMPTY)) {
615                            return true;
616                    }
617                    else {
618                            return false;
619                    }
620            }
621    
622            private static final int _CHAR_BEGIN = 65;
623    
624            private static final int _CHAR_END = 122;
625    
626            private static final int _DIGIT_BEGIN = 48;
627    
628            private static final int _DIGIT_END = 57;
629    
630            private static final char[] _EMAIL_ADDRESS_SPECIAL_CHAR = new char[] {
631                    '.', '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^',
632                    '_', '`', '{', '|', '}', '~'
633            };
634    
635            private static final String _VARIABLE_TERM_BEGIN = "[$";
636    
637            private static final String _VARIABLE_TERM_END = "$]";
638    
639            private static final String _XML_BEGIN = "<?xml";
640    
641            private static final String _XML_EMPTY = "<root />";
642    
643            private static Log _log = LogFactoryUtil.getLog(Validator.class);
644    
645            private static Pattern _ipAddressPattern = Pattern.compile(
646                    "\\b" +
647                    "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
648                    "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
649                    "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
650                    "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])" +
651                    "\\b");
652    
653            private static Pattern _variableNamePattern = Pattern.compile(
654                    "[_a-zA-Z]+[_a-zA-Z0-9]*");
655    
656    }