1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.net.MalformedURLException;
18  import java.net.URL;
19  
20  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  /**
24   * <a href="Validator.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Brian Wing Shun Chan
27   * @author Alysa Carver
28   */
29  public class Validator {
30  
31      public static boolean equals(Object obj1, Object obj2) {
32          if ((obj1 == null) && (obj2 == null)) {
33              return true;
34          }
35          else if ((obj1 == null) || (obj2 == null)) {
36              return false;
37          }
38          else {
39              return obj1.equals(obj2);
40          }
41      }
42  
43      public static boolean isAddress(String address) {
44          if (isNull(address)) {
45              return false;
46          }
47  
48          String[] tokens = address.split(StringPool.AT);
49  
50          if (tokens.length != 2) {
51              return false;
52          }
53  
54          for (String token : tokens) {
55              for (char c : token.toCharArray()) {
56                  if (Character.isWhitespace(c)) {
57                      return false;
58                  }
59              }
60          }
61  
62          return true;
63      }
64  
65      public static boolean isAscii(char c) {
66          int i = c;
67  
68          if ((i >= 32) && (i <= 126)) {
69              return true;
70          }
71          else {
72              return false;
73          }
74      }
75  
76      /**
77       * Returns true if c is a letter between a-z and A-Z.
78       *
79       * @return true if c is a letter between a-z and A-Z
80       */
81      public static boolean isChar(char c) {
82          int x = c;
83  
84          if ((x >= _CHAR_BEGIN) && (x <= _CHAR_END)) {
85              return true;
86          }
87  
88          return false;
89      }
90  
91      /**
92       * Returns true if s is a string of letters that are between a-z and A-Z.
93       *
94       * @return true if s is a string of letters that are between a-z and A-Z
95       */
96      public static boolean isChar(String s) {
97          if (isNull(s)) {
98              return false;
99          }
100 
101         for (char c : s.toCharArray()) {
102             if (!isChar(c)) {
103                 return false;
104             }
105         }
106 
107         return true;
108     }
109 
110     public static boolean isDate(int month, int day, int year) {
111         return isGregorianDate(month, day, year);
112     }
113 
114     /**
115      * Returns true if c is a digit between 0 and 9.
116      *
117      * @return true if c is a digit between 0 and 9
118      */
119     public static boolean isDigit(char c) {
120         int x = c;
121 
122         if ((x >= _DIGIT_BEGIN) && (x <= _DIGIT_END)) {
123             return true;
124         }
125 
126         return false;
127     }
128 
129     /**
130      * Returns true if s is a string of letters that are between 0 and 9.
131      *
132      * @return true if s is a string of letters that are between 0 and 9
133      */
134     public static boolean isDigit(String s) {
135         if (isNull(s)) {
136             return false;
137         }
138 
139         for (char c : s.toCharArray()) {
140             if (!isDigit(c)) {
141                 return false;
142             }
143         }
144 
145         return true;
146     }
147 
148     public static boolean isDomain(String domainName) {
149 
150         // See RFC-1034 (section 3), RFC-1123 (section 2.1), and RFC-952
151         // (section B. Lexical grammar)
152 
153         if (isNull(domainName)) {
154             return false;
155         }
156 
157         if (domainName.length() > 255) {
158             return false;
159         }
160 
161         String[] domainNameArray = StringUtil.split(
162             domainName, StringPool.PERIOD);
163 
164         for (String domainNamePart : domainNameArray) {
165             char[] domainNamePartCharArray = domainNamePart.toCharArray();
166 
167             for (int i = 0; i < domainNamePartCharArray.length; i++) {
168                 char c = domainNamePartCharArray[i];
169 
170                 if ((i == 0) && (c == CharPool.DASH)) {
171                     return false;
172                 }
173                 else if ((i == (domainNamePartCharArray.length - 1)) &&
174                          (c == CharPool.DASH)) {
175 
176                     return false;
177                 }
178                 else if ((!isChar(c)) && (!isDigit(c)) &&
179                          (c != CharPool.DASH)) {
180 
181                     return false;
182                 }
183             }
184         }
185 
186         return true;
187     }
188 
189     public static boolean isEmailAddress(String emailAddress) {
190         Matcher matcher = _emailAddressPattern.matcher(emailAddress);
191 
192         return matcher.matches();
193     }
194 
195     public static boolean isEmailAddressSpecialChar(char c) {
196 
197         // LEP-1445
198 
199         for (int i = 0; i < _EMAIL_ADDRESS_SPECIAL_CHAR.length; i++) {
200             if (c == _EMAIL_ADDRESS_SPECIAL_CHAR[i]) {
201                 return true;
202             }
203         }
204 
205         return false;
206     }
207 
208     public static boolean isGregorianDate(int month, int day, int year) {
209         if ((month < 0) || (month > 11)) {
210             return false;
211         }
212 
213         int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
214 
215         if (month == 1) {
216             int febMax = 28;
217 
218             if (((year % 4) == 0) && ((year % 100) != 0) ||
219                 ((year % 400) == 0)) {
220 
221                 febMax = 29;
222             }
223 
224             if ((day < 1) || (day > febMax)) {
225                 return false;
226             }
227         }
228         else if ((day < 1) || (day > months[month])) {
229             return false;
230         }
231 
232         return true;
233     }
234 
235     public static boolean isHex(String s) {
236         if (isNull(s)) {
237             return false;
238         }
239 
240         return true;
241     }
242 
243     public static boolean isHTML(String s) {
244         if (isNull(s)) {
245             return false;
246         }
247 
248         if (((s.indexOf("<html>") != -1) || (s.indexOf("<HTML>") != -1)) &&
249             ((s.indexOf("</html>") != -1) || (s.indexOf("</HTML>") != -1))) {
250 
251             return true;
252         }
253 
254         return false;
255     }
256 
257     public static boolean isIPAddress(String ipAddress) {
258         Matcher matcher = _ipAddressPattern.matcher(ipAddress);
259 
260         return matcher.matches();
261     }
262 
263     public static boolean isJulianDate(int month, int day, int year) {
264         if ((month < 0) || (month > 11)) {
265             return false;
266         }
267 
268         int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
269 
270         if (month == 1) {
271             int febMax = 28;
272 
273             if ((year % 4) == 0) {
274                 febMax = 29;
275             }
276 
277             if ((day < 1) || (day > febMax)) {
278                 return false;
279             }
280         }
281         else if ((day < 1) || (day > months[month])) {
282             return false;
283         }
284 
285         return true;
286     }
287 
288     public static boolean isLUHN(String number) {
289         if (number == null) {
290             return false;
291         }
292 
293         number = StringUtil.reverse(number);
294 
295         int total = 0;
296 
297         for (int i = 0; i < number.length(); i++) {
298             int x = 0;
299 
300             if (((i + 1) % 2) == 0) {
301                 x = Integer.parseInt(number.substring(i, i + 1)) * 2;
302 
303                 if (x >= 10) {
304                     String s = String.valueOf(x);
305 
306                     x = Integer.parseInt(s.substring(0, 1)) +
307                         Integer.parseInt(s.substring(1, 2));
308                 }
309             }
310             else {
311                 x = Integer.parseInt(number.substring(i, i + 1));
312             }
313 
314             total = total + x;
315         }
316 
317         if ((total % 10) == 0) {
318             return true;
319         }
320         else {
321             return false;
322         }
323     }
324 
325     public static boolean isName(String name) {
326         if (isNull(name)) {
327             return false;
328         }
329 
330         for (char c : name.trim().toCharArray()) {
331             if (!isChar(c) && !Character.isWhitespace(c)) {
332                 return false;
333             }
334         }
335 
336         return true;
337     }
338 
339     public static boolean isNotNull(Object obj) {
340         return !isNull(obj);
341     }
342 
343     public static boolean isNotNull(Long l) {
344         return !isNull(l);
345     }
346 
347     public static boolean isNotNull(String s) {
348         return !isNull(s);
349     }
350 
351     public static boolean isNotNull(Object[] array) {
352         return !isNull(array);
353     }
354 
355     public static boolean isNull(Object obj) {
356         if (obj instanceof Long) {
357             return isNull((Long)obj);
358         }
359         else if (obj instanceof String) {
360             return isNull((String)obj);
361         }
362         else if (obj == null) {
363             return true;
364         }
365         else {
366             return false;
367         }
368     }
369 
370     public static boolean isNull(Long l) {
371         if ((l == null) || (l.longValue() == 0)) {
372             return true;
373         }
374         else {
375             return false;
376         }
377     }
378 
379     public static boolean isNull(String s) {
380         if (s == null) {
381             return true;
382         }
383 
384         int counter = 0;
385 
386         for (int i = 0; i < s.length(); i++) {
387             char c = s.charAt(i);
388 
389             if (c == CharPool.SPACE) {
390                 continue;
391             }
392             else if (counter > 3) {
393                 return false;
394             }
395 
396             if (counter == 0) {
397                 if (c != CharPool.LOWER_CASE_N) {
398                     return false;
399                 }
400             }
401             else if (counter == 1) {
402                 if (c != CharPool.LOWER_CASE_U) {
403                     return false;
404                 }
405             }
406             else if ((counter == 2) || (counter == 3)) {
407                 if (c != CharPool.LOWER_CASE_L) {
408                     return false;
409                 }
410             }
411 
412             counter++;
413         }
414 
415         if ((counter == 0) || (counter == 4)) {
416             return true;
417         }
418 
419         return false;
420     }
421 
422     public static boolean isNull(Object[] array) {
423         if ((array == null) || (array.length == 0)) {
424             return true;
425         }
426         else {
427             return false;
428         }
429     }
430 
431     public static boolean isNumber(String number) {
432         if (isNull(number)) {
433             return false;
434         }
435 
436         for (char c : number.toCharArray()) {
437             if (!isDigit(c)) {
438                 return false;
439             }
440         }
441 
442         return true;
443     }
444 
445     public static boolean isPassword(String password) {
446         if (isNull(password)) {
447             return false;
448         }
449 
450         if (password.length() < 4) {
451             return false;
452         }
453 
454         for (char c : password.toCharArray()) {
455             if (!isChar(c) && !isDigit(c)) {
456                 return false;
457             }
458         }
459 
460         return true;
461     }
462 
463     public static boolean isPhoneNumber(String phoneNumber) {
464         return isNumber(StringUtil.extractDigits(phoneNumber));
465     }
466 
467     public static boolean isUrl(String url) {
468         if (Validator.isNotNull(url)) {
469             try {
470                 new URL(url);
471 
472                 return true;
473             }
474             catch (MalformedURLException murle) {
475             }
476         }
477 
478         return false;
479     }
480 
481     public static boolean isVariableTerm(String s) {
482         if (s.startsWith(_VARIABLE_TERM_BEGIN) &&
483             s.endsWith(_VARIABLE_TERM_END)) {
484 
485             return true;
486         }
487         else {
488             return false;
489         }
490     }
491 
492     public static boolean isWhitespace(char c) {
493         int i = c;
494 
495         if ((i == 0) || Character.isWhitespace(c)) {
496             return true;
497         }
498         else {
499             return false;
500         }
501     }
502 
503     public static boolean isXml(String s) {
504         if (s.startsWith(_XML_BEGIN) || s.startsWith(_XML_EMPTY)) {
505             return true;
506         }
507         else {
508             return false;
509         }
510     }
511 
512     private static final int _CHAR_BEGIN = 65;
513 
514     private static final int _CHAR_END = 122;
515 
516     private static final int _DIGIT_BEGIN = 48;
517 
518     private static final int _DIGIT_END = 57;
519 
520     private static final char[] _EMAIL_ADDRESS_SPECIAL_CHAR = new char[] {
521         '.', '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^',
522         '_', '`', '{', '|', '}', '~'
523     };
524 
525     private static final String _VARIABLE_TERM_BEGIN = "[$";
526 
527     private static final String _VARIABLE_TERM_END = "$]";
528 
529     private static final String _XML_BEGIN = "<?xml";
530 
531     private static final String _XML_EMPTY = "<root />";
532 
533     private static Pattern _emailAddressPattern = Pattern.compile(
534         "([\\w-]+\\.)*[\\w-]+@([\\w-]+\\.)+[A-Za-z]+");
535     private static Pattern _ipAddressPattern = Pattern.compile(
536         "\\b" +
537         "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
538         "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
539         "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
540         "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])" +
541         "\\b");
542 
543 }