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