/* * Copyright (C) 2003-2011 eXo Platform SAS. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.etk.common.utils; import java.net.MalformedURLException; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.etk.common.logging.Logger; /** * Created by The eXo Platform SAS * Author : eXoPlatform * thanhvucong.78@google.com * Aug 4, 2011 */ public class Validator { public static boolean equals(boolean boolean1, boolean boolean2) { if (boolean1 == boolean2) { return true; } else { return false; } } public static boolean equals(byte byte1, byte byte2) { if (byte1 == byte2) { return true; } else { return false; } } public static boolean equals(char char1, char char2) { if (char1 == char2) { return true; } else { return false; } } public static boolean equals(double double1, double double2) { if (Double.compare(double1, double2) == 0) { return true; } else { return false; } } public static boolean equals(float float1, float float2) { if (Float.compare(float1, float2) == 0) { return true; } else { return false; } } public static boolean equals(int int1, int int2) { if (int1 == int2) { return true; } else { return false; } } public static boolean equals(long long1, long long2) { if (long1 == long2) { return true; } else { return false; } } public static boolean equals(Object obj1, Object obj2) { if ((obj1 == null) && (obj2 == null)) { return true; } else if ((obj1 == null) || (obj2 == null)) { return false; } else { return obj1.equals(obj2); } } public static boolean equals(short short1, short short2) { if (short1 == short2) { return true; } else { return false; } } public static boolean isAddress(String address) { if (isNull(address)) { return false; } String[] tokens = address.split(StringPool.AT); if (tokens.length != 2) { return false; } for (String token : tokens) { for (char c : token.toCharArray()) { if (Character.isWhitespace(c)) { return false; } } } return true; } public static boolean isAscii(char c) { int i = c; if ((i >= 32) && (i <= 126)) { return true; } else { return false; } } /** * Returns <code>true</code> if c is a letter between a-z and A-Z. * * @return <code>true</code> if c is a letter between a-z and A-Z */ public static boolean isChar(char c) { int x = c; if ((x >= _CHAR_BEGIN) && (x <= _CHAR_END)) { return true; } return false; } /** * Returns <code>true</code> if s is a string of letters that are between * a-z and A-Z. * * @return <code>true</code> if s is a string of letters that are between * a-z and A-Z */ public static boolean isChar(String s) { if (isNull(s)) { return false; } for (char c : s.toCharArray()) { if (!isChar(c)) { return false; } } return true; } public static boolean isDate(int month, int day, int year) { return isGregorianDate(month, day, year); } /** * Returns <code>true</code> if c is a digit between 0 and 9. * * @return <code>true</code> if c is a digit between 0 and 9 */ public static boolean isDigit(char c) { int x = c; if ((x >= _DIGIT_BEGIN) && (x <= _DIGIT_END)) { return true; } return false; } /** * Returns <code>true</code> if s is a string of letters that are between 0 * and 9. * * @return <code>true</code> if s is a string of letters that are between 0 * and 9 */ public static boolean isDigit(String s) { if (isNull(s)) { return false; } for (char c : s.toCharArray()) { if (!isDigit(c)) { return false; } } return true; } public static boolean isDomain(String domainName) { // See RFC-1034 (section 3), RFC-1123 (section 2.1), and RFC-952 // (section B. Lexical grammar) if (isNull(domainName)) { return false; } if (domainName.length() > 255) { return false; } String[] domainNameArray = StringUtil.split( domainName, StringPool.PERIOD); for (String domainNamePart : domainNameArray) { char[] domainNamePartCharArray = domainNamePart.toCharArray(); for (int i = 0; i < domainNamePartCharArray.length; i++) { char c = domainNamePartCharArray[i]; if ((i == 0) && (c == CharPool.DASH)) { return false; } else if ((i == (domainNamePartCharArray.length - 1)) && (c == CharPool.DASH)) { return false; } else if ((!isChar(c)) && (!isDigit(c)) && (c != CharPool.DASH)) { return false; } } } return true; } public static boolean isEmailAddressSpecialChar(char c) { // LEP-1445 for (int i = 0; i < _EMAIL_ADDRESS_SPECIAL_CHAR.length; i++) { if (c == _EMAIL_ADDRESS_SPECIAL_CHAR[i]) { return true; } } return false; } public static boolean isGregorianDate(int month, int day, int year) { if ((month < 0) || (month > 11)) { return false; } int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 1) { int febMax = 28; if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) { febMax = 29; } if ((day < 1) || (day > febMax)) { return false; } } else if ((day < 1) || (day > months[month])) { return false; } return true; } public static boolean isHex(String s) { if (isNull(s)) { return false; } return true; } public static boolean isHTML(String s) { if (isNull(s)) { return false; } if (((s.indexOf("<html>") != -1) || (s.indexOf("<HTML>") != -1)) && ((s.indexOf("</html>") != -1) || (s.indexOf("</HTML>") != -1))) { return true; } return false; } public static boolean isIPAddress(String ipAddress) { Matcher matcher = _ipAddressPattern.matcher(ipAddress); return matcher.matches(); } public static boolean isJulianDate(int month, int day, int year) { if ((month < 0) || (month > 11)) { return false; } int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 1) { int febMax = 28; if ((year % 4) == 0) { febMax = 29; } if ((day < 1) || (day > febMax)) { return false; } } else if ((day < 1) || (day > months[month])) { return false; } return true; } public static boolean isLUHN(String number) { if (number == null) { return false; } number = StringUtil.reverse(number); int total = 0; for (int i = 0; i < number.length(); i++) { int x = 0; if (((i + 1) % 2) == 0) { x = Integer.parseInt(number.substring(i, i + 1)) * 2; if (x >= 10) { String s = String.valueOf(x); x = Integer.parseInt(s.substring(0, 1)) + Integer.parseInt(s.substring(1, 2)); } } else { x = Integer.parseInt(number.substring(i, i + 1)); } total = total + x; } if ((total % 10) == 0) { return true; } else { return false; } } public static boolean isName(String name) { if (isNull(name)) { return false; } for (char c : name.trim().toCharArray()) { if (((!isChar(c)) && (!Character.isWhitespace(c))) || (c == CharPool.COMMA)) { return false; } } return true; } public static boolean isNotNull(Long l) { return !isNull(l); } public static boolean isNotNull(Object obj) { return !isNull(obj); } public static boolean isNotNull(Object[] array) { return !isNull(array); } public static boolean isNotNull(String s) { return !isNull(s); } public static boolean isNull(Long l) { if ((l == null) || (l.longValue() == 0)) { return true; } else { return false; } } public static boolean isNull(Object obj) { if (obj instanceof Long) { return isNull((Long)obj); } else if (obj instanceof String) { return isNull((String)obj); } else if (obj == null) { return true; } else { return false; } } public static boolean isNull(Object[] array) { if ((array == null) || (array.length == 0)) { return true; } else { return false; } } public static boolean isNull(String s) { if (s == null) { return true; } int counter = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == CharPool.SPACE) { continue; } else if (counter > 3) { return false; } if (counter == 0) { if (c != CharPool.LOWER_CASE_N) { return false; } } else if (counter == 1) { if (c != CharPool.LOWER_CASE_U) { return false; } } else if ((counter == 2) || (counter == 3)) { if (c != CharPool.LOWER_CASE_L) { return false; } } counter++; } if ((counter == 0) || (counter == 4)) { return true; } return false; } public static boolean isNumber(String number) { if (isNull(number)) { return false; } for (char c : number.toCharArray()) { if (!isDigit(c)) { return false; } } return true; } public static boolean isPassword(String password) { if (isNull(password)) { return false; } if (password.length() < 4) { return false; } for (char c : password.toCharArray()) { if (!isChar(c) && !isDigit(c)) { return false; } } return true; } public static boolean isPhoneNumber(String phoneNumber) { return isNumber(StringUtil.extractDigits(phoneNumber)); } public static boolean isUrl(String url) { if (Validator.isNotNull(url)) { try { new URL(url); return true; } catch (MalformedURLException murle) { } } return false; } public static boolean isVariableName(String variableName) { if (isNull(variableName)) { return false; } Matcher matcher = _variableNamePattern.matcher(variableName); if (matcher.matches()) { return true; } else { return false; } } public static boolean isVariableTerm(String s) { if (s.startsWith(_VARIABLE_TERM_BEGIN) && s.endsWith(_VARIABLE_TERM_END)) { return true; } else { return false; } } public static boolean isWhitespace(char c) { int i = c; if ((i == 0) || Character.isWhitespace(c)) { return true; } else { return false; } } public static boolean isXml(String s) { if (s.startsWith(_XML_BEGIN) || s.startsWith(_XML_EMPTY)) { return true; } else { return false; } } private static final int _CHAR_BEGIN = 65; private static final int _CHAR_END = 122; private static final int _DIGIT_BEGIN = 48; private static final int _DIGIT_END = 57; private static final char[] _EMAIL_ADDRESS_SPECIAL_CHAR = new char[] { '.', '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^', '_', '`', '{', '|', '}', '~' }; private static final String _VARIABLE_TERM_BEGIN = "[$"; private static final String _VARIABLE_TERM_END = "$]"; private static final String _XML_BEGIN = "<?xml"; private static final String _XML_EMPTY = "<root />"; private static Logger _log = Logger.getLogger(Validator.class); private static Pattern _ipAddressPattern = Pattern.compile( "\\b" + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])" + "\\b"); private static MethodKey isValidMethodKey = new MethodKey( "com.liferay.util.mail.InternetAddressUtil", "isValid", String.class); private static Pattern _variableNamePattern = Pattern.compile( "[_a-zA-Z]+[_a-zA-Z0-9]*"); }