1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.util.regex.Matcher;
29  import java.util.regex.Pattern;
30  
31  /**
32   * <a href="Validator.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   * @author Alysa Carver
36   *
37   */
38  public class Validator {
39  
40      public static boolean equals(String s1, String s2) {
41          if ((s1 == null) && (s2 == null)) {
42              return true;
43          }
44          else if ((s1 == null) || (s2 == null)) {
45              return false;
46          }
47          else {
48              return s1.equals(s2);
49          }
50      }
51  
52      public static boolean isAddress(String address) {
53          if (isNull(address)) {
54              return false;
55          }
56  
57          String[] tokens = address.split(StringPool.AT);
58  
59          if (tokens.length != 2) {
60              return false;
61          }
62  
63          for (String token : tokens) {
64              for (char c : token.toCharArray()) {
65                  if (Character.isWhitespace(c)) {
66                      return false;
67                  }
68              }
69          }
70  
71          return true;
72      }
73  
74      /**
75       * Returns true if c is a letter between a-z and A-Z.
76       *
77       * @param       c a character
78       * @return      true if c is a letter between a-z and A-Z
79       */
80      public static boolean isChar(char c) {
81          int x = c;
82  
83          if ((x >= _CHAR_BEGIN) && (x <= _CHAR_END)) {
84              return true;
85          }
86  
87          return false;
88      }
89  
90      /**
91       * Returns true if s is a string of letters that are between a-z and A-Z.
92       *
93       * @param       s a string
94       * @return      true if s is a string of letters that are between a-z and
95       *              A-Z
96       */
97      public static boolean isChar(String s) {
98          if (isNull(s)) {
99              return false;
100         }
101 
102         for (char c : s.toCharArray()) {
103             if (!isChar(c)) {
104                 return false;
105             }
106         }
107 
108         return true;
109     }
110 
111     public static boolean isDate(int month, int day, int year) {
112         return isGregorianDate(month, day, year);
113     }
114 
115     /**
116      * Returns true if c is a digit between 0 and 9.
117      *
118      * @param       c a character
119      * @return      true if c is a digit between 0 and 9
120      */
121     public static boolean isDigit(char c) {
122         int x = c;
123 
124         if ((x >= _DIGIT_BEGIN) && (x <= _DIGIT_END)) {
125             return true;
126         }
127 
128         return false;
129     }
130 
131     /**
132      * Returns true if s is a string of letters that are between 0 and 9.
133      *
134      * @param       s a string
135      * @return      true if s is a string of letters that are between 0 and 9
136      */
137     public static boolean isDigit(String s) {
138         if (isNull(s)) {
139             return false;
140         }
141 
142         for (char c : s.toCharArray()) {
143             if (!isDigit(c)) {
144                 return false;
145             }
146         }
147 
148         return true;
149     }
150 
151     public static boolean isDomain(String domainName) {
152 
153         // See RFC-1034 (section 3), RFC-1123 (section 2.1), and RFC-952
154         // (section B. Lexical grammar)
155 
156         if (isNull(domainName)) {
157             return false;
158         }
159 
160         if (domainName.length() > 255) {
161             return false;
162         }
163 
164         String[] domainNameArray = StringUtil.split(
165             domainName, StringPool.PERIOD);
166 
167         for (String domainNamePart : domainNameArray) {
168             char[] domainNamePartCharArray = domainNamePart.toCharArray();
169 
170             for (int i = 0; i < domainNamePartCharArray.length; i++) {
171                 char c = domainNamePartCharArray[i];
172 
173                 if ((i == 0) && (c == CharPool.DASH)) {
174                     return false;
175                 }
176                 else if ((i == (domainNamePartCharArray.length - 1)) &&
177                          (c == CharPool.DASH)) {
178 
179                     return false;
180                 }
181                 else if ((!isChar(c)) && (!isDigit(c)) &&
182                          (c != CharPool.DASH)) {
183 
184                     return false;
185                 }
186             }
187         }
188 
189         return true;
190     }
191 
192     public static boolean isEmailAddress(String emailAddress) {
193         Boolean valid = null;
194 
195         try {
196             valid = (Boolean)PortalClassInvoker.invoke(
197                 "com.liferay.util.mail.InternetAddressUtil", "isValid",
198                 emailAddress);
199         }
200         catch (Exception e) {
201             if (_log.isWarnEnabled()) {
202                 _log.warn(e);
203             }
204         }
205 
206         if (valid == null) {
207             return false;
208         }
209         else {
210             return valid.booleanValue();
211         }
212     }
213 
214     public static boolean isEmailAddressSpecialChar(char c) {
215 
216         // LEP-1445
217 
218         for (int i = 0; i < _EMAIL_ADDRESS_SPECIAL_CHAR.length; i++) {
219             if (c == _EMAIL_ADDRESS_SPECIAL_CHAR[i]) {
220                 return true;
221             }
222         }
223 
224         return false;
225     }
226 
227     public static boolean isGregorianDate(int month, int day, int year) {
228         if ((month < 0) || (month > 11)) {
229             return false;
230         }
231 
232         int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
233 
234         if (month == 1) {
235             int febMax = 28;
236 
237             if (((year % 4) == 0) && ((year % 100) != 0) ||
238                 ((year % 400) == 0)) {
239 
240                 febMax = 29;
241             }
242 
243             if ((day < 1) || (day > febMax)) {
244                 return false;
245             }
246         }
247         else if ((day < 1) || (day > months[month])) {
248             return false;
249         }
250 
251         return true;
252     }
253 
254     public static boolean isHex(String s) {
255         if (isNull(s)) {
256             return false;
257         }
258 
259         return true;
260     }
261 
262     public static boolean isHTML(String s) {
263         if (isNull(s)) {
264             return false;
265         }
266 
267         if (((s.indexOf("<html>") != -1) || (s.indexOf("<HTML>") != -1)) &&
268             ((s.indexOf("</html>") != -1) || (s.indexOf("</HTML>") != -1))) {
269 
270             return true;
271         }
272 
273         return false;
274     }
275 
276     public static boolean isIPAddress(String ipAddress){
277         Matcher matcher = _ipAddressPattern.matcher(ipAddress);
278 
279         return matcher.matches();
280     }
281 
282     public static boolean isJulianDate(int month, int day, int year) {
283         if ((month < 0) || (month > 11)) {
284             return false;
285         }
286 
287         int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
288 
289         if (month == 1) {
290             int febMax = 28;
291 
292             if ((year % 4) == 0) {
293                 febMax = 29;
294             }
295 
296             if ((day < 1) || (day > febMax)) {
297                 return false;
298             }
299         }
300         else if ((day < 1) || (day > months[month])) {
301             return false;
302         }
303 
304         return true;
305     }
306 
307     public static boolean isLUHN(String number) {
308         if (number == null) {
309             return false;
310         }
311 
312         number = StringUtil.reverse(number);
313 
314         int total = 0;
315 
316         for (int i = 0; i < number.length(); i++) {
317             int x = 0;
318 
319             if (((i + 1) % 2) == 0) {
320                 x = Integer.parseInt(number.substring(i, i + 1)) * 2;
321 
322                 if (x >= 10) {
323                     String s = String.valueOf(x);
324 
325                     x = Integer.parseInt(s.substring(0, 1)) +
326                         Integer.parseInt(s.substring(1, 2));
327                 }
328             }
329             else {
330                 x = Integer.parseInt(number.substring(i, i + 1));
331             }
332 
333             total = total + x;
334         }
335 
336         if ((total % 10) == 0) {
337             return true;
338         }
339         else {
340             return false;
341         }
342     }
343 
344     public static boolean isName(String name) {
345         if (isNull(name)) {
346             return false;
347         }
348 
349         for (char c : name.trim().toCharArray()) {
350             if (((!isChar(c)) &&
351                 (!Character.isWhitespace(c))) || (c == CharPool.COMMA)) {
352 
353                 return false;
354             }
355         }
356 
357         return true;
358     }
359 
360     public static boolean isNotNull(Object obj) {
361         return !isNull(obj);
362     }
363 
364     public static boolean isNotNull(Long l) {
365         return !isNull(l);
366     }
367 
368     public static boolean isNotNull(String s) {
369         return !isNull(s);
370     }
371 
372     public static boolean isNotNull(Object[] array) {
373         return !isNull(array);
374     }
375 
376     public static boolean isNull(Object obj) {
377         if (obj instanceof Long) {
378             return isNull((Long)obj);
379         }
380         else if (obj instanceof String) {
381             return isNull((String)obj);
382         }
383         else if (obj == null) {
384             return true;
385         }
386         else {
387             return false;
388         }
389     }
390 
391     public static boolean isNull(Long l) {
392         if ((l == null) || l.longValue() == 0) {
393             return true;
394         }
395         else {
396             return false;
397         }
398     }
399 
400     public static boolean isNull(String s) {
401         if (s == null) {
402             return true;
403         }
404 
405         s = s.trim();
406 
407         if ((s.length() == 0) || (s.equals(StringPool.NULL))) {
408             return true;
409         }
410 
411         return false;
412     }
413 
414     public static boolean isNull(Object[] array) {
415         if ((array == null) || (array.length == 0)) {
416             return true;
417         }
418         else {
419             return false;
420         }
421     }
422 
423     public static boolean isNumber(String number) {
424         if (isNull(number)) {
425             return false;
426         }
427 
428         for (char c : number.toCharArray()) {
429             if (!isDigit(c)) {
430                 return false;
431             }
432         }
433 
434         return true;
435     }
436 
437     public static boolean isPassword(String password) {
438         if (isNull(password)) {
439             return false;
440         }
441 
442         if (password.length() < 4) {
443             return false;
444         }
445 
446         for (char c : password.toCharArray()) {
447             if (!isChar(c) && !isDigit(c)) {
448                 return false;
449             }
450         }
451 
452         return true;
453     }
454 
455     public static boolean isPhoneNumber(String phoneNumber) {
456         return isNumber(StringUtil.extractDigits(phoneNumber));
457     }
458 
459     public static boolean isVariableTerm(String s) {
460         if (s.startsWith(_VARIABLE_TERM_BEGIN) &&
461             s.endsWith(_VARIABLE_TERM_END)) {
462 
463             return true;
464         }
465         else {
466             return false;
467         }
468     }
469 
470     private static final int _CHAR_BEGIN = 65;
471 
472     private static final int _CHAR_END = 122;
473 
474     private static final int _DIGIT_BEGIN = 48;
475 
476     private static final int _DIGIT_END = 57;
477 
478     private static final char[] _EMAIL_ADDRESS_SPECIAL_CHAR = new char[] {
479         '.', '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^',
480         '_', '`', '{', '|', '}', '~'
481     };
482 
483     private static final String _VARIABLE_TERM_BEGIN = "[$";
484 
485     private static final String _VARIABLE_TERM_END = "$]";
486 
487     private static Log _log = LogFactoryUtil.getLog(Validator.class);
488 
489     private static Pattern _ipAddressPattern = Pattern.compile(
490         "\\b" +
491         "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
492         "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
493         "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
494         "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])" +
495         "\\b");
496 
497 }