Mon, 19 Nov 2012 16:10:34 -0800
8002304: Group methods by types in methods summary section
Reviewed-by: jjg
1 /*
2 * Copyright (c) 1997, 2012, 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 */
26 package com.sun.tools.doclets.formats.html.markup;
28 import java.io.*;
29 import java.util.*;
31 import com.sun.tools.doclets.internal.toolkit.*;
32 import com.sun.tools.doclets.internal.toolkit.util.*;
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 {
50 /**
51 * The window title of this file
52 */
53 protected String winTitle;
55 /**
56 * The configuration
57 */
58 protected Configuration configuration;
60 /**
61 * The flag to indicate whether a member details list is printed or not.
62 */
63 protected boolean memberDetailsListPrinted;
65 /**
66 * Header for tables displaying packages and description..
67 */
68 protected final String[] packageTableHeader;
70 /**
71 * Summary for use tables displaying class and package use.
72 */
73 protected final String useTableSummary;
75 /**
76 * Column header for class docs displaying Modifier and Type header.
77 */
78 protected final String modifierTypeHeader;
80 public final Content overviewLabel;
82 public final Content defaultPackageLabel;
84 public final Content packageLabel;
86 public final Content useLabel;
88 public final Content prevLabel;
90 public final Content nextLabel;
92 public final Content prevclassLabel;
94 public final Content nextclassLabel;
96 public final Content summaryLabel;
98 public final Content detailLabel;
100 public final Content framesLabel;
102 public final Content noframesLabel;
104 public final Content treeLabel;
106 public final Content classLabel;
108 public final Content deprecatedLabel;
110 public final Content deprecatedPhrase;
112 public final Content allclassesLabel;
114 public final Content indexLabel;
116 public final Content helpLabel;
118 public final Content seeLabel;
120 public final Content descriptionLabel;
122 public final Content prevpackageLabel;
124 public final Content nextpackageLabel;
126 public final Content packagesLabel;
128 public final Content methodDetailsLabel;
130 public final Content annotationTypeDetailsLabel;
132 public final Content fieldDetailsLabel;
134 public final Content constructorDetailsLabel;
136 public final Content enumConstantsDetailsLabel;
138 public final Content specifiedByLabel;
140 public final Content overridesLabel;
142 public final Content descfrmClassLabel;
144 public final Content descfrmInterfaceLabel;
146 private final Writer writer;
148 private Content script;
150 /**
151 * Constructor.
152 *
153 * @param path The directory path to be created for this file
154 * or null if none to be created.
155 * @exception IOException Exception raised by the FileWriter is passed on
156 * to next level.
157 * @exception UnsupportedEncodingException Exception raised by the
158 * OutputStreamWriter is passed on to next level.
159 */
160 public HtmlWriter(Configuration configuration, DocPath path)
161 throws IOException, UnsupportedEncodingException {
162 writer = DocFile.createFileForOutput(configuration, path).openWriter();
163 this.configuration = configuration;
164 this.memberDetailsListPrinted = false;
165 packageTableHeader = new String[] {
166 configuration.getText("doclet.Package"),
167 configuration.getText("doclet.Description")
168 };
169 useTableSummary = configuration.getText("doclet.Use_Table_Summary",
170 configuration.getText("doclet.packages"));
171 modifierTypeHeader = configuration.getText("doclet.0_and_1",
172 configuration.getText("doclet.Modifier"),
173 configuration.getText("doclet.Type"));
174 overviewLabel = getResource("doclet.Overview");
175 defaultPackageLabel = new RawHtml(
176 DocletConstants.DEFAULT_PACKAGE_NAME);
177 packageLabel = getResource("doclet.Package");
178 useLabel = getResource("doclet.navClassUse");
179 prevLabel = getResource("doclet.Prev");
180 nextLabel = getResource("doclet.Next");
181 prevclassLabel = getResource("doclet.Prev_Class");
182 nextclassLabel = getResource("doclet.Next_Class");
183 summaryLabel = getResource("doclet.Summary");
184 detailLabel = getResource("doclet.Detail");
185 framesLabel = getResource("doclet.Frames");
186 noframesLabel = getResource("doclet.No_Frames");
187 treeLabel = getResource("doclet.Tree");
188 classLabel = getResource("doclet.Class");
189 deprecatedLabel = getResource("doclet.navDeprecated");
190 deprecatedPhrase = getResource("doclet.Deprecated");
191 allclassesLabel = getResource("doclet.All_Classes");
192 indexLabel = getResource("doclet.Index");
193 helpLabel = getResource("doclet.Help");
194 seeLabel = getResource("doclet.See");
195 descriptionLabel = getResource("doclet.Description");
196 prevpackageLabel = getResource("doclet.Prev_Package");
197 nextpackageLabel = getResource("doclet.Next_Package");
198 packagesLabel = getResource("doclet.Packages");
199 methodDetailsLabel = getResource("doclet.Method_Detail");
200 annotationTypeDetailsLabel = getResource("doclet.Annotation_Type_Member_Detail");
201 fieldDetailsLabel = getResource("doclet.Field_Detail");
202 constructorDetailsLabel = getResource("doclet.Constructor_Detail");
203 enumConstantsDetailsLabel = getResource("doclet.Enum_Constant_Detail");
204 specifiedByLabel = getResource("doclet.Specified_By");
205 overridesLabel = getResource("doclet.Overrides");
206 descfrmClassLabel = getResource("doclet.Description_From_Class");
207 descfrmInterfaceLabel = getResource("doclet.Description_From_Interface");
208 }
210 public void write(Content c) throws IOException {
211 c.write(writer, true);
212 }
214 public void close() throws IOException {
215 writer.close();
216 }
218 /**
219 * Get the configuration string as a content.
220 *
221 * @param key the key to look for in the configuration file
222 * @return a content tree for the text
223 */
224 public Content getResource(String key) {
225 return new StringContent(configuration.getText(key));
226 }
228 /**
229 * Get the configuration string as a content.
230 *
231 * @param key the key to look for in the configuration file
232 * @param a1 string argument added to configuration text
233 * @return a content tree for the text
234 */
235 public Content getResource(String key, String a1) {
236 return new RawHtml(configuration.getText(key, a1));
237 }
239 /**
240 * Get the configuration string as a content.
241 *
242 * @param key the key to look for in the configuration file
243 * @param a1 string argument added to configuration text
244 * @param a2 string argument added to configuration text
245 * @return a content tree for the text
246 */
247 public Content getResource(String key, String a1, String a2) {
248 return new RawHtml(configuration.getText(key, a1, a2));
249 }
251 /**
252 * Returns an HtmlTree for the SCRIPT tag.
253 *
254 * @return an HtmlTree for the SCRIPT tag
255 */
256 protected HtmlTree getWinTitleScript(){
257 HtmlTree script = new HtmlTree(HtmlTag.SCRIPT);
258 if(winTitle != null && winTitle.length() > 0) {
259 script.addAttr(HtmlAttr.TYPE, "text/javascript");
260 String scriptCode = "<!--" + DocletConstants.NL +
261 " if (location.href.indexOf('is-external=true') == -1) {" + DocletConstants.NL +
262 " parent.document.title=\"" + winTitle + "\";" + DocletConstants.NL +
263 " }" + DocletConstants.NL +
264 "//-->" + DocletConstants.NL;
265 RawHtml scriptContent = new RawHtml(scriptCode);
266 script.addContent(scriptContent);
267 }
268 return script;
269 }
271 /**
272 * Returns a content tree for the SCRIPT tag for the main page(index.html).
273 *
274 * @return a content for the SCRIPT tag
275 */
276 protected Content getFramesetJavaScript(){
277 HtmlTree script = new HtmlTree(HtmlTag.SCRIPT);
278 script.addAttr(HtmlAttr.TYPE, "text/javascript");
279 String scriptCode = DocletConstants.NL + " targetPage = \"\" + window.location.search;" + DocletConstants.NL +
280 " if (targetPage != \"\" && targetPage != \"undefined\")" + DocletConstants.NL +
281 " targetPage = targetPage.substring(1);" + DocletConstants.NL +
282 " if (targetPage.indexOf(\":\") != -1)" + DocletConstants.NL +
283 " targetPage = \"undefined\";" + DocletConstants.NL +
284 " function loadFrames() {" + DocletConstants.NL +
285 " if (targetPage != \"\" && targetPage != \"undefined\")" + DocletConstants.NL +
286 " top.classFrame.location = top.targetPage;" + DocletConstants.NL +
287 " }" + DocletConstants.NL;
288 RawHtml scriptContent = new RawHtml(scriptCode);
289 script.addContent(scriptContent);
290 return script;
291 }
293 /**
294 * Returns an HtmlTree for the BODY tag.
295 *
296 * @param includeScript set true if printing windowtitle script
297 * @param title title for the window
298 * @return an HtmlTree for the BODY tag
299 */
300 public HtmlTree getBody(boolean includeScript, String title) {
301 HtmlTree body = new HtmlTree(HtmlTag.BODY);
302 // Set window title string which is later printed
303 this.winTitle = title;
304 // Don't print windowtitle script for overview-frame, allclasses-frame
305 // and package-frame
306 if (includeScript) {
307 this.script = getWinTitleScript();
308 body.addContent(script);
309 Content noScript = HtmlTree.NOSCRIPT(
310 HtmlTree.DIV(getResource("doclet.No_Script_Message")));
311 body.addContent(noScript);
312 }
313 return body;
314 }
316 /**
317 * Generated javascript variables for the document.
318 *
319 * @param typeMap map comprising of method and type relationship
320 * @param methodTypes set comprising of all methods types for this class
321 */
322 public void generateMethodTypesScript(Map<String,Integer> typeMap,
323 Set<MethodTypes> methodTypes) {
324 String sep = "";
325 StringBuilder vars = new StringBuilder("var methods = {");
326 for (Map.Entry<String,Integer> entry : typeMap.entrySet()) {
327 vars.append(sep);
328 sep = ",";
329 vars.append("\"");
330 vars.append(entry.getKey());
331 vars.append("\":");
332 vars.append(entry.getValue());
333 }
334 vars.append("};").append(DocletConstants.NL);
335 sep = "";
336 vars.append("var tabs = {");
337 for (MethodTypes entry : methodTypes) {
338 vars.append(sep);
339 sep = ",";
340 vars.append(entry.value()).append(":");
341 vars.append("[").append("\"").append(entry.tabId());
342 vars.append("\"").append(sep).append("\"").append(entry.text()).append("\"]");
343 }
344 vars.append("};").append(DocletConstants.NL);
345 addStyles(HtmlStyle.altColor, vars);
346 addStyles(HtmlStyle.rowColor, vars);
347 addStyles(HtmlStyle.tableTab, vars);
348 addStyles(HtmlStyle.activeTableTab, vars);
349 script.addContent(new RawHtml(vars.toString()));
350 }
352 /**
353 * Adds javascript style variables to the document.
354 *
355 * @param style style to be added as a javascript variable
356 * @param vars variable string to which the style variable will be added
357 */
358 public void addStyles(HtmlStyle style, StringBuilder vars) {
359 vars.append("var ").append(style).append(" = \"").append(style)
360 .append("\";").append(DocletConstants.NL);
361 }
363 /**
364 * Returns an HtmlTree for the TITLE tag.
365 *
366 * @return an HtmlTree for the TITLE tag
367 */
368 public HtmlTree getTitle() {
369 HtmlTree title = HtmlTree.TITLE(new StringContent(winTitle));
370 return title;
371 }
373 /**
374 * Return, text passed, with Italics <i> and </i> tags, surrounding it.
375 * So if the text passed is "Hi", then string returned will be "<i>Hi</i>".
376 *
377 * @param text String to be printed in between <I> and </I> tags.
378 */
379 public String italicsText(String text) {
380 return "<i>" + text + "</i>";
381 }
383 public String codeText(String text) {
384 return "<code>" + text + "</code>";
385 }
387 /**
388 * Return "&nbsp;", non-breaking space.
389 */
390 public Content getSpace() {
391 return RawHtml.nbsp;
392 }
394 /*
395 * Returns a header for Modifier and Type column of a table.
396 */
397 public String getModifierTypeHeader() {
398 return modifierTypeHeader;
399 }
400 }