Thu, 23 May 2013 12:50:40 +0100
Merge
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 StringContent(DocletConstants.DEFAULT_PACKAGE_NAME);
201 packageLabel = getResource("doclet.Package");
202 profileLabel = getResource("doclet.Profile");
203 useLabel = getResource("doclet.navClassUse");
204 prevLabel = getResource("doclet.Prev");
205 nextLabel = getResource("doclet.Next");
206 prevclassLabel = getResource("doclet.Prev_Class");
207 nextclassLabel = getResource("doclet.Next_Class");
208 summaryLabel = getResource("doclet.Summary");
209 detailLabel = getResource("doclet.Detail");
210 framesLabel = getResource("doclet.Frames");
211 noframesLabel = getResource("doclet.No_Frames");
212 treeLabel = getResource("doclet.Tree");
213 classLabel = getResource("doclet.Class");
214 deprecatedLabel = getResource("doclet.navDeprecated");
215 deprecatedPhrase = getResource("doclet.Deprecated");
216 allclassesLabel = getResource("doclet.All_Classes");
217 allpackagesLabel = getResource("doclet.All_Packages");
218 allprofilesLabel = getResource("doclet.All_Profiles");
219 indexLabel = getResource("doclet.Index");
220 helpLabel = getResource("doclet.Help");
221 seeLabel = getResource("doclet.See");
222 descriptionLabel = getResource("doclet.Description");
223 prevpackageLabel = getResource("doclet.Prev_Package");
224 nextpackageLabel = getResource("doclet.Next_Package");
225 prevprofileLabel = getResource("doclet.Prev_Profile");
226 nextprofileLabel = getResource("doclet.Next_Profile");
227 packagesLabel = getResource("doclet.Packages");
228 profilesLabel = getResource("doclet.Profiles");
229 methodDetailsLabel = getResource("doclet.Method_Detail");
230 annotationTypeDetailsLabel = getResource("doclet.Annotation_Type_Member_Detail");
231 fieldDetailsLabel = getResource("doclet.Field_Detail");
232 propertyDetailsLabel = getResource("doclet.Property_Detail");
233 constructorDetailsLabel = getResource("doclet.Constructor_Detail");
234 enumConstantsDetailsLabel = getResource("doclet.Enum_Constant_Detail");
235 specifiedByLabel = getResource("doclet.Specified_By");
236 overridesLabel = getResource("doclet.Overrides");
237 descfrmClassLabel = getResource("doclet.Description_From_Class");
238 descfrmInterfaceLabel = getResource("doclet.Description_From_Interface");
239 }
241 public void write(Content c) throws IOException {
242 c.write(writer, true);
243 }
245 public void close() throws IOException {
246 writer.close();
247 }
249 /**
250 * Get the configuration string as a content.
251 *
252 * @param key the key to look for in the configuration file
253 * @return a content tree for the text
254 */
255 public Content getResource(String key) {
256 return configuration.getResource(key);
257 }
259 /**
260 * Get the configuration string as a content.
261 *
262 * @param key the key to look for in the configuration file
263 * @param o string or content argument added to configuration text
264 * @return a content tree for the text
265 */
266 public Content getResource(String key, Object o) {
267 return configuration.getResource(key, o);
268 }
270 /**
271 * Get the configuration string as a content.
272 *
273 * @param key the key to look for in the configuration file
274 * @param o1 string or content argument added to configuration text
275 * @param o2 string or content argument added to configuration text
276 * @return a content tree for the text
277 */
278 public Content getResource(String key, Object o0, Object o1) {
279 return configuration.getResource(key, o0, o1);
280 }
282 /**
283 * Returns an HtmlTree for the SCRIPT tag.
284 *
285 * @return an HtmlTree for the SCRIPT tag
286 */
287 protected HtmlTree getWinTitleScript(){
288 HtmlTree script = new HtmlTree(HtmlTag.SCRIPT);
289 if(winTitle != null && winTitle.length() > 0) {
290 script.addAttr(HtmlAttr.TYPE, "text/javascript");
291 String scriptCode = "<!--" + DocletConstants.NL +
292 " if (location.href.indexOf('is-external=true') == -1) {" + DocletConstants.NL +
293 " parent.document.title=\"" + winTitle + "\";" + DocletConstants.NL +
294 " }" + DocletConstants.NL +
295 "//-->" + DocletConstants.NL;
296 RawHtml scriptContent = new RawHtml(scriptCode);
297 script.addContent(scriptContent);
298 }
299 return script;
300 }
302 /**
303 * Returns a content tree for the SCRIPT tag for the main page(index.html).
304 *
305 * @return a content for the SCRIPT tag
306 */
307 protected Content getFramesetJavaScript() {
308 HtmlTree script = new HtmlTree(HtmlTag.SCRIPT);
309 script.addAttr(HtmlAttr.TYPE, "text/javascript");
310 String scriptCode = DocletConstants.NL +
311 " targetPage = \"\" + window.location.search;" + DocletConstants.NL +
312 " if (targetPage != \"\" && targetPage != \"undefined\")" + DocletConstants.NL +
313 " targetPage = targetPage.substring(1);" + DocletConstants.NL +
314 " if (targetPage.indexOf(\":\") != -1 || (targetPage != \"\" && !validURL(targetPage)))" + DocletConstants.NL +
315 " targetPage = \"undefined\";" + DocletConstants.NL +
316 " function validURL(url) {" + DocletConstants.NL +
317 " if (!(url.indexOf(\".html\") == url.length - 5))" + DocletConstants.NL +
318 " return false;" + DocletConstants.NL +
319 " var allowNumber = false;" + DocletConstants.NL +
320 " var allowSep = false;" + DocletConstants.NL +
321 " var seenDot = false;" + DocletConstants.NL +
322 " for (var i = 0; i < url.length - 5; i++) {" + DocletConstants.NL +
323 " var ch = url.charAt(i);" + DocletConstants.NL +
324 " if ('a' <= ch && ch <= 'z' ||" + DocletConstants.NL +
325 " 'A' <= ch && ch <= 'Z' ||" + DocletConstants.NL +
326 " ch == '$' ||" + DocletConstants.NL +
327 " ch == '_') {" + DocletConstants.NL +
328 " allowNumber = true;" + DocletConstants.NL +
329 " allowSep = true;" + DocletConstants.NL +
330 " } else if ('0' <= ch && ch <= '9'" + DocletConstants.NL +
331 " || ch == '-') {" + DocletConstants.NL +
332 " if (!allowNumber)" + DocletConstants.NL +
333 " return false;" + DocletConstants.NL +
334 " } else if (ch == '/' || ch == '.') {" + DocletConstants.NL +
335 " if (!allowSep)" + DocletConstants.NL +
336 " return false;" + DocletConstants.NL +
337 " allowNumber = false;" + DocletConstants.NL +
338 " allowSep = false;" + DocletConstants.NL +
339 " if (ch == '.')" + DocletConstants.NL +
340 " seenDot = true;" + DocletConstants.NL +
341 " if (ch == '/' && seenDot)" + DocletConstants.NL +
342 " return false;" + DocletConstants.NL +
343 " } else {" + DocletConstants.NL +
344 " return false;"+ DocletConstants.NL +
345 " }" + DocletConstants.NL +
346 " }" + DocletConstants.NL +
347 " return true;" + DocletConstants.NL +
348 " }" + DocletConstants.NL +
349 " function loadFrames() {" + DocletConstants.NL +
350 " if (targetPage != \"\" && targetPage != \"undefined\")" + DocletConstants.NL +
351 " top.classFrame.location = top.targetPage;" + DocletConstants.NL +
352 " }" + DocletConstants.NL;
353 RawHtml scriptContent = new RawHtml(scriptCode);
354 script.addContent(scriptContent);
355 return script;
356 }
358 /**
359 * Returns an HtmlTree for the BODY tag.
360 *
361 * @param includeScript set true if printing windowtitle script
362 * @param title title for the window
363 * @return an HtmlTree for the BODY tag
364 */
365 public HtmlTree getBody(boolean includeScript, String title) {
366 HtmlTree body = new HtmlTree(HtmlTag.BODY);
367 // Set window title string which is later printed
368 this.winTitle = title;
369 // Don't print windowtitle script for overview-frame, allclasses-frame
370 // and package-frame
371 if (includeScript) {
372 this.script = getWinTitleScript();
373 body.addContent(script);
374 Content noScript = HtmlTree.NOSCRIPT(
375 HtmlTree.DIV(getResource("doclet.No_Script_Message")));
376 body.addContent(noScript);
377 }
378 return body;
379 }
381 /**
382 * Generated javascript variables for the document.
383 *
384 * @param typeMap map comprising of method and type relationship
385 * @param methodTypes set comprising of all methods types for this class
386 */
387 public void generateMethodTypesScript(Map<String,Integer> typeMap,
388 Set<MethodTypes> methodTypes) {
389 String sep = "";
390 StringBuilder vars = new StringBuilder("var methods = {");
391 for (Map.Entry<String,Integer> entry : typeMap.entrySet()) {
392 vars.append(sep);
393 sep = ",";
394 vars.append("\"");
395 vars.append(entry.getKey());
396 vars.append("\":");
397 vars.append(entry.getValue());
398 }
399 vars.append("};").append(DocletConstants.NL);
400 sep = "";
401 vars.append("var tabs = {");
402 for (MethodTypes entry : methodTypes) {
403 vars.append(sep);
404 sep = ",";
405 vars.append(entry.value()).append(":");
406 vars.append("[").append("\"").append(entry.tabId());
407 vars.append("\"").append(sep).append("\"").append(entry.text()).append("\"]");
408 }
409 vars.append("};").append(DocletConstants.NL);
410 addStyles(HtmlStyle.altColor, vars);
411 addStyles(HtmlStyle.rowColor, vars);
412 addStyles(HtmlStyle.tableTab, vars);
413 addStyles(HtmlStyle.activeTableTab, vars);
414 script.addContent(new RawHtml(vars.toString()));
415 }
417 /**
418 * Adds javascript style variables to the document.
419 *
420 * @param style style to be added as a javascript variable
421 * @param vars variable string to which the style variable will be added
422 */
423 public void addStyles(HtmlStyle style, StringBuilder vars) {
424 vars.append("var ").append(style).append(" = \"").append(style)
425 .append("\";").append(DocletConstants.NL);
426 }
428 /**
429 * Returns an HtmlTree for the TITLE tag.
430 *
431 * @return an HtmlTree for the TITLE tag
432 */
433 public HtmlTree getTitle() {
434 HtmlTree title = HtmlTree.TITLE(new StringContent(winTitle));
435 return title;
436 }
438 public String codeText(String text) {
439 return "<code>" + text + "</code>";
440 }
442 /**
443 * Return "&nbsp;", non-breaking space.
444 */
445 public Content getSpace() {
446 return RawHtml.nbsp;
447 }
449 /*
450 * Returns a header for Modifier and Type column of a table.
451 */
452 public String getModifierTypeHeader() {
453 return modifierTypeHeader;
454 }
455 }