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  /**
29   * <a href="Validator.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   * @author Alysa Carver
33   *
34   */
35  public class Validator {
36  
37      public static boolean equals(String s1, String s2) {
38          if ((s1 == null) && (s2 == null)) {
39              return true;
40          }
41          else if ((s1 == null) || (s2 == null)) {
42              return false;
43          }
44          else {
45              return s1.equals(s2);
46          }
47      }
48  
49      public static boolean isAddress(String address) {
50          if (isNull(address)) {
51              return false;
52          }
53  
54          String[] tokens = address.split(StringPool.AT);
55  
56          if (tokens.length != 2) {
57              return false;
58          }
59  
60          for (int i = 0; i < tokens.length; i++) {
61              char[] c = tokens[i].toCharArray();
62  
63              for (int j = 0; j < c.length; j++) {
64                  if (Character.isWhitespace(c[j])) {
65                      return false;
66                  }
67              }
68          }
69  
70          return true;
71      }
72  
73      public static boolean isChar(char c) {
74          return Character.isLetter(c);
75      }
76  
77      public static boolean isChar(String s) {
78          if (isNull(s)) {
79              return false;
80          }
81  
82          char[] c = s.toCharArray();
83  
84          for (int i = 0; i < c.length; i++) {
85              if (!isChar(c[i])) {
86                  return false;
87              }
88          }
89  
90          return true;
91      }
92  
93      public static boolean isDigit(char c) {
94          int x = c;
95  
96          if ((x >= 48) && (x <= 57)) {
97              return true;
98          }
99  
100         return false;
101     }
102 
103     public static boolean isDigit(String s) {
104         if (isNull(s)) {
105             return false;
106         }
107 
108         char[] c = s.toCharArray();
109 
110         for (int i = 0; i < c.length; i++) {
111             if (!isDigit(c[i])) {
112                 return false;
113             }
114         }
115 
116         return true;
117     }
118 
119     public static boolean isHex(String s) {
120         if (isNull(s)) {
121             return false;
122         }
123 
124         return true;
125     }
126 
127     public static boolean isHTML(String s) {
128         if (isNull(s)) {
129             return false;
130         }
131 
132         if (((s.indexOf("<html>") != -1) || (s.indexOf("<HTML>") != -1)) &&
133             ((s.indexOf("</html>") != -1) || (s.indexOf("</HTML>") != -1))) {
134 
135             return true;
136         }
137 
138         return false;
139     }
140 
141     public static boolean isLUHN(String number) {
142         if (number == null) {
143             return false;
144         }
145 
146         number = StringUtil.reverse(number);
147 
148         int total = 0;
149 
150         for (int i = 0; i < number.length(); i++) {
151             int x = 0;
152 
153             if (((i + 1) % 2) == 0) {
154                 x = Integer.parseInt(number.substring(i, i + 1)) * 2;
155 
156                 if (x >= 10) {
157                     String s = Integer.toString(x);
158 
159                     x = Integer.parseInt(s.substring(0, 1)) +
160                         Integer.parseInt(s.substring(1, 2));
161                 }
162             }
163             else {
164                 x = Integer.parseInt(number.substring(i, i + 1));
165             }
166 
167             total = total + x;
168         }
169 
170         if ((total % 10) == 0) {
171             return true;
172         }
173         else {
174             return false;
175         }
176     }
177 
178     public static boolean isDate(int month, int day, int year) {
179         return isGregorianDate(month, day, year);
180     }
181 
182     public static boolean isGregorianDate(int month, int day, int year) {
183         if ((month < 0) || (month > 11)) {
184             return false;
185         }
186 
187         int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
188 
189         if (month == 1) {
190             int febMax = 28;
191 
192             if (((year % 4) == 0) && ((year % 100) != 0) ||
193                 ((year % 400) == 0)) {
194 
195                 febMax = 29;
196             }
197 
198             if ((day < 1) || (day > febMax)) {
199                 return false;
200             }
201         }
202         else if ((day < 1) || (day > months[month])) {
203             return false;
204         }
205 
206         return true;
207     }
208 
209     public static boolean isJulianDate(int month, int day, int year) {
210         if ((month < 0) || (month > 11)) {
211             return false;
212         }
213 
214         int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
215 
216         if (month == 1) {
217             int febMax = 28;
218 
219             if ((year % 4) == 0) {
220                 febMax = 29;
221             }
222 
223             if ((day < 1) || (day > febMax)) {
224                 return false;
225             }
226         }
227         else if ((day < 1) || (day > months[month])) {
228             return false;
229         }
230 
231         return true;
232     }
233 
234     public static boolean isEmailAddress(String emailAddress) {
235         Boolean valid = null;
236 
237         try {
238             valid = (Boolean)PortalClassInvoker.invoke(
239                 "com.liferay.util.mail.InternetAddressUtil", "isValid",
240                 emailAddress);
241         }
242         catch (Exception e) {
243             if (_log.isWarnEnabled()) {
244                 _log.warn(e);
245             }
246         }
247 
248         if (valid == null) {
249             return false;
250         }
251         else {
252             return valid.booleanValue();
253         }
254     }
255 
256     public static boolean isEmailAddressSpecialChar(char c) {
257 
258         // LEP-1445
259 
260         for (int i = 0; i < _EMAIL_ADDRESS_SPECIAL_CHAR.length; i++) {
261             if (c == _EMAIL_ADDRESS_SPECIAL_CHAR[i]) {
262                 return true;
263             }
264         }
265 
266         return false;
267     }
268 
269     /**
270      * @deprecated Use <code>isEmailAddress</code>.
271      */
272     public static boolean isValidEmailAddress(String ea) {
273         return isEmailAddress(ea);
274     }
275 
276     public static boolean isName(String name) {
277         if (isNull(name)) {
278             return false;
279         }
280 
281         char[] c = name.trim().toCharArray();
282 
283         for (int i = 0; i < c.length; i++) {
284             if (((!isChar(c[i])) &&
285                 (!Character.isWhitespace(c[i]))) ||
286                     (c[i] == ',')) {
287 
288                 return false;
289             }
290         }
291 
292         return true;
293     }
294 
295     public static boolean isNumber(String number) {
296         if (isNull(number)) {
297             return false;
298         }
299 
300         char[] c = number.toCharArray();
301 
302         for (int i = 0; i < c.length; i++) {
303             if (!isDigit(c[i])) {
304                 return false;
305             }
306         }
307 
308         return true;
309     }
310 
311     public static boolean isNull(Object obj) {
312         if (obj instanceof Long) {
313             return isNull((Long)obj);
314         }
315         else if (obj instanceof String) {
316             return isNull((String)obj);
317         }
318         else if (obj == null) {
319             return true;
320         }
321         else {
322             return false;
323         }
324     }
325 
326     public static boolean isNull(Long l) {
327         if ((l == null) || l.longValue() == 0) {
328             return true;
329         }
330         else {
331             return false;
332         }
333     }
334 
335     public static boolean isNull(String s) {
336         if (s == null) {
337             return true;
338         }
339 
340         s = s.trim();
341 
342         if ((s.equals(StringPool.NULL)) || (s.equals(StringPool.BLANK))) {
343             return true;
344         }
345 
346         return false;
347     }
348 
349     public static boolean isNull(Object[] array) {
350         if ((array == null) || (array.length == 0)) {
351             return true;
352         }
353         else {
354             return false;
355         }
356     }
357 
358     public static boolean isNotNull(Object obj) {
359         return !isNull(obj);
360     }
361 
362     public static boolean isNotNull(Long l) {
363         return !isNull(l);
364     }
365 
366     public static boolean isNotNull(String s) {
367         return !isNull(s);
368     }
369 
370     public static boolean isNotNull(Object[] array) {
371         return !isNull(array);
372     }
373 
374     public static boolean isPassword(String password) {
375         if (isNull(password)) {
376             return false;
377         }
378 
379         if (password.length() < 4) {
380             return false;
381         }
382 
383         char[] c = password.toCharArray();
384 
385         for (int i = 0; i < c.length; i++) {
386             if ((!isChar(c[i])) &&
387                 (!isDigit(c[i]))) {
388 
389                 return false;
390             }
391         }
392 
393         return true;
394     }
395 
396     public static boolean isPhoneNumber(String phoneNumber) {
397         return isNumber(StringUtil.extractDigits(phoneNumber));
398     }
399 
400     public static boolean isVariableTerm(String s) {
401         if (s.startsWith("[$") && s.endsWith("$]")) {
402             return true;
403         }
404         else {
405             return false;
406         }
407     }
408 
409     private static char[] _EMAIL_ADDRESS_SPECIAL_CHAR = new char[] {
410         '.', '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^',
411         '_', '`', '{', '|', '}', '~'
412     };
413 
414     private static Log _log = LogFactoryUtil.getLog(Validator.class);
415 
416 }