Mon, 15 Oct 2012 17:07:55 -0700
8000666: javadoc should write directly to Writer instead of composing strings
Reviewed-by: bpatel
duke@1 | 1 | /* |
jjg@1359 | 2 | * Copyright (c) 1997, 2012, 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; |
jjg@197 | 33 | import com.sun.tools.doclets.internal.toolkit.*; |
duke@1 | 34 | |
duke@1 | 35 | |
duke@1 | 36 | /** |
duke@1 | 37 | * Class for the Html Format Code Generation specific to JavaDoc. |
duke@1 | 38 | * This Class contains methods related to the Html Code Generation which |
duke@1 | 39 | * are used by the Sub-Classes in the package com.sun.tools.doclets.standard |
duke@1 | 40 | * and com.sun.tools.doclets.oneone. |
duke@1 | 41 | * |
jjg@1359 | 42 | * <p><b>This is NOT part of any supported API. |
jjg@1359 | 43 | * If you write code that depends on this, you do so at your own risk. |
jjg@1359 | 44 | * This code and its internal interfaces are subject to change or |
jjg@1359 | 45 | * deletion without notice.</b> |
jjg@1359 | 46 | * |
duke@1 | 47 | * @since 1.2 |
duke@1 | 48 | * @author Atul M Dambalkar |
duke@1 | 49 | * @author Robert Field |
duke@1 | 50 | */ |
duke@1 | 51 | public abstract class HtmlDocWriter extends HtmlWriter { |
duke@1 | 52 | |
duke@1 | 53 | /** |
duke@1 | 54 | * Constructor. Initializes the destination file name through the super |
duke@1 | 55 | * class HtmlWriter. |
duke@1 | 56 | * |
duke@1 | 57 | * @param filename String file name. |
duke@1 | 58 | */ |
duke@1 | 59 | public HtmlDocWriter(Configuration configuration, |
duke@1 | 60 | String filename) throws IOException { |
duke@1 | 61 | super(configuration, |
duke@1 | 62 | null, configuration.destDirName + filename, |
duke@1 | 63 | configuration.docencoding); |
jjg@197 | 64 | // use File to normalize file separators |
duke@1 | 65 | configuration.message.notice("doclet.Generating_0", |
jjg@197 | 66 | new File(configuration.destDirName, filename)); |
duke@1 | 67 | } |
duke@1 | 68 | |
duke@1 | 69 | public HtmlDocWriter(Configuration configuration, |
duke@1 | 70 | String path, String filename) throws IOException { |
duke@1 | 71 | super(configuration, |
duke@1 | 72 | configuration.destDirName + path, filename, |
duke@1 | 73 | configuration.docencoding); |
jjg@197 | 74 | // use File to normalize file separators |
duke@1 | 75 | configuration.message.notice("doclet.Generating_0", |
jjg@197 | 76 | new File(configuration.destDirName, |
jjg@197 | 77 | ((path.length() > 0)? path + File.separator: "") + filename)); |
duke@1 | 78 | } |
duke@1 | 79 | |
duke@1 | 80 | /** |
duke@1 | 81 | * Accessor for configuration. |
duke@1 | 82 | */ |
duke@1 | 83 | public abstract Configuration configuration(); |
duke@1 | 84 | |
duke@1 | 85 | /** |
duke@1 | 86 | * Print Html Hyper Link. |
duke@1 | 87 | * |
duke@1 | 88 | * @param link String name of the file. |
duke@1 | 89 | * @param where Position of the link in the file. Character '#' is not |
duke@1 | 90 | * needed. |
duke@1 | 91 | * @param label Tag for the link. |
bpatel@182 | 92 | * @param strong Boolean that sets label to strong. |
duke@1 | 93 | */ |
duke@1 | 94 | public void printHyperLink(String link, String where, |
bpatel@182 | 95 | String label, boolean strong) { |
bpatel@766 | 96 | print(getHyperLinkString(link, where, label, strong, "", "", "")); |
duke@1 | 97 | } |
duke@1 | 98 | |
duke@1 | 99 | /** |
duke@1 | 100 | * Print Html Hyper Link. |
duke@1 | 101 | * |
duke@1 | 102 | * @param link String name of the file. |
duke@1 | 103 | * @param where Position of the link in the file. Character '#' is not |
duke@1 | 104 | * needed. |
duke@1 | 105 | * @param label Tag for the link. |
duke@1 | 106 | */ |
duke@1 | 107 | public void printHyperLink(String link, String where, String label) { |
duke@1 | 108 | printHyperLink(link, where, label, false); |
duke@1 | 109 | } |
duke@1 | 110 | |
duke@1 | 111 | /** |
duke@1 | 112 | * Print Html Hyper Link. |
duke@1 | 113 | * |
duke@1 | 114 | * @param link String name of the file. |
duke@1 | 115 | * @param where Position of the link in the file. Character '#' is not |
duke@1 | 116 | * needed. |
duke@1 | 117 | * @param label Tag for the link. |
bpatel@182 | 118 | * @param strong Boolean that sets label to strong. |
duke@1 | 119 | * @param stylename String style of text defined in style sheet. |
duke@1 | 120 | */ |
duke@1 | 121 | public void printHyperLink(String link, String where, |
bpatel@182 | 122 | String label, boolean strong, |
duke@1 | 123 | String stylename) { |
bpatel@766 | 124 | print(getHyperLinkString(link, where, label, strong, stylename, "", "")); |
duke@1 | 125 | } |
duke@1 | 126 | |
duke@1 | 127 | /** |
duke@1 | 128 | * Return Html Hyper Link string. |
duke@1 | 129 | * |
duke@1 | 130 | * @param link String name of the file. |
duke@1 | 131 | * @param where Position of the link in the file. Character '#' is not |
duke@1 | 132 | * needed. |
duke@1 | 133 | * @param label Tag for the link. |
bpatel@182 | 134 | * @param strong Boolean that sets label to strong. |
duke@1 | 135 | * @return String Hyper Link. |
duke@1 | 136 | */ |
bpatel@766 | 137 | public String getHyperLinkString(String link, String where, |
bpatel@182 | 138 | String label, boolean strong) { |
bpatel@766 | 139 | return getHyperLinkString(link, where, label, strong, "", "", ""); |
duke@1 | 140 | } |
duke@1 | 141 | |
duke@1 | 142 | /** |
duke@1 | 143 | * Get Html Hyper Link string. |
duke@1 | 144 | * |
duke@1 | 145 | * @param link String name of the file. |
duke@1 | 146 | * @param where Position of the link in the file. Character '#' is not |
duke@1 | 147 | * needed. |
duke@1 | 148 | * @param label Tag for the link. |
bpatel@182 | 149 | * @param strong Boolean that sets label to strong. |
duke@1 | 150 | * @param stylename String style of text defined in style sheet. |
duke@1 | 151 | * @return String Hyper Link. |
duke@1 | 152 | */ |
bpatel@766 | 153 | public String getHyperLinkString(String link, String where, |
bpatel@182 | 154 | String label, boolean strong, |
duke@1 | 155 | String stylename) { |
bpatel@766 | 156 | return getHyperLinkString(link, where, label, strong, stylename, "", ""); |
bpatel@766 | 157 | } |
bpatel@766 | 158 | |
bpatel@766 | 159 | /** |
bpatel@766 | 160 | * Get Html Hyper Link string. |
bpatel@766 | 161 | * |
bpatel@766 | 162 | * @param link String name of the file. |
bpatel@766 | 163 | * @param where Position of the link in the file. Character '#' is not |
bpatel@766 | 164 | * needed. |
bpatel@766 | 165 | * @param label Tag for the link. |
bpatel@766 | 166 | * @return a content tree for the hyper link |
bpatel@766 | 167 | */ |
bpatel@766 | 168 | public Content getHyperLink(String link, String where, |
bpatel@766 | 169 | Content label) { |
bpatel@766 | 170 | return getHyperLink(link, where, label, "", ""); |
duke@1 | 171 | } |
duke@1 | 172 | |
duke@1 | 173 | /** |
duke@1 | 174 | * Get Html Hyper Link string. |
duke@1 | 175 | * |
duke@1 | 176 | * @param link String name of the file. |
duke@1 | 177 | * @param where Position of the link in the file. Character '#' is not |
duke@1 | 178 | * needed. |
duke@1 | 179 | * @param label Tag for the link. |
bpatel@182 | 180 | * @param strong Boolean that sets label to strong. |
duke@1 | 181 | * @param stylename String style of text defined in style sheet. |
duke@1 | 182 | * @param title String that describes the link's content for accessibility. |
duke@1 | 183 | * @param target Target frame. |
duke@1 | 184 | * @return String Hyper Link. |
duke@1 | 185 | */ |
bpatel@766 | 186 | public String getHyperLinkString(String link, String where, |
bpatel@182 | 187 | String label, boolean strong, |
duke@1 | 188 | String stylename, String title, String target) { |
jjg@1362 | 189 | StringBuilder retlink = new StringBuilder(); |
bpatel@766 | 190 | retlink.append("<a href=\""); |
duke@1 | 191 | retlink.append(link); |
duke@1 | 192 | if (where != null && where.length() != 0) { |
duke@1 | 193 | retlink.append("#"); |
duke@1 | 194 | retlink.append(where); |
duke@1 | 195 | } |
duke@1 | 196 | retlink.append("\""); |
duke@1 | 197 | if (title != null && title.length() != 0) { |
jjg@1362 | 198 | retlink.append(" title=\"").append(title).append("\""); |
duke@1 | 199 | } |
duke@1 | 200 | if (target != null && target.length() != 0) { |
jjg@1362 | 201 | retlink.append(" target=\"").append(target).append("\""); |
duke@1 | 202 | } |
duke@1 | 203 | retlink.append(">"); |
duke@1 | 204 | if (stylename != null && stylename.length() != 0) { |
duke@1 | 205 | retlink.append("<FONT CLASS=\""); |
duke@1 | 206 | retlink.append(stylename); |
duke@1 | 207 | retlink.append("\">"); |
duke@1 | 208 | } |
bpatel@182 | 209 | if (strong) { |
bpatel@766 | 210 | retlink.append("<span class=\"strong\">"); |
duke@1 | 211 | } |
duke@1 | 212 | retlink.append(label); |
bpatel@182 | 213 | if (strong) { |
bpatel@766 | 214 | retlink.append("</span>"); |
duke@1 | 215 | } |
duke@1 | 216 | if (stylename != null && stylename.length() != 0) { |
duke@1 | 217 | retlink.append("</FONT>"); |
duke@1 | 218 | } |
bpatel@766 | 219 | retlink.append("</a>"); |
duke@1 | 220 | return retlink.toString(); |
duke@1 | 221 | } |
duke@1 | 222 | |
duke@1 | 223 | /** |
bpatel@766 | 224 | * Get Html Hyper Link. |
duke@1 | 225 | * |
duke@1 | 226 | * @param link String name of the file. |
bpatel@766 | 227 | * @param where Position of the link in the file. Character '#' is not |
bpatel@766 | 228 | * needed. |
duke@1 | 229 | * @param label Tag for the link. |
bpatel@766 | 230 | * @param title String that describes the link's content for accessibility. |
bpatel@766 | 231 | * @param target Target frame. |
bpatel@766 | 232 | * @return a content tree for the hyper link. |
duke@1 | 233 | */ |
bpatel@766 | 234 | public Content getHyperLink(String link, String where, |
bpatel@766 | 235 | Content label, String title, String target) { |
bpatel@766 | 236 | if (where != null && where.length() != 0) { |
bpatel@766 | 237 | link += "#" + where; |
bpatel@766 | 238 | } |
bpatel@766 | 239 | HtmlTree anchor = HtmlTree.A(link, label); |
bpatel@766 | 240 | if (title != null && title.length() != 0) { |
bpatel@766 | 241 | anchor.addAttr(HtmlAttr.TITLE, title); |
bpatel@766 | 242 | } |
bpatel@766 | 243 | if (target != null && target.length() != 0) { |
bpatel@766 | 244 | anchor.addAttr(HtmlAttr.TARGET, target); |
bpatel@766 | 245 | } |
bpatel@766 | 246 | return anchor; |
bpatel@766 | 247 | } |
bpatel@766 | 248 | |
bpatel@766 | 249 | /** |
bpatel@766 | 250 | * Get a hyperlink to a file. |
bpatel@766 | 251 | * |
bpatel@766 | 252 | * @param link String name of the file |
bpatel@766 | 253 | * @param label Label for the link |
bpatel@766 | 254 | * @return a content for the hyperlink to the file |
bpatel@766 | 255 | */ |
bpatel@766 | 256 | public Content getHyperLink(String link, Content label) { |
bpatel@766 | 257 | return getHyperLink(link, "", label); |
duke@1 | 258 | } |
duke@1 | 259 | |
duke@1 | 260 | /** |
duke@1 | 261 | * Get link string without positioning in the file. |
duke@1 | 262 | * |
duke@1 | 263 | * @param link String name of the file. |
duke@1 | 264 | * @param label Tag for the link. |
duke@1 | 265 | * @return Strign Hyper link. |
duke@1 | 266 | */ |
bpatel@766 | 267 | public String getHyperLinkString(String link, String label) { |
bpatel@766 | 268 | return getHyperLinkString(link, "", label, false); |
duke@1 | 269 | } |
duke@1 | 270 | |
duke@1 | 271 | /** |
duke@1 | 272 | * Print the name of the package, this class is in. |
duke@1 | 273 | * |
duke@1 | 274 | * @param cd ClassDoc. |
duke@1 | 275 | */ |
duke@1 | 276 | public void printPkgName(ClassDoc cd) { |
duke@1 | 277 | print(getPkgName(cd)); |
duke@1 | 278 | } |
duke@1 | 279 | |
duke@1 | 280 | /** |
duke@1 | 281 | * Get the name of the package, this class is in. |
duke@1 | 282 | * |
duke@1 | 283 | * @param cd ClassDoc. |
duke@1 | 284 | */ |
duke@1 | 285 | public String getPkgName(ClassDoc cd) { |
duke@1 | 286 | String pkgName = cd.containingPackage().name(); |
duke@1 | 287 | if (pkgName.length() > 0) { |
duke@1 | 288 | pkgName += "."; |
duke@1 | 289 | return pkgName; |
duke@1 | 290 | } |
duke@1 | 291 | return ""; |
duke@1 | 292 | } |
duke@1 | 293 | |
duke@1 | 294 | /** |
bpatel@233 | 295 | * Keep track of member details list. Print the definition list start tag |
bpatel@233 | 296 | * if it is not printed yet. |
bpatel@233 | 297 | */ |
bpatel@233 | 298 | public void printMemberDetailsListStartTag () { |
bpatel@233 | 299 | if (!getMemberDetailsListPrinted()) { |
bpatel@233 | 300 | dl(); |
bpatel@233 | 301 | memberDetailsListPrinted = true; |
bpatel@233 | 302 | } |
bpatel@233 | 303 | } |
bpatel@233 | 304 | |
bpatel@233 | 305 | /** |
bpatel@233 | 306 | * Print the definition list end tag if the list start tag was printed. |
bpatel@233 | 307 | */ |
bpatel@233 | 308 | public void printMemberDetailsListEndTag () { |
bpatel@233 | 309 | if (getMemberDetailsListPrinted()) { |
bpatel@233 | 310 | dlEnd(); |
bpatel@233 | 311 | memberDetailsListPrinted = false; |
bpatel@233 | 312 | } |
bpatel@233 | 313 | } |
bpatel@233 | 314 | |
bpatel@233 | 315 | public boolean getMemberDetailsListPrinted() { |
bpatel@233 | 316 | return memberDetailsListPrinted; |
bpatel@233 | 317 | } |
bpatel@233 | 318 | |
bpatel@233 | 319 | /** |
duke@1 | 320 | * Print the frameset version of the Html file header. |
duke@1 | 321 | * Called only when generating an HTML frameset file. |
duke@1 | 322 | * |
bpatel@766 | 323 | * @param title Title of this HTML document |
bpatel@766 | 324 | * @param noTimeStamp If true, don't print time stamp in header |
bpatel@766 | 325 | * @param frameset the frameset to be added to the HTML document |
duke@1 | 326 | */ |
bpatel@766 | 327 | public void printFramesetDocument(String title, boolean noTimeStamp, |
jjg@1364 | 328 | Content frameset) throws IOException { |
bpatel@766 | 329 | Content htmlDocType = DocType.Frameset(); |
bpatel@766 | 330 | Content htmlComment = new Comment(configuration.getText("doclet.New_Page")); |
bpatel@766 | 331 | Content head = new HtmlTree(HtmlTag.HEAD); |
duke@1 | 332 | if (! noTimeStamp) { |
jjg@1361 | 333 | Content headComment = new Comment(getGeneratedByString()); |
bpatel@766 | 334 | head.addContent(headComment); |
duke@1 | 335 | } |
duke@1 | 336 | if (configuration.charset.length() > 0) { |
bpatel@766 | 337 | Content meta = HtmlTree.META("Content-Type", "text/html", |
bpatel@766 | 338 | configuration.charset); |
bpatel@766 | 339 | head.addContent(meta); |
duke@1 | 340 | } |
bpatel@766 | 341 | Content windowTitle = HtmlTree.TITLE(new StringContent(title)); |
bpatel@766 | 342 | head.addContent(windowTitle); |
bpatel@766 | 343 | head.addContent(getFramesetJavaScript()); |
bpatel@766 | 344 | Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(), |
bpatel@766 | 345 | head, frameset); |
bpatel@766 | 346 | Content htmlDocument = new HtmlDocument(htmlDocType, |
bpatel@766 | 347 | htmlComment, htmlTree); |
jjg@1364 | 348 | htmlDocument.write(this, true); |
duke@1 | 349 | } |
duke@1 | 350 | |
duke@1 | 351 | /** |
duke@1 | 352 | * Print the appropriate spaces to format the class tree in the class page. |
duke@1 | 353 | * |
duke@1 | 354 | * @param len Number of spaces. |
duke@1 | 355 | */ |
duke@1 | 356 | public String spaces(int len) { |
duke@1 | 357 | String space = ""; |
duke@1 | 358 | |
duke@1 | 359 | for (int i = 0; i < len; i++) { |
duke@1 | 360 | space += " "; |
duke@1 | 361 | } |
duke@1 | 362 | return space; |
duke@1 | 363 | } |
duke@1 | 364 | |
duke@1 | 365 | /** |
duke@1 | 366 | * Print the closing </body> and </html> tags. |
duke@1 | 367 | */ |
duke@1 | 368 | public void printBodyHtmlEnd() { |
duke@1 | 369 | println(); |
duke@1 | 370 | bodyEnd(); |
duke@1 | 371 | htmlEnd(); |
duke@1 | 372 | } |
duke@1 | 373 | |
duke@1 | 374 | /** |
duke@1 | 375 | * Calls {@link #printBodyHtmlEnd()} method. |
duke@1 | 376 | */ |
duke@1 | 377 | public void printFooter() { |
duke@1 | 378 | printBodyHtmlEnd(); |
duke@1 | 379 | } |
duke@1 | 380 | |
duke@1 | 381 | /** |
duke@1 | 382 | * Print closing </html> tag. |
duke@1 | 383 | */ |
duke@1 | 384 | public void printFrameFooter() { |
duke@1 | 385 | htmlEnd(); |
duke@1 | 386 | } |
duke@1 | 387 | |
duke@1 | 388 | /** |
duke@1 | 389 | * Print ten non-breaking spaces("&nbsp;"). |
duke@1 | 390 | */ |
duke@1 | 391 | public void printNbsps() { |
duke@1 | 392 | print(" "); |
duke@1 | 393 | } |
duke@1 | 394 | |
jjg@1361 | 395 | protected String getGeneratedByString() { |
duke@1 | 396 | Calendar calendar = new GregorianCalendar(TimeZone.getDefault()); |
jjg@1361 | 397 | Date today = calendar.getTime(); |
jjg@1361 | 398 | return "Generated by javadoc ("+ ConfigurationImpl.BUILD_DATE + ") on " + today; |
duke@1 | 399 | } |
duke@1 | 400 | } |