001    /**
002     * Copyright (c) 2000-2011 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                                    true, isValidMethodKey, emailAddress);
272                    }
273                    catch (Exception e) {
274                            if (_log.isWarnEnabled()) {
275                                    _log.warn(e);
276                            }
277                    }
278    
279                    if (valid == null) {
280                            return false;
281                    }
282                    else {
283                            return valid.booleanValue();
284                    }
285            }
286    
287            public static boolean isEmailAddressSpecialChar(char c) {
288    
289                    // LEP-1445
290    
291                    for (int i = 0; i < _EMAIL_ADDRESS_SPECIAL_CHAR.length; i++) {
292                            if (c == _EMAIL_ADDRESS_SPECIAL_CHAR[i]) {
293                                    return true;
294                            }
295                    }
296    
297                    return false;
298            }
299    
300            public static boolean isGregorianDate(int month, int day, int year) {
301                    if ((month < 0) || (month > 11)) {
302                            return false;
303                    }
304    
305                    int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
306    
307                    if (month == 1) {
308                            int febMax = 28;
309    
310                            if (((year % 4) == 0) && ((year % 100) != 0) ||
311                                    ((year % 400) == 0)) {
312    
313                                    febMax = 29;
314                            }
315    
316                            if ((day < 1) || (day > febMax)) {
317                                    return false;
318                            }
319                    }
320                    else if ((day < 1) || (day > months[month])) {
321                            return false;
322                    }
323    
324                    return true;
325            }
326    
327            public static boolean isHex(String s) {
328                    if (isNull(s)) {
329                            return false;
330                    }
331    
332                    return true;
333            }
334    
335            public static boolean isHTML(String s) {
336                    if (isNull(s)) {
337                            return false;
338                    }
339    
340                    if (((s.indexOf("<html>") != -1) || (s.indexOf("<HTML>") != -1)) &&
341                            ((s.indexOf("</html>") != -1) || (s.indexOf("</HTML>") != -1))) {
342    
343                            return true;
344                    }
345    
346                    return false;
347            }
348    
349            public static boolean isIPAddress(String ipAddress) {
350                    Matcher matcher = _ipAddressPattern.matcher(ipAddress);
351    
352                    return matcher.matches();
353            }
354    
355            public static boolean isJulianDate(int month, int day, int year) {
356                    if ((month < 0) || (month > 11)) {
357                            return false;
358                    }
359    
360                    int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
361    
362                    if (month == 1) {
363                            int febMax = 28;
364    
365                            if ((year % 4) == 0) {
366                                    febMax = 29;
367                            }
368    
369                            if ((day < 1) || (day > febMax)) {
370                                    return false;
371                            }
372                    }
373                    else if ((day < 1) || (day > months[month])) {
374                            return false;
375                    }
376    
377                    return true;
378            }
379    
380            public static boolean isLUHN(String number) {
381                    if (number == null) {
382                            return false;
383                    }
384    
385                    number = StringUtil.reverse(number);
386    
387                    int total = 0;
388    
389                    for (int i = 0; i < number.length(); i++) {
390                            int x = 0;
391    
392                            if (((i + 1) % 2) == 0) {
393                                    x = Integer.parseInt(number.substring(i, i + 1)) * 2;
394    
395                                    if (x >= 10) {
396                                            String s = String.valueOf(x);
397    
398                                            x = Integer.parseInt(s.substring(0, 1)) +
399                                                    Integer.parseInt(s.substring(1, 2));
400                                    }
401                            }
402                            else {
403                                    x = Integer.parseInt(number.substring(i, i + 1));
404                            }
405    
406                            total = total + x;
407                    }
408    
409                    if ((total % 10) == 0) {
410                            return true;
411                    }
412                    else {
413                            return false;
414                    }
415            }
416    
417            public static boolean isName(String name) {
418                    if (isNull(name)) {
419                            return false;
420                    }
421    
422                    for (char c : name.trim().toCharArray()) {
423                            if (((!isChar(c)) &&
424                                    (!Character.isWhitespace(c))) || (c == CharPool.COMMA)) {
425    
426                                    return false;
427                            }
428                    }
429    
430                    return true;
431            }
432    
433            public static boolean isNotNull(Long l) {
434                    return !isNull(l);
435            }
436    
437            public static boolean isNotNull(Object obj) {
438                    return !isNull(obj);
439            }
440    
441            public static boolean isNotNull(Object[] array) {
442                    return !isNull(array);
443            }
444    
445            public static boolean isNotNull(String s) {
446                    return !isNull(s);
447            }
448    
449            public static boolean isNull(Long l) {
450                    if ((l == null) || (l.longValue() == 0)) {
451                            return true;
452                    }
453                    else {
454                            return false;
455                    }
456            }
457    
458            public static boolean isNull(Object obj) {
459                    if (obj instanceof Long) {
460                            return isNull((Long)obj);
461                    }
462                    else if (obj instanceof String) {
463                            return isNull((String)obj);
464                    }
465                    else if (obj == null) {
466                            return true;
467                    }
468                    else {
469                            return false;
470                    }
471            }
472    
473            public static boolean isNull(Object[] array) {
474                    if ((array == null) || (array.length == 0)) {
475                            return true;
476                    }
477                    else {
478                            return false;
479                    }
480            }
481    
482            public static boolean isNull(String s) {
483                    if (s == null) {
484                            return true;
485                    }
486    
487                    int counter = 0;
488    
489                    for (int i = 0; i < s.length(); i++) {
490                            char c = s.charAt(i);
491    
492                            if (c == CharPool.SPACE) {
493                                    continue;
494                            }
495                            else if (counter > 3) {
496                                    return false;
497                            }
498    
499                            if (counter == 0) {
500                                    if (c != CharPool.LOWER_CASE_N) {
501                                            return false;
502                                    }
503                            }
504                            else if (counter == 1) {
505                                    if (c != CharPool.LOWER_CASE_U) {
506                                            return false;
507                                    }
508                            }
509                            else if ((counter == 2) || (counter == 3)) {
510                                    if (c != CharPool.LOWER_CASE_L) {
511                                            return false;
512                                    }
513                            }
514    
515                            counter++;
516                    }
517    
518                    if ((counter == 0) || (counter == 4)) {
519                            return true;
520                    }
521    
522                    return false;
523            }
524    
525            public static boolean isNumber(String number) {
526                    if (isNull(number)) {
527                            return false;
528                    }
529    
530                    for (char c : number.toCharArray()) {
531                            if (!isDigit(c)) {
532                                    return false;
533                            }
534                    }
535    
536                    return true;
537            }
538    
539            public static boolean isPassword(String password) {
540                    if (isNull(password)) {
541                            return false;
542                    }
543    
544                    if (password.length() < 4) {
545                            return false;
546                    }
547    
548                    for (char c : password.toCharArray()) {
549                            if (!isChar(c) && !isDigit(c)) {
550                                    return false;
551                            }
552                    }
553    
554                    return true;
555            }
556    
557            public static boolean isPhoneNumber(String phoneNumber) {
558                    return isNumber(StringUtil.extractDigits(phoneNumber));
559            }
560    
561            public static boolean isUrl(String url) {
562                    if (Validator.isNotNull(url)) {
563                            try {
564                                    new URL(url);
565    
566                                    return true;
567                            }
568                            catch (MalformedURLException murle) {
569                            }
570                    }
571    
572                    return false;
573            }
574    
575            public static boolean isVariableName(String variableName) {
576                    if (isNull(variableName)) {
577                            return false;
578                    }
579    
580                    Matcher matcher = _variableNamePattern.matcher(variableName);
581    
582                    if (matcher.matches()) {
583                            return true;
584                    }
585                    else {
586                            return false;
587                    }
588            }
589    
590            public static boolean isVariableTerm(String s) {
591                    if (s.startsWith(_VARIABLE_TERM_BEGIN) &&
592                            s.endsWith(_VARIABLE_TERM_END)) {
593    
594                            return true;
595                    }
596                    else {
597                            return false;
598                    }
599            }
600    
601            public static boolean isWhitespace(char c) {
602                    int i = c;
603    
604                    if ((i == 0) || Character.isWhitespace(c)) {
605                            return true;
606                    }
607                    else {
608                            return false;
609                    }
610            }
611    
612            public static boolean isXml(String s) {
613                    if (s.startsWith(_XML_BEGIN) || s.startsWith(_XML_EMPTY)) {
614                            return true;
615                    }
616                    else {
617                            return false;
618                    }
619            }
620    
621            private static final int _CHAR_BEGIN = 65;
622    
623            private static final int _CHAR_END = 122;
624    
625            private static final int _DIGIT_BEGIN = 48;
626    
627            private static final int _DIGIT_END = 57;
628    
629            private static final char[] _EMAIL_ADDRESS_SPECIAL_CHAR = new char[] {
630                    '.', '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^',
631                    '_', '`', '{', '|', '}', '~'
632            };
633    
634            private static final String _VARIABLE_TERM_BEGIN = "[$";
635    
636            private static final String _VARIABLE_TERM_END = "$]";
637    
638            private static final String _XML_BEGIN = "<?xml";
639    
640            private static final String _XML_EMPTY = "<root />";
641    
642            private static Log _log = LogFactoryUtil.getLog(Validator.class);
643    
644            private static Pattern _ipAddressPattern = Pattern.compile(
645                    "\\b" +
646                    "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
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                    "\\b");
651            private static MethodKey isValidMethodKey = new MethodKey(
652                    "com.liferay.util.mail.InternetAddressUtil", "isValid", String.class);
653            private static Pattern _variableNamePattern = Pattern.compile(
654                    "[_a-zA-Z]+[_a-zA-Z0-9]*");
655    
656    }