1 /*
   2  * Copyright (c) 1997, 2013, 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 com.sun.tools.doclets.formats.html.markup;
  27 
  28 import java.io.*;
  29 import java.util.*;
  30 
  31 import com.sun.tools.doclets.internal.toolkit.*;
  32 import com.sun.tools.doclets.internal.toolkit.util.*;
  33 
  34 /**
  35  * Class for the Html format code generation.
  36  * Initializes PrintWriter with FileWriter, to enable print
  37  * related methods to generate the code to the named File through FileWriter.
  38  *
  39  *  <p><b>This is NOT part of any supported API.
  40  *  If you write code that depends on this, you do so at your own risk.
  41  *  This code and its internal interfaces are subject to change or
  42  *  deletion without notice.</b>
  43  *
  44  * @since 1.2
  45  * @author Atul M Dambalkar
  46  * @author Bhavesh Patel (Modified)
  47  */
  48 public class HtmlWriter {
  49 
  50     /**
  51      * The window title of this file
  52      */
  53     protected String winTitle;
  54 
  55     /**
  56      * The configuration
  57      */
  58     protected Configuration configuration;
  59 
  60     /**
  61      * The flag to indicate whether a member details list is printed or not.
  62      */
  63     protected boolean memberDetailsListPrinted;
  64 
  65     /**
  66      * Header for table displaying profiles and description..
  67      */
  68     protected final String[] profileTableHeader;
  69 
  70     /**
  71      * Header for tables displaying packages and description..
  72      */
  73     protected final 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 profileLabel;
  92 
  93     public final Content useLabel;
  94 
  95     public final Content prevLabel;
  96 
  97     public final Content nextLabel;
  98 
  99     public final Content prevclassLabel;
 100 
 101     public final Content nextclassLabel;
 102 
 103     public final Content summaryLabel;
 104 
 105     public final Content detailLabel;
 106 
 107     public final Content framesLabel;
 108 
 109     public final Content noframesLabel;
 110 
 111     public final Content treeLabel;
 112 
 113     public final Content classLabel;
 114 
 115     public final Content deprecatedLabel;
 116 
 117     public final Content deprecatedPhrase;
 118 
 119     public final Content allclassesLabel;
 120 
 121     public final Content allpackagesLabel;
 122 
 123     public final Content allprofilesLabel;
 124 
 125     public final Content indexLabel;
 126 
 127     public final Content helpLabel;
 128 
 129     public final Content seeLabel;
 130 
 131     public final Content descriptionLabel;
 132 
 133     public final Content prevpackageLabel;
 134 
 135     public final Content nextpackageLabel;
 136 
 137     public final Content prevprofileLabel;
 138 
 139     public final Content nextprofileLabel;
 140 
 141     public final Content packagesLabel;
 142 
 143     public final Content profilesLabel;
 144 
 145     public final Content methodDetailsLabel;
 146 
 147     public final Content annotationTypeDetailsLabel;
 148 
 149     public final Content fieldDetailsLabel;
 150 
 151     public final Content constructorDetailsLabel;
 152 
 153     public final Content enumConstantsDetailsLabel;
 154 
 155     public final Content specifiedByLabel;
 156 
 157     public final Content overridesLabel;
 158 
 159     public final Content descfrmClassLabel;
 160 
 161     public final Content descfrmInterfaceLabel;
 162 
 163     private final Writer writer;
 164 
 165     private Content script;
 166 
 167     /**
 168      * Constructor.
 169      *
 170      * @param path The directory path to be created for this file
 171      *             or null if none to be created.
 172      * @exception IOException Exception raised by the FileWriter is passed on
 173      * to next level.
 174      * @exception UnsupportedEncodingException Exception raised by the
 175      * OutputStreamWriter is passed on to next level.
 176      */
 177     public HtmlWriter(Configuration configuration, DocPath path)
 178             throws IOException, UnsupportedEncodingException {
 179         writer = DocFile.createFileForOutput(configuration, path).openWriter();
 180         this.configuration = configuration;
 181         this.memberDetailsListPrinted = false;
 182         profileTableHeader = new String[] {
 183             configuration.getText("doclet.Profile"),
 184             configuration.getText("doclet.Description")
 185         };
 186         packageTableHeader = new String[] {
 187             configuration.getText("doclet.Package"),
 188             configuration.getText("doclet.Description")
 189         };
 190         useTableSummary = configuration.getText("doclet.Use_Table_Summary",
 191                 configuration.getText("doclet.packages"));
 192         modifierTypeHeader = configuration.getText("doclet.0_and_1",
 193                 configuration.getText("doclet.Modifier"),
 194                 configuration.getText("doclet.Type"));
 195         overviewLabel = getResource("doclet.Overview");
 196         defaultPackageLabel = new RawHtml(
 197                 DocletConstants.DEFAULT_PACKAGE_NAME);
 198         packageLabel = getResource("doclet.Package");
 199         profileLabel = getResource("doclet.Profile");
 200         useLabel = getResource("doclet.navClassUse");
 201         prevLabel = getResource("doclet.Prev");
 202         nextLabel = getResource("doclet.Next");
 203         prevclassLabel = getResource("doclet.Prev_Class");
 204         nextclassLabel = getResource("doclet.Next_Class");
 205         summaryLabel = getResource("doclet.Summary");
 206         detailLabel = getResource("doclet.Detail");
 207         framesLabel = getResource("doclet.Frames");
 208         noframesLabel = getResource("doclet.No_Frames");
 209         treeLabel = getResource("doclet.Tree");
 210         classLabel = getResource("doclet.Class");
 211         deprecatedLabel = getResource("doclet.navDeprecated");
 212         deprecatedPhrase = getResource("doclet.Deprecated");
 213         allclassesLabel = getResource("doclet.All_Classes");
 214         allpackagesLabel = getResource("doclet.All_Packages");
 215         allprofilesLabel = getResource("doclet.All_Profiles");
 216         indexLabel = getResource("doclet.Index");
 217         helpLabel = getResource("doclet.Help");
 218         seeLabel = getResource("doclet.See");
 219         descriptionLabel = getResource("doclet.Description");
 220         prevpackageLabel = getResource("doclet.Prev_Package");
 221         nextpackageLabel = getResource("doclet.Next_Package");
 222         prevprofileLabel = getResource("doclet.Prev_Profile");
 223         nextprofileLabel = getResource("doclet.Next_Profile");
 224         packagesLabel = getResource("doclet.Packages");
 225         profilesLabel = getResource("doclet.Profiles");
 226         methodDetailsLabel = getResource("doclet.Method_Detail");
 227         annotationTypeDetailsLabel = getResource("doclet.Annotation_Type_Member_Detail");
 228         fieldDetailsLabel = getResource("doclet.Field_Detail");
 229         constructorDetailsLabel = getResource("doclet.Constructor_Detail");
 230         enumConstantsDetailsLabel = getResource("doclet.Enum_Constant_Detail");
 231         specifiedByLabel = getResource("doclet.Specified_By");
 232         overridesLabel = getResource("doclet.Overrides");
 233         descfrmClassLabel = getResource("doclet.Description_From_Class");
 234         descfrmInterfaceLabel = getResource("doclet.Description_From_Interface");
 235     }
 236 
 237     public void write(Content c) throws IOException {
 238         c.write(writer, true);
 239     }
 240 
 241     public void close() throws IOException {
 242         writer.close();
 243     }
 244 
 245     /**
 246      * Get the configuration string as a content.
 247      *
 248      * @param key the key to look for in the configuration file
 249      * @return a content tree for the text
 250      */
 251     public Content getResource(String key) {
 252         return new StringContent(configuration.getText(key));
 253     }
 254 
 255     /**
 256      * Get the configuration string as a content.
 257      *
 258      * @param key the key to look for in the configuration file
 259      * @param a1 string argument added to configuration text
 260      * @return a content tree for the text
 261      */
 262     public Content getResource(String key, String a1) {
 263         return new RawHtml(configuration.getText(key, a1));
 264     }
 265 
 266     /**
 267      * Get the configuration string as a content.
 268      *
 269      * @param key the key to look for in the configuration file
 270      * @param a1 string argument added to configuration text
 271      * @param a2 string argument added to configuration text
 272      * @return a content tree for the text
 273      */
 274     public Content getResource(String key, String a1, String a2) {
 275         return new RawHtml(configuration.getText(key, a1, a2));
 276     }
 277 
 278     /**
 279      * Returns an HtmlTree for the SCRIPT tag.
 280      *
 281      * @return an HtmlTree for the SCRIPT tag
 282      */
 283     protected HtmlTree getWinTitleScript(){
 284         HtmlTree script = new HtmlTree(HtmlTag.SCRIPT);
 285         if(winTitle != null && winTitle.length() > 0) {
 286             script.addAttr(HtmlAttr.TYPE, "text/javascript");
 287             String scriptCode = "<!--" + DocletConstants.NL +
 288                     "    if (location.href.indexOf('is-external=true') == -1) {" + DocletConstants.NL +
 289                     "        parent.document.title=\"" + winTitle + "\";" + DocletConstants.NL +
 290                     "    }" + DocletConstants.NL +
 291                     "//-->" + DocletConstants.NL;
 292             RawHtml scriptContent = new RawHtml(scriptCode);
 293             script.addContent(scriptContent);
 294         }
 295         return script;
 296     }
 297 
 298     /**
 299      * Returns a content tree for the SCRIPT tag for the main page(index.html).
 300      *
 301      * @return a content for the SCRIPT tag
 302      */
 303     protected Content getFramesetJavaScript(){
 304         HtmlTree script = new HtmlTree(HtmlTag.SCRIPT);
 305         script.addAttr(HtmlAttr.TYPE, "text/javascript");
 306         String scriptCode = DocletConstants.NL + "    targetPage = \"\" + window.location.search;" + DocletConstants.NL +
 307                 "    if (targetPage != \"\" && targetPage != \"undefined\")" + DocletConstants.NL +
 308                 "        targetPage = targetPage.substring(1);" + DocletConstants.NL +
 309                 "    if (targetPage.indexOf(\":\") != -1)" + DocletConstants.NL +
 310                 "        targetPage = \"undefined\";" + DocletConstants.NL +
 311                 "    function loadFrames() {" + DocletConstants.NL +
 312                 "        if (targetPage != \"\" && targetPage != \"undefined\")" + DocletConstants.NL +
 313                 "             top.classFrame.location = top.targetPage;" + DocletConstants.NL +
 314                 "    }" + DocletConstants.NL;
 315         RawHtml scriptContent = new RawHtml(scriptCode);
 316         script.addContent(scriptContent);
 317         return script;
 318     }
 319 
 320     /**
 321      * Returns an HtmlTree for the BODY tag.
 322      *
 323      * @param includeScript  set true if printing windowtitle script
 324      * @param title title for the window
 325      * @return an HtmlTree for the BODY tag
 326      */
 327     public HtmlTree getBody(boolean includeScript, String title) {
 328         HtmlTree body = new HtmlTree(HtmlTag.BODY);
 329         // Set window title string which is later printed
 330         this.winTitle = title;
 331         // Don't print windowtitle script for overview-frame, allclasses-frame
 332         // and package-frame
 333         if (includeScript) {
 334             this.script = getWinTitleScript();
 335             body.addContent(script);
 336             Content noScript = HtmlTree.NOSCRIPT(
 337                     HtmlTree.DIV(getResource("doclet.No_Script_Message")));
 338             body.addContent(noScript);
 339         }
 340         return body;
 341     }
 342 
 343     /**
 344      * Generated javascript variables for the document.
 345      *
 346      * @param typeMap map comprising of method and type relationship
 347      * @param methodTypes set comprising of all methods types for this class
 348      */
 349     public void generateMethodTypesScript(Map<String,Integer> typeMap,
 350             Set<MethodTypes> methodTypes) {
 351         String sep = "";
 352         StringBuilder vars = new StringBuilder("var methods = {");
 353         for (Map.Entry<String,Integer> entry : typeMap.entrySet()) {
 354             vars.append(sep);
 355             sep = ",";
 356             vars.append("\"");
 357             vars.append(entry.getKey());
 358             vars.append("\":");
 359             vars.append(entry.getValue());
 360         }
 361         vars.append("};").append(DocletConstants.NL);
 362         sep = "";
 363         vars.append("var tabs = {");
 364         for (MethodTypes entry : methodTypes) {
 365             vars.append(sep);
 366             sep = ",";
 367             vars.append(entry.value()).append(":");
 368             vars.append("[").append("\"").append(entry.tabId());
 369             vars.append("\"").append(sep).append("\"").append(entry.text()).append("\"]");
 370         }
 371         vars.append("};").append(DocletConstants.NL);
 372         addStyles(HtmlStyle.altColor, vars);
 373         addStyles(HtmlStyle.rowColor, vars);
 374         addStyles(HtmlStyle.tableTab, vars);
 375         addStyles(HtmlStyle.activeTableTab, vars);
 376         script.addContent(new RawHtml(vars.toString()));
 377     }
 378 
 379     /**
 380      * Adds javascript style variables to the document.
 381      *
 382      * @param style style to be added as a javascript variable
 383      * @param vars variable string to which the style variable will be added
 384      */
 385     public void addStyles(HtmlStyle style, StringBuilder vars) {
 386         vars.append("var ").append(style).append(" = \"").append(style)
 387                 .append("\";").append(DocletConstants.NL);
 388     }
 389 
 390     /**
 391      * Returns an HtmlTree for the TITLE tag.
 392      *
 393      * @return an HtmlTree for the TITLE tag
 394      */
 395     public HtmlTree getTitle() {
 396         HtmlTree title = HtmlTree.TITLE(new StringContent(winTitle));
 397         return title;
 398     }
 399 
 400     /**
 401      * Return, text passed, with Italics &lt;i&gt; and &lt;/i&gt; tags, surrounding it.
 402      * So if the text passed is "Hi", then string returned will be "&lt;i&gt;Hi&lt;/i&gt;".
 403      *
 404      * @param text String to be printed in between &lt;I&gt; and &lt;/I&gt; tags.
 405      */
 406     public String italicsText(String text) {
 407         return "<i>" + text + "</i>";
 408     }
 409 
 410     public String codeText(String text) {
 411         return "<code>" + text + "</code>";
 412     }
 413 
 414     /**
 415      * Return "&#38;nbsp;", non-breaking space.
 416      */
 417     public Content getSpace() {
 418         return RawHtml.nbsp;
 419     }
 420 
 421     /*
 422      * Returns a header for Modifier and Type column of a table.
 423      */
 424     public String getModifierTypeHeader() {
 425         return modifierTypeHeader;
 426     }
 427 }