1 /* 2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.javadoc.internal.doclets.formats.html.markup; 27 28 import java.io.*; 29 import java.util.*; 30 31 import jdk.javadoc.internal.doclets.toolkit.Configuration; 32 import jdk.javadoc.internal.doclets.toolkit.Content; 33 import jdk.javadoc.internal.doclets.toolkit.util.DocFile; 34 import jdk.javadoc.internal.doclets.toolkit.util.DocPath; 35 import jdk.javadoc.internal.doclets.toolkit.util.DocletConstants; 36 import jdk.javadoc.internal.doclets.toolkit.util.MethodTypes; 37 38 39 /** 40 * Class for the Html format code generation. 41 * Initializes PrintWriter with FileWriter, to enable print 42 * related methods to generate the code to the named File through FileWriter. 43 * 44 * <p><b>This is NOT part of any supported API. 45 * If you write code that depends on this, you do so at your own risk. 46 * This code and its internal interfaces are subject to change or 47 * deletion without notice.</b> 48 * 49 * @since 1.2 50 * @author Atul M Dambalkar 51 * @author Bhavesh Patel (Modified) 52 */ 53 public class HtmlWriter { 54 55 /** 56 * The window title of this file 57 */ 58 protected String winTitle; 59 60 /** 61 * The configuration 62 */ 63 protected Configuration configuration; 64 65 /** 66 * The flag to indicate whether a member details list is printed or not. 67 */ 68 protected boolean memberDetailsListPrinted; 69 70 /** 71 * Header for tables displaying packages and description.. 72 */ 73 protected final List<String> packageTableHeader; 74 75 /** 76 * Summary for use tables displaying class and package use. 77 */ 78 protected final String useTableSummary; 79 80 /** 81 * Column header for class docs displaying Modifier and Type header. 82 */ 83 protected final String modifierTypeHeader; 84 85 public final Content overviewLabel; 86 87 public final Content defaultPackageLabel; 88 89 public final Content packageLabel; 90 91 public final Content useLabel; 92 93 public final Content prevLabel; 94 95 public final Content nextLabel; 96 97 public final Content prevclassLabel; 98 99 public final Content nextclassLabel; 100 101 public final Content summaryLabel; 102 103 public final Content detailLabel; 104 105 public final Content framesLabel; 106 107 public final Content noframesLabel; 108 109 public final Content treeLabel; 110 111 public final Content classLabel; 112 113 public final Content deprecatedLabel; 114 115 public final Content deprecatedPhrase; 116 117 public final Content allclassesLabel; 118 119 public final Content allpackagesLabel; 120 121 public final Content indexLabel; 122 123 public final Content helpLabel; 124 125 public final Content seeLabel; 126 127 public final Content descriptionLabel; 128 129 public final Content prevpackageLabel; 130 131 public final Content nextpackageLabel; 132 133 public final Content packagesLabel; 134 135 public final Content methodDetailsLabel; 136 137 public final Content annotationTypeDetailsLabel; 138 139 public final Content fieldDetailsLabel; 140 141 public final Content propertyDetailsLabel; 142 143 public final Content constructorDetailsLabel; 144 145 public final Content enumConstantsDetailsLabel; 146 147 public final Content specifiedByLabel; 148 149 public final Content overridesLabel; 150 151 public final Content descfrmClassLabel; 152 153 public final Content descfrmInterfaceLabel; 154 155 private final Writer writer; 156 157 protected Content script; 158 159 160 /** 161 * Constructor. 162 * 163 * @param path The directory path to be created for this file 164 * or null if none to be created. 165 * @exception IOException Exception raised by the FileWriter is passed on 166 * to next level. 167 * @exception UnsupportedEncodingException Exception raised by the 168 * OutputStreamWriter is passed on to next level. 169 */ 170 public HtmlWriter(Configuration configuration, DocPath path) 171 throws IOException, UnsupportedEncodingException { 172 writer = DocFile.createFileForOutput(configuration, path).openWriter(); 173 this.configuration = configuration; 174 this.memberDetailsListPrinted = false; 175 packageTableHeader = new ArrayList<>(); 176 packageTableHeader.add(configuration.getText("doclet.Package")); 177 packageTableHeader.add(configuration.getText("doclet.Description")); 178 useTableSummary = configuration.getText("doclet.Use_Table_Summary", 179 configuration.getText("doclet.packages")); 180 modifierTypeHeader = configuration.getText("doclet.0_and_1", 181 configuration.getText("doclet.Modifier"), 182 configuration.getText("doclet.Type")); 183 overviewLabel = getResource("doclet.Overview"); 184 defaultPackageLabel = new StringContent(DocletConstants.DEFAULT_PACKAGE_NAME); 185 packageLabel = getResource("doclet.Package"); 186 useLabel = getResource("doclet.navClassUse"); 187 prevLabel = getResource("doclet.Prev"); 188 nextLabel = getResource("doclet.Next"); 189 prevclassLabel = getNonBreakResource("doclet.Prev_Class"); 190 nextclassLabel = getNonBreakResource("doclet.Next_Class"); 191 summaryLabel = getResource("doclet.Summary"); 192 detailLabel = getResource("doclet.Detail"); 193 framesLabel = getResource("doclet.Frames"); 194 noframesLabel = getNonBreakResource("doclet.No_Frames"); 195 treeLabel = getResource("doclet.Tree"); 196 classLabel = getResource("doclet.Class"); 197 deprecatedLabel = getResource("doclet.navDeprecated"); 198 deprecatedPhrase = getResource("doclet.Deprecated"); 199 allclassesLabel = getNonBreakResource("doclet.All_Classes"); 200 allpackagesLabel = getNonBreakResource("doclet.All_Packages"); 201 indexLabel = getResource("doclet.Index"); 202 helpLabel = getResource("doclet.Help"); 203 seeLabel = getResource("doclet.See"); 204 descriptionLabel = getResource("doclet.Description"); 205 prevpackageLabel = getNonBreakResource("doclet.Prev_Package"); 206 nextpackageLabel = getNonBreakResource("doclet.Next_Package"); 207 packagesLabel = getResource("doclet.Packages"); 208 methodDetailsLabel = getResource("doclet.Method_Detail"); 209 annotationTypeDetailsLabel = getResource("doclet.Annotation_Type_Member_Detail"); 210 fieldDetailsLabel = getResource("doclet.Field_Detail"); 211 propertyDetailsLabel = getResource("doclet.Property_Detail"); 212 constructorDetailsLabel = getResource("doclet.Constructor_Detail"); 213 enumConstantsDetailsLabel = getResource("doclet.Enum_Constant_Detail"); 214 specifiedByLabel = getResource("doclet.Specified_By"); 215 overridesLabel = getResource("doclet.Overrides"); 216 descfrmClassLabel = getResource("doclet.Description_From_Class"); 217 descfrmInterfaceLabel = getResource("doclet.Description_From_Interface"); 218 } 219 220 public void write(Content c) throws IOException { 221 c.write(writer, true); 222 } 223 224 public void close() throws IOException { 225 writer.close(); 226 } 227 228 /** 229 * Get the configuration string as a content. 230 * 231 * @param key the key to look for in the configuration file 232 * @return a content tree for the text 233 */ 234 public Content getResource(String key) { 235 return configuration.getResource(key); 236 } 237 238 /** 239 * Get the configuration string as a content, replacing spaces 240 * with non-breaking spaces. 241 * 242 * @param key the key to look for in the configuration file 243 * @return a content tree for the text 244 */ 245 public Content getNonBreakResource(String key) { 246 String text = configuration.getText(key); 247 Content c = configuration.newContent(); 248 int start = 0; 249 int p; 250 while ((p = text.indexOf(" ", start)) != -1) { 251 c.addContent(text.substring(start, p)); 252 c.addContent(RawHtml.nbsp); 253 start = p + 1; 254 } 255 c.addContent(text.substring(start)); 256 return c; 257 } 258 259 /** 260 * Get the configuration string as a content. 261 * 262 * @param key the key to look for in the configuration file 263 * @param o string or content argument added to configuration text 264 * @return a content tree for the text 265 */ 266 public Content getResource(String key, Object o) { 267 return configuration.getResource(key, o); 268 } 269 270 /** 271 * Get the configuration string as a content. 272 * 273 * @param key the key to look for in the configuration file 274 * @param o1 string or content argument added to configuration text 275 * @param o2 string or content argument added to configuration text 276 * @return a content tree for the text 277 */ 278 public Content getResource(String key, Object o0, Object o1) { 279 return configuration.getResource(key, o0, o1); 280 } 281 282 /** 283 * Returns an HtmlTree for the SCRIPT tag. 284 * 285 * @return an HtmlTree for the SCRIPT tag 286 */ 287 protected HtmlTree getWinTitleScript(){ 288 HtmlTree script = HtmlTree.SCRIPT(); 289 if(winTitle != null && winTitle.length() > 0) { 290 String scriptCode = "<!--" + DocletConstants.NL + 291 " try {" + DocletConstants.NL + 292 " if (location.href.indexOf('is-external=true') == -1) {" + DocletConstants.NL + 293 " parent.document.title=\"" + escapeJavaScriptChars(winTitle) + "\";" + DocletConstants.NL + 294 " }" + DocletConstants.NL + 295 " }" + DocletConstants.NL + 296 " catch(err) {" + DocletConstants.NL + 297 " }" + DocletConstants.NL + 298 "//-->" + DocletConstants.NL; 299 RawHtml scriptContent = new RawHtml(scriptCode); 300 script.addContent(scriptContent); 301 } 302 return script; 303 } 304 305 /** 306 * Returns a String with escaped special JavaScript characters. 307 * 308 * @param s String that needs to be escaped 309 * @return a valid escaped JavaScript string 310 */ 311 private static String escapeJavaScriptChars(String s) { 312 StringBuilder sb = new StringBuilder(); 313 for (int i = 0; i < s.length(); i++) { 314 char ch = s.charAt(i); 315 switch (ch) { 316 case '\b': 317 sb.append("\\b"); 318 break; 319 case '\t': 320 sb.append("\\t"); 321 break; 322 case '\n': 323 sb.append("\\n"); 324 break; 325 case '\f': 326 sb.append("\\f"); 327 break; 328 case '\r': 329 sb.append("\\r"); 330 break; 331 case '"': 332 sb.append("\\\""); 333 break; 334 case '\'': 335 sb.append("\\\'"); 336 break; 337 case '\\': 338 sb.append("\\\\"); 339 break; 340 default: 341 if (ch < 32 || ch >= 127) { 342 sb.append(String.format("\\u%04X", (int)ch)); 343 } else { 344 sb.append(ch); 345 } 346 break; 347 } 348 } 349 return sb.toString(); 350 } 351 352 /** 353 * Returns a content tree for the SCRIPT tag for the main page(index.html). 354 * 355 * @return a content for the SCRIPT tag 356 */ 357 protected Content getFramesJavaScript() { 358 HtmlTree script = HtmlTree.SCRIPT(); 359 String scriptCode = DocletConstants.NL + 360 " targetPage = \"\" + window.location.search;" + DocletConstants.NL + 361 " if (targetPage != \"\" && targetPage != \"undefined\")" + DocletConstants.NL + 362 " targetPage = targetPage.substring(1);" + DocletConstants.NL + 363 " if (targetPage.indexOf(\":\") != -1 || (targetPage != \"\" && !validURL(targetPage)))" + DocletConstants.NL + 364 " targetPage = \"undefined\";" + DocletConstants.NL + 365 " function validURL(url) {" + DocletConstants.NL + 366 " try {" + DocletConstants.NL + 367 " url = decodeURIComponent(url);" + DocletConstants.NL + 368 " }" + DocletConstants.NL + 369 " catch (error) {" + DocletConstants.NL + 370 " return false;" + DocletConstants.NL + 371 " }" + DocletConstants.NL + 372 " var pos = url.indexOf(\".html\");" + DocletConstants.NL + 373 " if (pos == -1 || pos != url.length - 5)" + DocletConstants.NL + 374 " return false;" + DocletConstants.NL + 375 " var allowNumber = false;" + DocletConstants.NL + 376 " var allowSep = false;" + DocletConstants.NL + 377 " var seenDot = false;" + DocletConstants.NL + 378 " for (var i = 0; i < url.length - 5; i++) {" + DocletConstants.NL + 379 " var ch = url.charAt(i);" + DocletConstants.NL + 380 " if ('a' <= ch && ch <= 'z' ||" + DocletConstants.NL + 381 " 'A' <= ch && ch <= 'Z' ||" + DocletConstants.NL + 382 " ch == '$' ||" + DocletConstants.NL + 383 " ch == '_' ||" + DocletConstants.NL + 384 " ch.charCodeAt(0) > 127) {" + DocletConstants.NL + 385 " allowNumber = true;" + DocletConstants.NL + 386 " allowSep = true;" + DocletConstants.NL + 387 " } else if ('0' <= ch && ch <= '9'" + DocletConstants.NL + 388 " || ch == '-') {" + DocletConstants.NL + 389 " if (!allowNumber)" + DocletConstants.NL + 390 " return false;" + DocletConstants.NL + 391 " } else if (ch == '/' || ch == '.') {" + DocletConstants.NL + 392 " if (!allowSep)" + DocletConstants.NL + 393 " return false;" + DocletConstants.NL + 394 " allowNumber = false;" + DocletConstants.NL + 395 " allowSep = false;" + DocletConstants.NL + 396 " if (ch == '.')" + DocletConstants.NL + 397 " seenDot = true;" + DocletConstants.NL + 398 " if (ch == '/' && seenDot)" + DocletConstants.NL + 399 " return false;" + DocletConstants.NL + 400 " } else {" + DocletConstants.NL + 401 " return false;"+ DocletConstants.NL + 402 " }" + DocletConstants.NL + 403 " }" + DocletConstants.NL + 404 " return true;" + DocletConstants.NL + 405 " }" + DocletConstants.NL; 406 RawHtml scriptContent = new RawHtml(scriptCode); 407 script.addContent(scriptContent); 408 return script; 409 } 410 411 /** 412 * Returns an HtmlTree for the BODY tag. 413 * 414 * @param includeScript set true if printing windowtitle script 415 * @param title title for the window 416 * @return an HtmlTree for the BODY tag 417 */ 418 public HtmlTree getBody(boolean includeScript, String title) { 419 HtmlTree body = new HtmlTree(HtmlTag.BODY); 420 // Set window title string which is later printed 421 this.winTitle = title; 422 // Don't print windowtitle script for overview-frame, allclasses-frame 423 // and package-frame 424 if (includeScript) { 425 this.script = getWinTitleScript(); 426 body.addContent(script); 427 Content noScript = HtmlTree.NOSCRIPT( 428 HtmlTree.DIV(getResource("doclet.No_Script_Message"))); 429 body.addContent(noScript); 430 } 431 return body; 432 } 433 434 /** 435 * Generated javascript variables for the document. 436 * 437 * @param typeMap map comprising of method and type relationship 438 * @param methodTypes set comprising of all methods types for this class 439 */ 440 public void generateMethodTypesScript(Map<String,Integer> typeMap, 441 Set<MethodTypes> methodTypes) { 442 String sep = ""; 443 StringBuilder vars = new StringBuilder("var methods = {"); 444 for (Map.Entry<String,Integer> entry : typeMap.entrySet()) { 445 vars.append(sep); 446 sep = ","; 447 vars.append("\"") 448 .append(entry.getKey()) 449 .append("\":") 450 .append(entry.getValue()); 451 } 452 vars.append("};").append(DocletConstants.NL); 453 sep = ""; 454 vars.append("var tabs = {"); 455 for (MethodTypes entry : methodTypes) { 456 vars.append(sep); 457 sep = ","; 458 vars.append(entry.value()) 459 .append(":") 460 .append("[") 461 .append("\"") 462 .append(entry.tabId()) 463 .append("\"") 464 .append(sep) 465 .append("\"") 466 .append(configuration.getText(entry.resourceKey())) 467 .append("\"]"); 468 } 469 vars.append("};") 470 .append(DocletConstants.NL); 471 addStyles(HtmlStyle.altColor, vars); 472 addStyles(HtmlStyle.rowColor, vars); 473 addStyles(HtmlStyle.tableTab, vars); 474 addStyles(HtmlStyle.activeTableTab, vars); 475 script.addContent(new RawHtml(vars.toString())); 476 } 477 478 /** 479 * Adds javascript style variables to the document. 480 * 481 * @param style style to be added as a javascript variable 482 * @param vars variable string to which the style variable will be added 483 */ 484 public void addStyles(HtmlStyle style, StringBuilder vars) { 485 vars.append("var ").append(style).append(" = \"").append(style) 486 .append("\";").append(DocletConstants.NL); 487 } 488 489 /** 490 * Returns an HtmlTree for the TITLE tag. 491 * 492 * @return an HtmlTree for the TITLE tag 493 */ 494 public HtmlTree getTitle() { 495 HtmlTree title = HtmlTree.TITLE(new StringContent(winTitle)); 496 return title; 497 } 498 499 public String codeText(String text) { 500 return "<code>" + text + "</code>"; 501 } 502 503 /** 504 * Return "&nbsp;", non-breaking space. 505 */ 506 public Content getSpace() { 507 return RawHtml.nbsp; 508 } 509 510 /* 511 * Returns a header for Modifier and Type column of a table. 512 */ 513 public String getModifierTypeHeader() { 514 return modifierTypeHeader; 515 } 516 }