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