diff --git a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/ClassUseWriter.java b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/ClassUseWriter.java index fcce1e75fde..8904c2997e8 100644 --- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/ClassUseWriter.java +++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/ClassUseWriter.java @@ -369,7 +369,7 @@ public class ClassUseWriter extends SubWriterHolderWriter { 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); diff --git a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/HtmlDocletWriter.java b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/HtmlDocletWriter.java index d9641615368..9a1279d7ace 100644 --- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/HtmlDocletWriter.java +++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/HtmlDocletWriter.java @@ -986,53 +986,6 @@ public class HtmlDocletWriter extends HtmlDocWriter { return pathToRoot.resolve(DocPath.forPackage(pd).resolve(name)); } - /** - * 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. - * @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. * @@ -1302,6 +1255,10 @@ public class HtmlDocletWriter extends HtmlDocWriter { 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 class HtmlDocletWriter extends HtmlDocWriter { */ 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 @@ public class HtmlDocletWriter extends HtmlDocWriter { 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); } } diff --git a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/LinkFactoryImpl.java b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/LinkFactoryImpl.java index e4662fe1f00..262d3163c2a 100644 --- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/LinkFactoryImpl.java +++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/LinkFactoryImpl.java @@ -179,16 +179,16 @@ public class LinkFactoryImpl extends LinkFactory { 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())); } } diff --git a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/PackageUseWriter.java b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/PackageUseWriter.java index 5ea25ca360b..496c6fa24ef 100644 --- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/PackageUseWriter.java +++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/PackageUseWriter.java @@ -154,7 +154,7 @@ public class PackageUseWriter extends SubWriterHolderWriter { 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 it = usingPackageToUsedClasses.keySet().iterator(); @@ -199,8 +199,8 @@ public class PackageUseWriter extends SubWriterHolderWriter { 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 itc = diff --git a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/PackageWriterImpl.java b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/PackageWriterImpl.java index 5d8c16e2ccb..2dae1b188e7 100644 --- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/PackageWriterImpl.java +++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/PackageWriterImpl.java @@ -102,7 +102,7 @@ public class PackageWriterImpl extends HtmlDocletWriter 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); diff --git a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/SubWriterHolderWriter.java b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/SubWriterHolderWriter.java index f3af8432e24..94bde2e35ec 100644 --- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/SubWriterHolderWriter.java +++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/SubWriterHolderWriter.java @@ -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 @@ public abstract class SubWriterHolderWriter extends HtmlDocletWriter { * @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; } diff --git a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlDocWriter.java b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlDocWriter.java index caa94f90023..6d9caf32274 100644 --- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlDocWriter.java +++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlDocWriter.java @@ -71,25 +71,10 @@ public abstract class HtmlDocWriter extends HtmlWriter { */ 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. * diff --git a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlTree.java b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlTree.java index 2c0cab13a75..a7dc40196ca 100644 --- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlTree.java +++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlTree.java @@ -78,8 +78,8 @@ public class HtmlTree extends Content { */ public void addAttr(HtmlAttr attrName, String attrValue) { if (attrs.isEmpty()) - attrs = new LinkedHashMap(); - attrs.put(nullCheck(attrName), nullCheck(attrValue)); + attrs = new LinkedHashMap(3); + attrs.put(nullCheck(attrName), escapeHtmlChars(attrValue)); } /** @@ -130,6 +130,35 @@ public class HtmlTree extends Content { return n; } + /** + * 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("<"); break; + case '>': sb.append(">"); break; + case '&': sb.append("&"); break; + default: sb.append(ch); break; + } + } + return sb.toString(); + } + } + return s; + } + /** * Generates an HTML anchor tag. * @@ -139,7 +168,7 @@ public class HtmlTree extends Content { */ 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 @@ public class HtmlTree extends Content { 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 @@ public class HtmlTree extends Content { 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(); + } } diff --git a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlWriter.java b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlWriter.java index 55b3c7d231c..6fe85c0ee6d 100644 --- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlWriter.java +++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlWriter.java @@ -197,8 +197,7 @@ public class HtmlWriter { 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"); diff --git a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/StringContent.java b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/StringContent.java index 65d5c3c0584..21d67c00cfa 100644 --- a/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/StringContent.java +++ b/langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/StringContent.java @@ -58,8 +58,8 @@ public class StringContent extends Content { * @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 @@ public class StringContent extends Content { * @param strContent string content to be added */ public void addContent(String strContent) { - stringContent.append(Util.escapeHtmlChars(nullCheck(strContent))); + appendChars(strContent); } /** @@ -111,4 +111,16 @@ public class StringContent extends Content { 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("<"); break; + case '>': stringContent.append(">"); break; + case '&': stringContent.append("&"); break; + default: stringContent.append(ch); break; + } + } + } } diff --git a/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/builders/PackageSummaryBuilder.java b/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/builders/PackageSummaryBuilder.java index 2e51227735e..b6e8fecc133 100644 --- a/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/builders/PackageSummaryBuilder.java +++ b/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/builders/PackageSummaryBuilder.java @@ -120,8 +120,7 @@ public class PackageSummaryBuilder extends AbstractBuilder { * @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); diff --git a/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ValueTaglet.java b/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ValueTaglet.java index 90794bff625..bda8eb175d0 100644 --- a/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ValueTaglet.java +++ b/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ValueTaglet.java @@ -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 @@ package com.sun.tools.doclets.internal.toolkit.taglets; 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 @@ public class ValueTaglet extends BaseInlineTaglet { "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. diff --git a/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/Util.java b/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/Util.java index 7eb0c7fc13a..120bbb48fd0 100644 --- a/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/Util.java +++ b/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/Util.java @@ -419,69 +419,6 @@ public class Util { return originalStr.replace(oldStr, newStr); } - /** - * 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("<"); break; - case '>': sb.append(">"); break; - case '&': sb.append("&"); 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, "<"); break; - case '>': sb.replace(i, i+1, ">"); break; - case '&': sb.replace(i, i+1, "&"); 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.