8012178: Cleanup use of Util.escapeHtmlChars

Reviewed-by: darcy
This commit is contained in:
Jonathan Gibbons 2013-05-14 10:14:55 -07:00
parent 37eaa21584
commit 160967e991
13 changed files with 94 additions and 154 deletions

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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()));
}
}

View File

@ -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<String> 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<ClassDoc> itc =

View File

@ -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);

View File

@ -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;
}

View File

@ -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.
*

View File

@ -78,8 +78,8 @@ public class HtmlTree extends Content {
*/
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));
}
/**
@ -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("&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.
*
@ -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();
}
}

View File

@ -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");

View File

@ -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("&lt;"); break;
case '>': stringContent.append("&gt;"); break;
case '&': stringContent.append("&amp;"); break;
default: stringContent.append(ch); break;
}
}
}
}

View File

@ -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);

View File

@ -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.

View File

@ -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("&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.