8012178: Cleanup use of Util.escapeHtmlChars
authorjjg
Tue, 14 May 2013 10:14:55 -0700
changeset 17569 ef80738645e2
parent 17568 d9691936f5f9
child 17570 78512b2899db
8012178: Cleanup use of Util.escapeHtmlChars Reviewed-by: darcy
langtools/src/share/classes/com/sun/tools/doclets/formats/html/ClassUseWriter.java
langtools/src/share/classes/com/sun/tools/doclets/formats/html/HtmlDocletWriter.java
langtools/src/share/classes/com/sun/tools/doclets/formats/html/LinkFactoryImpl.java
langtools/src/share/classes/com/sun/tools/doclets/formats/html/PackageUseWriter.java
langtools/src/share/classes/com/sun/tools/doclets/formats/html/PackageWriterImpl.java
langtools/src/share/classes/com/sun/tools/doclets/formats/html/SubWriterHolderWriter.java
langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlDocWriter.java
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/src/share/classes/com/sun/tools/doclets/formats/html/markup/StringContent.java
langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/builders/PackageSummaryBuilder.java
langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ValueTaglet.java
langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/Util.java
--- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/ClassUseWriter.java	Tue May 14 10:14:55 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/ClassUseWriter.java	Tue May 14 10:14:55 2013 -0700
@@ -369,7 +369,7 @@
     protected void addClassUse(PackageDoc pkg, Content contentTree) throws IOException {
         String classLink = getLink(new LinkInfoImpl(configuration,
             LinkInfoImpl.Kind.CLASS_USE_HEADER, classdoc)).toString();
-        String pkgLink = getPackageLinkString(pkg, Util.escapeHtmlChars(Util.getPackageName(pkg)), false);
+        String pkgLink = getPackageLink(pkg, Util.getPackageName(pkg)).toString();
         classSubWriter.addUseInfo(pkgToClassAnnotations.get(pkg.name()),
                 configuration.getText("doclet.ClassUse_Annotation", classLink,
                 pkgLink), classUseTableSummary, contentTree);
--- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/HtmlDocletWriter.java	Tue May 14 10:14:55 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/HtmlDocletWriter.java	Tue May 14 10:14:55 2013 -0700
@@ -991,53 +991,6 @@
      *
      * @param pkg the package to link to.
      * @param label the label for the link.
-     * @param isStrong true if the label should be strong.
-     * @return the link to the given package.
-     */
-    public String getPackageLinkString(PackageDoc pkg, String label,
-                                 boolean isStrong) {
-        return getPackageLinkString(pkg, label, isStrong, "");
-    }
-
-    /**
-     * Return the link to the given package.
-     *
-     * @param pkg the package to link to.
-     * @param label the label for the link.
-     * @param isStrong true if the label should be strong.
-     * @param style  the font of the package link label.
-     * @return the link to the given package.
-     */
-    public String getPackageLinkString(PackageDoc pkg, String label, boolean isStrong,
-            String style) {
-        boolean included = pkg != null && pkg.isIncluded();
-        if (! included) {
-            PackageDoc[] packages = configuration.packages;
-            for (int i = 0; i < packages.length; i++) {
-                if (packages[i].equals(pkg)) {
-                    included = true;
-                    break;
-                }
-            }
-        }
-        if (included || pkg == null) {
-            return getHyperLinkString(pathString(pkg, DocPaths.PACKAGE_SUMMARY),
-                                label, isStrong, style);
-        } else {
-            DocLink crossPkgLink = getCrossPackageLink(Util.getPackageName(pkg));
-            if (crossPkgLink != null) {
-                return getHyperLinkString(crossPkgLink, label, isStrong, style);
-            } else {
-                return label;
-            }
-        }
-    }
-
-    /**
-     * Return the link to the given package.
-     *
-     * @param pkg the package to link to.
-     * @param label the label for the link.
      * @return a content tree for the package link.
      */
     public Content getPackageLink(PackageDoc pkg, String label) {
@@ -1302,6 +1255,10 @@
             String label, boolean strong) {
         return getDocLink(context, classDoc, doc, label, strong, false);
     }
+    public Content getDocLink(LinkInfoImpl.Kind context, ClassDoc classDoc, MemberDoc doc,
+            Content label, boolean strong) {
+        return getDocLink(context, classDoc, doc, label, strong, false);
+    }
 
    /**
      * Return the link for the given member.
@@ -1318,7 +1275,12 @@
      */
     public Content getDocLink(LinkInfoImpl.Kind context, ClassDoc classDoc, MemberDoc doc,
             String label, boolean strong, boolean isProperty) {
-        return getDocLink(context, classDoc, doc, new RawHtml(label), strong, isProperty);
+        return getDocLink(context, classDoc, doc, new StringContent(check(label)), strong, isProperty);
+    }
+
+    String check(String s) {
+        if (s.matches(".*[&<>].*"))throw new IllegalArgumentException(s);
+        return s;
     }
 
     public Content getDocLink(LinkInfoImpl.Kind context, ClassDoc classDoc, MemberDoc doc,
@@ -1487,7 +1449,7 @@
             text = plainOrCode(plain, new StringContent(refMemName));
 
             return getDocLink(LinkInfoImpl.Kind.SEE_TAG, containing,
-                refMem, (label.isEmpty() ? text: label).toString(), false);
+                refMem, (label.isEmpty() ? text: label), false);
         }
     }
 
--- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/LinkFactoryImpl.java	Tue May 14 10:14:55 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/LinkFactoryImpl.java	Tue May 14 10:14:55 2013 -0700
@@ -179,16 +179,16 @@
                 classDoc.name());
         } else if (classDoc.isInterface()){
             return configuration.getText("doclet.Href_Interface_Title",
-                Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage())));
+                Util.getPackageName(classDoc.containingPackage()));
         } else if (classDoc.isAnnotationType()) {
             return configuration.getText("doclet.Href_Annotation_Title",
-                Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage())));
+                Util.getPackageName(classDoc.containingPackage()));
         } else if (classDoc.isEnum()) {
             return configuration.getText("doclet.Href_Enum_Title",
-                Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage())));
+                Util.getPackageName(classDoc.containingPackage()));
         } else {
             return configuration.getText("doclet.Href_Class_Title",
-                Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage())));
+                Util.getPackageName(classDoc.containingPackage()));
         }
     }
 
--- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/PackageUseWriter.java	Tue May 14 10:14:55 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/PackageUseWriter.java	Tue May 14 10:14:55 2013 -0700
@@ -154,7 +154,7 @@
         Content table = HtmlTree.TABLE(0, 3, 0, useTableSummary,
                 getTableCaption(configuration.getText(
                 "doclet.ClassUse_Packages.that.use.0",
-                getPackageLinkString(pkgdoc, Util.escapeHtmlChars(Util.getPackageName(pkgdoc)), false))));
+                getPackageLink(pkgdoc, Util.getPackageName(pkgdoc)).toString())));
         table.addContent(getSummaryTableHeader(packageTableHeader, "col"));
         Content tbody = new HtmlTree(HtmlTag.TBODY);
         Iterator<String> it = usingPackageToUsedClasses.keySet().iterator();
@@ -199,8 +199,8 @@
             Content table = HtmlTree.TABLE(0, 3, 0, tableSummary,
                     getTableCaption(configuration.getText(
                     "doclet.ClassUse_Classes.in.0.used.by.1",
-                    getPackageLinkString(pkgdoc, Util.escapeHtmlChars(Util.getPackageName(pkgdoc)), false),
-                    getPackageLinkString(usingPackage, Util.escapeHtmlChars(Util.getPackageName(usingPackage)), false))));
+                    getPackageLink(pkgdoc, Util.getPackageName(pkgdoc)).toString(),
+                    getPackageLink(usingPackage, Util.getPackageName(usingPackage)).toString())));
             table.addContent(getSummaryTableHeader(classTableHeader, "col"));
             Content tbody = new HtmlTree(HtmlTag.TBODY);
             Iterator<ClassDoc> itc =
--- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/PackageWriterImpl.java	Tue May 14 10:14:55 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/PackageWriterImpl.java	Tue May 14 10:14:55 2013 -0700
@@ -102,7 +102,7 @@
         Content tHeading = HtmlTree.HEADING(HtmlConstants.TITLE_HEADING, true,
                 HtmlStyle.title, packageLabel);
         tHeading.addContent(getSpace());
-        Content packageHead = new RawHtml(heading);
+        Content packageHead = new StringContent(heading);
         tHeading.addContent(packageHead);
         div.addContent(tHeading);
         addDeprecationInfo(div);
--- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/SubWriterHolderWriter.java	Tue May 14 10:14:55 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/SubWriterHolderWriter.java	Tue May 14 10:14:55 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 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
@@ -135,10 +135,8 @@
      * @return the content tree for the method type link
      */
     public Content getMethodTypeLinks(MethodTypes methodType) {
-        StringBuilder jsShow = new StringBuilder("javascript:show(");
-        jsShow.append(methodType.value()).append(");");
-        HtmlTree link = HtmlTree.A(jsShow.toString(),
-                new StringContent(methodType.text()));
+        String jsShow = "javascript:show(" + methodType.value() +");";
+        HtmlTree link = HtmlTree.A(jsShow, new StringContent(methodType.text()));
         return link;
     }
 
--- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlDocWriter.java	Tue May 14 10:14:55 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlDocWriter.java	Tue May 14 10:14:55 2013 -0700
@@ -71,25 +71,10 @@
      */
     public abstract Configuration configuration();
 
-    /**
-     * Return Html hyperlink string.
-     *
-     * @param link       String name of the file.
-     * @param label      Tag for the link.
-     * @return String    Hyper Link.
-     */
-    public String getHyperLinkString(DocPath link, String label) {
-        return getHyperLinkString(link, label, false, "", "", "");
-    }
-
     public Content getHyperLink(DocPath link, String label) {
         return getHyperLink(link, new StringContent(label), false, "", "", "");
     }
 
-    public String getHyperLinkString(DocLink link, String label) {
-        return getHyperLinkString(link, label, false, "", "", "");
-    }
-
     /**
      * Get Html Hyper Link string.
      *
--- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlTree.java	Tue May 14 10:14:55 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlTree.java	Tue May 14 10:14:55 2013 -0700
@@ -78,8 +78,8 @@
      */
     public void addAttr(HtmlAttr attrName, String attrValue) {
         if (attrs.isEmpty())
-            attrs = new LinkedHashMap<HtmlAttr,String>();
-        attrs.put(nullCheck(attrName), nullCheck(attrValue));
+            attrs = new LinkedHashMap<HtmlAttr,String>(3);
+        attrs.put(nullCheck(attrName), escapeHtmlChars(attrValue));
     }
 
     /**
@@ -131,6 +131,35 @@
     }
 
     /**
+     * Given a string, escape all special html characters and
+     * return the result.
+     *
+     * @param s The string to check.
+     * @return the original string with all of the HTML characters escaped.
+     */
+    private static String escapeHtmlChars(String s) {
+        for (int i = 0; i < s.length(); i++) {
+            char ch = s.charAt(i);
+            switch (ch) {
+                // only start building a new string if we need to
+                case '<': case '>': case '&':
+                    StringBuilder sb = new StringBuilder(s.substring(0, i));
+                    for ( ; i < s.length(); i++) {
+                        ch = s.charAt(i);
+                        switch (ch) {
+                            case '<': sb.append("&lt;");  break;
+                            case '>': sb.append("&gt;");  break;
+                            case '&': sb.append("&amp;"); break;
+                            default:  sb.append(ch);      break;
+                        }
+                    }
+                    return sb.toString();
+            }
+        }
+        return s;
+    }
+
+    /**
      * Generates an HTML anchor tag.
      *
      * @param ref reference url for the anchor tag
@@ -139,7 +168,7 @@
      */
     public static HtmlTree A(String ref, Content body) {
         HtmlTree htmltree = new HtmlTree(HtmlTag.A, nullCheck(body));
-        htmltree.addAttr(HtmlAttr.HREF, Util.escapeHtmlChars(nullCheck(ref)));
+        htmltree.addAttr(HtmlAttr.HREF, ref);
         return htmltree;
     }
 
