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