aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.doclets.formats.html.markup; aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.util.*; aoqi@0: import java.util.regex.Matcher; aoqi@0: import java.util.regex.Pattern; aoqi@0: aoqi@0: import com.sun.tools.doclets.internal.toolkit.*; aoqi@0: import com.sun.tools.doclets.internal.toolkit.util.*; aoqi@0: aoqi@0: /** aoqi@0: * Class for the Html format code generation. aoqi@0: * Initializes PrintWriter with FileWriter, to enable print aoqi@0: * related methods to generate the code to the named File through FileWriter. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own risk. aoqi@0: * This code and its internal interfaces are subject to change or aoqi@0: * deletion without notice. aoqi@0: * aoqi@0: * @since 1.2 aoqi@0: * @author Atul M Dambalkar aoqi@0: * @author Bhavesh Patel (Modified) aoqi@0: */ aoqi@0: public class HtmlWriter { aoqi@0: aoqi@0: /** aoqi@0: * The window title of this file aoqi@0: */ aoqi@0: protected String winTitle; aoqi@0: aoqi@0: /** aoqi@0: * The configuration aoqi@0: */ aoqi@0: protected Configuration configuration; aoqi@0: aoqi@0: /** aoqi@0: * The flag to indicate whether a member details list is printed or not. aoqi@0: */ aoqi@0: protected boolean memberDetailsListPrinted; aoqi@0: aoqi@0: /** aoqi@0: * Header for table displaying profiles and description.. aoqi@0: */ aoqi@0: protected final String[] profileTableHeader; aoqi@0: aoqi@0: /** aoqi@0: * Header for tables displaying packages and description.. aoqi@0: */ aoqi@0: protected final String[] packageTableHeader; aoqi@0: aoqi@0: /** aoqi@0: * Summary for use tables displaying class and package use. aoqi@0: */ aoqi@0: protected final String useTableSummary; aoqi@0: aoqi@0: /** aoqi@0: * Column header for class docs displaying Modifier and Type header. aoqi@0: */ aoqi@0: protected final String modifierTypeHeader; aoqi@0: aoqi@0: public final Content overviewLabel; aoqi@0: aoqi@0: public final Content defaultPackageLabel; aoqi@0: aoqi@0: public final Content packageLabel; aoqi@0: aoqi@0: public final Content profileLabel; aoqi@0: aoqi@0: public final Content useLabel; aoqi@0: aoqi@0: public final Content prevLabel; aoqi@0: aoqi@0: public final Content nextLabel; aoqi@0: aoqi@0: public final Content prevclassLabel; aoqi@0: aoqi@0: public final Content nextclassLabel; aoqi@0: aoqi@0: public final Content summaryLabel; aoqi@0: aoqi@0: public final Content detailLabel; aoqi@0: aoqi@0: public final Content framesLabel; aoqi@0: aoqi@0: public final Content noframesLabel; aoqi@0: aoqi@0: public final Content treeLabel; aoqi@0: aoqi@0: public final Content classLabel; aoqi@0: aoqi@0: public final Content deprecatedLabel; aoqi@0: aoqi@0: public final Content deprecatedPhrase; aoqi@0: aoqi@0: public final Content allclassesLabel; aoqi@0: aoqi@0: public final Content allpackagesLabel; aoqi@0: aoqi@0: public final Content allprofilesLabel; aoqi@0: aoqi@0: public final Content indexLabel; aoqi@0: aoqi@0: public final Content helpLabel; aoqi@0: aoqi@0: public final Content seeLabel; aoqi@0: aoqi@0: public final Content descriptionLabel; aoqi@0: aoqi@0: public final Content prevpackageLabel; aoqi@0: aoqi@0: public final Content nextpackageLabel; aoqi@0: aoqi@0: public final Content prevprofileLabel; aoqi@0: aoqi@0: public final Content nextprofileLabel; aoqi@0: aoqi@0: public final Content packagesLabel; aoqi@0: aoqi@0: public final Content profilesLabel; aoqi@0: aoqi@0: public final Content methodDetailsLabel; aoqi@0: aoqi@0: public final Content annotationTypeDetailsLabel; aoqi@0: aoqi@0: public final Content fieldDetailsLabel; aoqi@0: aoqi@0: public final Content propertyDetailsLabel; aoqi@0: aoqi@0: public final Content constructorDetailsLabel; aoqi@0: aoqi@0: public final Content enumConstantsDetailsLabel; aoqi@0: aoqi@0: public final Content specifiedByLabel; aoqi@0: aoqi@0: public final Content overridesLabel; aoqi@0: aoqi@0: public final Content descfrmClassLabel; aoqi@0: aoqi@0: public final Content descfrmInterfaceLabel; aoqi@0: aoqi@0: private final Writer writer; aoqi@0: aoqi@0: private Content script; aoqi@0: aoqi@0: /** aoqi@0: * Constructor. aoqi@0: * aoqi@0: * @param path The directory path to be created for this file aoqi@0: * or null if none to be created. aoqi@0: * @exception IOException Exception raised by the FileWriter is passed on aoqi@0: * to next level. aoqi@0: * @exception UnsupportedEncodingException Exception raised by the aoqi@0: * OutputStreamWriter is passed on to next level. aoqi@0: */ aoqi@0: public HtmlWriter(Configuration configuration, DocPath path) aoqi@0: throws IOException, UnsupportedEncodingException { aoqi@0: writer = DocFile.createFileForOutput(configuration, path).openWriter(); aoqi@0: this.configuration = configuration; aoqi@0: this.memberDetailsListPrinted = false; aoqi@0: profileTableHeader = new String[] { aoqi@0: configuration.getText("doclet.Profile"), aoqi@0: configuration.getText("doclet.Description") aoqi@0: }; aoqi@0: packageTableHeader = new String[] { aoqi@0: configuration.getText("doclet.Package"), aoqi@0: configuration.getText("doclet.Description") aoqi@0: }; aoqi@0: useTableSummary = configuration.getText("doclet.Use_Table_Summary", aoqi@0: configuration.getText("doclet.packages")); aoqi@0: modifierTypeHeader = configuration.getText("doclet.0_and_1", aoqi@0: configuration.getText("doclet.Modifier"), aoqi@0: configuration.getText("doclet.Type")); aoqi@0: overviewLabel = getResource("doclet.Overview"); aoqi@0: defaultPackageLabel = new StringContent(DocletConstants.DEFAULT_PACKAGE_NAME); aoqi@0: packageLabel = getResource("doclet.Package"); aoqi@0: profileLabel = getResource("doclet.Profile"); aoqi@0: useLabel = getResource("doclet.navClassUse"); aoqi@0: prevLabel = getResource("doclet.Prev"); aoqi@0: nextLabel = getResource("doclet.Next"); aoqi@0: prevclassLabel = getNonBreakResource("doclet.Prev_Class"); aoqi@0: nextclassLabel = getNonBreakResource("doclet.Next_Class"); aoqi@0: summaryLabel = getResource("doclet.Summary"); aoqi@0: detailLabel = getResource("doclet.Detail"); aoqi@0: framesLabel = getResource("doclet.Frames"); aoqi@0: noframesLabel = getNonBreakResource("doclet.No_Frames"); aoqi@0: treeLabel = getResource("doclet.Tree"); aoqi@0: classLabel = getResource("doclet.Class"); aoqi@0: deprecatedLabel = getResource("doclet.navDeprecated"); aoqi@0: deprecatedPhrase = getResource("doclet.Deprecated"); aoqi@0: allclassesLabel = getNonBreakResource("doclet.All_Classes"); aoqi@0: allpackagesLabel = getNonBreakResource("doclet.All_Packages"); aoqi@0: allprofilesLabel = getNonBreakResource("doclet.All_Profiles"); aoqi@0: indexLabel = getResource("doclet.Index"); aoqi@0: helpLabel = getResource("doclet.Help"); aoqi@0: seeLabel = getResource("doclet.See"); aoqi@0: descriptionLabel = getResource("doclet.Description"); aoqi@0: prevpackageLabel = getNonBreakResource("doclet.Prev_Package"); aoqi@0: nextpackageLabel = getNonBreakResource("doclet.Next_Package"); aoqi@0: prevprofileLabel = getNonBreakResource("doclet.Prev_Profile"); aoqi@0: nextprofileLabel = getNonBreakResource("doclet.Next_Profile"); aoqi@0: packagesLabel = getResource("doclet.Packages"); aoqi@0: profilesLabel = getResource("doclet.Profiles"); aoqi@0: methodDetailsLabel = getResource("doclet.Method_Detail"); aoqi@0: annotationTypeDetailsLabel = getResource("doclet.Annotation_Type_Member_Detail"); aoqi@0: fieldDetailsLabel = getResource("doclet.Field_Detail"); aoqi@0: propertyDetailsLabel = getResource("doclet.Property_Detail"); aoqi@0: constructorDetailsLabel = getResource("doclet.Constructor_Detail"); aoqi@0: enumConstantsDetailsLabel = getResource("doclet.Enum_Constant_Detail"); aoqi@0: specifiedByLabel = getResource("doclet.Specified_By"); aoqi@0: overridesLabel = getResource("doclet.Overrides"); aoqi@0: descfrmClassLabel = getResource("doclet.Description_From_Class"); aoqi@0: descfrmInterfaceLabel = getResource("doclet.Description_From_Interface"); aoqi@0: } aoqi@0: aoqi@0: public void write(Content c) throws IOException { aoqi@0: c.write(writer, true); aoqi@0: } aoqi@0: aoqi@0: public void close() throws IOException { aoqi@0: writer.close(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Get the configuration string as a content. aoqi@0: * aoqi@0: * @param key the key to look for in the configuration file aoqi@0: * @return a content tree for the text aoqi@0: */ aoqi@0: public Content getResource(String key) { aoqi@0: return configuration.getResource(key); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Get the configuration string as a content, replacing spaces aoqi@0: * with non-breaking spaces. aoqi@0: * aoqi@0: * @param key the key to look for in the configuration file aoqi@0: * @return a content tree for the text aoqi@0: */ aoqi@0: public Content getNonBreakResource(String key) { aoqi@0: String text = configuration.getText(key); aoqi@0: Content c = configuration.newContent(); aoqi@0: int start = 0; aoqi@0: int p; aoqi@0: while ((p = text.indexOf(" ", start)) != -1) { aoqi@0: c.addContent(text.substring(start, p)); aoqi@0: c.addContent(RawHtml.nbsp); aoqi@0: start = p + 1; aoqi@0: } aoqi@0: c.addContent(text.substring(start)); aoqi@0: return c; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Get the configuration string as a content. aoqi@0: * aoqi@0: * @param key the key to look for in the configuration file aoqi@0: * @param o string or content argument added to configuration text aoqi@0: * @return a content tree for the text aoqi@0: */ aoqi@0: public Content getResource(String key, Object o) { aoqi@0: return configuration.getResource(key, o); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Get the configuration string as a content. aoqi@0: * aoqi@0: * @param key the key to look for in the configuration file aoqi@0: * @param o1 string or content argument added to configuration text aoqi@0: * @param o2 string or content argument added to configuration text aoqi@0: * @return a content tree for the text aoqi@0: */ aoqi@0: public Content getResource(String key, Object o0, Object o1) { aoqi@0: return configuration.getResource(key, o0, o1); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns an HtmlTree for the SCRIPT tag. aoqi@0: * aoqi@0: * @return an HtmlTree for the SCRIPT tag aoqi@0: */ aoqi@0: protected HtmlTree getWinTitleScript(){ aoqi@0: HtmlTree script = new HtmlTree(HtmlTag.SCRIPT); aoqi@0: if(winTitle != null && winTitle.length() > 0) { aoqi@0: script.addAttr(HtmlAttr.TYPE, "text/javascript"); aoqi@0: String scriptCode = "" + DocletConstants.NL; aoqi@0: RawHtml scriptContent = new RawHtml(scriptCode); aoqi@0: script.addContent(scriptContent); aoqi@0: } aoqi@0: return script; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a String with escaped special JavaScript characters. aoqi@0: * aoqi@0: * @param s String that needs to be escaped aoqi@0: * @return a valid escaped JavaScript string aoqi@0: */ aoqi@0: private static String escapeJavaScriptChars(String s) { aoqi@0: StringBuilder sb = new StringBuilder(); aoqi@0: for (int i = 0; i < s.length(); i++) { aoqi@0: char ch = s.charAt(i); aoqi@0: switch (ch) { aoqi@0: case '\b': aoqi@0: sb.append("\\b"); aoqi@0: break; aoqi@0: case '\t': aoqi@0: sb.append("\\t"); aoqi@0: break; aoqi@0: case '\n': aoqi@0: sb.append("\\n"); aoqi@0: break; aoqi@0: case '\f': aoqi@0: sb.append("\\f"); aoqi@0: break; aoqi@0: case '\r': aoqi@0: sb.append("\\r"); aoqi@0: break; aoqi@0: case '"': aoqi@0: sb.append("\\\""); aoqi@0: break; aoqi@0: case '\'': aoqi@0: sb.append("\\\'"); aoqi@0: break; aoqi@0: case '\\': aoqi@0: sb.append("\\\\"); aoqi@0: break; aoqi@0: default: aoqi@0: if (ch < 32 || ch >= 127) { aoqi@0: sb.append(String.format("\\u%04X", (int)ch)); aoqi@0: } else { aoqi@0: sb.append(ch); aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: return sb.toString(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a content tree for the SCRIPT tag for the main page(index.html). aoqi@0: * aoqi@0: * @return a content for the SCRIPT tag aoqi@0: */ aoqi@0: protected Content getFramesetJavaScript() { aoqi@0: HtmlTree script = new HtmlTree(HtmlTag.SCRIPT); aoqi@0: script.addAttr(HtmlAttr.TYPE, "text/javascript"); aoqi@0: String scriptCode = DocletConstants.NL + aoqi@0: " targetPage = \"\" + window.location.search;" + DocletConstants.NL + aoqi@0: " if (targetPage != \"\" && targetPage != \"undefined\")" + DocletConstants.NL + aoqi@0: " targetPage = targetPage.substring(1);" + DocletConstants.NL + aoqi@0: " if (targetPage.indexOf(\":\") != -1 || (targetPage != \"\" && !validURL(targetPage)))" + DocletConstants.NL + aoqi@0: " targetPage = \"undefined\";" + DocletConstants.NL + aoqi@0: " function validURL(url) {" + DocletConstants.NL + aoqi@0: " try {" + DocletConstants.NL + aoqi@0: " url = decodeURIComponent(url);" + DocletConstants.NL + aoqi@0: " }" + DocletConstants.NL + aoqi@0: " catch (error) {" + DocletConstants.NL + aoqi@0: " return false;" + DocletConstants.NL + aoqi@0: " }" + DocletConstants.NL + aoqi@0: " var pos = url.indexOf(\".html\");" + DocletConstants.NL + aoqi@0: " if (pos == -1 || pos != url.length - 5)" + DocletConstants.NL + aoqi@0: " return false;" + DocletConstants.NL + aoqi@0: " var allowNumber = false;" + DocletConstants.NL + aoqi@0: " var allowSep = false;" + DocletConstants.NL + aoqi@0: " var seenDot = false;" + DocletConstants.NL + aoqi@0: " for (var i = 0; i < url.length - 5; i++) {" + DocletConstants.NL + aoqi@0: " var ch = url.charAt(i);" + DocletConstants.NL + aoqi@0: " if ('a' <= ch && ch <= 'z' ||" + DocletConstants.NL + aoqi@0: " 'A' <= ch && ch <= 'Z' ||" + DocletConstants.NL + aoqi@0: " ch == '$' ||" + DocletConstants.NL + aoqi@0: " ch == '_' ||" + DocletConstants.NL + aoqi@0: " ch.charCodeAt(0) > 127) {" + DocletConstants.NL + aoqi@0: " allowNumber = true;" + DocletConstants.NL + aoqi@0: " allowSep = true;" + DocletConstants.NL + aoqi@0: " } else if ('0' <= ch && ch <= '9'" + DocletConstants.NL + aoqi@0: " || ch == '-') {" + DocletConstants.NL + aoqi@0: " if (!allowNumber)" + DocletConstants.NL + aoqi@0: " return false;" + DocletConstants.NL + aoqi@0: " } else if (ch == '/' || ch == '.') {" + DocletConstants.NL + aoqi@0: " if (!allowSep)" + DocletConstants.NL + aoqi@0: " return false;" + DocletConstants.NL + aoqi@0: " allowNumber = false;" + DocletConstants.NL + aoqi@0: " allowSep = false;" + DocletConstants.NL + aoqi@0: " if (ch == '.')" + DocletConstants.NL + aoqi@0: " seenDot = true;" + DocletConstants.NL + aoqi@0: " if (ch == '/' && seenDot)" + DocletConstants.NL + aoqi@0: " return false;" + DocletConstants.NL + aoqi@0: " } else {" + DocletConstants.NL + aoqi@0: " return false;"+ DocletConstants.NL + aoqi@0: " }" + DocletConstants.NL + aoqi@0: " }" + DocletConstants.NL + aoqi@0: " return true;" + DocletConstants.NL + aoqi@0: " }" + DocletConstants.NL + aoqi@0: " function loadFrames() {" + DocletConstants.NL + aoqi@0: " if (targetPage != \"\" && targetPage != \"undefined\")" + DocletConstants.NL + aoqi@0: " top.classFrame.location = top.targetPage;" + DocletConstants.NL + aoqi@0: " }" + DocletConstants.NL; aoqi@0: RawHtml scriptContent = new RawHtml(scriptCode); aoqi@0: script.addContent(scriptContent); aoqi@0: return script; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns an HtmlTree for the BODY tag. aoqi@0: * aoqi@0: * @param includeScript set true if printing windowtitle script aoqi@0: * @param title title for the window aoqi@0: * @return an HtmlTree for the BODY tag aoqi@0: */ aoqi@0: public HtmlTree getBody(boolean includeScript, String title) { aoqi@0: HtmlTree body = new HtmlTree(HtmlTag.BODY); aoqi@0: // Set window title string which is later printed aoqi@0: this.winTitle = title; aoqi@0: // Don't print windowtitle script for overview-frame, allclasses-frame aoqi@0: // and package-frame aoqi@0: if (includeScript) { aoqi@0: this.script = getWinTitleScript(); aoqi@0: body.addContent(script); aoqi@0: Content noScript = HtmlTree.NOSCRIPT( aoqi@0: HtmlTree.DIV(getResource("doclet.No_Script_Message"))); aoqi@0: body.addContent(noScript); aoqi@0: } aoqi@0: return body; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Generated javascript variables for the document. aoqi@0: * aoqi@0: * @param typeMap map comprising of method and type relationship aoqi@0: * @param methodTypes set comprising of all methods types for this class aoqi@0: */ aoqi@0: public void generateMethodTypesScript(Map typeMap, aoqi@0: Set methodTypes) { aoqi@0: String sep = ""; aoqi@0: StringBuilder vars = new StringBuilder("var methods = {"); aoqi@0: for (Map.Entry entry : typeMap.entrySet()) { aoqi@0: vars.append(sep); aoqi@0: sep = ","; bpatel@2800: vars.append("\"") bpatel@2800: .append(entry.getKey()) bpatel@2800: .append("\":") bpatel@2800: .append(entry.getValue()); aoqi@0: } aoqi@0: vars.append("};").append(DocletConstants.NL); aoqi@0: sep = ""; aoqi@0: vars.append("var tabs = {"); aoqi@0: for (MethodTypes entry : methodTypes) { aoqi@0: vars.append(sep); aoqi@0: sep = ","; bpatel@2800: vars.append(entry.value()) bpatel@2800: .append(":") bpatel@2800: .append("[") bpatel@2800: .append("\"") bpatel@2800: .append(entry.tabId()) bpatel@2800: .append("\"") bpatel@2800: .append(sep) bpatel@2800: .append("\"") bpatel@2800: .append(configuration.getText(entry.resourceKey())) bpatel@2800: .append("\"]"); aoqi@0: } bpatel@2800: vars.append("};") bpatel@2800: .append(DocletConstants.NL); aoqi@0: addStyles(HtmlStyle.altColor, vars); aoqi@0: addStyles(HtmlStyle.rowColor, vars); aoqi@0: addStyles(HtmlStyle.tableTab, vars); aoqi@0: addStyles(HtmlStyle.activeTableTab, vars); aoqi@0: script.addContent(new RawHtml(vars.toString())); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Adds javascript style variables to the document. aoqi@0: * aoqi@0: * @param style style to be added as a javascript variable aoqi@0: * @param vars variable string to which the style variable will be added aoqi@0: */ aoqi@0: public void addStyles(HtmlStyle style, StringBuilder vars) { aoqi@0: vars.append("var ").append(style).append(" = \"").append(style) aoqi@0: .append("\";").append(DocletConstants.NL); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns an HtmlTree for the TITLE tag. aoqi@0: * aoqi@0: * @return an HtmlTree for the TITLE tag aoqi@0: */ aoqi@0: public HtmlTree getTitle() { aoqi@0: HtmlTree title = HtmlTree.TITLE(new StringContent(winTitle)); aoqi@0: return title; aoqi@0: } aoqi@0: aoqi@0: public String codeText(String text) { aoqi@0: return "" + text + ""; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return "&nbsp;", non-breaking space. aoqi@0: */ aoqi@0: public Content getSpace() { aoqi@0: return RawHtml.nbsp; aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Returns a header for Modifier and Type column of a table. aoqi@0: */ aoqi@0: public String getModifierTypeHeader() { aoqi@0: return modifierTypeHeader; aoqi@0: } aoqi@0: }