@@ -324,7 +353,7 @@
             HtmlStyle styleClass, Content body) {
         HtmlTree htmltree = new HtmlTree(headingTag, nullCheck(body));
         if (printTitle)
-            htmltree.addAttr(HtmlAttr.TITLE, Util.stripHtml(body.toString()));
+            htmltree.addAttr(HtmlAttr.TITLE, stripHtml(body));
         if (styleClass != null)
             htmltree.addStyle(styleClass);
         return htmltree;
@@ -837,4 +866,22 @@
             return false;
         }
     }
+
+    /**
+     * Given a Content node, strips all html characters and
+     * return the result.
+     *
+     * @param body The content node to check.
+     * @return the plain text from the content node
+     *
+     */
+    private static String stripHtml(Content body) {
+        String rawString = body.toString();
+        // remove HTML tags
+        rawString = rawString.replaceAll("\\<.*?>", " ");
+        // consolidate multiple spaces between a word to a single space
+        rawString = rawString.replaceAll("\\b\\s{2,}\\b", " ");
+        // remove extra whitespaces
+        return rawString.trim();
+    }
 }
--- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlWriter.java	Tue May 14 10:14:55 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlWriter.java	Tue May 14 10:14:55 2013 -0700
@@ -197,8 +197,7 @@
                 configuration.getText("doclet.Modifier"),
                 configuration.getText("doclet.Type"));
         overviewLabel = getResource("doclet.Overview");
-        defaultPackageLabel = new RawHtml(
-                Util.escapeHtmlChars(DocletConstants.DEFAULT_PACKAGE_NAME));
+        defaultPackageLabel = new StringContent(DocletConstants.DEFAULT_PACKAGE_NAME);
         packageLabel = getResource("doclet.Package");
         profileLabel = getResource("doclet.Profile");
         useLabel = getResource("doclet.navClassUse");
--- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/StringContent.java	Tue May 14 10:14:55 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/StringContent.java	Tue May 14 10:14:55 2013 -0700
@@ -58,8 +58,8 @@
      * @param initialContent initial content for the object
      */
     public StringContent(String initialContent) {
-        stringContent = new StringBuilder(
-                Util.escapeHtmlChars(nullCheck(initialContent)));
+        stringContent = new StringBuilder();
+        appendChars(initialContent);
     }
 
     /**
@@ -81,7 +81,7 @@
      * @param strContent string content to be added
      */
     public void addContent(String strContent) {
-        stringContent.append(Util.escapeHtmlChars(nullCheck(strContent)));
+        appendChars(strContent);
     }
 
     /**
@@ -111,4 +111,16 @@
         out.write(s);
         return s.endsWith(DocletConstants.NL);
     }
+
+    private void appendChars(String s) {
+        for (int i = 0; i < s.length(); i++) {
+            char ch = s.charAt(i);
+            switch (ch) {
+                case '<': stringContent.append("&lt;");  break;
+                case '>': stringContent.append("&gt;");  break;
+                case '&': stringContent.append("&amp;"); break;
+                default:  stringContent.append(ch);      break;
+            }
+        }
+    }
 }
--- a/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/builders/PackageSummaryBuilder.java	Tue May 14 10:14:55 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/builders/PackageSummaryBuilder.java	Tue May 14 10:14:55 2013 -0700
@@ -120,8 +120,7 @@
      * @param contentTree the content tree to which the documentation will be added
      */
     public void buildPackageDoc(XMLNode node, Content contentTree) throws Exception {
-        contentTree = packageWriter.getPackageHeader(
-                Util.escapeHtmlChars(Util.getPackageName(packageDoc)));
+        contentTree = packageWriter.getPackageHeader(Util.getPackageName(packageDoc));
         buildChildren(node, contentTree);
         packageWriter.addPackageFooter(contentTree);
         packageWriter.printDocument(contentTree);
--- a/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ValueTaglet.java	Tue May 14 10:14:55 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ValueTaglet.java	Tue May 14 10:14:55 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 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
@@ -28,6 +28,7 @@
 import java.util.*;
 
 import com.sun.javadoc.*;
+import com.sun.tools.doclets.formats.html.markup.StringContent;
 import com.sun.tools.doclets.internal.toolkit.Configuration;
 import com.sun.tools.doclets.internal.toolkit.util.*;
 
@@ -169,7 +170,7 @@
                 "doclet.value_tag_invalid_reference", tag.text());
         } else if (field.constantValue() != null) {
             return writer.valueTagOutput(field,
-                Util.escapeHtmlChars(field.constantValueExpression()),
+                field.constantValueExpression(),
                 ! field.equals(tag.holder()));
         } else {
             //Referenced field is not a constant.
--- a/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/Util.java	Tue May 14 10:14:55 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/Util.java	Tue May 14 10:14:55 2013 -0700
@@ -420,69 +420,6 @@
     }
 
     /**
-     * Given a string, escape all special html characters and
-     * return the result.
-     *
-     * @param s The string to check.
-     * @return the original string with all of the HTML characters escaped.
-     */
-    public static String escapeHtmlChars(String s) {
-        for (int i = 0; i < s.length(); i++) {
-            char ch = s.charAt(i);
-            switch (ch) {
-                // only start building a new string if we need to
-                case '<': case '>': case '&':
-                    StringBuilder sb = new StringBuilder(s.substring(0, i));
-                    for ( ; i < s.length(); i++) {
-                        ch = s.charAt(i);
-                        switch (ch) {
-                            case '<': sb.append("&lt;");  break;
-                            case '>': sb.append("&gt;");  break;
-                            case '&': sb.append("&amp;"); break;
-                            default:  sb.append(ch);      break;
-                        }
-                    }
-                    return sb.toString();
-            }
-        }
-        return s;
-    }
-
-    /**
-     * Escape all special html characters in a string buffer.
-     *
-     * @param sb The string buffer to update
-     */
-    public static void escapeHtmlChars(StringBuilder sb) {
-        // scan backwards, replacing characters as needed.
-        for (int i = sb.length() - 1; i >= 0; i--) {
-            switch (sb.charAt(i)) {
-                case '<': sb.replace(i, i+1, "&lt;"); break;
-                case '>': sb.replace(i, i+1, "&gt;"); break;
-                case '&': sb.replace(i, i+1, "&amp;"); break;
-            }
-        }
-    }
-
-    /**
-     * Given a string, strips all html characters and
-     * return the result.
-     *
-     * @param rawString The string to check.
-     * @return the original string with all of the HTML characters
-     * stripped.
-     *
-     */
-    public static String stripHtml(String rawString) {
-        // remove HTML tags
-        rawString = rawString.replaceAll("\\<.*?>", " ");
-        // consolidate multiple spaces between a word to a single space
-        rawString = rawString.replaceAll("\\b\\s{2,}\\b", " ");
-        // remove extra whitespaces
-        return rawString.trim();
-    }
-
-    /**
      * Given an annotation, return true if it should be documented and false
      * otherwise.
      *