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