Thu, 15 Nov 2012 19:54:20 -0800
8002079: update DocFile to use a JavaFileManager
Reviewed-by: darcy
1 /*
2 * Copyright (c) 1998, 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;
28 import java.io.*;
29 import java.text.SimpleDateFormat;
30 import java.util.*;
32 import com.sun.javadoc.*;
33 import com.sun.tools.doclets.formats.html.markup.*;
34 import com.sun.tools.doclets.internal.toolkit.*;
35 import com.sun.tools.doclets.internal.toolkit.taglets.*;
36 import com.sun.tools.doclets.internal.toolkit.util.*;
38 /**
39 * Class for the Html Format Code Generation specific to JavaDoc.
40 * This Class contains methods related to the Html Code Generation which
41 * are used extensively while generating the entire documentation.
42 *
43 * <p><b>This is NOT part of any supported API.
44 * If you write code that depends on this, you do so at your own risk.
45 * This code and its internal interfaces are subject to change or
46 * deletion without notice.</b>
47 *
48 * @since 1.2
49 * @author Atul M Dambalkar
50 * @author Robert Field
51 * @author Bhavesh Patel (Modified)
52 */
53 public class HtmlDocletWriter extends HtmlDocWriter {
55 /**
56 * Relative path from the file getting generated to the destination
57 * directory. For example, if the file getting generated is
58 * "java/lang/Object.html", then the path to the root is "../..".
59 * This string can be empty if the file getting generated is in
60 * the destination directory.
61 */
62 public final DocPath pathToRoot;
64 /**
65 * Platform-independent path from the current or the
66 * destination directory to the file getting generated.
67 * Used when creating the file.
68 */
69 public final DocPath path;
71 /**
72 * Name of the file getting generated. If the file getting generated is
73 * "java/lang/Object.html", then the filename is "Object.html".
74 */
75 public final DocPath filename;
77 /**
78 * The display length used for indentation while generating the class page.
79 */
80 public int displayLength = 0;
82 /**
83 * The global configuration information for this run.
84 */
85 public final ConfigurationImpl configuration;
87 /**
88 * To check whether annotation heading is printed or not.
89 */
90 protected boolean printedAnnotationHeading = false;
92 /**
93 * Constructor to construct the HtmlStandardWriter object.
94 *
95 * @param path File to be generated.
96 */
97 public HtmlDocletWriter(ConfigurationImpl configuration, DocPath path)
98 throws IOException {
99 super(configuration, path);
100 this.configuration = configuration;
101 this.path = path;
102 this.pathToRoot = path.parent().invert();
103 this.filename = path.basename();
104 }
106 /**
107 * Replace {@docRoot} tag used in options that accept HTML text, such
108 * as -header, -footer, -top and -bottom, and when converting a relative
109 * HREF where commentTagsToString inserts a {@docRoot} where one was
110 * missing. (Also see DocRootTaglet for {@docRoot} tags in doc
111 * comments.)
112 * <p>
113 * Replace {@docRoot} tag in htmlstr with the relative path to the
114 * destination directory from the directory where the file is being
115 * written, looping to handle all such tags in htmlstr.
116 * <p>
117 * For example, for "-d docs" and -header containing {@docRoot}, when
118 * the HTML page for source file p/C1.java is being generated, the
119 * {@docRoot} tag would be inserted into the header as "../",
120 * the relative path from docs/p/ to docs/ (the document root).
121 * <p>
122 * Note: This doc comment was written with '&#064;' representing '@'
123 * to prevent the inline tag from being interpreted.
124 */
125 public String replaceDocRootDir(String htmlstr) {
126 // Return if no inline tags exist
127 int index = htmlstr.indexOf("{@");
128 if (index < 0) {
129 return htmlstr;
130 }
131 String lowerHtml = htmlstr.toLowerCase();
132 // Return index of first occurrence of {@docroot}
133 // Note: {@docRoot} is not case sensitive when passed in w/command line option
134 index = lowerHtml.indexOf("{@docroot}", index);
135 if (index < 0) {
136 return htmlstr;
137 }
138 StringBuilder buf = new StringBuilder();
139 int previndex = 0;
140 while (true) {
141 if (configuration.docrootparent.length() > 0) {
142 final String docroot_parent = "{@docroot}/..";
143 // Search for lowercase version of {@docRoot}/..
144 index = lowerHtml.indexOf(docroot_parent, previndex);
145 // If next {@docRoot}/.. pattern not found, append rest of htmlstr and exit loop
146 if (index < 0) {
147 buf.append(htmlstr.substring(previndex));
148 break;
149 }
150 // If next {@docroot}/.. pattern found, append htmlstr up to start of tag
151 buf.append(htmlstr.substring(previndex, index));
152 previndex = index + docroot_parent.length();
153 // Insert docrootparent absolute path where {@docRoot}/.. was located
155 buf.append(configuration.docrootparent);
156 // Append slash if next character is not a slash
157 if (previndex < htmlstr.length() && htmlstr.charAt(previndex) != '/') {
158 buf.append('/');
159 }
160 } else {
161 final String docroot = "{@docroot}";
162 // Search for lowercase version of {@docRoot}
163 index = lowerHtml.indexOf(docroot, previndex);
164 // If next {@docRoot} tag not found, append rest of htmlstr and exit loop
165 if (index < 0) {
166 buf.append(htmlstr.substring(previndex));
167 break;
168 }
169 // If next {@docroot} tag found, append htmlstr up to start of tag
170 buf.append(htmlstr.substring(previndex, index));
171 previndex = index + docroot.length();
172 // Insert relative path where {@docRoot} was located
173 buf.append(pathToRoot.isEmpty() ? "." : pathToRoot.getPath());
174 // Append slash if next character is not a slash
175 if (previndex < htmlstr.length() && htmlstr.charAt(previndex) != '/') {
176 buf.append('/');
177 }
178 }
179 }
180 return buf.toString();
181 }
183 /**
184 * Get the script to show or hide the All classes link.
185 *
186 * @param id id of the element to show or hide
187 * @return a content tree for the script
188 */
189 public Content getAllClassesLinkScript(String id) {
190 HtmlTree script = new HtmlTree(HtmlTag.SCRIPT);
191 script.addAttr(HtmlAttr.TYPE, "text/javascript");
192 String scriptCode = "<!--" + DocletConstants.NL +
193 " allClassesLink = document.getElementById(\"" + id + "\");" + DocletConstants.NL +
194 " if(window==top) {" + DocletConstants.NL +
195 " allClassesLink.style.display = \"block\";" + DocletConstants.NL +
196 " }" + DocletConstants.NL +
197 " else {" + DocletConstants.NL +
198 " allClassesLink.style.display = \"none\";" + DocletConstants.NL +
199 " }" + DocletConstants.NL +
200 " //-->" + DocletConstants.NL;
201 Content scriptContent = new RawHtml(scriptCode);
202 script.addContent(scriptContent);
203 Content div = HtmlTree.DIV(script);
204 return div;
205 }
207 /**
208 * Add method information.
209 *
210 * @param method the method to be documented
211 * @param dl the content tree to which the method information will be added
212 */
213 private void addMethodInfo(MethodDoc method, Content dl) {
214 ClassDoc[] intfacs = method.containingClass().interfaces();
215 MethodDoc overriddenMethod = method.overriddenMethod();
216 // Check whether there is any implementation or overridden info to be
217 // printed. If no overridden or implementation info needs to be
218 // printed, do not print this section.
219 if ((intfacs.length > 0 &&
220 new ImplementedMethods(method, this.configuration).build().length > 0) ||
221 overriddenMethod != null) {
222 MethodWriterImpl.addImplementsInfo(this, method, dl);
223 if (overriddenMethod != null) {
224 MethodWriterImpl.addOverridden(this,
225 method.overriddenType(), overriddenMethod, dl);
226 }
227 }
228 }
230 /**
231 * Adds the tags information.
232 *
233 * @param doc the doc for which the tags will be generated
234 * @param htmltree the documentation tree to which the tags will be added
235 */
236 protected void addTagsInfo(Doc doc, Content htmltree) {
237 if (configuration.nocomment) {
238 return;
239 }
240 Content dl = new HtmlTree(HtmlTag.DL);
241 if (doc instanceof MethodDoc) {
242 addMethodInfo((MethodDoc) doc, dl);
243 }
244 TagletOutputImpl output = new TagletOutputImpl("");
245 TagletWriter.genTagOuput(configuration.tagletManager, doc,
246 configuration.tagletManager.getCustomTags(doc),
247 getTagletWriterInstance(false), output);
248 String outputString = output.toString().trim();
249 if (!outputString.isEmpty()) {
250 Content resultString = new RawHtml(outputString);
251 dl.addContent(resultString);
252 }
253 htmltree.addContent(dl);
254 }
256 /**
257 * Check whether there are any tags for Serialization Overview
258 * section to be printed.
259 *
260 * @param field the FieldDoc object to check for tags.
261 * @return true if there are tags to be printed else return false.
262 */
263 protected boolean hasSerializationOverviewTags(FieldDoc field) {
264 TagletOutputImpl output = new TagletOutputImpl("");
265 TagletWriter.genTagOuput(configuration.tagletManager, field,
266 configuration.tagletManager.getCustomTags(field),
267 getTagletWriterInstance(false), output);
268 return (!output.toString().trim().isEmpty());
269 }
271 /**
272 * Returns a TagletWriter that knows how to write HTML.
273 *
274 * @return a TagletWriter that knows how to write HTML.
275 */
276 public TagletWriter getTagletWriterInstance(boolean isFirstSentence) {
277 return new TagletWriterImpl(this, isFirstSentence);
278 }
280 /**
281 * Get Package link, with target frame.
282 *
283 * @param pd The link will be to the "package-summary.html" page for this package
284 * @param target name of the target frame
285 * @param label tag for the link
286 * @return a content for the target package link
287 */
288 public Content getTargetPackageLink(PackageDoc pd, String target,
289 Content label) {
290 return getHyperLink(pathString(pd, DocPaths.PACKAGE_SUMMARY), label, "", target);
291 }
293 /**
294 * Generates the HTML document tree and prints it out.
295 *
296 * @param metakeywords Array of String keywords for META tag. Each element
297 * of the array is assigned to a separate META tag.
298 * Pass in null for no array
299 * @param includeScript true if printing windowtitle script
300 * false for files that appear in the left-hand frames
301 * @param body the body htmltree to be included in the document
302 */
303 public void printHtmlDocument(String[] metakeywords, boolean includeScript,
304 Content body) throws IOException {
305 Content htmlDocType = DocType.TRANSITIONAL;
306 Content htmlComment = new Comment(configuration.getText("doclet.New_Page"));
307 Content head = new HtmlTree(HtmlTag.HEAD);
308 if (!configuration.notimestamp) {
309 Content headComment = new Comment(getGeneratedByString());
310 head.addContent(headComment);
311 }
312 if (configuration.charset.length() > 0) {
313 Content meta = HtmlTree.META("Content-Type", "text/html",
314 configuration.charset);
315 head.addContent(meta);
316 }
317 head.addContent(getTitle());
318 if (!configuration.notimestamp) {
319 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
320 Content meta = HtmlTree.META("date", dateFormat.format(new Date()));
321 head.addContent(meta);
322 }
323 if (metakeywords != null) {
324 for (int i=0; i < metakeywords.length; i++) {
325 Content meta = HtmlTree.META("keywords", metakeywords[i]);
326 head.addContent(meta);
327 }
328 }
329 head.addContent(getStyleSheetProperties());
330 Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(),
331 head, body);
332 Content htmlDocument = new HtmlDocument(htmlDocType,
333 htmlComment, htmlTree);
334 write(htmlDocument);
335 }
337 /**
338 * Get the window title.
339 *
340 * @param title the title string to construct the complete window title
341 * @return the window title string
342 */
343 public String getWindowTitle(String title) {
344 if (configuration.windowtitle.length() > 0) {
345 title += " (" + configuration.windowtitle + ")";
346 }
347 return title;
348 }
350 /**
351 * Get user specified header and the footer.
352 *
353 * @param header if true print the user provided header else print the
354 * user provided footer.
355 */
356 public Content getUserHeaderFooter(boolean header) {
357 String content;
358 if (header) {
359 content = replaceDocRootDir(configuration.header);
360 } else {
361 if (configuration.footer.length() != 0) {
362 content = replaceDocRootDir(configuration.footer);
363 } else {
364 content = replaceDocRootDir(configuration.header);
365 }
366 }
367 Content rawContent = new RawHtml(content);
368 Content em = HtmlTree.EM(rawContent);
369 return em;
370 }
372 /**
373 * Adds the user specified top.
374 *
375 * @param body the content tree to which user specified top will be added
376 */
377 public void addTop(Content body) {
378 Content top = new RawHtml(replaceDocRootDir(configuration.top));
379 body.addContent(top);
380 }
382 /**
383 * Adds the user specified bottom.
384 *
385 * @param body the content tree to which user specified bottom will be added
386 */
387 public void addBottom(Content body) {
388 Content bottom = new RawHtml(replaceDocRootDir(configuration.bottom));
389 Content small = HtmlTree.SMALL(bottom);
390 Content p = HtmlTree.P(HtmlStyle.legalCopy, small);
391 body.addContent(p);
392 }
394 /**
395 * Adds the navigation bar for the Html page at the top and and the bottom.
396 *
397 * @param header If true print navigation bar at the top of the page else
398 * @param body the HtmlTree to which the nav links will be added
399 */
400 protected void addNavLinks(boolean header, Content body) {
401 if (!configuration.nonavbar) {
402 String allClassesId = "allclasses_";
403 HtmlTree navDiv = new HtmlTree(HtmlTag.DIV);
404 if (header) {
405 body.addContent(HtmlConstants.START_OF_TOP_NAVBAR);
406 navDiv.addStyle(HtmlStyle.topNav);
407 allClassesId += "navbar_top";
408 Content a = getMarkerAnchor("navbar_top");
409 navDiv.addContent(a);
410 Content skipLinkContent = getHyperLink(DocLink.fragment("skip-navbar_top"),
411 HtmlTree.EMPTY,
412 configuration.getText("doclet.Skip_navigation_links"),
413 "");
414 navDiv.addContent(skipLinkContent);
415 } else {
416 body.addContent(HtmlConstants.START_OF_BOTTOM_NAVBAR);
417 navDiv.addStyle(HtmlStyle.bottomNav);
418 allClassesId += "navbar_bottom";
419 Content a = getMarkerAnchor("navbar_bottom");
420 navDiv.addContent(a);
421 Content skipLinkContent = getHyperLink(DocLink.fragment("skip-navbar_bottom"),
422 HtmlTree.EMPTY,
423 configuration.getText("doclet.Skip_navigation_links"),
424 "");
425 navDiv.addContent(skipLinkContent);
426 }
427 if (header) {
428 navDiv.addContent(getMarkerAnchor("navbar_top_firstrow"));
429 } else {
430 navDiv.addContent(getMarkerAnchor("navbar_bottom_firstrow"));
431 }
432 HtmlTree navList = new HtmlTree(HtmlTag.UL);
433 navList.addStyle(HtmlStyle.navList);
434 navList.addAttr(HtmlAttr.TITLE, "Navigation");
435 if (configuration.createoverview) {
436 navList.addContent(getNavLinkContents());
437 }
438 if (configuration.packages.length == 1) {
439 navList.addContent(getNavLinkPackage(configuration.packages[0]));
440 } else if (configuration.packages.length > 1) {
441 navList.addContent(getNavLinkPackage());
442 }
443 navList.addContent(getNavLinkClass());
444 if(configuration.classuse) {
445 navList.addContent(getNavLinkClassUse());
446 }
447 if(configuration.createtree) {
448 navList.addContent(getNavLinkTree());
449 }
450 if(!(configuration.nodeprecated ||
451 configuration.nodeprecatedlist)) {
452 navList.addContent(getNavLinkDeprecated());
453 }
454 if(configuration.createindex) {
455 navList.addContent(getNavLinkIndex());
456 }
457 if (!configuration.nohelp) {
458 navList.addContent(getNavLinkHelp());
459 }
460 navDiv.addContent(navList);
461 Content aboutDiv = HtmlTree.DIV(HtmlStyle.aboutLanguage, getUserHeaderFooter(header));
462 navDiv.addContent(aboutDiv);
463 body.addContent(navDiv);
464 Content ulNav = HtmlTree.UL(HtmlStyle.navList, getNavLinkPrevious());
465 ulNav.addContent(getNavLinkNext());
466 Content subDiv = HtmlTree.DIV(HtmlStyle.subNav, ulNav);
467 Content ulFrames = HtmlTree.UL(HtmlStyle.navList, getNavShowLists());
468 ulFrames.addContent(getNavHideLists(filename));
469 subDiv.addContent(ulFrames);
470 HtmlTree ulAllClasses = HtmlTree.UL(HtmlStyle.navList, getNavLinkClassIndex());
471 ulAllClasses.addAttr(HtmlAttr.ID, allClassesId.toString());
472 subDiv.addContent(ulAllClasses);
473 subDiv.addContent(getAllClassesLinkScript(allClassesId.toString()));
474 addSummaryDetailLinks(subDiv);
475 if (header) {
476 subDiv.addContent(getMarkerAnchor("skip-navbar_top"));
477 body.addContent(subDiv);
478 body.addContent(HtmlConstants.END_OF_TOP_NAVBAR);
479 } else {
480 subDiv.addContent(getMarkerAnchor("skip-navbar_bottom"));
481 body.addContent(subDiv);
482 body.addContent(HtmlConstants.END_OF_BOTTOM_NAVBAR);
483 }
484 }
485 }
487 /**
488 * Get the word "NEXT" to indicate that no link is available. Override
489 * this method to customize next link.
490 *
491 * @return a content tree for the link
492 */
493 protected Content getNavLinkNext() {
494 return getNavLinkNext(null);
495 }
497 /**
498 * Get the word "PREV" to indicate that no link is available. Override
499 * this method to customize prev link.
500 *
501 * @return a content tree for the link
502 */
503 protected Content getNavLinkPrevious() {
504 return getNavLinkPrevious(null);
505 }
507 /**
508 * Do nothing. This is the default method.
509 */
510 protected void addSummaryDetailLinks(Content navDiv) {
511 }
513 /**
514 * Get link to the "overview-summary.html" page.
515 *
516 * @return a content tree for the link
517 */
518 protected Content getNavLinkContents() {
519 Content linkContent = getHyperLink(pathToRoot.resolve(DocPaths.OVERVIEW_SUMMARY),
520 overviewLabel, "", "");
521 Content li = HtmlTree.LI(linkContent);
522 return li;
523 }
525 /**
526 * Get link to the "package-summary.html" page for the package passed.
527 *
528 * @param pkg Package to which link will be generated
529 * @return a content tree for the link
530 */
531 protected Content getNavLinkPackage(PackageDoc pkg) {
532 Content linkContent = getPackageLink(pkg,
533 packageLabel);
534 Content li = HtmlTree.LI(linkContent);
535 return li;
536 }
538 /**
539 * Get the word "Package" , to indicate that link is not available here.
540 *
541 * @return a content tree for the link
542 */
543 protected Content getNavLinkPackage() {
544 Content li = HtmlTree.LI(packageLabel);
545 return li;
546 }
548 /**
549 * Get the word "Use", to indicate that link is not available.
550 *
551 * @return a content tree for the link
552 */
553 protected Content getNavLinkClassUse() {
554 Content li = HtmlTree.LI(useLabel);
555 return li;
556 }
558 /**
559 * Get link for previous file.
560 *
561 * @param prev File name for the prev link
562 * @return a content tree for the link
563 */
564 public Content getNavLinkPrevious(DocPath prev) {
565 Content li;
566 if (prev != null) {
567 li = HtmlTree.LI(getHyperLink(prev, prevLabel, "", ""));
568 }
569 else
570 li = HtmlTree.LI(prevLabel);
571 return li;
572 }
574 /**
575 * Get link for next file. If next is null, just print the label
576 * without linking it anywhere.
577 *
578 * @param next File name for the next link
579 * @return a content tree for the link
580 */
581 public Content getNavLinkNext(DocPath next) {
582 Content li;
583 if (next != null) {
584 li = HtmlTree.LI(getHyperLink(next, nextLabel, "", ""));
585 }
586 else
587 li = HtmlTree.LI(nextLabel);
588 return li;
589 }
591 /**
592 * Get "FRAMES" link, to switch to the frame version of the output.
593 *
594 * @param link File to be linked, "index.html"
595 * @return a content tree for the link
596 */
597 protected Content getNavShowLists(DocPath link) {
598 DocLink dl = new DocLink(link, path.getPath(), null);
599 Content framesContent = getHyperLink(dl, framesLabel, "", "_top");
600 Content li = HtmlTree.LI(framesContent);
601 return li;
602 }
604 /**
605 * Get "FRAMES" link, to switch to the frame version of the output.
606 *
607 * @return a content tree for the link
608 */
609 protected Content getNavShowLists() {
610 return getNavShowLists(pathToRoot.resolve(DocPaths.INDEX));
611 }
613 /**
614 * Get "NO FRAMES" link, to switch to the non-frame version of the output.
615 *
616 * @param link File to be linked
617 * @return a content tree for the link
618 */
619 protected Content getNavHideLists(DocPath link) {
620 Content noFramesContent = getHyperLink(link, noframesLabel, "", "_top");
621 Content li = HtmlTree.LI(noFramesContent);
622 return li;
623 }
625 /**
626 * Get "Tree" link in the navigation bar. If there is only one package
627 * specified on the command line, then the "Tree" link will be to the
628 * only "package-tree.html" file otherwise it will be to the
629 * "overview-tree.html" file.
630 *
631 * @return a content tree for the link
632 */
633 protected Content getNavLinkTree() {
634 Content treeLinkContent;
635 PackageDoc[] packages = configuration.root.specifiedPackages();
636 if (packages.length == 1 && configuration.root.specifiedClasses().length == 0) {
637 treeLinkContent = getHyperLink(pathString(packages[0],
638 DocPaths.PACKAGE_TREE), treeLabel,
639 "", "");
640 } else {
641 treeLinkContent = getHyperLink(pathToRoot.resolve(DocPaths.OVERVIEW_TREE),
642 treeLabel, "", "");
643 }
644 Content li = HtmlTree.LI(treeLinkContent);
645 return li;
646 }
648 /**
649 * Get the overview tree link for the main tree.
650 *
651 * @param label the label for the link
652 * @return a content tree for the link
653 */
654 protected Content getNavLinkMainTree(String label) {
655 Content mainTreeContent = getHyperLink(pathToRoot.resolve(DocPaths.OVERVIEW_TREE),
656 new StringContent(label));
657 Content li = HtmlTree.LI(mainTreeContent);
658 return li;
659 }
661 /**
662 * Get the word "Class", to indicate that class link is not available.
663 *
664 * @return a content tree for the link
665 */
666 protected Content getNavLinkClass() {
667 Content li = HtmlTree.LI(classLabel);
668 return li;
669 }
671 /**
672 * Get "Deprecated" API link in the navigation bar.
673 *
674 * @return a content tree for the link
675 */
676 protected Content getNavLinkDeprecated() {
677 Content linkContent = getHyperLink(pathToRoot.resolve(DocPaths.DEPRECATED_LIST),
678 deprecatedLabel, "", "");
679 Content li = HtmlTree.LI(linkContent);
680 return li;
681 }
683 /**
684 * Get link for generated index. If the user has used "-splitindex"
685 * command line option, then link to file "index-files/index-1.html" is
686 * generated otherwise link to file "index-all.html" is generated.
687 *
688 * @return a content tree for the link
689 */
690 protected Content getNavLinkClassIndex() {
691 Content allClassesContent = getHyperLink(pathToRoot.resolve(
692 DocPaths.ALLCLASSES_NOFRAME),
693 allclassesLabel, "", "");
694 Content li = HtmlTree.LI(allClassesContent);
695 return li;
696 }
698 /**
699 * Get link for generated class index.
700 *
701 * @return a content tree for the link
702 */
703 protected Content getNavLinkIndex() {
704 Content linkContent = getHyperLink(pathToRoot.resolve(
705 (configuration.splitindex
706 ? DocPaths.INDEX_FILES.resolve(DocPaths.indexN(1))
707 : DocPaths.INDEX_ALL)),
708 indexLabel, "", "");
709 Content li = HtmlTree.LI(linkContent);
710 return li;
711 }
713 /**
714 * Get help file link. If user has provided a help file, then generate a
715 * link to the user given file, which is already copied to current or
716 * destination directory.
717 *
718 * @return a content tree for the link
719 */
720 protected Content getNavLinkHelp() {
721 String helpfile = configuration.helpfile;
722 DocPath helpfilenm;
723 if (helpfile.isEmpty()) {
724 helpfilenm = DocPaths.HELP_DOC;
725 } else {
726 DocFile file = DocFile.createFileForInput(configuration, helpfile);
727 helpfilenm = DocPath.create(file.getName());
728 }
729 Content linkContent = getHyperLink(pathToRoot.resolve(helpfilenm),
730 helpLabel, "", "");
731 Content li = HtmlTree.LI(linkContent);
732 return li;
733 }
735 /**
736 * Get summary table header.
737 *
738 * @param header the header for the table
739 * @param scope the scope of the headers
740 * @return a content tree for the header
741 */
742 public Content getSummaryTableHeader(String[] header, String scope) {
743 Content tr = new HtmlTree(HtmlTag.TR);
744 int size = header.length;
745 Content tableHeader;
746 if (size == 1) {
747 tableHeader = new StringContent(header[0]);
748 tr.addContent(HtmlTree.TH(HtmlStyle.colOne, scope, tableHeader));
749 return tr;
750 }
751 for (int i = 0; i < size; i++) {
752 tableHeader = new StringContent(header[i]);
753 if(i == 0)
754 tr.addContent(HtmlTree.TH(HtmlStyle.colFirst, scope, tableHeader));
755 else if(i == (size - 1))
756 tr.addContent(HtmlTree.TH(HtmlStyle.colLast, scope, tableHeader));
757 else
758 tr.addContent(HtmlTree.TH(scope, tableHeader));
759 }
760 return tr;
761 }
763 /**
764 * Get table caption.
765 *
766 * @param rawText the caption for the table which could be raw Html
767 * @return a content tree for the caption
768 */
769 public Content getTableCaption(String rawText) {
770 Content title = new RawHtml(rawText);
771 Content captionSpan = HtmlTree.SPAN(title);
772 Content space = getSpace();
773 Content tabSpan = HtmlTree.SPAN(HtmlStyle.tabEnd, space);
774 Content caption = HtmlTree.CAPTION(captionSpan);
775 caption.addContent(tabSpan);
776 return caption;
777 }
779 /**
780 * Get the marker anchor which will be added to the documentation tree.
781 *
782 * @param anchorName the anchor name attribute
783 * @return a content tree for the marker anchor
784 */
785 public Content getMarkerAnchor(String anchorName) {
786 return getMarkerAnchor(anchorName, null);
787 }
789 /**
790 * Get the marker anchor which will be added to the documentation tree.
791 *
792 * @param anchorName the anchor name attribute
793 * @param anchorContent the content that should be added to the anchor
794 * @return a content tree for the marker anchor
795 */
796 public Content getMarkerAnchor(String anchorName, Content anchorContent) {
797 if (anchorContent == null)
798 anchorContent = new Comment(" ");
799 Content markerAnchor = HtmlTree.A_NAME(anchorName, anchorContent);
800 return markerAnchor;
801 }
803 /**
804 * Returns a packagename content.
805 *
806 * @param packageDoc the package to check
807 * @return package name content
808 */
809 public Content getPackageName(PackageDoc packageDoc) {
810 return packageDoc == null || packageDoc.name().length() == 0 ?
811 defaultPackageLabel :
812 getPackageLabel(packageDoc.name());
813 }
815 /**
816 * Returns a package name label.
817 *
818 * @param packageName the package name
819 * @return the package name content
820 */
821 public Content getPackageLabel(String packageName) {
822 return new StringContent(packageName);
823 }
825 /**
826 * Add package deprecation information to the documentation tree
827 *
828 * @param deprPkgs list of deprecated packages
829 * @param headingKey the caption for the deprecated package table
830 * @param tableSummary the summary for the deprecated package table
831 * @param tableHeader table headers for the deprecated package table
832 * @param contentTree the content tree to which the deprecated package table will be added
833 */
834 protected void addPackageDeprecatedAPI(List<Doc> deprPkgs, String headingKey,
835 String tableSummary, String[] tableHeader, Content contentTree) {
836 if (deprPkgs.size() > 0) {
837 Content table = HtmlTree.TABLE(0, 3, 0, tableSummary,
838 getTableCaption(configuration.getText(headingKey)));
839 table.addContent(getSummaryTableHeader(tableHeader, "col"));
840 Content tbody = new HtmlTree(HtmlTag.TBODY);
841 for (int i = 0; i < deprPkgs.size(); i++) {
842 PackageDoc pkg = (PackageDoc) deprPkgs.get(i);
843 HtmlTree td = HtmlTree.TD(HtmlStyle.colOne,
844 getPackageLink(pkg, getPackageName(pkg)));
845 if (pkg.tags("deprecated").length > 0) {
846 addInlineDeprecatedComment(pkg, pkg.tags("deprecated")[0], td);
847 }
848 HtmlTree tr = HtmlTree.TR(td);
849 if (i % 2 == 0) {
850 tr.addStyle(HtmlStyle.altColor);
851 } else {
852 tr.addStyle(HtmlStyle.rowColor);
853 }
854 tbody.addContent(tr);
855 }
856 table.addContent(tbody);
857 Content li = HtmlTree.LI(HtmlStyle.blockList, table);
858 Content ul = HtmlTree.UL(HtmlStyle.blockList, li);
859 contentTree.addContent(ul);
860 }
861 }
863 /**
864 * Return the path to the class page for a classdoc.
865 *
866 * @param cd Class to which the path is requested.
867 * @param name Name of the file(doesn't include path).
868 */
869 protected DocPath pathString(ClassDoc cd, DocPath name) {
870 return pathString(cd.containingPackage(), name);
871 }
873 /**
874 * Return path to the given file name in the given package. So if the name
875 * passed is "Object.html" and the name of the package is "java.lang", and
876 * if the relative path is "../.." then returned string will be
877 * "../../java/lang/Object.html"
878 *
879 * @param pd Package in which the file name is assumed to be.
880 * @param name File name, to which path string is.
881 */
882 protected DocPath pathString(PackageDoc pd, DocPath name) {
883 return pathToRoot.resolve(DocPath.forPackage(pd).resolve(name));
884 }
886 /**
887 * Return the link to the given package.
888 *
889 * @param pkg the package to link to.
890 * @param label the label for the link.
891 * @param isStrong true if the label should be strong.
892 * @return the link to the given package.
893 */
894 public String getPackageLinkString(PackageDoc pkg, String label,
895 boolean isStrong) {
896 return getPackageLinkString(pkg, label, isStrong, "");
897 }
899 /**
900 * Return the link to the given package.
901 *
902 * @param pkg the package to link to.
903 * @param label the label for the link.
904 * @param isStrong true if the label should be strong.
905 * @param style the font of the package link label.
906 * @return the link to the given package.
907 */
908 public String getPackageLinkString(PackageDoc pkg, String label, boolean isStrong,
909 String style) {
910 boolean included = pkg != null && pkg.isIncluded();
911 if (! included) {
912 PackageDoc[] packages = configuration.packages;
913 for (int i = 0; i < packages.length; i++) {
914 if (packages[i].equals(pkg)) {
915 included = true;
916 break;
917 }
918 }
919 }
920 if (included || pkg == null) {
921 return getHyperLinkString(pathString(pkg, DocPaths.PACKAGE_SUMMARY),
922 label, isStrong, style);
923 } else {
924 DocLink crossPkgLink = getCrossPackageLink(Util.getPackageName(pkg));
925 if (crossPkgLink != null) {
926 return getHyperLinkString(crossPkgLink, label, isStrong, style);
927 } else {
928 return label;
929 }
930 }
931 }
933 /**
934 * Return the link to the given package.
935 *
936 * @param pkg the package to link to.
937 * @param label the label for the link.
938 * @return a content tree for the package link.
939 */
940 public Content getPackageLink(PackageDoc pkg, Content label) {
941 boolean included = pkg != null && pkg.isIncluded();
942 if (! included) {
943 PackageDoc[] packages = configuration.packages;
944 for (int i = 0; i < packages.length; i++) {
945 if (packages[i].equals(pkg)) {
946 included = true;
947 break;
948 }
949 }
950 }
951 if (included || pkg == null) {
952 return getHyperLink(pathString(pkg, DocPaths.PACKAGE_SUMMARY),
953 label);
954 } else {
955 DocLink crossPkgLink = getCrossPackageLink(Util.getPackageName(pkg));
956 if (crossPkgLink != null) {
957 return getHyperLink(crossPkgLink, label);
958 } else {
959 return label;
960 }
961 }
962 }
964 public String italicsClassName(ClassDoc cd, boolean qual) {
965 String name = (qual)? cd.qualifiedName(): cd.name();
966 return (cd.isInterface())? italicsText(name): name;
967 }
969 /**
970 * Add the link to the content tree.
971 *
972 * @param doc program element doc for which the link will be added
973 * @param label label for the link
974 * @param htmltree the content tree to which the link will be added
975 */
976 public void addSrcLink(ProgramElementDoc doc, Content label, Content htmltree) {
977 if (doc == null) {
978 return;
979 }
980 ClassDoc cd = doc.containingClass();
981 if (cd == null) {
982 //d must be a class doc since in has no containing class.
983 cd = (ClassDoc) doc;
984 }
985 DocPath href = pathToRoot
986 .resolve(DocPaths.SOURCE_OUTPUT)
987 .resolve(DocPath.forClass(cd));
988 Content linkContent = getHyperLink(href.fragment(SourceToHTMLConverter.getAnchorName(doc)), label, "", "");
989 htmltree.addContent(linkContent);
990 }
992 /**
993 * Return the link to the given class.
994 *
995 * @param linkInfo the information about the link.
996 *
997 * @return the link for the given class.
998 */
999 public String getLink(LinkInfoImpl linkInfo) {
1000 LinkFactoryImpl factory = new LinkFactoryImpl(this);
1001 String link = factory.getLinkOutput(linkInfo).toString();
1002 displayLength += linkInfo.displayLength;
1003 return link;
1004 }
1006 /**
1007 * Return the type parameters for the given class.
1008 *
1009 * @param linkInfo the information about the link.
1010 * @return the type for the given class.
1011 */
1012 public String getTypeParameterLinks(LinkInfoImpl linkInfo) {
1013 LinkFactoryImpl factory = new LinkFactoryImpl(this);
1014 return factory.getTypeParameterLinks(linkInfo, false).toString();
1015 }
1017 /*************************************************************
1018 * Return a class cross link to external class documentation.
1019 * The name must be fully qualified to determine which package
1020 * the class is in. The -link option does not allow users to
1021 * link to external classes in the "default" package.
1022 *
1023 * @param qualifiedClassName the qualified name of the external class.
1024 * @param refMemName the name of the member being referenced. This should
1025 * be null or empty string if no member is being referenced.
1026 * @param label the label for the external link.
1027 * @param strong true if the link should be strong.
1028 * @param style the style of the link.
1029 * @param code true if the label should be code font.
1030 */
1031 public String getCrossClassLink(String qualifiedClassName, String refMemName,
1032 String label, boolean strong, String style,
1033 boolean code) {
1034 String className = "";
1035 String packageName = qualifiedClassName == null ? "" : qualifiedClassName;
1036 int periodIndex;
1037 while ((periodIndex = packageName.lastIndexOf('.')) != -1) {
1038 className = packageName.substring(periodIndex + 1, packageName.length()) +
1039 (className.length() > 0 ? "." + className : "");
1040 String defaultLabel = code ? codeText(className) : className;
1041 packageName = packageName.substring(0, periodIndex);
1042 if (getCrossPackageLink(packageName) != null) {
1043 //The package exists in external documentation, so link to the external
1044 //class (assuming that it exists). This is definitely a limitation of
1045 //the -link option. There are ways to determine if an external package
1046 //exists, but no way to determine if the external class exists. We just
1047 //have to assume that it does.
1048 DocLink link = configuration.extern.getExternalLink(packageName, pathToRoot,
1049 className + ".html", refMemName);
1050 return getHyperLinkString(link,
1051 (label == null) || label.length() == 0 ? defaultLabel : label,
1054 strong, style,
1055 configuration.getText("doclet.Href_Class_Or_Interface_Title", packageName),
1056 "");
1057 }
1058 }
1059 return null;
1060 }
1062 public boolean isClassLinkable(ClassDoc cd) {
1063 if (cd.isIncluded()) {
1064 return configuration.isGeneratedDoc(cd);
1065 }
1066 return configuration.extern.isExternal(cd);
1067 }
1069 public DocLink getCrossPackageLink(String pkgName) {
1070 return configuration.extern.getExternalLink(pkgName, pathToRoot,
1071 DocPaths.PACKAGE_SUMMARY.getPath());
1072 }
1074 /**
1075 * Get the class link.
1076 *
1077 * @param context the id of the context where the link will be added
1078 * @param cd the class doc to link to
1079 * @return a content tree for the link
1080 */
1081 public Content getQualifiedClassLink(int context, ClassDoc cd) {
1082 return new RawHtml(getLink(new LinkInfoImpl(configuration, context, cd,
1083 configuration.getClassName(cd), "")));
1084 }
1086 /**
1087 * Add the class link.
1088 *
1089 * @param context the id of the context where the link will be added
1090 * @param cd the class doc to link to
1091 * @param contentTree the content tree to which the link will be added
1092 */
1093 public void addPreQualifiedClassLink(int context, ClassDoc cd, Content contentTree) {
1094 addPreQualifiedClassLink(context, cd, false, contentTree);
1095 }
1097 /**
1098 * Retrieve the class link with the package portion of the label in
1099 * plain text. If the qualifier is excluded, it will not be included in the
1100 * link label.
1101 *
1102 * @param cd the class to link to.
1103 * @param isStrong true if the link should be strong.
1104 * @return the link with the package portion of the label in plain text.
1105 */
1106 public String getPreQualifiedClassLink(int context,
1107 ClassDoc cd, boolean isStrong) {
1108 String classlink = "";
1109 PackageDoc pd = cd.containingPackage();
1110 if(pd != null && ! configuration.shouldExcludeQualifier(pd.name())) {
1111 classlink = getPkgName(cd);
1112 }
1113 classlink += getLink(new LinkInfoImpl(configuration,
1114 context, cd, cd.name(), isStrong));
1115 return classlink;
1116 }
1118 /**
1119 * Add the class link with the package portion of the label in
1120 * plain text. If the qualifier is excluded, it will not be included in the
1121 * link label.
1122 *
1123 * @param context the id of the context where the link will be added
1124 * @param cd the class to link to
1125 * @param isStrong true if the link should be strong
1126 * @param contentTree the content tree to which the link with be added
1127 */
1128 public void addPreQualifiedClassLink(int context,
1129 ClassDoc cd, boolean isStrong, Content contentTree) {
1130 PackageDoc pd = cd.containingPackage();
1131 if(pd != null && ! configuration.shouldExcludeQualifier(pd.name())) {
1132 contentTree.addContent(getPkgName(cd));
1133 }
1134 contentTree.addContent(new RawHtml(getLink(new LinkInfoImpl(configuration,
1135 context, cd, cd.name(), isStrong))));
1136 }
1138 /**
1139 * Add the class link, with only class name as the strong link and prefixing
1140 * plain package name.
1141 *
1142 * @param context the id of the context where the link will be added
1143 * @param cd the class to link to
1144 * @param contentTree the content tree to which the link with be added
1145 */
1146 public void addPreQualifiedStrongClassLink(int context, ClassDoc cd, Content contentTree) {
1147 addPreQualifiedClassLink(context, cd, true, contentTree);
1148 }
1150 /**
1151 * Get the link for the given member.
1152 *
1153 * @param context the id of the context where the link will be added
1154 * @param doc the member being linked to
1155 * @param label the label for the link
1156 * @return a content tree for the doc link
1157 */
1158 public Content getDocLink(int context, MemberDoc doc, String label) {
1159 return getDocLink(context, doc.containingClass(), doc, label);
1160 }
1162 /**
1163 * Return the link for the given member.
1164 *
1165 * @param context the id of the context where the link will be printed.
1166 * @param doc the member being linked to.
1167 * @param label the label for the link.
1168 * @param strong true if the link should be strong.
1169 * @return the link for the given member.
1170 */
1171 public String getDocLink(int context, MemberDoc doc, String label,
1172 boolean strong) {
1173 return getDocLink(context, doc.containingClass(), doc, label, strong);
1174 }
1176 /**
1177 * Return the link for the given member.
1178 *
1179 * @param context the id of the context where the link will be printed.
1180 * @param classDoc the classDoc that we should link to. This is not
1181 * necessarily equal to doc.containingClass(). We may be
1182 * inheriting comments.
1183 * @param doc the member being linked to.
1184 * @param label the label for the link.
1185 * @param strong true if the link should be strong.
1186 * @return the link for the given member.
1187 */
1188 public String getDocLink(int context, ClassDoc classDoc, MemberDoc doc,
1189 String label, boolean strong) {
1190 if (! (doc.isIncluded() ||
1191 Util.isLinkable(classDoc, configuration))) {
1192 return label;
1193 } else if (doc instanceof ExecutableMemberDoc) {
1194 ExecutableMemberDoc emd = (ExecutableMemberDoc)doc;
1195 return getLink(new LinkInfoImpl(configuration, context, classDoc,
1196 getAnchor(emd), label, strong));
1197 } else if (doc instanceof MemberDoc) {
1198 return getLink(new LinkInfoImpl(configuration, context, classDoc,
1199 doc.name(), label, strong));
1200 } else {
1201 return label;
1202 }
1203 }
1205 /**
1206 * Return the link for the given member.
1207 *
1208 * @param context the id of the context where the link will be added
1209 * @param classDoc the classDoc that we should link to. This is not
1210 * necessarily equal to doc.containingClass(). We may be
1211 * inheriting comments
1212 * @param doc the member being linked to
1213 * @param label the label for the link
1214 * @return the link for the given member
1215 */
1216 public Content getDocLink(int context, ClassDoc classDoc, MemberDoc doc,
1217 String label) {
1218 if (! (doc.isIncluded() ||
1219 Util.isLinkable(classDoc, configuration))) {
1220 return new StringContent(label);
1221 } else if (doc instanceof ExecutableMemberDoc) {
1222 ExecutableMemberDoc emd = (ExecutableMemberDoc)doc;
1223 return new RawHtml(getLink(new LinkInfoImpl(configuration, context, classDoc,
1224 getAnchor(emd), label, false)));
1225 } else if (doc instanceof MemberDoc) {
1226 return new RawHtml(getLink(new LinkInfoImpl(configuration, context, classDoc,
1227 doc.name(), label, false)));
1228 } else {
1229 return new StringContent(label);
1230 }
1231 }
1233 public String getAnchor(ExecutableMemberDoc emd) {
1234 StringBuilder signature = new StringBuilder(emd.signature());
1235 StringBuilder signatureParsed = new StringBuilder();
1236 int counter = 0;
1237 for (int i = 0; i < signature.length(); i++) {
1238 char c = signature.charAt(i);
1239 if (c == '<') {
1240 counter++;
1241 } else if (c == '>') {
1242 counter--;
1243 } else if (counter == 0) {
1244 signatureParsed.append(c);
1245 }
1246 }
1247 return emd.name() + signatureParsed.toString();
1248 }
1250 public String seeTagToString(SeeTag see) {
1251 String tagName = see.name();
1252 if (! (tagName.startsWith("@link") || tagName.equals("@see"))) {
1253 return "";
1254 }
1256 String seetext = replaceDocRootDir(see.text());
1258 //Check if @see is an href or "string"
1259 if (seetext.startsWith("<") || seetext.startsWith("\"")) {
1260 return seetext;
1261 }
1263 boolean plain = tagName.equalsIgnoreCase("@linkplain");
1264 String label = plainOrCodeText(plain, see.label());
1266 //The text from the @see tag. We will output this text when a label is not specified.
1267 String text = plainOrCodeText(plain, seetext);
1269 ClassDoc refClass = see.referencedClass();
1270 String refClassName = see.referencedClassName();
1271 MemberDoc refMem = see.referencedMember();
1272 String refMemName = see.referencedMemberName();
1274 if (refClass == null) {
1275 //@see is not referencing an included class
1276 PackageDoc refPackage = see.referencedPackage();
1277 if (refPackage != null && refPackage.isIncluded()) {
1278 //@see is referencing an included package
1279 if (label.isEmpty())
1280 label = plainOrCodeText(plain, refPackage.name());
1281 return getPackageLinkString(refPackage, label, false);
1282 } else {
1283 //@see is not referencing an included class or package. Check for cross links.
1284 String classCrossLink;
1285 DocLink packageCrossLink = getCrossPackageLink(refClassName);
1286 if (packageCrossLink != null) {
1287 //Package cross link found
1288 return getHyperLinkString(packageCrossLink,
1289 (label.isEmpty() ? text : label), false);
1290 } else if ((classCrossLink = getCrossClassLink(refClassName,
1291 refMemName, label, false, "", !plain)) != null) {
1292 //Class cross link found (possibly to a member in the class)
1293 return classCrossLink;
1294 } else {
1295 //No cross link found so print warning
1296 configuration.getDocletSpecificMsg().warning(see.position(), "doclet.see.class_or_package_not_found",
1297 tagName, seetext);
1298 return (label.isEmpty() ? text: label);
1299 }
1300 }
1301 } else if (refMemName == null) {
1302 // Must be a class reference since refClass is not null and refMemName is null.
1303 if (label.isEmpty()) {
1304 label = plainOrCodeText(plain, refClass.name());
1305 }
1306 return getLink(new LinkInfoImpl(configuration, refClass, label));
1307 } else if (refMem == null) {
1308 // Must be a member reference since refClass is not null and refMemName is not null.
1309 // However, refMem is null, so this referenced member does not exist.
1310 return (label.isEmpty() ? text: label);
1311 } else {
1312 // Must be a member reference since refClass is not null and refMemName is not null.
1313 // refMem is not null, so this @see tag must be referencing a valid member.
1314 ClassDoc containing = refMem.containingClass();
1315 if (see.text().trim().startsWith("#") &&
1316 ! (containing.isPublic() ||
1317 Util.isLinkable(containing, configuration))) {
1318 // Since the link is relative and the holder is not even being
1319 // documented, this must be an inherited link. Redirect it.
1320 // The current class either overrides the referenced member or
1321 // inherits it automatically.
1322 if (this instanceof ClassWriterImpl) {
1323 containing = ((ClassWriterImpl) this).getClassDoc();
1324 } else if (!containing.isPublic()){
1325 configuration.getDocletSpecificMsg().warning(
1326 see.position(), "doclet.see.class_or_package_not_accessible",
1327 tagName, containing.qualifiedName());
1328 } else {
1329 configuration.getDocletSpecificMsg().warning(
1330 see.position(), "doclet.see.class_or_package_not_found",
1331 tagName, seetext);
1332 }
1333 }
1334 if (configuration.currentcd != containing) {
1335 refMemName = containing.name() + "." + refMemName;
1336 }
1337 if (refMem instanceof ExecutableMemberDoc) {
1338 if (refMemName.indexOf('(') < 0) {
1339 refMemName += ((ExecutableMemberDoc)refMem).signature();
1340 }
1341 }
1343 text = plainOrCodeText(plain, Util.escapeHtmlChars(refMemName));
1345 return getDocLink(LinkInfoImpl.CONTEXT_SEE_TAG, containing,
1346 refMem, (label.isEmpty() ? text: label), false);
1347 }
1348 }
1350 private String plainOrCodeText(boolean plain, String text) {
1351 return (plain || text.isEmpty()) ? text : codeText(text);
1352 }
1354 /**
1355 * Add the inline comment.
1356 *
1357 * @param doc the doc for which the inline comment will be added
1358 * @param tag the inline tag to be added
1359 * @param htmltree the content tree to which the comment will be added
1360 */
1361 public void addInlineComment(Doc doc, Tag tag, Content htmltree) {
1362 addCommentTags(doc, tag.inlineTags(), false, false, htmltree);
1363 }
1365 /**
1366 * Add the inline deprecated comment.
1367 *
1368 * @param doc the doc for which the inline deprecated comment will be added
1369 * @param tag the inline tag to be added
1370 * @param htmltree the content tree to which the comment will be added
1371 */
1372 public void addInlineDeprecatedComment(Doc doc, Tag tag, Content htmltree) {
1373 addCommentTags(doc, tag.inlineTags(), true, false, htmltree);
1374 }
1376 /**
1377 * Adds the summary content.
1378 *
1379 * @param doc the doc for which the summary will be generated
1380 * @param htmltree the documentation tree to which the summary will be added
1381 */
1382 public void addSummaryComment(Doc doc, Content htmltree) {
1383 addSummaryComment(doc, doc.firstSentenceTags(), htmltree);
1384 }
1386 /**
1387 * Adds the summary content.
1388 *
1389 * @param doc the doc for which the summary will be generated
1390 * @param firstSentenceTags the first sentence tags for the doc
1391 * @param htmltree the documentation tree to which the summary will be added
1392 */
1393 public void addSummaryComment(Doc doc, Tag[] firstSentenceTags, Content htmltree) {
1394 addCommentTags(doc, firstSentenceTags, false, true, htmltree);
1395 }
1397 public void addSummaryDeprecatedComment(Doc doc, Tag tag, Content htmltree) {
1398 addCommentTags(doc, tag.firstSentenceTags(), true, true, htmltree);
1399 }
1401 /**
1402 * Adds the inline comment.
1403 *
1404 * @param doc the doc for which the inline comments will be generated
1405 * @param htmltree the documentation tree to which the inline comments will be added
1406 */
1407 public void addInlineComment(Doc doc, Content htmltree) {
1408 addCommentTags(doc, doc.inlineTags(), false, false, htmltree);
1409 }
1411 /**
1412 * Adds the comment tags.
1413 *
1414 * @param doc the doc for which the comment tags will be generated
1415 * @param tags the first sentence tags for the doc
1416 * @param depr true if it is deprecated
1417 * @param first true if the first sentence tags should be added
1418 * @param htmltree the documentation tree to which the comment tags will be added
1419 */
1420 private void addCommentTags(Doc doc, Tag[] tags, boolean depr,
1421 boolean first, Content htmltree) {
1422 if(configuration.nocomment){
1423 return;
1424 }
1425 Content div;
1426 Content result = new RawHtml(commentTagsToString(null, doc, tags, first));
1427 if (depr) {
1428 Content italic = HtmlTree.I(result);
1429 div = HtmlTree.DIV(HtmlStyle.block, italic);
1430 htmltree.addContent(div);
1431 }
1432 else {
1433 div = HtmlTree.DIV(HtmlStyle.block, result);
1434 htmltree.addContent(div);
1435 }
1436 if (tags.length == 0) {
1437 htmltree.addContent(getSpace());
1438 }
1439 }
1441 /**
1442 * Converts inline tags and text to text strings, expanding the
1443 * inline tags along the way. Called wherever text can contain
1444 * an inline tag, such as in comments or in free-form text arguments
1445 * to non-inline tags.
1446 *
1447 * @param holderTag specific tag where comment resides
1448 * @param doc specific doc where comment resides
1449 * @param tags array of text tags and inline tags (often alternating)
1450 * present in the text of interest for this doc
1451 * @param isFirstSentence true if text is first sentence
1452 */
1453 public String commentTagsToString(Tag holderTag, Doc doc, Tag[] tags,
1454 boolean isFirstSentence) {
1455 StringBuilder result = new StringBuilder();
1456 boolean textTagChange = false;
1457 // Array of all possible inline tags for this javadoc run
1458 configuration.tagletManager.checkTags(doc, tags, true);
1459 for (int i = 0; i < tags.length; i++) {
1460 Tag tagelem = tags[i];
1461 String tagName = tagelem.name();
1462 if (tagelem instanceof SeeTag) {
1463 result.append(seeTagToString((SeeTag)tagelem));
1464 } else if (! tagName.equals("Text")) {
1465 int originalLength = result.length();
1466 TagletOutput output = TagletWriter.getInlineTagOuput(
1467 configuration.tagletManager, holderTag,
1468 tagelem, getTagletWriterInstance(isFirstSentence));
1469 result.append(output == null ? "" : output.toString());
1470 if (originalLength == 0 && isFirstSentence && tagelem.name().equals("@inheritDoc") && result.length() > 0) {
1471 break;
1472 } else if (configuration.docrootparent.length() > 0 &&
1473 tagelem.name().equals("@docRoot") &&
1474 ((tags[i + 1]).text()).startsWith("/..")) {
1475 //If Xdocrootparent switch ON, set the flag to remove the /.. occurance after
1476 //{@docRoot} tag in the very next Text tag.
1477 textTagChange = true;
1478 continue;
1479 } else {
1480 continue;
1481 }
1482 } else {
1483 String text = tagelem.text();
1484 //If Xdocrootparent switch ON, remove the /.. occurance after {@docRoot} tag.
1485 if (textTagChange) {
1486 text = text.replaceFirst("/..", "");
1487 textTagChange = false;
1488 }
1489 //This is just a regular text tag. The text may contain html links (<a>)
1490 //or inline tag {@docRoot}, which will be handled as special cases.
1491 text = redirectRelativeLinks(tagelem.holder(), text);
1493 // Replace @docRoot only if not represented by an instance of DocRootTaglet,
1494 // that is, only if it was not present in a source file doc comment.
1495 // This happens when inserted by the doclet (a few lines
1496 // above in this method). [It might also happen when passed in on the command
1497 // line as a text argument to an option (like -header).]
1498 text = replaceDocRootDir(text);
1499 if (isFirstSentence) {
1500 text = removeNonInlineHtmlTags(text);
1501 }
1502 StringTokenizer lines = new StringTokenizer(text, "\r\n", true);
1503 StringBuilder textBuff = new StringBuilder();
1504 while (lines.hasMoreTokens()) {
1505 StringBuilder line = new StringBuilder(lines.nextToken());
1506 Util.replaceTabs(configuration, line);
1507 textBuff.append(line.toString());
1508 }
1509 result.append(textBuff);
1510 }
1511 }
1512 return result.toString();
1513 }
1515 /**
1516 * Return true if relative links should not be redirected.
1517 *
1518 * @return Return true if a relative link should not be redirected.
1519 */
1520 private boolean shouldNotRedirectRelativeLinks() {
1521 return this instanceof AnnotationTypeWriter ||
1522 this instanceof ClassWriter ||
1523 this instanceof PackageSummaryWriter;
1524 }
1526 /**
1527 * Suppose a piece of documentation has a relative link. When you copy
1528 * that documentation to another place such as the index or class-use page,
1529 * that relative link will no longer work. We should redirect those links
1530 * so that they will work again.
1531 * <p>
1532 * Here is the algorithm used to fix the link:
1533 * <p>
1534 * {@literal <relative link> => docRoot + <relative path to file> + <relative link> }
1535 * <p>
1536 * For example, suppose com.sun.javadoc.RootDoc has this link:
1537 * {@literal <a href="package-summary.html">The package Page</a> }
1538 * <p>
1539 * If this link appeared in the index, we would redirect
1540 * the link like this:
1541 *
1542 * {@literal <a href="./com/sun/javadoc/package-summary.html">The package Page</a>}
1543 *
1544 * @param doc the Doc object whose documentation is being written.
1545 * @param text the text being written.
1546 *
1547 * @return the text, with all the relative links redirected to work.
1548 */
1549 private String redirectRelativeLinks(Doc doc, String text) {
1550 if (doc == null || shouldNotRedirectRelativeLinks()) {
1551 return text;
1552 }
1554 DocPath redirectPathFromRoot;
1555 if (doc instanceof ClassDoc) {
1556 redirectPathFromRoot = DocPath.forPackage(((ClassDoc) doc).containingPackage());
1557 } else if (doc instanceof MemberDoc) {
1558 redirectPathFromRoot = DocPath.forPackage(((MemberDoc) doc).containingPackage());
1559 } else if (doc instanceof PackageDoc) {
1560 redirectPathFromRoot = DocPath.forPackage((PackageDoc) doc);
1561 } else {
1562 return text;
1563 }
1565 //Redirect all relative links.
1566 int end, begin = text.toLowerCase().indexOf("<a");
1567 if(begin >= 0){
1568 StringBuilder textBuff = new StringBuilder(text);
1570 while(begin >=0){
1571 if (textBuff.length() > begin + 2 && ! Character.isWhitespace(textBuff.charAt(begin+2))) {
1572 begin = textBuff.toString().toLowerCase().indexOf("<a", begin + 1);
1573 continue;
1574 }
1576 begin = textBuff.indexOf("=", begin) + 1;
1577 end = textBuff.indexOf(">", begin +1);
1578 if(begin == 0){
1579 //Link has no equal symbol.
1580 configuration.root.printWarning(
1581 doc.position(),
1582 configuration.getText("doclet.malformed_html_link_tag", text));
1583 break;
1584 }
1585 if (end == -1) {
1586 //Break without warning. This <a> tag is not necessarily malformed. The text
1587 //might be missing '>' character because the href has an inline tag.
1588 break;
1589 }
1590 if (textBuff.substring(begin, end).indexOf("\"") != -1){
1591 begin = textBuff.indexOf("\"", begin) + 1;
1592 end = textBuff.indexOf("\"", begin +1);
1593 if (begin == 0 || end == -1){
1594 //Link is missing a quote.
1595 break;
1596 }
1597 }
1598 String relativeLink = textBuff.substring(begin, end);
1599 if (!(relativeLink.toLowerCase().startsWith("mailto:") ||
1600 relativeLink.toLowerCase().startsWith("http:") ||
1601 relativeLink.toLowerCase().startsWith("https:") ||
1602 relativeLink.toLowerCase().startsWith("file:"))) {
1603 relativeLink = "{@"+(new DocRootTaglet()).getName() + "}/"
1604 + redirectPathFromRoot.resolve(relativeLink).getPath();
1605 textBuff.replace(begin, end, relativeLink);
1606 }
1607 begin = textBuff.toString().toLowerCase().indexOf("<a", begin + 1);
1608 }
1609 return textBuff.toString();
1610 }
1611 return text;
1612 }
1614 public String removeNonInlineHtmlTags(String text) {
1615 if (text.indexOf('<') < 0) {
1616 return text;
1617 }
1618 String noninlinetags[] = { "<ul>", "</ul>", "<ol>", "</ol>",
1619 "<dl>", "</dl>", "<table>", "</table>",
1620 "<tr>", "</tr>", "<td>", "</td>",
1621 "<th>", "</th>", "<p>", "</p>",
1622 "<li>", "</li>", "<dd>", "</dd>",
1623 "<dir>", "</dir>", "<dt>", "</dt>",
1624 "<h1>", "</h1>", "<h2>", "</h2>",
1625 "<h3>", "</h3>", "<h4>", "</h4>",
1626 "<h5>", "</h5>", "<h6>", "</h6>",
1627 "<pre>", "</pre>", "<menu>", "</menu>",
1628 "<listing>", "</listing>", "<hr>",
1629 "<blockquote>", "</blockquote>",
1630 "<center>", "</center>",
1631 "<UL>", "</UL>", "<OL>", "</OL>",
1632 "<DL>", "</DL>", "<TABLE>", "</TABLE>",
1633 "<TR>", "</TR>", "<TD>", "</TD>",
1634 "<TH>", "</TH>", "<P>", "</P>",
1635 "<LI>", "</LI>", "<DD>", "</DD>",
1636 "<DIR>", "</DIR>", "<DT>", "</DT>",
1637 "<H1>", "</H1>", "<H2>", "</H2>",
1638 "<H3>", "</H3>", "<H4>", "</H4>",
1639 "<H5>", "</H5>", "<H6>", "</H6>",
1640 "<PRE>", "</PRE>", "<MENU>", "</MENU>",
1641 "<LISTING>", "</LISTING>", "<HR>",
1642 "<BLOCKQUOTE>", "</BLOCKQUOTE>",
1643 "<CENTER>", "</CENTER>"
1644 };
1645 for (int i = 0; i < noninlinetags.length; i++) {
1646 text = replace(text, noninlinetags[i], "");
1647 }
1648 return text;
1649 }
1651 public String replace(String text, String tobe, String by) {
1652 while (true) {
1653 int startindex = text.indexOf(tobe);
1654 if (startindex < 0) {
1655 return text;
1656 }
1657 int endindex = startindex + tobe.length();
1658 StringBuilder replaced = new StringBuilder();
1659 if (startindex > 0) {
1660 replaced.append(text.substring(0, startindex));
1661 }
1662 replaced.append(by);
1663 if (text.length() > endindex) {
1664 replaced.append(text.substring(endindex));
1665 }
1666 text = replaced.toString();
1667 }
1668 }
1670 /**
1671 * Returns a link to the stylesheet file.
1672 *
1673 * @return an HtmlTree for the lINK tag which provides the stylesheet location
1674 */
1675 public HtmlTree getStyleSheetProperties() {
1676 String stylesheetfile = configuration.stylesheetfile;
1677 DocPath stylesheet;
1678 if (stylesheetfile.isEmpty()) {
1679 stylesheet = DocPaths.STYLESHEET;
1680 } else {
1681 DocFile file = DocFile.createFileForInput(configuration, stylesheetfile);
1682 stylesheet = DocPath.create(file.getName());
1683 }
1684 HtmlTree link = HtmlTree.LINK("stylesheet", "text/css",
1685 pathToRoot.resolve(stylesheet).getPath(),
1686 "Style");
1687 return link;
1688 }
1690 /**
1691 * According to
1692 * <cite>The Java™ Language Specification</cite>,
1693 * all the outer classes and static nested classes are core classes.
1694 */
1695 public boolean isCoreClass(ClassDoc cd) {
1696 return cd.containingClass() == null || cd.isStatic();
1697 }
1699 /**
1700 * Adds the annotatation types for the given packageDoc.
1701 *
1702 * @param packageDoc the package to write annotations for.
1703 * @param htmltree the documentation tree to which the annotation info will be
1704 * added
1705 */
1706 public void addAnnotationInfo(PackageDoc packageDoc, Content htmltree) {
1707 addAnnotationInfo(packageDoc, packageDoc.annotations(), htmltree);
1708 }
1710 /**
1711 * Adds the annotatation types for the given doc.
1712 *
1713 * @param doc the package to write annotations for
1714 * @param htmltree the content tree to which the annotation types will be added
1715 */
1716 public void addAnnotationInfo(ProgramElementDoc doc, Content htmltree) {
1717 addAnnotationInfo(doc, doc.annotations(), htmltree);
1718 }
1720 /**
1721 * Add the annotatation types for the given doc and parameter.
1722 *
1723 * @param indent the number of spaces to indent the parameters.
1724 * @param doc the doc to write annotations for.
1725 * @param param the parameter to write annotations for.
1726 * @param tree the content tree to which the annotation types will be added
1727 */
1728 public boolean addAnnotationInfo(int indent, Doc doc, Parameter param,
1729 Content tree) {
1730 return addAnnotationInfo(indent, doc, param.annotations(), false, tree);
1731 }
1733 /**
1734 * Adds the annotatation types for the given doc.
1735 *
1736 * @param doc the doc to write annotations for.
1737 * @param descList the array of {@link AnnotationDesc}.
1738 * @param htmltree the documentation tree to which the annotation info will be
1739 * added
1740 */
1741 private void addAnnotationInfo(Doc doc, AnnotationDesc[] descList,
1742 Content htmltree) {
1743 addAnnotationInfo(0, doc, descList, true, htmltree);
1744 }
1746 /**
1747 * Adds the annotatation types for the given doc.
1748 *
1749 * @param indent the number of extra spaces to indent the annotations.
1750 * @param doc the doc to write annotations for.
1751 * @param descList the array of {@link AnnotationDesc}.
1752 * @param htmltree the documentation tree to which the annotation info will be
1753 * added
1754 */
1755 private boolean addAnnotationInfo(int indent, Doc doc,
1756 AnnotationDesc[] descList, boolean lineBreak, Content htmltree) {
1757 List<String> annotations = getAnnotations(indent, descList, lineBreak);
1758 if (annotations.size() == 0) {
1759 return false;
1760 }
1761 Content annotationContent;
1762 for (Iterator<String> iter = annotations.iterator(); iter.hasNext();) {
1763 annotationContent = new RawHtml(iter.next());
1764 htmltree.addContent(annotationContent);
1765 }
1766 return true;
1767 }
1769 /**
1770 * Return the string representations of the annotation types for
1771 * the given doc.
1772 *
1773 * @param indent the number of extra spaces to indent the annotations.
1774 * @param descList the array of {@link AnnotationDesc}.
1775 * @param linkBreak if true, add new line between each member value.
1776 * @return an array of strings representing the annotations being
1777 * documented.
1778 */
1779 private List<String> getAnnotations(int indent, AnnotationDesc[] descList, boolean linkBreak) {
1780 List<String> results = new ArrayList<String>();
1781 StringBuilder annotation;
1782 for (int i = 0; i < descList.length; i++) {
1783 AnnotationTypeDoc annotationDoc = descList[i].annotationType();
1784 if (! Util.isDocumentedAnnotation(annotationDoc)){
1785 continue;
1786 }
1787 annotation = new StringBuilder();
1788 LinkInfoImpl linkInfo = new LinkInfoImpl(configuration,
1789 LinkInfoImpl.CONTEXT_ANNOTATION, annotationDoc);
1790 linkInfo.label = "@" + annotationDoc.name();
1791 annotation.append(getLink(linkInfo));
1792 AnnotationDesc.ElementValuePair[] pairs = descList[i].elementValues();
1793 if (pairs.length > 0) {
1794 annotation.append('(');
1795 for (int j = 0; j < pairs.length; j++) {
1796 if (j > 0) {
1797 annotation.append(",");
1798 if (linkBreak) {
1799 annotation.append(DocletConstants.NL);
1800 int spaces = annotationDoc.name().length() + 2;
1801 for (int k = 0; k < (spaces + indent); k++) {
1802 annotation.append(' ');
1803 }
1804 }
1805 }
1806 annotation.append(getDocLink(LinkInfoImpl.CONTEXT_ANNOTATION,
1807 pairs[j].element(), pairs[j].element().name(), false));
1808 annotation.append('=');
1809 AnnotationValue annotationValue = pairs[j].value();
1810 List<AnnotationValue> annotationTypeValues = new ArrayList<AnnotationValue>();
1811 if (annotationValue.value() instanceof AnnotationValue[]) {
1812 AnnotationValue[] annotationArray =
1813 (AnnotationValue[]) annotationValue.value();
1814 for (int k = 0; k < annotationArray.length; k++) {
1815 annotationTypeValues.add(annotationArray[k]);
1816 }
1817 } else {
1818 annotationTypeValues.add(annotationValue);
1819 }
1820 annotation.append(annotationTypeValues.size() == 1 ? "" : "{");
1821 for (Iterator<AnnotationValue> iter = annotationTypeValues.iterator(); iter.hasNext(); ) {
1822 annotation.append(annotationValueToString(iter.next()));
1823 annotation.append(iter.hasNext() ? "," : "");
1824 }
1825 annotation.append(annotationTypeValues.size() == 1 ? "" : "}");
1826 }
1827 annotation.append(")");
1828 }
1829 annotation.append(linkBreak ? DocletConstants.NL : "");
1830 results.add(annotation.toString());
1831 }
1832 return results;
1833 }
1835 private String annotationValueToString(AnnotationValue annotationValue) {
1836 if (annotationValue.value() instanceof Type) {
1837 Type type = (Type) annotationValue.value();
1838 if (type.asClassDoc() != null) {
1839 LinkInfoImpl linkInfo = new LinkInfoImpl(configuration,
1840 LinkInfoImpl.CONTEXT_ANNOTATION, type);
1841 linkInfo.label = (type.asClassDoc().isIncluded() ?
1842 type.typeName() :
1843 type.qualifiedTypeName()) + type.dimension() + ".class";
1844 return getLink(linkInfo);
1845 } else {
1846 return type.typeName() + type.dimension() + ".class";
1847 }
1848 } else if (annotationValue.value() instanceof AnnotationDesc) {
1849 List<String> list = getAnnotations(0,
1850 new AnnotationDesc[]{(AnnotationDesc) annotationValue.value()},
1851 false);
1852 StringBuilder buf = new StringBuilder();
1853 for (String s: list) {
1854 buf.append(s);
1855 }
1856 return buf.toString();
1857 } else if (annotationValue.value() instanceof MemberDoc) {
1858 return getDocLink(LinkInfoImpl.CONTEXT_ANNOTATION,
1859 (MemberDoc) annotationValue.value(),
1860 ((MemberDoc) annotationValue.value()).name(), false);
1861 } else {
1862 return annotationValue.toString();
1863 }
1864 }
1866 /**
1867 * Return the configuation for this doclet.
1868 *
1869 * @return the configuration for this doclet.
1870 */
1871 public Configuration configuration() {
1872 return configuration;
1873 }
1874 }