Thu, 10 Oct 2013 10:51:15 -0700
8025633: Fix javadoc to generate valid anchor names
Reviewed-by: jjg
duke@1 | 1 | /* |
jjg@1737 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.doclets.formats.html.markup; |
duke@1 | 27 | |
jjg@197 | 28 | import java.io.*; |
jjg@197 | 29 | import java.util.*; |
duke@1 | 30 | |
duke@1 | 31 | import com.sun.javadoc.*; |
jjg@1361 | 32 | import com.sun.tools.doclets.formats.html.ConfigurationImpl; |
bpatel@2101 | 33 | import com.sun.tools.doclets.formats.html.SectionName; |
jjg@197 | 34 | import com.sun.tools.doclets.internal.toolkit.*; |
jjg@1412 | 35 | import com.sun.tools.doclets.internal.toolkit.util.DocFile; |
jjg@1373 | 36 | import com.sun.tools.doclets.internal.toolkit.util.DocLink; |
jjg@1372 | 37 | import com.sun.tools.doclets.internal.toolkit.util.DocPath; |
duke@1 | 38 | |
duke@1 | 39 | |
duke@1 | 40 | /** |
duke@1 | 41 | * Class for the Html Format Code Generation specific to JavaDoc. |
duke@1 | 42 | * This Class contains methods related to the Html Code Generation which |
duke@1 | 43 | * are used by the Sub-Classes in the package com.sun.tools.doclets.standard |
duke@1 | 44 | * and com.sun.tools.doclets.oneone. |
duke@1 | 45 | * |
jjg@1359 | 46 | * <p><b>This is NOT part of any supported API. |
jjg@1359 | 47 | * If you write code that depends on this, you do so at your own risk. |
jjg@1359 | 48 | * This code and its internal interfaces are subject to change or |
jjg@1359 | 49 | * deletion without notice.</b> |
jjg@1359 | 50 | * |
duke@1 | 51 | * @since 1.2 |
duke@1 | 52 | * @author Atul M Dambalkar |
duke@1 | 53 | * @author Robert Field |
duke@1 | 54 | */ |
duke@1 | 55 | public abstract class HtmlDocWriter extends HtmlWriter { |
duke@1 | 56 | |
bpatel@1981 | 57 | public static final String CONTENT_TYPE = "text/html"; |
bpatel@1981 | 58 | |
duke@1 | 59 | /** |
duke@1 | 60 | * Constructor. Initializes the destination file name through the super |
duke@1 | 61 | * class HtmlWriter. |
duke@1 | 62 | * |
duke@1 | 63 | * @param filename String file name. |
duke@1 | 64 | */ |
jjg@1372 | 65 | public HtmlDocWriter(Configuration configuration, DocPath filename) |
jjg@1372 | 66 | throws IOException { |
jjg@1372 | 67 | super(configuration, filename); |
duke@1 | 68 | configuration.message.notice("doclet.Generating_0", |
jjg@1412 | 69 | DocFile.createFileForOutput(configuration, filename).getPath()); |
duke@1 | 70 | } |
duke@1 | 71 | |
duke@1 | 72 | /** |
duke@1 | 73 | * Accessor for configuration. |
duke@1 | 74 | */ |
duke@1 | 75 | public abstract Configuration configuration(); |
duke@1 | 76 | |
jjg@1740 | 77 | public Content getHyperLink(DocPath link, String label) { |
jjg@1740 | 78 | return getHyperLink(link, new StringContent(label), false, "", "", ""); |
jjg@1740 | 79 | } |
jjg@1740 | 80 | |
duke@1 | 81 | /** |
bpatel@2101 | 82 | * Get Html Hyper Link Content. |
duke@1 | 83 | * |
jjg@1372 | 84 | * @param where Position of the link in the file. Character '#' is not |
jjg@1372 | 85 | * needed. |
jjg@1372 | 86 | * @param label Tag for the link. |
jjg@1372 | 87 | * @return a content tree for the hyper link |
jjg@1372 | 88 | */ |
jjg@1372 | 89 | public Content getHyperLink(String where, |
jjg@1372 | 90 | Content label) { |
bpatel@2101 | 91 | return getHyperLink(getDocLink(where), label, "", ""); |
bpatel@2101 | 92 | } |
bpatel@2101 | 93 | |
bpatel@2101 | 94 | /** |
bpatel@2101 | 95 | * Get Html Hyper Link Content. |
bpatel@2101 | 96 | * |
bpatel@2101 | 97 | * @param sectionName The section name to which the link will be created. |
bpatel@2101 | 98 | * @param label Tag for the link. |
bpatel@2101 | 99 | * @return a content tree for the hyper link |
bpatel@2101 | 100 | */ |
bpatel@2101 | 101 | public Content getHyperLink(SectionName sectionName, |
bpatel@2101 | 102 | Content label) { |
bpatel@2101 | 103 | return getHyperLink(getDocLink(sectionName), label, "", ""); |
bpatel@2101 | 104 | } |
bpatel@2101 | 105 | |
bpatel@2101 | 106 | /** |
bpatel@2101 | 107 | * Get Html Hyper Link Content. |
bpatel@2101 | 108 | * |
bpatel@2101 | 109 | * @param sectionName The section name combined with where to which the link |
bpatel@2101 | 110 | * will be created. |
bpatel@2101 | 111 | * @param where The fragment combined with sectionName to which the link |
bpatel@2101 | 112 | * will be created. |
bpatel@2101 | 113 | * @param label Tag for the link. |
bpatel@2101 | 114 | * @return a content tree for the hyper link |
bpatel@2101 | 115 | */ |
bpatel@2101 | 116 | public Content getHyperLink(SectionName sectionName, String where, |
bpatel@2101 | 117 | Content label) { |
bpatel@2101 | 118 | return getHyperLink(getDocLink(sectionName, where), label, "", ""); |
bpatel@2101 | 119 | } |
bpatel@2101 | 120 | |
bpatel@2101 | 121 | /** |
bpatel@2101 | 122 | * Get the link. |
bpatel@2101 | 123 | * |
bpatel@2101 | 124 | * @param where Position of the link in the file. |
bpatel@2101 | 125 | * @return a DocLink object for the hyper link |
bpatel@2101 | 126 | */ |
bpatel@2101 | 127 | public DocLink getDocLink(String where) { |
bpatel@2101 | 128 | return DocLink.fragment(getName(where)); |
bpatel@2101 | 129 | } |
bpatel@2101 | 130 | |
bpatel@2101 | 131 | /** |
bpatel@2101 | 132 | * Get the link. |
bpatel@2101 | 133 | * |
bpatel@2101 | 134 | * @param sectionName The section name to which the link will be created. |
bpatel@2101 | 135 | * @return a DocLink object for the hyper link |
bpatel@2101 | 136 | */ |
bpatel@2101 | 137 | public DocLink getDocLink(SectionName sectionName) { |
bpatel@2101 | 138 | return DocLink.fragment(sectionName.getName()); |
bpatel@2101 | 139 | } |
bpatel@2101 | 140 | |
bpatel@2101 | 141 | /** |
bpatel@2101 | 142 | * Get the link. |
bpatel@2101 | 143 | * |
bpatel@2101 | 144 | * @param sectionName The section name combined with where to which the link |
bpatel@2101 | 145 | * will be created. |
bpatel@2101 | 146 | * @param where The fragment combined with sectionName to which the link |
bpatel@2101 | 147 | * will be created. |
bpatel@2101 | 148 | * @return a DocLink object for the hyper link |
bpatel@2101 | 149 | */ |
bpatel@2101 | 150 | public DocLink getDocLink(SectionName sectionName, String where) { |
bpatel@2101 | 151 | return DocLink.fragment(sectionName.getName() + getName(where)); |
bpatel@2101 | 152 | } |
bpatel@2101 | 153 | |
bpatel@2101 | 154 | /** |
bpatel@2101 | 155 | * Convert the name to a valid HTML name. |
bpatel@2101 | 156 | * |
bpatel@2101 | 157 | * @param name the name that needs to be converted to valid HTML name. |
bpatel@2101 | 158 | * @return a valid HTML name string. |
bpatel@2101 | 159 | */ |
bpatel@2101 | 160 | public String getName(String name) { |
bpatel@2101 | 161 | StringBuilder sb = new StringBuilder(); |
bpatel@2101 | 162 | char ch; |
bpatel@2101 | 163 | /* The HTML 4 spec at http://www.w3.org/TR/html4/types.html#h-6.2 mentions |
bpatel@2101 | 164 | * that the name/id should begin with a letter followed by other valid characters. |
bpatel@2101 | 165 | * The HTML 5 spec (draft) is more permissive on names/ids where the only restriction |
bpatel@2101 | 166 | * is that it should be at least one character long and should not contain spaces. |
bpatel@2101 | 167 | * The spec draft is @ http://www.w3.org/html/wg/drafts/html/master/dom.html#the-id-attribute. |
bpatel@2101 | 168 | * |
bpatel@2101 | 169 | * For HTML 4, we need to check for non-characters at the beginning of the name and |
bpatel@2101 | 170 | * substitute it accordingly, "_" and "$" can appear at the beginning of a member name. |
bpatel@2101 | 171 | * The method substitutes "$" with "Z:Z:D" and will prefix "_" with "Z:Z". |
bpatel@2101 | 172 | */ |
bpatel@2101 | 173 | for (int i = 0; i < name.length(); i++) { |
bpatel@2101 | 174 | ch = name.charAt(i); |
bpatel@2101 | 175 | switch (ch) { |
bpatel@2101 | 176 | case '(': |
bpatel@2101 | 177 | case ')': |
bpatel@2101 | 178 | case '<': |
bpatel@2101 | 179 | case '>': |
bpatel@2101 | 180 | case ',': |
bpatel@2101 | 181 | sb.append('-'); |
bpatel@2101 | 182 | break; |
bpatel@2101 | 183 | case ' ': |
bpatel@2101 | 184 | case '[': |
bpatel@2101 | 185 | break; |
bpatel@2101 | 186 | case ']': |
bpatel@2101 | 187 | sb.append(":A"); |
bpatel@2101 | 188 | break; |
bpatel@2101 | 189 | // Any appearance of $ needs to be substituted with ":D" and not with hyphen |
bpatel@2101 | 190 | // since a field name "P$$ and a method P(), both valid member names, can end |
bpatel@2101 | 191 | // up as "P--". A member name beginning with $ needs to be substituted with |
bpatel@2101 | 192 | // "Z:Z:D". |
bpatel@2101 | 193 | case '$': |
bpatel@2101 | 194 | if (i == 0) |
bpatel@2101 | 195 | sb.append("Z:Z"); |
bpatel@2101 | 196 | sb.append(":D"); |
bpatel@2101 | 197 | break; |
bpatel@2101 | 198 | // A member name beginning with _ needs to be prefixed with "Z:Z" since valid anchor |
bpatel@2101 | 199 | // names can only begin with a letter. |
bpatel@2101 | 200 | case '_': |
bpatel@2101 | 201 | if (i == 0) |
bpatel@2101 | 202 | sb.append("Z:Z"); |
bpatel@2101 | 203 | sb.append(ch); |
bpatel@2101 | 204 | break; |
bpatel@2101 | 205 | default: |
bpatel@2101 | 206 | sb.append(ch); |
bpatel@2101 | 207 | } |
bpatel@2101 | 208 | } |
bpatel@2101 | 209 | return sb.toString(); |
jjg@1372 | 210 | } |
jjg@1372 | 211 | |
jjg@1372 | 212 | /** |
jjg@1737 | 213 | * Get Html hyperlink. |
jjg@1372 | 214 | * |
jjg@1737 | 215 | * @param link path of the file. |
bpatel@766 | 216 | * @param label Tag for the link. |
bpatel@766 | 217 | * @return a content tree for the hyper link |
bpatel@766 | 218 | */ |
jjg@1737 | 219 | public Content getHyperLink(DocPath link, Content label) { |
jjg@1373 | 220 | return getHyperLink(link, label, "", ""); |
jjg@1373 | 221 | } |
jjg@1373 | 222 | |
jjg@1737 | 223 | public Content getHyperLink(DocLink link, Content label) { |
jjg@1373 | 224 | return getHyperLink(link, label, "", ""); |
duke@1 | 225 | } |
duke@1 | 226 | |
jjg@1740 | 227 | public Content getHyperLink(DocPath link, |
jjg@1740 | 228 | Content label, boolean strong, |
jjg@1740 | 229 | String stylename, String title, String target) { |
jjg@1740 | 230 | return getHyperLink(new DocLink(link), label, strong, |
jjg@1740 | 231 | stylename, title, target); |
jjg@1740 | 232 | } |
jjg@1740 | 233 | |
jjg@1737 | 234 | public Content getHyperLink(DocLink link, |
jjg@1737 | 235 | Content label, boolean strong, |
jjg@1737 | 236 | String stylename, String title, String target) { |
jjg@1737 | 237 | Content body = label; |
jjg@1737 | 238 | if (strong) { |
jjg@1737 | 239 | body = HtmlTree.SPAN(HtmlStyle.strong, body); |
jjg@1737 | 240 | } |
jjg@1737 | 241 | if (stylename != null && stylename.length() != 0) { |
jjg@1737 | 242 | HtmlTree t = new HtmlTree(HtmlTag.FONT, body); |
jjg@1737 | 243 | t.addAttr(HtmlAttr.CLASS, stylename); |
jjg@1737 | 244 | body = t; |
jjg@1737 | 245 | } |
jjg@1737 | 246 | HtmlTree l = HtmlTree.A(link.toString(), body); |
jjg@1737 | 247 | if (title != null && title.length() != 0) { |
jjg@1737 | 248 | l.addAttr(HtmlAttr.TITLE, title); |
jjg@1737 | 249 | } |
jjg@1737 | 250 | if (target != null && target.length() != 0) { |
jjg@1737 | 251 | l.addAttr(HtmlAttr.TARGET, target); |
jjg@1737 | 252 | } |
jjg@1737 | 253 | return l; |
jjg@1737 | 254 | } |
jjg@1737 | 255 | |
duke@1 | 256 | /** |
bpatel@766 | 257 | * Get Html Hyper Link. |
duke@1 | 258 | * |
duke@1 | 259 | * @param link String name of the file. |
duke@1 | 260 | * @param label Tag for the link. |
bpatel@766 | 261 | * @param title String that describes the link's content for accessibility. |
bpatel@766 | 262 | * @param target Target frame. |
bpatel@766 | 263 | * @return a content tree for the hyper link. |
duke@1 | 264 | */ |
jjg@1373 | 265 | public Content getHyperLink(DocPath link, |
jjg@1372 | 266 | Content label, String title, String target) { |
jjg@1373 | 267 | return getHyperLink(new DocLink(link), label, title, target); |
jjg@1372 | 268 | } |
jjg@1373 | 269 | |
jjg@1373 | 270 | public Content getHyperLink(DocLink link, |
bpatel@766 | 271 | Content label, String title, String target) { |
jjg@1373 | 272 | HtmlTree anchor = HtmlTree.A(link.toString(), label); |
bpatel@766 | 273 | if (title != null && title.length() != 0) { |
bpatel@766 | 274 | anchor.addAttr(HtmlAttr.TITLE, title); |
bpatel@766 | 275 | } |
bpatel@766 | 276 | if (target != null && target.length() != 0) { |
bpatel@766 | 277 | anchor.addAttr(HtmlAttr.TARGET, target); |
bpatel@766 | 278 | } |
bpatel@766 | 279 | return anchor; |
bpatel@766 | 280 | } |
bpatel@766 | 281 | |
bpatel@766 | 282 | /** |
duke@1 | 283 | * Get the name of the package, this class is in. |
duke@1 | 284 | * |
duke@1 | 285 | * @param cd ClassDoc. |
duke@1 | 286 | */ |
duke@1 | 287 | public String getPkgName(ClassDoc cd) { |
duke@1 | 288 | String pkgName = cd.containingPackage().name(); |
duke@1 | 289 | if (pkgName.length() > 0) { |
duke@1 | 290 | pkgName += "."; |
duke@1 | 291 | return pkgName; |
duke@1 | 292 | } |
duke@1 | 293 | return ""; |
duke@1 | 294 | } |
duke@1 | 295 | |
bpatel@233 | 296 | public boolean getMemberDetailsListPrinted() { |
bpatel@233 | 297 | return memberDetailsListPrinted; |
bpatel@233 | 298 | } |
bpatel@233 | 299 | |
bpatel@233 | 300 | /** |
duke@1 | 301 | * Print the frameset version of the Html file header. |
duke@1 | 302 | * Called only when generating an HTML frameset file. |
duke@1 | 303 | * |
bpatel@766 | 304 | * @param title Title of this HTML document |
bpatel@766 | 305 | * @param noTimeStamp If true, don't print time stamp in header |
bpatel@766 | 306 | * @param frameset the frameset to be added to the HTML document |
duke@1 | 307 | */ |
bpatel@766 | 308 | public void printFramesetDocument(String title, boolean noTimeStamp, |
jjg@1364 | 309 | Content frameset) throws IOException { |
jjg@1410 | 310 | Content htmlDocType = DocType.FRAMESET; |
bpatel@766 | 311 | Content htmlComment = new Comment(configuration.getText("doclet.New_Page")); |
bpatel@766 | 312 | Content head = new HtmlTree(HtmlTag.HEAD); |
jjg@2006 | 313 | head.addContent(getGeneratedBy(!noTimeStamp)); |
duke@1 | 314 | if (configuration.charset.length() > 0) { |
bpatel@1981 | 315 | Content meta = HtmlTree.META("Content-Type", CONTENT_TYPE, |
bpatel@766 | 316 | configuration.charset); |
bpatel@766 | 317 | head.addContent(meta); |
duke@1 | 318 | } |
bpatel@766 | 319 | Content windowTitle = HtmlTree.TITLE(new StringContent(title)); |
bpatel@766 | 320 | head.addContent(windowTitle); |
bpatel@766 | 321 | head.addContent(getFramesetJavaScript()); |
bpatel@766 | 322 | Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(), |
bpatel@766 | 323 | head, frameset); |
bpatel@766 | 324 | Content htmlDocument = new HtmlDocument(htmlDocType, |
bpatel@766 | 325 | htmlComment, htmlTree); |
jjg@1365 | 326 | write(htmlDocument); |
duke@1 | 327 | } |
duke@1 | 328 | |
jjg@2006 | 329 | protected Comment getGeneratedBy(boolean timestamp) { |
jjg@2006 | 330 | String text = "Generated by javadoc"; // marker string, deliberately not localized |
jjg@2006 | 331 | if (timestamp) { |
jjg@2006 | 332 | Calendar calendar = new GregorianCalendar(TimeZone.getDefault()); |
jjg@2006 | 333 | Date today = calendar.getTime(); |
jjg@2006 | 334 | text += " ("+ ConfigurationImpl.BUILD_DATE + ") on " + today; |
jjg@2006 | 335 | } |
jjg@2006 | 336 | return new Comment(text); |
duke@1 | 337 | } |
duke@1 | 338 | } |