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