001
014
015 package com.liferay.portal.kernel.util;
016
017 import java.net.MalformedURLException;
018 import java.net.URL;
019
020 import java.util.Arrays;
021 import java.util.regex.Matcher;
022 import java.util.regex.Pattern;
023
024
030 public class Validator {
031
032
040 public static boolean equals(boolean boolean1, boolean boolean2) {
041 if (boolean1 == boolean2) {
042 return true;
043 }
044 else {
045 return false;
046 }
047 }
048
049
057 public static boolean equals(byte byte1, byte byte2) {
058 if (byte1 == byte2) {
059 return true;
060 }
061 else {
062 return false;
063 }
064 }
065
066
074 public static boolean equals(char char1, char char2) {
075 if (char1 == char2) {
076 return true;
077 }
078 else {
079 return false;
080 }
081 }
082
083
091 public static boolean equals(double double1, double double2) {
092 if (Double.compare(double1, double2) == 0) {
093 return true;
094 }
095 else {
096 return false;
097 }
098 }
099
100
108 public static boolean equals(float float1, float float2) {
109 if (Float.compare(float1, float2) == 0) {
110 return true;
111 }
112 else {
113 return false;
114 }
115 }
116
117
125 public static boolean equals(int int1, int int2) {
126 if (int1 == int2) {
127 return true;
128 }
129 else {
130 return false;
131 }
132 }
133
134
142 public static boolean equals(long long1, long long2) {
143 if (long1 == long2) {
144 return true;
145 }
146 else {
147 return false;
148 }
149 }
150
151
160 public static boolean equals(Object obj1, Object obj2) {
161 if ((obj1 == null) && (obj2 == null)) {
162 return true;
163 }
164 else if ((obj1 == null) || (obj2 == null)) {
165 return false;
166 }
167 else {
168 return obj1.equals(obj2);
169 }
170 }
171
172
180 public static boolean equals(short short1, short short2) {
181 if (short1 == short2) {
182 return true;
183 }
184 else {
185 return false;
186 }
187 }
188
189
197 public static boolean equalsSorted(
198 boolean[] booleanArray1, boolean[] booleanArray2) {
199
200 Boolean[] booleanObjArray1 = ArrayUtil.toArray(booleanArray1);
201
202 Arrays.sort(booleanObjArray1);
203
204 Boolean[] booleanObjArray2 = ArrayUtil.toArray(booleanArray2);
205
206 Arrays.sort(booleanObjArray2);
207
208 return Arrays.equals(booleanObjArray1, booleanObjArray2);
209 }
210
211
219 public static boolean equalsSorted(byte[] byteArray1, byte[] byteArray2) {
220 byteArray1 = ArrayUtil.clone(byteArray1);
221
222 Arrays.sort(byteArray1);
223
224 byteArray2 = ArrayUtil.clone(byteArray2);
225
226 Arrays.sort(byteArray2);
227
228 return Arrays.equals(byteArray1, byteArray2);
229 }
230
231
239 public static boolean equalsSorted(char[] charArray1, char[] charArray2) {
240 charArray1 = ArrayUtil.clone(charArray1);
241
242 Arrays.sort(charArray1);
243
244 charArray2 = ArrayUtil.clone(charArray2);
245
246 Arrays.sort(charArray2);
247
248 return Arrays.equals(charArray1, charArray2);
249 }
250
251
259 public static boolean equalsSorted(
260 double[] doubleArray1, double[] doubleArray2) {
261
262 doubleArray1 = ArrayUtil.clone(doubleArray1);
263
264 Arrays.sort(doubleArray1);
265
266 doubleArray2 = ArrayUtil.clone(doubleArray2);
267
268 Arrays.sort(doubleArray2);
269
270 return Arrays.equals(doubleArray1, doubleArray2);
271 }
272
273
281 public static boolean equalsSorted(
282 float[] floatArray1, float[] floatArray2) {
283
284 floatArray1 = ArrayUtil.clone(floatArray1);
285
286 Arrays.sort(floatArray1);
287
288 floatArray2 = ArrayUtil.clone(floatArray2);
289
290 Arrays.sort(floatArray2);
291
292 return Arrays.equals(floatArray1, floatArray2);
293 }
294
295
303 public static boolean equalsSorted(int[] intArray1, int[] intArray2) {
304 intArray1 = ArrayUtil.clone(intArray1);
305
306 Arrays.sort(intArray1);
307
308 intArray2 = ArrayUtil.clone(intArray2);
309
310 Arrays.sort(intArray2);
311
312 return Arrays.equals(intArray1, intArray2);
313 }
314
315
323 public static boolean equalsSorted(long[] longArray1, long[] longArray2) {
324 longArray1 = ArrayUtil.clone(longArray1);
325
326 Arrays.sort(longArray1);
327
328 longArray2 = ArrayUtil.clone(longArray2);
329
330 Arrays.sort(longArray2);
331
332 return Arrays.equals(longArray1, longArray2);
333 }
334
335
343 public static boolean equalsSorted(Object[] objArray1, Object[] objArray2) {
344 objArray1 = ArrayUtil.clone(objArray1);
345
346 Arrays.sort(objArray1);
347
348 objArray2 = ArrayUtil.clone(objArray2);
349
350 Arrays.sort(objArray2);
351
352 return Arrays.equals(objArray1, objArray2);
353 }
354
355
363 public static boolean equalsSorted(
364 short[] shortArray1, short[] shortArray2) {
365
366 shortArray1 = ArrayUtil.clone(shortArray1);
367
368 Arrays.sort(shortArray1);
369
370 shortArray2 = ArrayUtil.clone(shortArray2);
371
372 Arrays.sort(shortArray2);
373
374 return Arrays.equals(shortArray1, shortArray2);
375 }
376
377
386 public static boolean isAddress(String address) {
387 if (isNull(address)) {
388 return false;
389 }
390
391 String[] tokens = address.split(StringPool.AT);
392
393 if (tokens.length != 2) {
394 return false;
395 }
396
397 for (String token : tokens) {
398 for (char c : token.toCharArray()) {
399 if (Character.isWhitespace(c)) {
400 return false;
401 }
402 }
403 }
404
405 return true;
406 }
407
408
416 public static boolean isAlphanumericName(String name) {
417 if (isNull(name)) {
418 return false;
419 }
420
421 for (char c : name.trim().toCharArray()) {
422 if (!isChar(c) && !isDigit(c) && !Character.isWhitespace(c)) {
423 return false;
424 }
425 }
426
427 return true;
428 }
429
430
439 public static boolean isAscii(char c) {
440 int i = c;
441
442 if ((i >= 32) && (i <= 126)) {
443 return true;
444 }
445 else {
446 return false;
447 }
448 }
449
450 public static boolean isBlank(String s) {
451 if (s == null) {
452 return true;
453 }
454
455 if (s.length() == 0) {
456 return true;
457 }
458
459 return false;
460 }
461
462
470 public static boolean isChar(char c) {
471 int x = c;
472
473 if (((x >= _CHAR_LOWER_CASE_BEGIN) && (x <= _CHAR_LOWER_CASE_END)) ||
474 ((x >= _CHAR_UPPER_CASE_BEGIN) && (x <= _CHAR_UPPER_CASE_END))) {
475
476 return true;
477 }
478
479 return false;
480 }
481
482
490 public static boolean isChar(String s) {
491 if (isNull(s)) {
492 return false;
493 }
494
495 for (char c : s.toCharArray()) {
496 if (!isChar(c)) {
497 return false;
498 }
499 }
500
501 return true;
502 }
503
504
513 public static boolean isDate(int month, int day, int year) {
514 return isGregorianDate(month, day, year);
515 }
516
517
525 public static boolean isDigit(char c) {
526 int x = c;
527
528 if ((x >= _DIGIT_BEGIN) && (x <= _DIGIT_END)) {
529 return true;
530 }
531
532 return false;
533 }
534
535
543 public static boolean isDigit(String s) {
544 if (isNull(s)) {
545 return false;
546 }
547
548 for (char c : s.toCharArray()) {
549 if (!isDigit(c)) {
550 return false;
551 }
552 }
553
554 return true;
555 }
556
557
566 public static boolean isDomain(String domainName) {
567
568
569
570
571 if (isNull(domainName)) {
572 return false;
573 }
574
575 if (domainName.length() > 255) {
576 return false;
577 }
578
579 if (domainName.startsWith(StringPool.PERIOD) ||
580 domainName.endsWith(StringPool.PERIOD)) {
581
582 return false;
583 }
584
585 if (!domainName.contains(StringPool.PERIOD) &&
586 !domainName.equals(_LOCALHOST)) {
587
588 return false;
589 }
590
591 String[] domainNameArray = StringUtil.split(
592 domainName, CharPool.PERIOD);
593
594 for (String domainNamePart : domainNameArray) {
595 char[] domainNamePartCharArray = domainNamePart.toCharArray();
596
597 for (int i = 0; i < domainNamePartCharArray.length; i++) {
598 char c = domainNamePartCharArray[i];
599
600 if ((i == 0) && (c == CharPool.DASH)) {
601 return false;
602 }
603
604 if ((i == (domainNamePartCharArray.length - 1)) &&
605 (c == CharPool.DASH)) {
606
607 return false;
608 }
609
610 if (!isChar(c) && !isDigit(c) && (c != CharPool.DASH)) {
611 return false;
612 }
613 }
614 }
615
616 return true;
617 }
618
619
626 public static boolean isEmailAddress(String emailAddress) {
627 Matcher matcher = _emailAddressPattern.matcher(emailAddress);
628
629 return matcher.matches();
630 }
631
632
640 public static boolean isEmailAddressSpecialChar(char c) {
641
642
643
644 for (char specialChar : _EMAIL_ADDRESS_SPECIAL_CHAR) {
645 if (c == specialChar) {
646 return true;
647 }
648 }
649
650 return false;
651 }
652
653
660 public static boolean isFileExtension(String fileExtension) {
661 if (isNull(fileExtension) ||
662 fileExtension.contains(StringPool.BACK_SLASH) ||
663 fileExtension.contains(StringPool.NULL_CHAR) ||
664 fileExtension.contains(StringPool.SLASH)) {
665
666 return false;
667 }
668
669 return true;
670 }
671
672 public static boolean isFileName(String name) {
673 if (isNull(name) || name.equals(StringPool.PERIOD) ||
674 name.equals(StringPool.DOUBLE_PERIOD) ||
675 name.contains(StringPool.BACK_SLASH) ||
676 name.contains(StringPool.NULL_CHAR) ||
677 name.contains(StringPool.SLASH)) {
678
679 return false;
680 }
681
682 return true;
683 }
684
685 public static boolean isFilePath(String path, boolean isParentDirAllowed) {
686 if (Validator.isNull(path)) {
687 return false;
688 }
689
690 if (path.contains(StringPool.NULL_CHAR)) {
691 return false;
692 }
693
694 if (isParentDirAllowed) {
695 return true;
696 }
697
698 if (path.equals(StringPool.DOUBLE_PERIOD)) {
699 return false;
700 }
701
702 String normalizedPath = path.replace(
703 CharPool.BACK_SLASH, CharPool.SLASH);
704
705 if (normalizedPath.startsWith(
706 StringPool.DOUBLE_PERIOD.concat(StringPool.SLASH))) {
707
708 return false;
709 }
710
711 if (normalizedPath.endsWith(
712 StringPool.SLASH.concat(StringPool.DOUBLE_PERIOD))) {
713
714 return false;
715 }
716
717 if (normalizedPath.contains(
718 StringPool.SLASH.concat(
719 StringPool.DOUBLE_PERIOD).concat(StringPool.SLASH))) {
720
721 return false;
722 }
723
724 return true;
725 }
726
727
736 public static boolean isGregorianDate(int month, int day, int year) {
737 if ((month < 0) || (month > 11)) {
738 return false;
739 }
740
741 int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
742
743 if (month == 1) {
744 int febMax = 28;
745
746 if (((year % 4) == 0) && ((year % 100) != 0) ||
747 ((year % 400) == 0)) {
748
749 febMax = 29;
750 }
751
752 if ((day < 1) || (day > febMax)) {
753 return false;
754 }
755 }
756 else if ((day < 1) || (day > months[month])) {
757 return false;
758 }
759
760 return true;
761 }
762
763
773 public static boolean isHex(String s) {
774 if (isNull(s)) {
775 return false;
776 }
777
778 return true;
779 }
780
781
788 public static boolean isHostName(String name) {
789 if (isNull(name)) {
790 return false;
791 }
792
793 char[] nameCharArray = name.toCharArray();
794
795 if ((nameCharArray[0] == CharPool.DASH) ||
796 (nameCharArray[0] == CharPool.PERIOD) ||
797 (nameCharArray[nameCharArray.length - 1] == CharPool.DASH)) {
798
799 return false;
800 }
801
802 for (char c : nameCharArray) {
803 if (!isChar(c) && !isDigit(c) && (c != CharPool.CLOSE_BRACKET) &&
804 (c != CharPool.COLON) && (c != CharPool.DASH) &&
805 (c != CharPool.OPEN_BRACKET) && (c != CharPool.PERIOD)) {
806
807 return false;
808 }
809 }
810
811 return true;
812 }
813
814
822 public static boolean isHTML(String s) {
823 if (isNull(s)) {
824 return false;
825 }
826
827 if ((s.contains("<html>") || s.contains("<HTML>")) &&
828 (s.contains("</html>") || s.contains("</HTML>"))) {
829
830 return true;
831 }
832
833 return false;
834 }
835
836
843 public static boolean isIPAddress(String ipAddress) {
844 Matcher matcher = _ipAddressPattern.matcher(ipAddress);
845
846 return matcher.matches();
847 }
848
849
858 public static boolean isJulianDate(int month, int day, int year) {
859 if ((month < 0) || (month > 11)) {
860 return false;
861 }
862
863 int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
864
865 if (month == 1) {
866 int febMax = 28;
867
868 if ((year % 4) == 0) {
869 febMax = 29;
870 }
871
872 if ((day < 1) || (day > febMax)) {
873 return false;
874 }
875 }
876 else if ((day < 1) || (day > months[month])) {
877 return false;
878 }
879
880 return true;
881 }
882
883
891 public static boolean isLUHN(String number) {
892 if (number == null) {
893 return false;
894 }
895
896 number = StringUtil.reverse(number);
897
898 int total = 0;
899
900 for (int i = 0; i < number.length(); i++) {
901 int x = 0;
902
903 if (((i + 1) % 2) == 0) {
904 x = GetterUtil.getInteger(number.substring(i, i + 1)) * 2;
905
906 if (x >= 10) {
907 String s = String.valueOf(x);
908
909 x =
910 GetterUtil.getInteger(s.substring(0, 1)) +
911 GetterUtil.getInteger(s.substring(1, 2));
912 }
913 }
914 else {
915 x = GetterUtil.getInteger(number.substring(i, i + 1));
916 }
917
918 total = total + x;
919 }
920
921 if ((total % 10) == 0) {
922 return true;
923 }
924 else {
925 return false;
926 }
927 }
928
929
937 public static boolean isName(String name) {
938 if (isNull(name)) {
939 return false;
940 }
941
942 for (char c : name.trim().toCharArray()) {
943 if (!isChar(c) && !Character.isWhitespace(c)) {
944 return false;
945 }
946 }
947
948 return true;
949 }
950
951
960 public static boolean isNotNull(Long l) {
961 return !isNull(l);
962 }
963
964
973 public static boolean isNotNull(Object obj) {
974 return !isNull(obj);
975 }
976
977
985 public static boolean isNotNull(Object[] array) {
986 return !isNull(array);
987 }
988
989
998 public static boolean isNotNull(String s) {
999 return !isNull(s);
1000 }
1001
1002
1010 public static boolean isNull(Long l) {
1011 if ((l == null) || (l.longValue() == 0)) {
1012 return true;
1013 }
1014 else {
1015 return false;
1016 }
1017 }
1018
1019
1028 public static boolean isNull(Object obj) {
1029 if (obj instanceof Long) {
1030 return isNull((Long)obj);
1031 }
1032 else if (obj instanceof String) {
1033 return isNull((String)obj);
1034 }
1035 else if (obj == null) {
1036 return true;
1037 }
1038 else {
1039 return false;
1040 }
1041 }
1042
1043
1051 public static boolean isNull(Object[] array) {
1052 if ((array == null) || (array.length == 0)) {
1053 return true;
1054 }
1055 else {
1056 return false;
1057 }
1058 }
1059
1060
1069 public static boolean isNull(String s) {
1070 if (s == null) {
1071 return true;
1072 }
1073
1074 int counter = 0;
1075
1076 for (int i = 0; i < s.length(); i++) {
1077 char c = s.charAt(i);
1078
1079 if (c == CharPool.SPACE) {
1080 continue;
1081 }
1082 else if (counter > 3) {
1083 return false;
1084 }
1085
1086 if (counter == 0) {
1087 if (c != CharPool.LOWER_CASE_N) {
1088 return false;
1089 }
1090 }
1091 else if (counter == 1) {
1092 if (c != CharPool.LOWER_CASE_U) {
1093 return false;
1094 }
1095 }
1096 else if ((counter == 2) || (counter == 3)) {
1097 if (c != CharPool.LOWER_CASE_L) {
1098 return false;
1099 }
1100 }
1101
1102 counter++;
1103 }
1104
1105 if ((counter == 0) || (counter == 4)) {
1106 return true;
1107 }
1108
1109 return false;
1110 }
1111
1112
1120 public static boolean isNumber(String number) {
1121 if (isNull(number)) {
1122 return false;
1123 }
1124
1125 for (char c : number.toCharArray()) {
1126 if (!isDigit(c)) {
1127 return false;
1128 }
1129 }
1130
1131 return true;
1132 }
1133
1134
1143 public static boolean isPassword(String password) {
1144 if (isNull(password)) {
1145 return false;
1146 }
1147
1148 if (password.length() < 4) {
1149 return false;
1150 }
1151
1152 for (char c : password.toCharArray()) {
1153 if (!isChar(c) && !isDigit(c)) {
1154 return false;
1155 }
1156 }
1157
1158 return true;
1159 }
1160
1161
1170 public static boolean isPhoneNumber(String phoneNumber) {
1171 return isNumber(StringUtil.extractDigits(phoneNumber));
1172 }
1173
1174
1182 public static boolean isUrl(String url) {
1183 if (Validator.isNotNull(url)) {
1184 if (url.indexOf(CharPool.COLON) == -1) {
1185 return false;
1186 }
1187
1188 try {
1189 new URL(url);
1190
1191 return true;
1192 }
1193 catch (MalformedURLException murle) {
1194 }
1195 }
1196
1197 return false;
1198 }
1199
1200
1207 public static boolean isVariableName(String variableName) {
1208 if (isNull(variableName)) {
1209 return false;
1210 }
1211
1212 Matcher matcher = _variableNamePattern.matcher(variableName);
1213
1214 if (matcher.matches()) {
1215 return true;
1216 }
1217 else {
1218 return false;
1219 }
1220 }
1221
1222
1230 public static boolean isVariableTerm(String s) {
1231 if (s.startsWith(_VARIABLE_TERM_BEGIN) &&
1232 s.endsWith(_VARIABLE_TERM_END)) {
1233
1234 return true;
1235 }
1236 else {
1237 return false;
1238 }
1239 }
1240
1241
1250 public static boolean isWhitespace(char c) {
1251 int i = c;
1252
1253 if ((i == 0) || Character.isWhitespace(c)) {
1254 return true;
1255 }
1256 else {
1257 return false;
1258 }
1259 }
1260
1261
1270 public static boolean isXml(String s) {
1271 if (s.startsWith(_XML_BEGIN) || s.startsWith(_XML_EMPTY)) {
1272 return true;
1273 }
1274 else {
1275 return false;
1276 }
1277 }
1278
1279 private static final int _CHAR_LOWER_CASE_BEGIN = 97;
1280
1281 private static final int _CHAR_LOWER_CASE_END = 122;
1282
1283 private static final int _CHAR_UPPER_CASE_BEGIN = 65;
1284
1285 private static final int _CHAR_UPPER_CASE_END = 90;
1286
1287 private static final int _DIGIT_BEGIN = 48;
1288
1289 private static final int _DIGIT_END = 57;
1290
1291 private static final char[] _EMAIL_ADDRESS_SPECIAL_CHAR = new char[] {
1292 '.', '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^',
1293 '_', '`', '{', '|', '}', '~'
1294 };
1295
1296 private static final String _LOCALHOST = "localhost";
1297
1298 private static final String _VARIABLE_TERM_BEGIN = "[$";
1299
1300 private static final String _VARIABLE_TERM_END = "$]";
1301
1302 private static final String _XML_BEGIN = "<?xml";
1303
1304 private static final String _XML_EMPTY = "<root />";
1305
1306 private static Pattern _emailAddressPattern = Pattern.compile(
1307 "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@" +
1308 "(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?");
1309 private static Pattern _ipAddressPattern = Pattern.compile(
1310 "\\b" +
1311 "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
1312 "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
1313 "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
1314 "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])" +
1315 "\\b");
1316 private static Pattern _variableNamePattern = Pattern.compile(
1317 "[_a-zA-Z]+[_a-zA-Z0-9]*");
1318
1319 }