commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1649095 - /commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java
Date Fri, 02 Jan 2015 19:12:59 GMT
Author: sebb
Date: Fri Jan  2 19:12:59 2015
New Revision: 1649095

URL: http://svn.apache.org/r1649095
Log:
Initial pass at handling XN-- TLD names

Modified:
    commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java

Modified: commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java?rev=1649095&r1=1649094&r2=1649095&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java
(original)
+++ commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java
Fri Jan  2 19:12:59 2015
@@ -23,6 +23,7 @@ import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.net.HttpURLConnection;
 import java.net.URL;
@@ -176,20 +177,36 @@ public class DomainValidatorTest extends
         System.out.println("Entries missing from TLD List\n");
         String line;
         final String header;
+        final String version;
         line = br.readLine(); // header
         if (line.startsWith("# Version ")) {
             header = line.substring(2);
             System.out.println("        // Taken from " + header);
+            version = header.substring(0,header.indexOf(","));
         } else {
             br.close();
             throw new IOException("File does not have expected Version header");
         }
+        final Method toUnicode = getIDNMethod();
+        if (toUnicode == null) {
+            System.err.println("Cannot convert XN-- entries (no access to java.net.IDN)");
+        }
         while((line = br.readLine()) != null) {
-            if (!line.startsWith("#") && !line.startsWith("XN--")) {
-                if (!dv.isValidTld(line)) {
-                    System.out.println("        \""+line.toLowerCase(Locale.ENGLISH)+"\",");
+            if (!line.startsWith("#")) {
+                final String item;
+                if (line.startsWith("XN--")) {
+                    if (toUnicode != null) {
+                        item = toUnicode(toUnicode, line);                        
+                    } else {
+                        continue;
+                    }
+                } else {
+                    item = line.toLowerCase(Locale.ENGLISH);
+                }
+                if (!dv.isValidTld(item)) {
+                    System.out.println("        \""+item+"\", // " + line + " added from
" + version);
                 }
-                ianaTlds.add(line.toLowerCase(Locale.ENGLISH));
+                ianaTlds.add(item);
             }
         }
         br.close();
@@ -201,6 +218,23 @@ public class DomainValidatorTest extends
         // Don't check local TLDS isInIanaList("LOCAL_TLDS", ianaTlds);
     }
 
+    private static String toUnicode(Method m, String line) {
+        try {
+            return (String) m.invoke(null, new String[]{line.toLowerCase(Locale.ENGLISH)});
+        } catch (Exception e) {
+        }
+        return line;
+    }
+
+    private static Method getIDNMethod() {
+        try {
+            Class clazz = Class.forName("java.net.IDN", false, DomainValidatorTest.class.getClassLoader());
+            return clazz.getDeclaredMethod("toUnicode", new Class[]{String.class});
+        } catch (Exception e) {
+          return null;
+        }
+    }
+
     // isInIanaList and isSorted are split into two methods.
     // If/when access to the arrays is possible without reflection, the intermediate
     // methods can be dropped



Mime
View raw message