From 0211166ffddc5f83218dee37a510f94f1676d574 Mon Sep 17 00:00:00 2001 From: "changhyun1.lee" Date: Wed, 13 Feb 2013 21:17:42 +0900 Subject: [PATCH] [Title] AS for CheckForIRI [Desc.] [Issue] Redmine-8381 Change-Id: I93917c38b4fe9efeb1ff14b0917e1f774044e759 --- .../src/org/tizen/common/util/ValidationUtil.java | 66 ++++++++++++---------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/org.tizen.common/src/org/tizen/common/util/ValidationUtil.java b/org.tizen.common/src/org/tizen/common/util/ValidationUtil.java index 281b87c..ada8fc5 100644 --- a/org.tizen.common/src/org/tizen/common/util/ValidationUtil.java +++ b/org.tizen.common/src/org/tizen/common/util/ValidationUtil.java @@ -40,74 +40,78 @@ import java.util.regex.Pattern; */ public class ValidationUtil { + // IRI(RFC 3987: Internationalized Resource Identifiers) + private static final String REGEX_IRI = "([a-z]([a-z]|\\d|\\+|-|\\.)*):(\\/\\/(((([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])" + + "|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:)*@)?((\\[(|(v[\\da-f]{1,}\\." + + "(([a-z]|\\d|-|\\.|_|~)|[!\\$&'\\(\\)\\*\\+,;=]|:)+))\\])|" + + "((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\." + + "(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]))|" + + "(([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=])*)(:\\d*)?)" + + "(\\/(([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:|@)*)*|" + + "(\\/((([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:|@)+" + + "(\\/(([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:|@)*)*)?)|" + + "((([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:|@)+" + + "(\\/(([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:|@)*)*)|" + + "((([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:|@)){0})" + + "(\\?((([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:|@)|[\\uE000-\\uF8FF]|\\/|\\?)*)?" + + "(\\#((([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:|@)|\\/|\\?)*)?"; + + private static final String REGEX_EMAIL = "[A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])??"; //$NON-NLS-1$ + private static final String REGEX_URL = "\\b(https?)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; //$NON-NLS-1$ + private static final String REGEX_VERSION = "^\\d+\\.\\d+\\.\\d+$"; //$NON-NLS-1$ + /** - * Check for Email address. RFC 2822 (simplified)Matches a normal email address. Does not check the top-level domain. + * Checking for Email address. RFC 2822 (simplified)Matches a normal email address. Does not check the top-level domain. * * @param value email string * - * @return if value is valied email + * @return {@code true} if value is valid email */ public static boolean checkForEmail(String value) { - String regex = "[A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])??"; //$NON-NLS-1$ - Pattern pattern = Pattern.compile(regex); + Pattern pattern = Pattern.compile(REGEX_EMAIL); Matcher matcher = pattern.matcher(value); return matcher.matches(); } /** - * Check for IRI(RFC 3987: Internationalized Resource Identifiers) string. + * Checking for IRI(RFC 3987: Internationalized Resource Identifiers) string. * * @param value IRI string * - * @return if value is valied IRI + * @return {@code true} if value is valid IRI * * @link RFC 3987: http://www.ietf.org/rfc/rfc3987
* Online validation: http://projects.scottsplayground.com/iri/demo/ */ public static boolean checkForIRI(String value) { - String regex = "([a-z]([a-z]|\\d|\\+|-|\\.)*):(\\/\\/(((([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])" + - "|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:)*@)?((\\[(|(v[\\da-f]{1,}\\." + - "(([a-z]|\\d|-|\\.|_|~)|[!\\$&'\\(\\)\\*\\+,;=]|:)+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[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:|@)|[\\uE000-\\uF8FF]|\\/|\\?)*)?" + - "(\\#((([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:|@)|\\/|\\?)*)?"; - Pattern pattern = Pattern.compile(regex); + Pattern pattern = Pattern.compile(REGEX_IRI); Matcher matcher = pattern.matcher(value); return matcher.matches(); } /** - * Check for URL string. + * Checking for URL string. * * @param value url string * - * @return if value is valied url + * @return {@code true} if value is valid url */ public static boolean checkForURL(String value) { - String regex = "\\b(https?)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; //$NON-NLS-1$ - Pattern pattern = Pattern.compile(regex); + Pattern pattern = Pattern.compile(REGEX_URL); Matcher matcher = pattern.matcher(value); return matcher.matches(); } /** - * Check for File extension. + * Checking for File extension. * * @param value file name - * @param extensions valied file extensions + * @param extensions valid file extensions * - * @return if value is valied file + * @return {@code true} if value is valid file */ public static boolean checkForFileExtension(String value, String[] extensions) { if (StringUtil.isEmpty(value) || extensions == null) { @@ -123,10 +127,10 @@ public class ValidationUtil { } /** - * Check for version. The version should be (x.y.z) formate where 0<=x<=35, 0<=y<=35, 0<=z<=1295. + * Checking for version. The version should be (x.y.z) formate where 0<=x<=35, 0<=y<=35, 0<=z<=1295. * * @param version - * @return if version is valied + * @return {@code true} if version is valid version */ public static boolean checkForVersion(String version) { if (version == null) { @@ -134,7 +138,7 @@ public class ValidationUtil { } String strVersion = version.trim(); - if (strVersion.isEmpty() || !strVersion.matches("^\\d+\\.\\d+\\.\\d+$") || strVersion.equals("0.0.0")) { + if (strVersion.isEmpty() || !strVersion.matches(REGEX_VERSION) || strVersion.equals("0.0.0")) { return false; } -- 2.7.4