001    /**
002     * Copyright (c) 2000-2013 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 java.net.MalformedURLException;
018    import java.net.URI;
019    import java.net.URISyntaxException;
020    import java.net.URL;
021    
022    import java.util.Arrays;
023    import java.util.regex.Matcher;
024    import java.util.regex.Pattern;
025    
026    /**
027     * Provides utility methods related to data validation and format checking.
028     *
029     * @author Brian Wing Shun Chan
030     * @author Alysa Carver
031     */
032    public class Validator {
033    
034            /**
035             * Returns <code>true</code> if the booleans are equal.
036             *
037             * @param  boolean1 the first boolean
038             * @param  boolean2 the second boolean
039             * @return <code>true</code> if the booleans are equal; <code>false</code>
040             *         otherwise
041             */
042            public static boolean equals(boolean boolean1, boolean boolean2) {
043                    if (boolean1 == boolean2) {
044                            return true;
045                    }
046                    else {
047                            return false;
048                    }
049            }
050    
051            /**
052             * Returns <code>true</code> if the bytes are equal.
053             *
054             * @param  byte1 the first byte
055             * @param  byte2 the second byte
056             * @return <code>true</code> if the bytes are equal; <code>false</code>
057             *         otherwise
058             */
059            public static boolean equals(byte byte1, byte byte2) {
060                    if (byte1 == byte2) {
061                            return true;
062                    }
063                    else {
064                            return false;
065                    }
066            }
067    
068            /**
069             * Returns <code>true</code> if the characters are equal.
070             *
071             * @param  char1 the first character
072             * @param  char2 the second character
073             * @return <code>true</code> if the characters are equal; <code>false</code>
074             *         otherwise
075             */
076            public static boolean equals(char char1, char char2) {
077                    if (char1 == char2) {
078                            return true;
079                    }
080                    else {
081                            return false;
082                    }
083            }
084    
085            /**
086             * Returns <code>true</code> if the doubles are equal.
087             *
088             * @param  double1 the first double
089             * @param  double2 the second double
090             * @return <code>true</code> if the doubles are equal; <code>false</code>
091             *         otherwise
092             */
093            public static boolean equals(double double1, double double2) {
094                    if (Double.compare(double1, double2) == 0) {
095                            return true;
096                    }
097                    else {
098                            return false;
099                    }
100            }
101    
102            /**
103             * Returns <code>true</code> if the floats are equal.
104             *
105             * @param  float1 the first float
106             * @param  float2 the second float
107             * @return <code>true</code> if the floats are equal; <code>false</code>
108             *         otherwise
109             */
110            public static boolean equals(float float1, float float2) {
111                    if (Float.compare(float1, float2) == 0) {
112                            return true;
113                    }
114                    else {
115                            return false;
116                    }
117            }
118    
119            /**
120             * Returns <code>true</code> if the integers are equal.
121             *
122             * @param  int1 the first integer
123             * @param  int2 the second integer
124             * @return <code>true</code> if the integers are equal; <code>false</code>
125             *         otherwise
126             */
127            public static boolean equals(int int1, int int2) {
128                    if (int1 == int2) {
129                            return true;
130                    }
131                    else {
132                            return false;
133                    }
134            }
135    
136            /**
137             * Returns <code>true</code> if the long integers are equal.
138             *
139             * @param  long1 the first long integer
140             * @param  long2 the second long integer
141             * @return <code>true</code> if the long integers are equal;
142             *         <code>false</code> otherwise
143             */
144            public static boolean equals(long long1, long long2) {
145                    if (long1 == long2) {
146                            return true;
147                    }
148                    else {
149                            return false;
150                    }
151            }
152    
153            /**
154             * Returns <code>true</code> if the objects are either equal, the same
155             * instance, or both <code>null</code>.
156             *
157             * @param  obj1 the first object
158             * @param  obj2 the second object
159             * @return <code>true</code> if the objects are either equal, the same
160             *         instance, or both <code>null</code>; <code>false</code> otherwise
161             */
162            public static boolean equals(Object obj1, Object obj2) {
163                    if ((obj1 == null) && (obj2 == null)) {
164                            return true;
165                    }
166                    else if ((obj1 == null) || (obj2 == null)) {
167                            return false;
168                    }
169                    else {
170                            return obj1.equals(obj2);
171                    }
172            }
173    
174            /**
175             * Returns <code>true</code> if the short integers are equal.
176             *
177             * @param  short1 the first short integer
178             * @param  short2 the second short integer
179             * @return <code>true</code> if the short integers are equal;
180             *         <code>false</code> otherwise
181             */
182            public static boolean equals(short short1, short short2) {
183                    if (short1 == short2) {
184                            return true;
185                    }
186                    else {
187                            return false;
188                    }
189            }
190    
191            /**
192             * Returns <code>true</code> if the boolean arrays are equal.
193             *
194             * @param  booleanArray1 the first boolean array
195             * @param  booleanArray2 the second boolean array
196             * @return <code>true</code> if the booleans arrays are equal; <code>false
197             *         </code>otherwise
198             */
199            public static boolean equalsSorted(
200                    boolean[] booleanArray1, boolean[] booleanArray2) {
201    
202                    Boolean[] booleanObjArray1 = ArrayUtil.toArray(booleanArray1);
203    
204                    Arrays.sort(booleanObjArray1);
205    
206                    Boolean[] booleanObjArray2 = ArrayUtil.toArray(booleanArray2);
207    
208                    Arrays.sort(booleanObjArray2);
209    
210                    return Arrays.equals(booleanObjArray1, booleanObjArray2);
211            }
212    
213            /**
214             * Returns <code>true</code> if the byte arrays are equal.
215             *
216             * @param  byteArray1 the first byte array
217             * @param  byteArray2 the second byte array
218             * @return <code>true</code> if the byte arrays are equal; <code>false
219             *         </code>otherwise
220             */
221            public static boolean equalsSorted(byte[] byteArray1, byte[] byteArray2) {
222                    byteArray1 = ArrayUtil.clone(byteArray1);
223    
224                    Arrays.sort(byteArray1);
225    
226                    byteArray2 = ArrayUtil.clone(byteArray2);
227    
228                    Arrays.sort(byteArray2);
229    
230                    return Arrays.equals(byteArray1, byteArray2);
231            }
232    
233            /**
234             * Returns <code>true</code> if the char arrays are equal.
235             *
236             * @param  charArray1 the first char array
237             * @param  charArray2 the second char array
238             * @return <code>true</code> if the char arrays are equal; <code>false
239             *         </code>otherwise
240             */
241            public static boolean equalsSorted(char[] charArray1, char[] charArray2) {
242                    charArray1 = ArrayUtil.clone(charArray1);
243    
244                    Arrays.sort(charArray1);
245    
246                    charArray2 = ArrayUtil.clone(charArray2);
247    
248                    Arrays.sort(charArray2);
249    
250                    return Arrays.equals(charArray1, charArray2);
251            }
252    
253            /**
254             * Returns <code>true</code> if the double arrays are equal.
255             *
256             * @param  doubleArray1 the first double array
257             * @param  doubleArray2 the second double array
258             * @return <code>true</code> if the double arrays are equal; <code>false
259             *         </code>otherwise
260             */
261            public static boolean equalsSorted(
262                    double[] doubleArray1, double[] doubleArray2) {
263    
264                    doubleArray1 = ArrayUtil.clone(doubleArray1);
265    
266                    Arrays.sort(doubleArray1);
267    
268                    doubleArray2 = ArrayUtil.clone(doubleArray2);
269    
270                    Arrays.sort(doubleArray2);
271    
272                    return Arrays.equals(doubleArray1, doubleArray2);
273            }
274    
275            /**
276             * Returns <code>true</code> if the float arrays are equal.
277             *
278             * @param  floatArray1 the first float array
279             * @param  floatArray2 the second char array
280             * @return <code>true</code> if the float arrays are equal; <code>false
281             *         </code>otherwise
282             */
283            public static boolean equalsSorted(
284                    float[] floatArray1, float[] floatArray2) {
285    
286                    floatArray1 = ArrayUtil.clone(floatArray1);
287    
288                    Arrays.sort(floatArray1);
289    
290                    floatArray2 = ArrayUtil.clone(floatArray2);
291    
292                    Arrays.sort(floatArray2);
293    
294                    return Arrays.equals(floatArray1, floatArray2);
295            }
296    
297            /**
298             * Returns <code>true</code> if the int arrays are equal.
299             *
300             * @param  intArray1 the first int array
301             * @param  intArray2 the second int array
302             * @return <code>true</code> if the int arrays are equal; <code>false</code>
303             *         otherwise
304             */
305            public static boolean equalsSorted(int[] intArray1, int[] intArray2) {
306                    intArray1 = ArrayUtil.clone(intArray1);
307    
308                    Arrays.sort(intArray1);
309    
310                    intArray2 = ArrayUtil.clone(intArray2);
311    
312                    Arrays.sort(intArray2);
313    
314                    return Arrays.equals(intArray1, intArray2);
315            }
316    
317            /**
318             * Returns <code>true</code> if the long arrays are equal.
319             *
320             * @param  longArray1 the first long array
321             * @param  longArray2 the second long array
322             * @return <code>true</code> if the long arrays are equal; <code>false
323             *         </code>otherwise
324             */
325            public static boolean equalsSorted(long[] longArray1, long[] longArray2) {
326                    longArray1 = ArrayUtil.clone(longArray1);
327    
328                    Arrays.sort(longArray1);
329    
330                    longArray2 = ArrayUtil.clone(longArray2);
331    
332                    Arrays.sort(longArray2);
333    
334                    return Arrays.equals(longArray1, longArray2);
335            }
336    
337            /**
338             * Returns <code>true</code> if the object arrays are equal.
339             *
340             * @param  objArray1 the first object array
341             * @param  objArray2 the second object array
342             * @return <code>true</code> if the object arrays are equal; <code>false
343             *         </code>otherwise
344             */
345            public static boolean equalsSorted(Object[] objArray1, Object[] objArray2) {
346                    objArray1 = ArrayUtil.clone(objArray1);
347    
348                    Arrays.sort(objArray1);
349    
350                    objArray2 = ArrayUtil.clone(objArray2);
351    
352                    Arrays.sort(objArray2);
353    
354                    return Arrays.equals(objArray1, objArray2);
355            }
356    
357            /**
358             * Returns <code>true</code> if the short arrays are equal.
359             *
360             * @param  shortArray1 the first short array
361             * @param  shortArray2 the second short array
362             * @return <code>true</code> if the short arrays are equal; <code>false
363             *         </code>otherwise
364             */
365            public static boolean equalsSorted(
366                    short[] shortArray1, short[] shortArray2) {
367    
368                    shortArray1 = ArrayUtil.clone(shortArray1);
369    
370                    Arrays.sort(shortArray1);
371    
372                    shortArray2 = ArrayUtil.clone(shortArray2);
373    
374                    Arrays.sort(shortArray2);
375    
376                    return Arrays.equals(shortArray1, shortArray2);
377            }
378    
379            /**
380             * Returns <code>true</code> if the string is an email address. The only
381             * requirements are that the string consist of two parts separated by an @
382             * symbol, and that it contain no whitespace.
383             *
384             * @param  address the string to check
385             * @return <code>true</code> if the string is an email address;
386             *         <code>false</code> otherwise
387             */
388            public static boolean isAddress(String address) {
389                    if (isNull(address)) {
390                            return false;
391                    }
392    
393                    String[] tokens = address.split(StringPool.AT);
394    
395                    if (tokens.length != 2) {
396                            return false;
397                    }
398    
399                    for (String token : tokens) {
400                            for (char c : token.toCharArray()) {
401                                    if (Character.isWhitespace(c)) {
402                                            return false;
403                                    }
404                            }
405                    }
406    
407                    return true;
408            }
409    
410            /**
411             * Returns <code>true</code> if the string is an alphanumeric name, meaning
412             * it contains nothing but English letters, numbers, and spaces.
413             *
414             * @param  name the string to check
415             * @return <code>true</code> if the string is an Alphanumeric name;
416             *         <code>false</code> otherwise
417             */
418            public static boolean isAlphanumericName(String name) {
419                    if (isNull(name)) {
420                            return false;
421                    }
422    
423                    for (char c : name.trim().toCharArray()) {
424                            if (!isChar(c) && !isDigit(c) && !Character.isWhitespace(c)) {
425                                    return false;
426                            }
427                    }
428    
429                    return true;
430            }
431    
432            /**
433             * Returns <code>true</code> if the character is in the ASCII character set.
434             * This includes characters with integer values between 32 and 126
435             * (inclusive).
436             *
437             * @param  c the character to check
438             * @return <code>true</code> if the character is in the ASCII character set;
439             *         <code>false</code> otherwise
440             */
441            public static boolean isAscii(char c) {
442                    int i = c;
443    
444                    if ((i >= 32) && (i <= 126)) {
445                            return true;
446                    }
447                    else {
448                            return false;
449                    }
450            }
451    
452            public static boolean isBlank(String s) {
453                    if (s == null) {
454                            return true;
455                    }
456    
457                    if (s.length() == 0) {
458                            return true;
459                    }
460    
461                    return false;
462            }
463    
464            /**
465             * Returns <code>true</code> if the character is an upper or lower case
466             * English letter.
467             *
468             * @param  c the character to check
469             * @return <code>true</code> if the character is an upper or lower case
470             *         English letter; <code>false</code> otherwise
471             */
472            public static boolean isChar(char c) {
473                    int x = c;
474    
475                    if (((x >= _CHAR_LOWER_CASE_BEGIN) && (x <= _CHAR_LOWER_CASE_END)) ||
476                            ((x >= _CHAR_UPPER_CASE_BEGIN) && (x <= _CHAR_UPPER_CASE_END))) {
477    
478                            return true;
479                    }
480    
481                    return false;
482            }
483    
484            /**
485             * Returns <code>true</code> if string consists only of upper and lower case
486             * English letters.
487             *
488             * @param  s the string to check
489             * @return <code>true</code> if the string consists only of upper and lower
490             *         case English letters
491             */
492            public static boolean isChar(String s) {
493                    if (isNull(s)) {
494                            return false;
495                    }
496    
497                    for (char c : s.toCharArray()) {
498                            if (!isChar(c)) {
499                                    return false;
500                            }
501                    }
502    
503                    return true;
504            }
505    
506            /**
507             * Returns <code>true</code> if the date is valid in the Gregorian calendar.
508             *
509             * @param  month the month to check
510             * @param  day the day to check
511             * @return <code>true</code> if the date is valid in the Gregorian calendar;
512             *         <code>false</code> otherwise
513             */
514            public static boolean isDate(int month, int day, int year) {
515                    return isGregorianDate(month, day, year);
516            }
517    
518            /**
519             * Returns <code>true</code> if the character is a digit between 0 and 9
520             * (inclusive).
521             *
522             * @param  c the character to check
523             * @return <code>true</code> if the character is a digit between 0 and 9
524             *         (inclusive); <code>false</code> otherwise
525             */
526            public static boolean isDigit(char c) {
527                    int x = c;
528    
529                    if ((x >= _DIGIT_BEGIN) && (x <= _DIGIT_END)) {
530                            return true;
531                    }
532    
533                    return false;
534            }
535    
536            /**
537             * Returns <code>true</code> if the string consists of only digits between 0
538             * and 9 (inclusive).
539             *
540             * @param  s the string to check
541             * @return <code>true</code> if the string consists of only digits between 0
542             *         and 9 (inclusive); <code>false</code> otherwise
543             */
544            public static boolean isDigit(String s) {
545                    if (isNull(s)) {
546                            return false;
547                    }
548    
549                    for (char c : s.toCharArray()) {
550                            if (!isDigit(c)) {
551                                    return false;
552                            }
553                    }
554    
555                    return true;
556            }
557    
558            /**
559             * Returns <code>true</code> if the string is a valid domain name. See
560             * RFC-1034 (section 3), RFC-1123 (section 2.1), and RFC-952 (section B.
561             * Lexical grammar).
562             *
563             * @param  domainName the string to check
564             * @return <code>true</code> if the string is a valid domain name;
565             *         <code>false</code> otherwise
566             */
567            public static boolean isDomain(String domainName) {
568    
569                    // See RFC-1034 (section 3), RFC-1123 (section 2.1), and RFC-952
570                    // (section B. Lexical grammar)
571    
572                    if (isNull(domainName)) {
573                            return false;
574                    }
575    
576                    if (domainName.length() > 255) {
577                            return false;
578                    }
579    
580                    if (domainName.startsWith(StringPool.PERIOD)) {
581                            return false;
582                    }
583    
584                    String[] domainNameArray = StringUtil.split(
585                            domainName, CharPool.PERIOD);
586    
587                    for (String domainNamePart : domainNameArray) {
588                            char[] domainNamePartCharArray = domainNamePart.toCharArray();
589    
590                            for (int i = 0; i < domainNamePartCharArray.length; i++) {
591                                    char c = domainNamePartCharArray[i];
592    
593                                    if ((i == 0) && (c == CharPool.DASH)) {
594                                            return false;
595                                    }
596    
597                                    if ((i == (domainNamePartCharArray.length - 1)) &&
598                                            (c == CharPool.DASH)) {
599    
600                                            return false;
601                                    }
602    
603                                    if (!isChar(c) && !isDigit(c) && (c != CharPool.DASH)) {
604                                            return false;
605                                    }
606                            }
607                    }
608    
609                    return true;
610            }
611    
612            /**
613             * Returns <code>true</code> if the string is a valid email address.
614             *
615             * @param  emailAddress the string to check
616             * @return <code>true</code> if the string is a valid email address;
617             *         <code>false</code> otherwise
618             */
619            public static boolean isEmailAddress(String emailAddress) {
620                    Matcher matcher = _emailAddressPattern.matcher(emailAddress);
621    
622                    return matcher.matches();
623            }
624    
625            /**
626             * Returns <code>true</code> if the character is a special character in an
627             * email address.
628             *
629             * @param  c the character to check
630             * @return <code>true</code> if the character is a special character in an
631             *         email address; <code>false</code> otherwise
632             */
633            public static boolean isEmailAddressSpecialChar(char c) {
634    
635                    // LEP-1445
636    
637                    for (char specialChar : _EMAIL_ADDRESS_SPECIAL_CHAR) {
638                            if (c == specialChar) {
639                                    return true;
640                            }
641                    }
642    
643                    return false;
644            }
645    
646            /**
647             * Returns <code>true</code> if the file extension is valid.
648             *
649             * @param  fileExtension string to check
650             * @return <code>true</code> if the extension is valid; <code>false</code>
651             *         otherwise
652             */
653            public static boolean isFileExtension(String fileExtension) {
654                    if (isNull(fileExtension) ||
655                            fileExtension.contains(StringPool.BACK_SLASH) ||
656                            fileExtension.contains(StringPool.NULL_CHAR) ||
657                            fileExtension.contains(StringPool.SLASH)) {
658    
659                            return false;
660                    }
661    
662                    return true;
663            }
664    
665            public static boolean isFileName(String name) {
666                    if (isNull(name) || name.equals(StringPool.PERIOD) ||
667                            name.equals(StringPool.DOUBLE_PERIOD) ||
668                            name.contains(StringPool.BACK_SLASH) ||
669                            name.contains(StringPool.NULL_CHAR) ||
670                            name.contains(StringPool.SLASH)) {
671    
672                            return false;
673                    }
674    
675                    return true;
676            }
677    
678            public static boolean isFilePath(String path, boolean isParentDirAllowed) {
679                    if (isNull(path)) {
680                            return false;
681                    }
682    
683                    if (path.contains(StringPool.NULL_CHAR)) {
684                            return false;
685                    }
686    
687                    if (isParentDirAllowed) {
688                            return true;
689                    }
690    
691                    if (path.equals(StringPool.DOUBLE_PERIOD)) {
692                            return false;
693                    }
694    
695                    String normalizedPath = path.replace(
696                            CharPool.BACK_SLASH, CharPool.SLASH);
697    
698                    if (normalizedPath.startsWith(
699                                    StringPool.DOUBLE_PERIOD.concat(StringPool.SLASH))) {
700    
701                            return false;
702                    }
703    
704                    if (normalizedPath.endsWith(
705                                    StringPool.SLASH.concat(StringPool.DOUBLE_PERIOD))) {
706    
707                            return false;
708                    }
709    
710                    if (normalizedPath.contains(
711                                    StringPool.SLASH.concat(
712                                            StringPool.DOUBLE_PERIOD).concat(StringPool.SLASH))) {
713    
714                            return false;
715                    }
716    
717                    return true;
718            }
719    
720            /**
721             * Returns <code>true</code> if the date is valid in the Gregorian calendar.
722             *
723             * @param  month the month (0-based, meaning 0 for January)
724             * @param  day the day of the month
725             * @param  year the year
726             * @return <code>true</code> if the date is valid; <code>false</code>
727             *         otherwise
728             */
729            public static boolean isGregorianDate(int month, int day, int year) {
730                    if ((month < 0) || (month > 11)) {
731                            return false;
732                    }
733    
734                    int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
735    
736                    if (month == 1) {
737                            int febMax = 28;
738    
739                            if (((year % 4) == 0) && ((year % 100) != 0) ||
740                                    ((year % 400) == 0)) {
741    
742                                    febMax = 29;
743                            }
744    
745                            if ((day < 1) || (day > febMax)) {
746                                    return false;
747                            }
748                    }
749                    else if ((day < 1) || (day > months[month])) {
750                            return false;
751                    }
752    
753                    return true;
754            }
755    
756            /**
757             * Returns <code>true</code> if the string is a hexidecimal number. At
758             * present the only requirement is that the string is not <code>null</code>;
759             * it does not actually check the format of the string.
760             *
761             * @param  s the string to check
762             * @return <code>true</code> if the string is a hexidecimal number;
763             *         <code>false</code> otherwise
764             * @see    #isNull(String)
765             */
766            public static boolean isHex(String s) {
767                    if (isNull(s)) {
768                            return false;
769                    }
770    
771                    return true;
772            }
773    
774            /**
775             * Returns <code>true</code> if the string is a valid host name.
776             *
777             * @param  name the string to check
778             * @return <code>true</code> if the string is a valid host name;
779             *         <code>false</code> otherwise
780             */
781            public static boolean isHostName(String name) {
782                    if (isNull(name)) {
783                            return false;
784                    }
785    
786                    char[] nameCharArray = name.toCharArray();
787    
788                    if ((nameCharArray[0] == CharPool.DASH) ||
789                            (nameCharArray[0] == CharPool.PERIOD) ||
790                            (nameCharArray[nameCharArray.length - 1] == CharPool.DASH)) {
791    
792                            return false;
793                    }
794    
795                    for (char c : nameCharArray) {
796                            if (!isChar(c) && !isDigit(c) && (c != CharPool.CLOSE_BRACKET) &&
797                                    (c != CharPool.COLON) && (c != CharPool.DASH) &&
798                                    (c != CharPool.OPEN_BRACKET) && (c != CharPool.PERIOD)) {
799    
800                                    return false;
801                            }
802                    }
803    
804                    return true;
805            }
806    
807            /**
808             * Returns <code>true</code> if the string is an HTML document. The only
809             * requirement is that it contain the opening and closing html tags.
810             *
811             * @param  s the string to check
812             * @return <code>true</code> if the string is an HTML document;
813             *         <code>false</code> otherwise
814             */
815            public static boolean isHTML(String s) {
816                    if (isNull(s)) {
817                            return false;
818                    }
819    
820                    if ((s.contains("<html>") || s.contains("<HTML>")) &&
821                            (s.contains("</html>") || s.contains("</HTML>"))) {
822    
823                            return true;
824                    }
825    
826                    return false;
827            }
828    
829            /**
830             * Returns <code>true</code> if the string is a valid IPv4 or IPv6 IP
831             * address.
832             *
833             * @param  ipAddress the string to check
834             * @return <code>true</code> if the string is a valid IPv4 or IPv6 IP
835             *         address; <code>false</code> otherwise
836             */
837            public static boolean isIPAddress(String ipAddress) {
838                    if (isIPv4Address(ipAddress) || isIPv6Address(ipAddress)) {
839                            return true;
840                    }
841    
842                    return false;
843            }
844    
845            /**
846             * Returns <code>true</code> if the string is a valid IPv4 IP address.
847             *
848             * @param  ipAddress the string to check
849             * @return <code>true</code> if the string is a valid IPv4 IP address;
850             *         <code>false</code> otherwise
851             */
852            public static boolean isIPv4Address(String ipAddress) {
853                    Matcher matcher = _ipv4AddressPattern.matcher(ipAddress);
854    
855                    return matcher.matches();
856            }
857    
858            /**
859             * Returns <code>true</code> if the string is a valid IPv6 IP address.
860             *
861             * @param  ipAddress the string to check
862             * @return <code>true</code> if the string is a valid IPv6 IP address;
863             *         <code>false</code> otherwise
864             */
865            public static boolean isIPv6Address(String ipAddress) {
866                    if (isNull(ipAddress)) {
867                            return false;
868                    }
869    
870                    if (StringUtil.startsWith(ipAddress, CharPool.OPEN_BRACKET) &&
871                            StringUtil.endsWith(ipAddress, CharPool.CLOSE_BRACKET)) {
872    
873                            ipAddress = ipAddress.substring(1, ipAddress.length() - 1);
874                    }
875    
876                    Matcher matcher = _ipv6AddressPattern.matcher(ipAddress);
877    
878                    return matcher.matches();
879            }
880    
881            /**
882             * Returns <code>true</code> if the date is valid in the Julian calendar.
883             *
884             * @param  month the month (0-based, meaning 0 for January)
885             * @param  day the day of the month
886             * @param  year the year
887             * @return <code>true</code> if the date is valid in the Julian calendar;
888             *         <code>false</code> otherwise
889             */
890            public static boolean isJulianDate(int month, int day, int year) {
891                    if ((month < 0) || (month > 11)) {
892                            return false;
893                    }
894    
895                    int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
896    
897                    if (month == 1) {
898                            int febMax = 28;
899    
900                            if ((year % 4) == 0) {
901                                    febMax = 29;
902                            }
903    
904                            if ((day < 1) || (day > febMax)) {
905                                    return false;
906                            }
907                    }
908                    else if ((day < 1) || (day > months[month])) {
909                            return false;
910                    }
911    
912                    return true;
913            }
914    
915            /**
916             * Returns <code>true</code> if the string contains a valid number according
917             * to the Luhn algorithm, commonly used to validate credit card numbers.
918             *
919             * @param  number the string to check
920             * @return <code>true</code> if the string contains a valid number according
921             *         to the Luhn algorithm; <code>false</code> otherwise
922             */
923            public static boolean isLUHN(String number) {
924                    if (number == null) {
925                            return false;
926                    }
927    
928                    number = StringUtil.reverse(number);
929    
930                    int total = 0;
931    
932                    for (int i = 0; i < number.length(); i++) {
933                            int x = 0;
934    
935                            if (((i + 1) % 2) == 0) {
936                                    x = GetterUtil.getInteger(number.substring(i, i + 1)) * 2;
937    
938                                    if (x >= 10) {
939                                            String s = String.valueOf(x);
940    
941                                            x =
942                                                    GetterUtil.getInteger(s.substring(0, 1)) +
943                                                            GetterUtil.getInteger(s.substring(1, 2));
944                                    }
945                            }
946                            else {
947                                    x = GetterUtil.getInteger(number.substring(i, i + 1));
948                            }
949    
950                            total = total + x;
951                    }
952    
953                    if ((total % 10) == 0) {
954                            return true;
955                    }
956                    else {
957                            return false;
958                    }
959            }
960    
961            /**
962             * Returns <code>true</code> if the string is a name, meaning it contains
963             * nothing but English letters and spaces.
964             *
965             * @param  name the string to check
966             * @return <code>true</code> if the string is a name; <code>false</code>
967             *         otherwise
968             */
969            public static boolean isName(String name) {
970                    if (isNull(name)) {
971                            return false;
972                    }
973    
974                    for (char c : name.trim().toCharArray()) {
975                            if (!isChar(c) && !Character.isWhitespace(c)) {
976                                    return false;
977                            }
978                    }
979    
980                    return true;
981            }
982    
983            /**
984             * Returns <code>true</code> if the long number object is not
985             * <code>null</code>, meaning it is neither a <code>null</code> reference or
986             * zero.
987             *
988             * @param  l the long number object to check
989             * @return <code>true</code> if the long number object is not
990             *         <code>null</code>; <code>false</code> otherwise
991             */
992            public static boolean isNotNull(Long l) {
993                    return !isNull(l);
994            }
995    
996            /**
997             * Returns <code>true</code> if the object is not <code>null</code>, using
998             * the rules from {@link #isNotNull(Long)} or {@link #isNotNull(String)} if
999             * the object is one of these types.
1000             *
1001             * @param  obj the object to check
1002             * @return <code>true</code> if the object is not <code>null</code>;
1003             *         <code>false</code> otherwise
1004             */
1005            public static boolean isNotNull(Object obj) {
1006                    return !isNull(obj);
1007            }
1008    
1009            /**
1010             * Returns <code>true</code> if the array is not <code>null</code>, meaning
1011             * it is neither a <code>null</code> reference or empty.
1012             *
1013             * @param  array the array to check
1014             * @return <code>true</code> if the array is not <code>null</code>;
1015             *         <code>false</code> otherwise
1016             */
1017            public static boolean isNotNull(Object[] array) {
1018                    return !isNull(array);
1019            }
1020    
1021            /**
1022             * Returns <code>true</code> if the string is not <code>null</code>, meaning
1023             * it is not a <code>null</code> reference, nothing but spaces, or the
1024             * string "<code>null</code>".
1025             *
1026             * @param  s the string to check
1027             * @return <code>true</code> if the string is not <code>null</code>;
1028             *         <code>false</code> otherwise
1029             */
1030            public static boolean isNotNull(String s) {
1031                    return !isNull(s);
1032            }
1033    
1034            /**
1035             * Returns <code>true</code> if the long number object is <code>null</code>,
1036             * meaning it is either a <code>null</code> reference or zero.
1037             *
1038             * @param  l the long number object to check
1039             * @return <code>true</code> if the long number object is <code>null</code>;
1040             *         <code>false</code> otherwise
1041             */
1042            public static boolean isNull(Long l) {
1043                    if ((l == null) || (l.longValue() == 0)) {
1044                            return true;
1045                    }
1046                    else {
1047                            return false;
1048                    }
1049            }
1050    
1051            /**
1052             * Returns <code>true</code> if the object is <code>null</code>, using the
1053             * rules from {@link #isNull(Long)} or {@link #isNull(String)} if the object
1054             * is one of these types.
1055             *
1056             * @param  obj the object to check
1057             * @return <code>true</code> if the object is <code>null</code>;
1058             *         <code>false</code> otherwise
1059             */
1060            public static boolean isNull(Object obj) {
1061                    if (obj instanceof Long) {
1062                            return isNull((Long)obj);
1063                    }
1064                    else if (obj instanceof String) {
1065                            return isNull((String)obj);
1066                    }
1067                    else if (obj == null) {
1068                            return true;
1069                    }
1070                    else {
1071                            return false;
1072                    }
1073            }
1074    
1075            /**
1076             * Returns <code>true</code> if the array is <code>null</code>, meaning it
1077             * is either a <code>null</code> reference or empty.
1078             *
1079             * @param  array the array to check
1080             * @return <code>true</code> if the array is <code>null</code>;
1081             *         <code>false</code> otherwise
1082             */
1083            public static boolean isNull(Object[] array) {
1084                    if ((array == null) || (array.length == 0)) {
1085                            return true;
1086                    }
1087                    else {
1088                            return false;
1089                    }
1090            }
1091    
1092            /**
1093             * Returns <code>true</code> if the string is <code>null</code>, meaning it
1094             * is a <code>null</code> reference, nothing but spaces, or the string
1095             * "<code>null</code>".
1096             *
1097             * @param  s the string to check
1098             * @return <code>true</code> if the string is <code>null</code>;
1099             *         <code>false</code> otherwise
1100             */
1101            public static boolean isNull(String s) {
1102                    if (s == null) {
1103                            return true;
1104                    }
1105    
1106                    int counter = 0;
1107    
1108                    for (int i = 0; i < s.length(); i++) {
1109                            char c = s.charAt(i);
1110    
1111                            if (c == CharPool.SPACE) {
1112                                    continue;
1113                            }
1114                            else if (counter > 3) {
1115                                    return false;
1116                            }
1117    
1118                            if (counter == 0) {
1119                                    if (c != CharPool.LOWER_CASE_N) {
1120                                            return false;
1121                                    }
1122                            }
1123                            else if (counter == 1) {
1124                                    if (c != CharPool.LOWER_CASE_U) {
1125                                            return false;
1126                                    }
1127                            }
1128                            else if ((counter == 2) || (counter == 3)) {
1129                                    if (c != CharPool.LOWER_CASE_L) {
1130                                            return false;
1131                                    }
1132                            }
1133    
1134                            counter++;
1135                    }
1136    
1137                    if ((counter == 0) || (counter == 4)) {
1138                            return true;
1139                    }
1140    
1141                    return false;
1142            }
1143    
1144            /**
1145             * Returns <code>true</code> if the string is a decimal integer number,
1146             * meaning it contains nothing but decimal digits.
1147             *
1148             * @param  number the string to check
1149             * @return <code>true</code> if the string is a decimal integer number;
1150             *         <code>false</code> otherwise
1151             */
1152            public static boolean isNumber(String number) {
1153                    if (isNull(number)) {
1154                            return false;
1155                    }
1156    
1157                    for (char c : number.toCharArray()) {
1158                            if (!isDigit(c)) {
1159                                    return false;
1160                            }
1161                    }
1162    
1163                    return true;
1164            }
1165    
1166            /**
1167             * Returns <code>true</code> if the string is a valid password, meaning it
1168             * is at least four characters long and contains only letters and decimal
1169             * digits.
1170             *
1171             * @param  password the string to check
1172             * @return <code>true</code> if the string is a valid password;
1173             *         <code>false</code> otherwise
1174             */
1175            public static boolean isPassword(String password) {
1176                    if (isNull(password)) {
1177                            return false;
1178                    }
1179    
1180                    if (password.length() < 4) {
1181                            return false;
1182                    }
1183    
1184                    for (char c : password.toCharArray()) {
1185                            if (!isChar(c) && !isDigit(c)) {
1186                                    return false;
1187                            }
1188                    }
1189    
1190                    return true;
1191            }
1192    
1193            /**
1194             * Returns <code>true</code> if the string is a valid phone number. The only
1195             * requirement is that there are decimal digits in the string; length and
1196             * format are not checked.
1197             *
1198             * @param  phoneNumber the string to check
1199             * @return <code>true</code> if the string is a valid phone number;
1200             *         <code>false</code> otherwise
1201             */
1202            public static boolean isPhoneNumber(String phoneNumber) {
1203                    return isNumber(StringUtil.extractDigits(phoneNumber));
1204            }
1205    
1206            public static boolean isUri(String uri) {
1207                    if (isNotNull(uri)) {
1208                            try {
1209                                    new URI(uri);
1210    
1211                                    return true;
1212                            }
1213                            catch (URISyntaxException urise) {
1214                            }
1215                    }
1216    
1217                    return false;
1218            }
1219    
1220            /**
1221             * Returns <code>true</code> if the string is a valid URL based on the rules
1222             * in {@link java.net.URL}.
1223             *
1224             * @param  url the string to check
1225             * @return <code>true</code> if the string is a valid URL;
1226             *         <code>false</code> otherwise
1227             */
1228            public static boolean isUrl(String url) {
1229                    if (isNotNull(url)) {
1230                            if (url.indexOf(CharPool.COLON) == -1) {
1231                                    return false;
1232                            }
1233    
1234                            try {
1235                                    new URL(url);
1236    
1237                                    return true;
1238                            }
1239                            catch (MalformedURLException murle) {
1240                            }
1241                    }
1242    
1243                    return false;
1244            }
1245    
1246            /**
1247             * Returns <code>true</code> if the string is a valid variable name in Java.
1248             *
1249             * @param  variableName the string to check
1250             * @return <code>true</code> if the string is a valid variable name in Java;
1251             *         <code>false</code> otherwise
1252             */
1253            public static boolean isVariableName(String variableName) {
1254                    if (isNull(variableName)) {
1255                            return false;
1256                    }
1257    
1258                    Matcher matcher = _variableNamePattern.matcher(variableName);
1259    
1260                    if (matcher.matches()) {
1261                            return true;
1262                    }
1263                    else {
1264                            return false;
1265                    }
1266            }
1267    
1268            /**
1269             * Returns <code>true</code> if the string is a valid variable term, meaning
1270             * it begins with "[$" and ends with "$]".
1271             *
1272             * @param  s the string to check
1273             * @return <code>true</code> if the string is a valid variable term;
1274             *         <code>false</code> otherwise
1275             */
1276            public static boolean isVariableTerm(String s) {
1277                    if (s.startsWith(_VARIABLE_TERM_BEGIN) &&
1278                            s.endsWith(_VARIABLE_TERM_END)) {
1279    
1280                            return true;
1281                    }
1282                    else {
1283                            return false;
1284                    }
1285            }
1286    
1287            /**
1288             * Returns <code>true</code> if the character is whitespace, meaning it is
1289             * either the <code>null</code> character '0' or whitespace according to
1290             * {@link java.lang.Character#isWhitespace(char)}.
1291             *
1292             * @param  c the character to check
1293             * @return <code>true</code> if the character is whitespace;
1294             *         <code>false</code> otherwise
1295             */
1296            public static boolean isWhitespace(char c) {
1297                    int i = c;
1298    
1299                    if ((i == 0) || Character.isWhitespace(c)) {
1300                            return true;
1301                    }
1302                    else {
1303                            return false;
1304                    }
1305            }
1306    
1307            /**
1308             * Returns <code>true</code> if the string is an XML document. The only
1309             * requirement is that it contain either the xml start tag "<?xml" or the
1310             * empty document tag "<root />".
1311             *
1312             * @param  s the string to check
1313             * @return <code>true</code> if the string is an XML document;
1314             *         <code>false</code> otherwise
1315             */
1316            public static boolean isXml(String s) {
1317                    if (s.startsWith(_XML_BEGIN) || s.startsWith(_XML_EMPTY)) {
1318                            return true;
1319                    }
1320                    else {
1321                            return false;
1322                    }
1323            }
1324    
1325            private static final int _CHAR_LOWER_CASE_BEGIN = 97;
1326    
1327            private static final int _CHAR_LOWER_CASE_END = 122;
1328    
1329            private static final int _CHAR_UPPER_CASE_BEGIN = 65;
1330    
1331            private static final int _CHAR_UPPER_CASE_END = 90;
1332    
1333            private static final int _DIGIT_BEGIN = 48;
1334    
1335            private static final int _DIGIT_END = 57;
1336    
1337            private static final char[] _EMAIL_ADDRESS_SPECIAL_CHAR = new char[] {
1338                    '.', '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^',
1339                    '_', '`', '{', '|', '}', '~'
1340            };
1341    
1342            private static final String _VARIABLE_TERM_BEGIN = "[$";
1343    
1344            private static final String _VARIABLE_TERM_END = "$]";
1345    
1346            private static final String _XML_BEGIN = "<?xml";
1347    
1348            private static final String _XML_EMPTY = "<root />";
1349    
1350            private static Pattern _emailAddressPattern = Pattern.compile(
1351                    "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@" +
1352                    "(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?");
1353            private static Pattern _ipv4AddressPattern = Pattern.compile(
1354                    "^" +
1355                    "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
1356                    "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
1357                    "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
1358                    "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" +
1359                    "$");
1360            private static Pattern _ipv6AddressPattern = Pattern.compile(
1361                    "^" +
1362                    "\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|" +
1363                    "(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|" +
1364                    "((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" +
1365                    "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|" +
1366                    "(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:" +
1367                    "((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" +
1368                    "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|" +
1369                    "(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|" +
1370                    "((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" +
1371                    "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|" +
1372                    "(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|" +
1373                    "((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" +
1374                    "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|" +
1375                    "(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|" +
1376                    "((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" +
1377                    "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|" +
1378                    "(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|" +
1379                    "((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" +
1380                    "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|" +
1381                    "(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:" +
1382                    "((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\." +
1383                    "(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*" +
1384                    "$");
1385            private static Pattern _variableNamePattern = Pattern.compile(
1386                    "[_a-zA-Z]+[_a-zA-Z0-9]*");
1387    
1388    }