Thu, 24 May 2018 18:02:46 +0800
Merge
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 */
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 DocFile file;
169 private Writer writer;
171 private Content script;
173 /**
174 * Constructor.
175 *
176 * @param path The directory path to be created for this file
177 * or null if none to be created.
178 * @exception IOException Exception raised by the FileWriter is passed on
179 * to next level.
180 * @exception UnsupportedEncodingException Exception raised by the
181 * OutputStreamWriter is passed on to next level.
182 */
183 public HtmlWriter(Configuration configuration, DocPath path)
184 throws IOException, UnsupportedEncodingException {
185 file = DocFile.createFileForOutput(configuration, path);
186 this.configuration = configuration;
187 this.memberDetailsListPrinted = false;
188 profileTableHeader = new String[] {
189 configuration.getText("doclet.Profile"),
190 configuration.getText("doclet.Description")
191 };
192 packageTableHeader = new String[] {
193 configuration.getText("doclet.Package"),
194 configuration.getText("doclet.Description")
195 };
196 useTableSummary = configuration.getText("doclet.Use_Table_Summary",
197 configuration.getText("doclet.packages"));
198 modifierTypeHeader = configuration.getText("doclet.0_and_1",
199 configuration.getText("doclet.Modifier"),
200 configuration.getText("doclet.Type"));
201 overviewLabel = getResource("doclet.Overview");
202 defaultPackageLabel = new StringContent(DocletConstants.DEFAULT_PACKAGE_NAME);
203 packageLabel = getResource("doclet.Package");
204 profileLabel = getResource("doclet.Profile");
205 useLabel = getResource("doclet.navClassUse");
206 prevLabel = getResource("doclet.Prev");
207 nextLabel = getResource("doclet.Next");
208 prevclassLabel = getNonBreakResource("doclet.Prev_Class");
209 nextclassLabel = getNonBreakResource("doclet.Next_Class");
210 summaryLabel = getResource("doclet.Summary");
211 detailLabel = getResource("doclet.Detail");
212 framesLabel = getResource("doclet.Frames");
213 noframesLabel = getNonBreakResource("doclet.No_Frames");
214 treeLabel = getResource("doclet.Tree");
215 classLabel = getResource("doclet.Class");
216 deprecatedLabel = getResource("doclet.navDeprecated");
217 deprecatedPhrase = getResource("doclet.Deprecated");
218 allclassesLabel = getNonBreakResource("doclet.All_Classes");
219 allpackagesLabel = getNonBreakResource("doclet.All_Packages");
220 allprofilesLabel = getNonBreakResource("doclet.All_Profiles");
221 indexLabel = getResource("doclet.Index");
222 helpLabel = getResource("doclet.Help");
223 seeLabel = getResource("doclet.See");
224 descriptionLabel = getResource("doclet.Description");
225 prevpackageLabel = getNonBreakResource("doclet.Prev_Package");
226 nextpackageLabel = getNonBreakResource("doclet.Next_Package");
227 prevprofileLabel = getNonBreakResource("doclet.Prev_Profile");
228 nextprofileLabel = getNonBreakResource("doclet.Next_Profile");
229 packagesLabel = getResource("doclet.Packages");
230 profilesLabel = getResource("doclet.Profiles");
231 methodDetailsLabel = getResource("doclet.Method_Detail");
232 annotationTypeDetailsLabel = getResource("doclet.Annotation_Type_Member_Detail");
233 fieldDetailsLabel = getResource("doclet.Field_Detail");
234 propertyDetailsLabel = getResource("doclet.Property_Detail");
235 constructorDetailsLabel = getResource("doclet.Constructor_Detail");
236 enumConstantsDetailsLabel = getResource("doclet.Enum_Constant_Detail");
237 specifiedByLabel = getResource("doclet.Specified_By");
238 overridesLabel = getResource("doclet.Overrides");
239 descfrmClassLabel = getResource("doclet.Description_From_Class");
240 descfrmInterfaceLabel = getResource("doclet.Description_From_Interface");
241 }
243 public void write(Content c) throws IOException {
244 writer = file.openWriter();
245 c.write(writer, true);
246 }
248 public void close() throws IOException {
249 writer.close();
250 }
252 /**
253 * Get the configuration string as a content.
254 *
255 * @param key the key to look for in the configuration file
256 * @return a content tree for the text
257 */
258 public Content getResource(String key) {
259 return configuration.getResource(key);
260 }
262 /**
263 * Get the configuration string as a content, replacing spaces
264 * with non-breaking spaces.
265 *
266 * @param key the key to look for in the configuration file
267 * @return a content tree for the text
268 */
269 public Content getNonBreakResource(String key) {
270 String text = configuration.getText(key);
271 Content c = configuration.newContent();
272 int start = 0;
273 int p;
274 while ((p = text.indexOf(" ", start)) != -1) {
275 c.addContent(text.substring(start, p));
276 c.addContent(RawHtml.nbsp);
277 start = p + 1;
278 }
279 c.addContent(text.substring(start));
280 return c;
281 }
283 /**
284 * Get the configuration string as a content.
285 *
286 * @param key the key to look for in the configuration file
287 * @param o string or content argument added to configuration text
288 * @return a content tree for the text
289 */
290 public Content getResource(String key, Object o) {
291 return configuration.getResource(key, o);
292 }
294 /**
295 * Get the configuration string as a content.
296 *
297 * @param key the key to look for in the configuration file
298 * @param o1 string or content argument added to configuration text
299 * @param o2 string or content argument added to configuration text
300 * @return a content tree for the text
301 */
302 public Content getResource(String key, Object o0, Object o1) {
303 return configuration.getResource(key, o0, o1);
304 }
306 /**
307 * Returns an HtmlTree for the SCRIPT tag.
308 *
309 * @return an HtmlTree for the SCRIPT tag
310 */
311 protected HtmlTree getWinTitleScript(){
312 HtmlTree script = new HtmlTree(HtmlTag.SCRIPT);
313 if(winTitle != null && winTitle.length() > 0) {
314 script.addAttr(HtmlAttr.TYPE, "text/javascript");
315 String scriptCode = "<!--" + DocletConstants.NL +
316 " try {" + DocletConstants.NL +
317 " if (location.href.indexOf('is-external=true') == -1) {" + DocletConstants.NL +
318 " parent.document.title=\"" + escapeJavaScriptChars(winTitle) + "\";" + DocletConstants.NL +
319 " }" + DocletConstants.NL +
320 " }" + DocletConstants.NL +
321 " catch(err) {" + DocletConstants.NL +
322 " }" + DocletConstants.NL +
323 "//-->" + DocletConstants.NL;
324 RawHtml scriptContent = new RawHtml(scriptCode);
325 script.addContent(scriptContent);
326 }
327 return script;
328 }
330 /**
331 * Returns a String with escaped special JavaScript characters.
332 *
333 * @param s String that needs to be escaped
334 * @return a valid escaped JavaScript string
335 */
336 private static String escapeJavaScriptChars(String s) {
337 StringBuilder sb = new StringBuilder();
338 for (int i = 0; i < s.length(); i++) {
339 char ch = s.charAt(i);
340 switch (ch) {
341 case '\b':
342 sb.append("\\b");
343 break;
344 case '\t':
345 sb.append("\\t");
346 break;
347 case '\n':
348 sb.append("\\n");
349 break;
350 case '\f':
351 sb.append("\\f");
352 break;
353 case '\r':
354 sb.append("\\r");
355 break;
356 case '"':
357 sb.append("\\\"");
358 break;
359 case '\'':
360 sb.append("\\\'");
361 break;
362 case '\\':
363 sb.append("\\\\");
364 break;
365 default:
366 if (ch < 32 || ch >= 127) {
367 sb.append(String.format("\\u%04X", (int)ch));
368 } else {
369 sb.append(ch);
370 }
371 break;
372 }
373 }
374 return sb.toString();
375 }
377 /**
378 * Returns a content tree for the SCRIPT tag for the main page(index.html).
379 *
380 * @return a content for the SCRIPT tag
381 */
382 protected Content getFramesetJavaScript() {
383 HtmlTree script = new HtmlTree(HtmlTag.SCRIPT);
384 script.addAttr(HtmlAttr.TYPE, "text/javascript");
385 String scriptCode = DocletConstants.NL +
386 " tmpTargetPage = \"\" + window.location.search;" + DocletConstants.NL +
387 " if (tmpTargetPage != \"\" && tmpTargetPage != \"undefined\")" + DocletConstants.NL +
388 " tmpTargetPage = tmpTargetPage.substring(1);" + DocletConstants.NL +
389 " if (tmpTargetPage.indexOf(\":\") != -1 || (tmpTargetPage != \"\" && !validURL(tmpTargetPage)))" + DocletConstants.NL +
390 " tmpTargetPage = \"undefined\";" + DocletConstants.NL +
391 " targetPage = tmpTargetPage;" + DocletConstants.NL +
392 " function validURL(url) {" + DocletConstants.NL +
393 " try {" + DocletConstants.NL +
394 " url = decodeURIComponent(url);" + DocletConstants.NL +
395 " }" + DocletConstants.NL +
396 " catch (error) {" + DocletConstants.NL +
397 " return false;" + DocletConstants.NL +
398 " }" + DocletConstants.NL +
399 " var pos = url.indexOf(\".html\");" + DocletConstants.NL +
400 " if (pos == -1 || pos != url.length - 5)" + DocletConstants.NL +
401 " return false;" + DocletConstants.NL +
402 " var allowNumber = false;" + DocletConstants.NL +
403 " var allowSep = false;" + DocletConstants.NL +
404 " var seenDot = false;" + DocletConstants.NL +
405 " for (var i = 0; i < url.length - 5; i++) {" + DocletConstants.NL +
406 " var ch = url.charAt(i);" + DocletConstants.NL +
407 " if ('a' <= ch && ch <= 'z' ||" + DocletConstants.NL +
408 " 'A' <= ch && ch <= 'Z' ||" + DocletConstants.NL +
409 " ch == '$' ||" + DocletConstants.NL +
410 " ch == '_' ||" + DocletConstants.NL +
411 " ch.charCodeAt(0) > 127) {" + DocletConstants.NL +
412 " allowNumber = true;" + DocletConstants.NL +
413 " allowSep = true;" + DocletConstants.NL +
414 " } else if ('0' <= ch && ch <= '9'" + DocletConstants.NL +
415 " || ch == '-') {" + DocletConstants.NL +
416 " if (!allowNumber)" + DocletConstants.NL +
417 " return false;" + DocletConstants.NL +
418 " } else if (ch == '/' || ch == '.') {" + DocletConstants.NL +
419 " if (!allowSep)" + DocletConstants.NL +
420 " return false;" + DocletConstants.NL +
421 " allowNumber = false;" + DocletConstants.NL +
422 " allowSep = false;" + DocletConstants.NL +
423 " if (ch == '.')" + DocletConstants.NL +
424 " seenDot = true;" + DocletConstants.NL +
425 " if (ch == '/' && seenDot)" + DocletConstants.NL +
426 " return false;" + DocletConstants.NL +
427 " } else {" + DocletConstants.NL +
428 " return false;"+ DocletConstants.NL +
429 " }" + DocletConstants.NL +
430 " }" + DocletConstants.NL +
431 " return true;" + DocletConstants.NL +
432 " }" + DocletConstants.NL +
433 " function loadFrames() {" + DocletConstants.NL +
434 " if (targetPage != \"\" && targetPage != \"undefined\")" + DocletConstants.NL +
435 " top.classFrame.location = top.targetPage;" + DocletConstants.NL +
436 " }" + DocletConstants.NL;
437 RawHtml scriptContent = new RawHtml(scriptCode);
438 script.addContent(scriptContent);
439 return script;
440 }
442 /**
443 * Returns an HtmlTree for the BODY tag.
444 *
445 * @param includeScript set true if printing windowtitle script
446 * @param title title for the window
447 * @return an HtmlTree for the BODY tag
448 */
449 public HtmlTree getBody(boolean includeScript, String title) {
450 HtmlTree body = new HtmlTree(HtmlTag.BODY);
451 // Set window title string which is later printed
452 this.winTitle = title;
453 // Don't print windowtitle script for overview-frame, allclasses-frame
454 // and package-frame
455 if (includeScript) {
456 this.script = getWinTitleScript();
457 body.addContent(script);
458 Content noScript = HtmlTree.NOSCRIPT(
459 HtmlTree.DIV(getResource("doclet.No_Script_Message")));
460 body.addContent(noScript);
461 }
462 return body;
463 }
465 /**
466 * Generated javascript variables for the document.
467 *
468 * @param typeMap map comprising of method and type relationship
469 * @param methodTypes set comprising of all methods types for this class
470 */
471 public void generateMethodTypesScript(Map<String,Integer> typeMap,
472 Set<MethodTypes> methodTypes) {
473 String sep = "";
474 StringBuilder vars = new StringBuilder("var methods = {");
475 for (Map.Entry<String,Integer> entry : typeMap.entrySet()) {
476 vars.append(sep);
477 sep = ",";
478 vars.append("\"")
479 .append(entry.getKey())
480 .append("\":")
481 .append(entry.getValue());
482 }
483 vars.append("};").append(DocletConstants.NL);
484 sep = "";
485 vars.append("var tabs = {");
486 for (MethodTypes entry : methodTypes) {
487 vars.append(sep);
488 sep = ",";
489 vars.append(entry.value())
490 .append(":")
491 .append("[")
492 .append("\"")
493 .append(entry.tabId())
494 .append("\"")
495 .append(sep)
496 .append("\"")
497 .append(configuration.getText(entry.resourceKey()))
498 .append("\"]");
499 }
500 vars.append("};")
501 .append(DocletConstants.NL);
502 addStyles(HtmlStyle.altColor, vars);
503 addStyles(HtmlStyle.rowColor, vars);
504 addStyles(HtmlStyle.tableTab, vars);
505 addStyles(HtmlStyle.activeTableTab, vars);
506 script.addContent(new RawHtml(vars.toString()));
507 }
509 /**
510 * Adds javascript style variables to the document.
511 *
512 * @param style style to be added as a javascript variable
513 * @param vars variable string to which the style variable will be added
514 */
515 public void addStyles(HtmlStyle style, StringBuilder vars) {
516 vars.append("var ").append(style).append(" = \"").append(style)
517 .append("\";").append(DocletConstants.NL);
518 }
520 /**
521 * Returns an HtmlTree for the TITLE tag.
522 *
523 * @return an HtmlTree for the TITLE tag
524 */
525 public HtmlTree getTitle() {
526 HtmlTree title = HtmlTree.TITLE(new StringContent(winTitle));
527 return title;
528 }
530 public String codeText(String text) {
531 return "<code>" + text + "</code>";
532 }
534 /**
535 * Return "&nbsp;", non-breaking space.
536 */
537 public Content getSpace() {
538 return RawHtml.nbsp;
539 }
541 /*
542 * Returns a header for Modifier and Type column of a table.
543 */
544 public String getModifierTypeHeader() {
545 return modifierTypeHeader;
546 }
547 }