[Title] Added CheckForIRI function
authorchanghyun1.lee <changhyun1.lee@samsung.com>
Tue, 12 Feb 2013 08:17:31 +0000 (17:17 +0900)
committerBon-Yong Lee <bonyong.lee@samsung.com>
Wed, 13 Feb 2013 08:03:46 +0000 (17:03 +0900)
[Desc.]
[Issue] Redmine-8381

Change-Id: I9fb5839ece2af39827123bc666b80a44baab4e39

org.tizen.common/src/org/tizen/common/util/ValidationUtil.java
org.tizen.common/test/src/org/tizen/common/util/ValidationUtilTest.java [changed mode: 0644->0755]

index 2f5583f..281b87c 100644 (file)
@@ -56,6 +56,37 @@ public class ValidationUtil {
     }
 
     /**
+     * Check for IRI(RFC 3987: Internationalized Resource Identifiers) string.
+     *
+     * @param value IRI string
+     *
+     * @return if value is valied IRI
+     * 
+     * @link RFC 3987: http://www.ietf.org/rfc/rfc3987<br>
+     * 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|-|\\.|_|~)|[!\\$&'\\(\\)\\*\\+,;=]|:)+))\\])|" +
+                "((\\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})|[!\\$&'\\(\\)\\*\\+,;=]|:|@)|\\/|\\?)*)?";
+        Pattern pattern = Pattern.compile(regex);
+        Matcher matcher = pattern.matcher(value);
+
+        return matcher.matches();
+    }
+
+    /**
      * Check for URL string.
      *
      * @param value url string
old mode 100644 (file)
new mode 100755 (executable)
index 2dc395e..77053b2
@@ -99,6 +99,35 @@ public class ValidationUtilTest {
     }
 
     /**
+     * Test {@link ValidationUtil#test_checkForIRI(String value)}
+     *
+     * @throws Exception in case of failure in test
+     *
+     * @see {@link ValidationUtil#test_checkForIRI(String value)}
+     */
+    @Test
+    public void test_checkForIRI() throws Exception {
+        final String[] validedIRIs = {"http://tizen.org",
+                "http://www.tizen.org",
+                "http://tizen.org/",
+                "http://tizen.org/id",
+                "http://myapp",
+                "http://myapp/",
+                "http://bonyong.lee:test@myapp:4000/test.dd/aaa#test",
+                "http://example.org/about/mother.fr",
+                "http://example.org/contact.nl",
+                "http://example.org/contact?lang=nl",
+                "http://example.org/nl/contact",
+                "cxd:dsf",
+                "ftp://fds",
+                "irc://fdf"
+            };
+        for (String url : validedIRIs) {
+            assertTrue(ValidationUtil.checkForIRI(url));
+        }
+    }
+
+    /**
      * Test {@link ValidationUtil#checkForURL(String value)}
      *
      * @throws Exception in case of failure in test