8016328: Regression : Javadoc i18n regression caused by fix for 8012375
authorbpatel
Tue, 24 Sep 2013 16:12:06 -0700
changeset 20257 9da4f6c30089
parent 20256 9154c5cc0d9f
child 20258 bdaa691b4da4
child 20261 1f40f52b3ccc
8016328: Regression : Javadoc i18n regression caused by fix for 8012375 Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlTree.java
langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlWriter.java
langtools/test/com/sun/javadoc/testHref/TestHref.java
langtools/test/com/sun/javadoc/testJavascript/TestJavascript.java
langtools/test/com/sun/javadoc/testLinkTaglet/TestLinkTaglet.java
langtools/test/com/sun/javadoc/testPrivateClasses/TestPrivateClasses.java
langtools/test/com/sun/javadoc/testUseOption/TestUseOption.java
--- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlTree.java	Tue Sep 24 13:48:12 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlTree.java	Tue Sep 24 16:12:06 2013 -0700
@@ -28,6 +28,7 @@
 import java.io.IOException;
 import java.io.Writer;
 import java.util.*;
+import java.nio.charset.*;
 
 import com.sun.tools.doclets.internal.toolkit.Content;
 import com.sun.tools.doclets.internal.toolkit.util.*;
@@ -164,6 +165,46 @@
     }
 
     /**
+     * A set of ASCII URI characters to be left unencoded.
+     */
+    public static BitSet NONENCODING_CHARS = new BitSet(256);
+
+    static {
+        // alphabetic characters
+        for (int i = 'a'; i <= 'z'; i++) {
+            NONENCODING_CHARS.set(i);
+        }
+        for (int i = 'A'; i <= 'Z'; i++) {
+            NONENCODING_CHARS.set(i);
+        }
+        // numeric characters
+        for (int i = '0'; i <= '9'; i++) {
+            NONENCODING_CHARS.set(i);
+        }
+        // Reserved characters as per RFC 3986. These are set of delimiting characters.
+        String noEnc = ":/?#[]@!$&'()*+,;=";
+        // Unreserved characters as per RFC 3986 which should not be percent encoded.
+        noEnc += "-._~";
+        for (int i = 0; i < noEnc.length(); i++) {
+            NONENCODING_CHARS.set(noEnc.charAt(i));
+        }
+    }
+
+    private static String encodeURL(String url) {
+        byte[] urlBytes = url.getBytes(Charset.forName("UTF-8"));
+        StringBuilder sb = new StringBuilder();
+        for (int i = 0; i < urlBytes.length; i++) {
+            int c = urlBytes[i];
+            if (NONENCODING_CHARS.get(c & 0xFF)) {
+                sb.append((char) c);
+            } else {
+                sb.append(String.format("%%%02X", c & 0xFF));
+            }
+        }
+        return sb.toString();
+    }
+
+    /**
      * Generates an HTML anchor tag.
      *
      * @param ref reference url for the anchor tag
@@ -172,7 +213,7 @@
      */
     public static HtmlTree A(String ref, Content body) {
         HtmlTree htmltree = new HtmlTree(HtmlTag.A, nullCheck(body));
-        htmltree.addAttr(HtmlAttr.HREF, ref);
+        htmltree.addAttr(HtmlAttr.HREF, encodeURL(ref));
         return htmltree;
     }
 
--- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlWriter.java	Tue Sep 24 13:48:12 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlWriter.java	Tue Sep 24 16:12:06 2013 -0700
@@ -335,6 +335,12 @@
                 "    if (targetPage.indexOf(\":\") != -1 || (targetPage != \"\" && !validURL(targetPage)))" + DocletConstants.NL +
                 "        targetPage = \"undefined\";" + DocletConstants.NL +
                 "    function validURL(url) {" + DocletConstants.NL +
+                "        try {" + DocletConstants.NL +
+                "            url = decodeURIComponent(url);" + DocletConstants.NL +
+                "        }" + DocletConstants.NL +
+                "        catch (error) {" + DocletConstants.NL +
+                "            return false;" + DocletConstants.NL +
+                "        }" + DocletConstants.NL +
                 "        var pos = url.indexOf(\".html\");" + DocletConstants.NL +
                 "        if (pos == -1 || pos != url.length - 5)" + DocletConstants.NL +
                 "            return false;" + DocletConstants.NL +
@@ -346,7 +352,8 @@
                 "            if ('a' <= ch && ch <= 'z' ||" + DocletConstants.NL +
                 "                    'A' <= ch && ch <= 'Z' ||" + DocletConstants.NL +
                 "                    ch == '$' ||" + DocletConstants.NL +
-                "                    ch == '_') {" + DocletConstants.NL +
+                "                    ch == '_' ||" + DocletConstants.NL +
+                "                    ch.charCodeAt(0) > 127) {" + DocletConstants.NL +
                 "                allowNumber = true;" + DocletConstants.NL +
                 "                allowSep = true;" + DocletConstants.NL +
                 "            } else if ('0' <= ch && ch <= '9'" + DocletConstants.NL +
--- a/langtools/test/com/sun/javadoc/testHref/TestHref.java	Tue Sep 24 13:48:12 2013 -0700
+++ b/langtools/test/com/sun/javadoc/testHref/TestHref.java	Tue Sep 24 16:12:06 2013 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug      4663254
+ * @bug      4663254 8016328
  * @summary  Verify that spaces do not appear in hrefs and anchors.
  * @author   jamieh
  * @library  ../lib/
@@ -46,11 +46,11 @@
     private static final String[][] TEST = {
         //External link.
         {BUG_ID + FS + "pkg" + FS + "C1.html",
-            "href=\"http://java.sun.com/j2se/1.4/docs/api/java/lang/Object.html?is-external=true#wait(long, int)\""
+            "href=\"http://java.sun.com/j2se/1.4/docs/api/java/lang/Object.html?is-external=true#wait(long,%20int)\""
         },
         //Member summary table link.
         {BUG_ID + FS + "pkg" + FS + "C1.html",
-            "href=\"../pkg/C1.html#method(int, int, java.util.ArrayList)\""
+            "href=\"../pkg/C1.html#method(int,%20int,%20java.util.ArrayList)\""
         },
         //Anchor test.
         {BUG_ID + FS + "pkg" + FS + "C1.html",
@@ -66,11 +66,11 @@
         },
         //{@link} test.
         {BUG_ID + FS + "pkg" + FS + "C2.html",
-            "Link: <a href=\"../pkg/C1.html#method(int, int, java.util.ArrayList)\">"
+            "Link: <a href=\"../pkg/C1.html#method(int,%20int,%20java.util.ArrayList)\">"
         },
         //@see test.
         {BUG_ID + FS + "pkg" + FS + "C2.html",
-            "See Also:</span></dt>" + NL + "<dd><a href=\"../pkg/C1.html#method(int, int, java.util.ArrayList)\">"
+            "See Also:</span></dt>" + NL + "<dd><a href=\"../pkg/C1.html#method(int,%20int,%20java.util.ArrayList)\">"
         },
 
         //Header does not link to the page itself.
--- a/langtools/test/com/sun/javadoc/testJavascript/TestJavascript.java	Tue Sep 24 13:48:12 2013 -0700
+++ b/langtools/test/com/sun/javadoc/testJavascript/TestJavascript.java	Tue Sep 24 16:12:06 2013 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug      4665566 4855876 7025314 8012375 8015997
+ * @bug      4665566 4855876 7025314 8012375 8015997 8016328
  * @summary  Verify that the output has the right javascript.
  * @author   jamieh
  * @library  ../lib/
@@ -56,6 +56,12 @@
             "    if (targetPage.indexOf(\":\") != -1 || (targetPage != \"\" && !validURL(targetPage)))" + NL +
             "        targetPage = \"undefined\";" + NL +
             "    function validURL(url) {" + NL +
+            "        try {" + NL +
+            "            url = decodeURIComponent(url);" + NL +
+            "        }" + NL +
+            "        catch (error) {" + NL +
+            "            return false;" + NL +
+            "        }" + NL +
             "        var pos = url.indexOf(\".html\");" + NL +
             "        if (pos == -1 || pos != url.length - 5)" + NL +
             "            return false;" + NL +
@@ -67,7 +73,8 @@
             "            if ('a' <= ch && ch <= 'z' ||" + NL +
             "                    'A' <= ch && ch <= 'Z' ||" + NL +
             "                    ch == '$' ||" + NL +
-            "                    ch == '_') {" + NL +
+            "                    ch == '_' ||" + NL +
+            "                    ch.charCodeAt(0) > 127) {" + NL +
             "                allowNumber = true;" + NL +
             "                allowSep = true;" + NL +
             "            } else if ('0' <= ch && ch <= '9'" + NL +
--- a/langtools/test/com/sun/javadoc/testLinkTaglet/TestLinkTaglet.java	Tue Sep 24 13:48:12 2013 -0700
+++ b/langtools/test/com/sun/javadoc/testLinkTaglet/TestLinkTaglet.java	Tue Sep 24 16:12:06 2013 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug      4732864 6280605 7064544 8014636
+ * @bug      4732864 6280605 7064544 8014636 8016328
  * @summary  Make sure that you can link from one member to another using
  *           non-qualified name, furthermore, ensure the right one is linked.
  * @author   jamieh
@@ -49,9 +49,9 @@
             "Qualified Link: <a href=\"../pkg/C.InnerC.html\" title=\"class in pkg\"><code>C.InnerC</code></a>.<br/>" + NL +
             " Unqualified Link1: <a href=\"../pkg/C.InnerC.html\" title=\"class in pkg\"><code>C.InnerC</code></a>.<br/>" + NL +
             " Unqualified Link2: <a href=\"../pkg/C.InnerC.html\" title=\"class in pkg\"><code>C.InnerC</code></a>.<br/>" + NL +
-            " Qualified Link: <a href=\"../pkg/C.html#method(pkg.C.InnerC, pkg.C.InnerC2)\"><code>method(pkg.C.InnerC, pkg.C.InnerC2)</code></a>.<br/>" + NL +
-            " Unqualified Link: <a href=\"../pkg/C.html#method(pkg.C.InnerC, pkg.C.InnerC2)\"><code>method(C.InnerC, C.InnerC2)</code></a>.<br/>" + NL +
-            " Unqualified Link: <a href=\"../pkg/C.html#method(pkg.C.InnerC, pkg.C.InnerC2)\"><code>method(InnerC, InnerC2)</code></a>.<br/>"
+            " Qualified Link: <a href=\"../pkg/C.html#method(pkg.C.InnerC,%20pkg.C.InnerC2)\"><code>method(pkg.C.InnerC, pkg.C.InnerC2)</code></a>.<br/>" + NL +
+            " Unqualified Link: <a href=\"../pkg/C.html#method(pkg.C.InnerC,%20pkg.C.InnerC2)\"><code>method(C.InnerC, C.InnerC2)</code></a>.<br/>" + NL +
+            " Unqualified Link: <a href=\"../pkg/C.html#method(pkg.C.InnerC,%20pkg.C.InnerC2)\"><code>method(InnerC, InnerC2)</code></a>.<br/>"
         },
         {BUG_ID + FS + "pkg" + FS + "C.InnerC.html",
             "Link to member in outer class: <a href=\"../pkg/C.html#MEMBER\"><code>C.MEMBER</code></a> <br/>" + NL +
--- a/langtools/test/com/sun/javadoc/testPrivateClasses/TestPrivateClasses.java	Tue Sep 24 13:48:12 2013 -0700
+++ b/langtools/test/com/sun/javadoc/testPrivateClasses/TestPrivateClasses.java	Tue Sep 24 16:12:06 2013 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug      4780441 4874845 4978816 8014017
+ * @bug      4780441 4874845 4978816 8014017 8016328
  * @summary  Make sure that when the -private flag is not used, members
  *           inherited from package private class are documented in the child.
  *
@@ -177,7 +177,7 @@
         // Should document that a method overrides method from private class.
        {BUG_ID + "-2" + FS + "pkg" + FS + "PublicChild.html",
             "<dt><span class=\"strong\">Overrides:</span></dt>" + NL +
-            "<dd><code><a href=\"../pkg/PrivateParent.html#methodOverridenFromParent(char[], int, T, V, java.util.List)\">" +
+            "<dd><code><a href=\"../pkg/PrivateParent.html#methodOverridenFromParent(char[],%20int,%20T,%20V,%20java.util.List)\">" +
             "methodOverridenFromParent</a></code>&nbsp;in class&nbsp;<code>" +
             "<a href=\"../pkg/PrivateParent.html\" title=\"class in pkg\">" +
             "PrivateParent</a></code></dd>"},
--- a/langtools/test/com/sun/javadoc/testUseOption/TestUseOption.java	Tue Sep 24 13:48:12 2013 -0700
+++ b/langtools/test/com/sun/javadoc/testUseOption/TestUseOption.java	Tue Sep 24 16:12:06 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 4496290 4985072 7006178 7068595
+ * @bug 4496290 4985072 7006178 7068595 8016328
  * @summary A simple test to determine if -use works.
  * @author jamieh
  * @library ../lib/
@@ -60,7 +60,7 @@
                  "UsedInC</a> in <a href=\"../package-summary.html\">&lt;Unnamed&gt;</a>"
         },
         {BUG_ID + "-3" + FS + "package-use.html", "<td class=\"colOne\">" +
-                 "<a href=\"class-use/UsedInC.html#&lt;Unnamed&gt;\">UsedInC</a>&nbsp;</td>"
+                 "<a href=\"class-use/UsedInC.html#%3CUnnamed%3E\">UsedInC</a>&nbsp;</td>"
         }
     };