Thu, 15 Nov 2012 19:54:20 -0800
8002079: update DocFile to use a JavaFileManager
Reviewed-by: darcy
1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.doclets.formats.html;
28 import java.lang.reflect.Modifier;
29 import java.util.*;
31 import com.sun.javadoc.*;
32 import com.sun.tools.doclets.formats.html.markup.*;
33 import com.sun.tools.doclets.internal.toolkit.*;
34 import com.sun.tools.doclets.internal.toolkit.taglets.*;
35 import com.sun.tools.doclets.internal.toolkit.util.*;
37 /**
38 * The base class for member writers.
39 *
40 * <p><b>This is NOT part of any supported API.
41 * If you write code that depends on this, you do so at your own risk.
42 * This code and its internal interfaces are subject to change or
43 * deletion without notice.</b>
44 *
45 * @author Robert Field
46 * @author Atul M Dambalkar
47 * @author Jamie Ho (Re-write)
48 * @author Bhavesh Patel (Modified)
49 */
50 public abstract class AbstractMemberWriter {
52 protected final ConfigurationImpl configuration;
53 protected final SubWriterHolderWriter writer;
54 protected final ClassDoc classdoc;
55 public final boolean nodepr;
57 protected boolean printedSummaryHeader = false;
59 public AbstractMemberWriter(SubWriterHolderWriter writer, ClassDoc classdoc) {
60 this.configuration = writer.configuration;
61 this.writer = writer;
62 this.nodepr = configuration.nodeprecated;
63 this.classdoc = classdoc;
64 }
66 public AbstractMemberWriter(SubWriterHolderWriter writer) {
67 this(writer, null);
68 }
70 /*** abstracts ***/
72 /**
73 * Add the summary label for the member.
74 *
75 * @param memberTree the content tree to which the label will be added
76 */
77 public abstract void addSummaryLabel(Content memberTree);
79 /**
80 * Get the summary for the member summary table.
81 *
82 * @return a string for the table summary
83 */
84 public abstract String getTableSummary();
86 /**
87 * Get the caption for the member summary table.
88 *
89 * @return a string for the table caption
90 */
91 public abstract String getCaption();
93 /**
94 * Get the summary table header for the member.
95 *
96 * @param member the member to be documented
97 * @return the summary table header
98 */
99 public abstract String[] getSummaryTableHeader(ProgramElementDoc member);
101 /**
102 * Add inherited summary lable for the member.
103 *
104 * @param cd the class doc to which to link to
105 * @param inheritedTree the content tree to which the inherited summary label will be added
106 */
107 public abstract void addInheritedSummaryLabel(ClassDoc cd, Content inheritedTree);
109 /**
110 * Add the anchor for the summary section of the member.
111 *
112 * @param cd the class doc to be documented
113 * @param memberTree the content tree to which the summary anchor will be added
114 */
115 public abstract void addSummaryAnchor(ClassDoc cd, Content memberTree);
117 /**
118 * Add the anchor for the inherited summary section of the member.
119 *
120 * @param cd the class doc to be documented
121 * @param inheritedTree the content tree to which the inherited summary anchor will be added
122 */
123 public abstract void addInheritedSummaryAnchor(ClassDoc cd, Content inheritedTree);
125 /**
126 * Add the summary type for the member.
127 *
128 * @param member the member to be documented
129 * @param tdSummaryType the content tree to which the type will be added
130 */
131 protected abstract void addSummaryType(ProgramElementDoc member,
132 Content tdSummaryType);
134 /**
135 * Add the summary link for the member.
136 *
137 * @param cd the class doc to be documented
138 * @param member the member to be documented
139 * @param tdSummary the content tree to which the link will be added
140 */
141 protected void addSummaryLink(ClassDoc cd, ProgramElementDoc member,
142 Content tdSummary) {
143 addSummaryLink(LinkInfoImpl.CONTEXT_MEMBER, cd, member, tdSummary);
144 }
146 /**
147 * Add the summary link for the member.
148 *
149 * @param context the id of the context where the link will be printed
150 * @param cd the class doc to be documented
151 * @param member the member to be documented
152 * @param tdSummary the content tree to which the summary link will be added
153 */
154 protected abstract void addSummaryLink(int context,
155 ClassDoc cd, ProgramElementDoc member, Content tdSummary);
157 /**
158 * Add the inherited summary link for the member.
159 *
160 * @param cd the class doc to be documented
161 * @param member the member to be documented
162 * @param linksTree the content tree to which the inherited summary link will be added
163 */
164 protected abstract void addInheritedSummaryLink(ClassDoc cd,
165 ProgramElementDoc member, Content linksTree);
167 /**
168 * Get the deprecated link.
169 *
170 * @param member the member being linked to
171 * @return a content tree representing the link
172 */
173 protected abstract Content getDeprecatedLink(ProgramElementDoc member);
175 /**
176 * Get the navigation summary link.
177 *
178 * @param cd the class doc to be documented
179 * @param link true if its a link else the label to be printed
180 * @return a content tree for the navigation summary link.
181 */
182 protected abstract Content getNavSummaryLink(ClassDoc cd, boolean link);
184 /**
185 * Add the navigation detail link.
186 *
187 * @param link true if its a link else the label to be printed
188 * @param liNav the content tree to which the navigation detail link will be added
189 */
190 protected abstract void addNavDetailLink(boolean link, Content liNav);
192 /**
193 * Add the member name to the content tree and modifies the display length.
194 *
195 * @param name the member name to be added to the content tree.
196 * @param htmltree the content tree to which the name will be added.
197 */
198 protected void addName(String name, Content htmltree) {
199 htmltree.addContent(name);
200 writer.displayLength += name.length();
201 }
203 /**
204 * Return a string describing the access modifier flags.
205 * Don't include native or synchronized.
206 *
207 * The modifier names are returned in canonical order, as
208 * specified by <em>The Java Language Specification</em>.
209 */
210 protected String modifierString(MemberDoc member) {
211 int ms = member.modifierSpecifier();
212 int no = Modifier.NATIVE | Modifier.SYNCHRONIZED;
213 return Modifier.toString(ms & ~no);
214 }
216 protected String typeString(MemberDoc member) {
217 String type = "";
218 if (member instanceof MethodDoc) {
219 type = ((MethodDoc)member).returnType().toString();
220 } else if (member instanceof FieldDoc) {
221 type = ((FieldDoc)member).type().toString();
222 }
223 return type;
224 }
226 /**
227 * Add the modifier for the member.
228 *
229 * @param member the member for which teh modifier will be added.
230 * @param htmltree the content tree to which the modifier information will be added.
231 */
232 protected void addModifiers(MemberDoc member, Content htmltree) {
233 String mod = modifierString(member);
234 // According to JLS, we should not be showing public modifier for
235 // interface methods.
236 if ((member.isField() || member.isMethod()) &&
237 writer instanceof ClassWriterImpl &&
238 ((ClassWriterImpl) writer).getClassDoc().isInterface()) {
239 mod = Util.replaceText(mod, "public", "").trim();
240 }
241 if(mod.length() > 0) {
242 htmltree.addContent(mod);
243 htmltree.addContent(writer.getSpace());
244 }
245 }
247 protected String makeSpace(int len) {
248 if (len <= 0) {
249 return "";
250 }
251 StringBuilder sb = new StringBuilder(len);
252 for(int i = 0; i < len; i++) {
253 sb.append(' ');
254 }
255 return sb.toString();
256 }
258 /**
259 * Add the modifier and type for the member in the member summary.
260 *
261 * @param member the member to add the type for
262 * @param type the type to add
263 * @param tdSummaryType the content tree to which the modified and type will be added
264 */
265 protected void addModifierAndType(ProgramElementDoc member, Type type,
266 Content tdSummaryType) {
267 HtmlTree code = new HtmlTree(HtmlTag.CODE);
268 addModifier(member, code);
269 if (type == null) {
270 if (member.isClass()) {
271 code.addContent("class");
272 } else {
273 code.addContent("interface");
274 }
275 code.addContent(writer.getSpace());
276 } else {
277 if (member instanceof ExecutableMemberDoc &&
278 ((ExecutableMemberDoc) member).typeParameters().length > 0) {
279 //Code to avoid ugly wrapping in member summary table.
280 int displayLength = ((AbstractExecutableMemberWriter) this).addTypeParameters(
281 (ExecutableMemberDoc) member, code);
282 if (displayLength > 10) {
283 code.addContent(new HtmlTree(HtmlTag.BR));
284 }
285 code.addContent(new RawHtml(
286 writer.getLink(new LinkInfoImpl(configuration,
287 LinkInfoImpl.CONTEXT_SUMMARY_RETURN_TYPE, type))));
288 } else {
289 code.addContent(new RawHtml(
290 writer.getLink(new LinkInfoImpl(configuration,
291 LinkInfoImpl.CONTEXT_SUMMARY_RETURN_TYPE, type))));
292 }
294 }
295 tdSummaryType.addContent(code);
296 }
298 /**
299 * Add the modifier for the member.
300 *
301 * @param member the member to add the type for
302 * @param code the content tree to which the modified will be added
303 */
304 private void addModifier(ProgramElementDoc member, Content code) {
305 if (member.isProtected()) {
306 code.addContent("protected ");
307 } else if (member.isPrivate()) {
308 code.addContent("private ");
309 } else if (!member.isPublic()) { // Package private
310 code.addContent(configuration.getText("doclet.Package_private"));
311 code.addContent(" ");
312 }
313 if (member.isMethod() && ((MethodDoc)member).isAbstract()) {
314 code.addContent("abstract ");
315 }
316 if (member.isStatic()) {
317 code.addContent("static ");
318 }
319 }
321 /**
322 * Add the deprecated information for the given member.
323 *
324 * @param member the member being documented.
325 * @param contentTree the content tree to which the deprecated information will be added.
326 */
327 protected void addDeprecatedInfo(ProgramElementDoc member, Content contentTree) {
328 String output = (new DeprecatedTaglet()).getTagletOutput(member,
329 writer.getTagletWriterInstance(false)).toString().trim();
330 if (!output.isEmpty()) {
331 Content deprecatedContent = new RawHtml(output);
332 Content div = HtmlTree.DIV(HtmlStyle.block, deprecatedContent);
333 contentTree.addContent(div);
334 }
335 }
337 /**
338 * Add the comment for the given member.
339 *
340 * @param member the member being documented.
341 * @param htmltree the content tree to which the comment will be added.
342 */
343 protected void addComment(ProgramElementDoc member, Content htmltree) {
344 if (member.inlineTags().length > 0) {
345 writer.addInlineComment(member, htmltree);
346 }
347 }
349 protected String name(ProgramElementDoc member) {
350 return member.name();
351 }
353 /**
354 * Get the header for the section.
355 *
356 * @param member the member being documented.
357 * @return a header content for the section.
358 */
359 protected Content getHead(MemberDoc member) {
360 Content memberContent = new RawHtml(member.name());
361 Content heading = HtmlTree.HEADING(HtmlConstants.MEMBER_HEADING, memberContent);
362 return heading;
363 }
365 /**
366 * Return true if the given <code>ProgramElement</code> is inherited
367 * by the class that is being documented.
368 *
369 * @param ped The <code>ProgramElement</code> being checked.
370 * return true if the <code>ProgramElement</code> is being inherited and
371 * false otherwise.
372 */
373 protected boolean isInherited(ProgramElementDoc ped){
374 if(ped.isPrivate() || (ped.isPackagePrivate() &&
375 ! ped.containingPackage().equals(classdoc.containingPackage()))){
376 return false;
377 }
378 return true;
379 }
381 /**
382 * Add deprecated information to the documentation tree
383 *
384 * @param deprmembers list of deprecated members
385 * @param headingKey the caption for the deprecated members table
386 * @param tableSummary the summary for the deprecated members table
387 * @param tableHeader table headers for the deprecated members table
388 * @param contentTree the content tree to which the deprecated members table will be added
389 */
390 protected void addDeprecatedAPI(List<Doc> deprmembers, String headingKey,
391 String tableSummary, String[] tableHeader, Content contentTree) {
392 if (deprmembers.size() > 0) {
393 Content table = HtmlTree.TABLE(0, 3, 0, tableSummary,
394 writer.getTableCaption(configuration.getText(headingKey)));
395 table.addContent(writer.getSummaryTableHeader(tableHeader, "col"));
396 Content tbody = new HtmlTree(HtmlTag.TBODY);
397 for (int i = 0; i < deprmembers.size(); i++) {
398 ProgramElementDoc member =(ProgramElementDoc)deprmembers.get(i);
399 HtmlTree td = HtmlTree.TD(HtmlStyle.colOne, getDeprecatedLink(member));
400 if (member.tags("deprecated").length > 0)
401 writer.addInlineDeprecatedComment(member,
402 member.tags("deprecated")[0], td);
403 HtmlTree tr = HtmlTree.TR(td);
404 if (i%2 == 0)
405 tr.addStyle(HtmlStyle.altColor);
406 else
407 tr.addStyle(HtmlStyle.rowColor);
408 tbody.addContent(tr);
409 }
410 table.addContent(tbody);
411 Content li = HtmlTree.LI(HtmlStyle.blockList, table);
412 Content ul = HtmlTree.UL(HtmlStyle.blockList, li);
413 contentTree.addContent(ul);
414 }
415 }
417 /**
418 * Add use information to the documentation tree.
419 *
420 * @param mems list of program elements for which the use information will be added
421 * @param heading the section heading
422 * @param tableSummary the summary for the use table
423 * @param contentTree the content tree to which the use information will be added
424 */
425 protected void addUseInfo(List<? extends ProgramElementDoc> mems,
426 String heading, String tableSummary, Content contentTree) {
427 if (mems == null) {
428 return;
429 }
430 List<? extends ProgramElementDoc> members = mems;
431 boolean printedUseTableHeader = false;
432 if (members.size() > 0) {
433 Content table = HtmlTree.TABLE(0, 3, 0, tableSummary,
434 writer.getTableCaption(heading));
435 Content tbody = new HtmlTree(HtmlTag.TBODY);
436 Iterator<? extends ProgramElementDoc> it = members.iterator();
437 for (int i = 0; it.hasNext(); i++) {
438 ProgramElementDoc pgmdoc = it.next();
439 ClassDoc cd = pgmdoc.containingClass();
440 if (!printedUseTableHeader) {
441 table.addContent(writer.getSummaryTableHeader(
442 this.getSummaryTableHeader(pgmdoc), "col"));
443 printedUseTableHeader = true;
444 }
445 HtmlTree tr = new HtmlTree(HtmlTag.TR);
446 if (i % 2 == 0) {
447 tr.addStyle(HtmlStyle.altColor);
448 } else {
449 tr.addStyle(HtmlStyle.rowColor);
450 }
451 HtmlTree tdFirst = new HtmlTree(HtmlTag.TD);
452 tdFirst.addStyle(HtmlStyle.colFirst);
453 writer.addSummaryType(this, pgmdoc, tdFirst);
454 tr.addContent(tdFirst);
455 HtmlTree tdLast = new HtmlTree(HtmlTag.TD);
456 tdLast.addStyle(HtmlStyle.colLast);
457 if (cd != null && !(pgmdoc instanceof ConstructorDoc)
458 && !(pgmdoc instanceof ClassDoc)) {
459 HtmlTree name = new HtmlTree(HtmlTag.SPAN);
460 name.addStyle(HtmlStyle.strong);
461 name.addContent(cd.name() + ".");
462 tdLast.addContent(name);
463 }
464 addSummaryLink(pgmdoc instanceof ClassDoc ?
465 LinkInfoImpl.CONTEXT_CLASS_USE : LinkInfoImpl.CONTEXT_MEMBER,
466 cd, pgmdoc, tdLast);
467 writer.addSummaryLinkComment(this, pgmdoc, tdLast);
468 tr.addContent(tdLast);
469 tbody.addContent(tr);
470 }
471 table.addContent(tbody);
472 contentTree.addContent(table);
473 }
474 }
476 /**
477 * Add the navigation detail link.
478 *
479 * @param members the members to be linked
480 * @param liNav the content tree to which the navigation detail link will be added
481 */
482 protected void addNavDetailLink(List<?> members, Content liNav) {
483 addNavDetailLink(members.size() > 0 ? true : false, liNav);
484 }
486 /**
487 * Add the navigation summary link.
488 *
489 * @param members members to be linked
490 * @param visibleMemberMap the visible inherited members map
491 * @param liNav the content tree to which the navigation summary link will be added
492 */
493 protected void addNavSummaryLink(List<?> members,
494 VisibleMemberMap visibleMemberMap, Content liNav) {
495 if (members.size() > 0) {
496 liNav.addContent(getNavSummaryLink(null, true));
497 return;
498 }
499 ClassDoc icd = classdoc.superclass();
500 while (icd != null) {
501 List<?> inhmembers = visibleMemberMap.getMembersFor(icd);
502 if (inhmembers.size() > 0) {
503 liNav.addContent(getNavSummaryLink(icd, true));
504 return;
505 }
506 icd = icd.superclass();
507 }
508 liNav.addContent(getNavSummaryLink(null, false));
509 }
511 protected void serialWarning(SourcePosition pos, String key, String a1, String a2) {
512 if (configuration.serialwarn) {
513 configuration.getDocletSpecificMsg().warning(pos, key, a1, a2);
514 }
515 }
517 public ProgramElementDoc[] eligibleMembers(ProgramElementDoc[] members) {
518 return nodepr? Util.excludeDeprecatedMembers(members): members;
519 }
521 /**
522 * Add the member summary for the given class.
523 *
524 * @param classDoc the class that is being documented
525 * @param member the member being documented
526 * @param firstSentenceTags the first sentence tags to be added to the summary
527 * @param tableTree the content tree to which the documentation will be added
528 * @param counter the counter for determing style for the table row
529 */
530 public void addMemberSummary(ClassDoc classDoc, ProgramElementDoc member,
531 Tag[] firstSentenceTags, Content tableTree, int counter) {
532 HtmlTree tdSummaryType = new HtmlTree(HtmlTag.TD);
533 tdSummaryType.addStyle(HtmlStyle.colFirst);
534 writer.addSummaryType(this, member, tdSummaryType);
535 HtmlTree tdSummary = new HtmlTree(HtmlTag.TD);
536 setSummaryColumnStyle(tdSummary);
537 addSummaryLink(classDoc, member, tdSummary);
538 writer.addSummaryLinkComment(this, member, firstSentenceTags, tdSummary);
539 HtmlTree tr = HtmlTree.TR(tdSummaryType);
540 tr.addContent(tdSummary);
541 if (counter%2 == 0)
542 tr.addStyle(HtmlStyle.altColor);
543 else
544 tr.addStyle(HtmlStyle.rowColor);
545 tableTree.addContent(tr);
546 }
548 /**
549 * Set the style for the summary column.
550 *
551 * @param tdTree the column for which the style will be set
552 */
553 public void setSummaryColumnStyle(HtmlTree tdTree) {
554 tdTree.addStyle(HtmlStyle.colLast);
555 }
557 /**
558 * Add inherited member summary for the given class and member.
559 *
560 * @param classDoc the class the inherited member belongs to
561 * @param nestedClass the inherited member that is summarized
562 * @param isFirst true if this is the first member in the list
563 * @param isLast true if this is the last member in the list
564 * @param linksTree the content tree to which the summary will be added
565 */
566 public void addInheritedMemberSummary(ClassDoc classDoc,
567 ProgramElementDoc nestedClass, boolean isFirst, boolean isLast,
568 Content linksTree) {
569 writer.addInheritedMemberSummary(this, classDoc, nestedClass, isFirst,
570 linksTree);
571 }
573 /**
574 * Get the inherited summary header for the given class.
575 *
576 * @param classDoc the class the inherited member belongs to
577 * @return a content tree for the inherited summary header
578 */
579 public Content getInheritedSummaryHeader(ClassDoc classDoc) {
580 Content inheritedTree = writer.getMemberTreeHeader();
581 writer.addInheritedSummaryHeader(this, classDoc, inheritedTree);
582 return inheritedTree;
583 }
585 /**
586 * Get the inherited summary links tree.
587 *
588 * @return a content tree for the inherited summary links
589 */
590 public Content getInheritedSummaryLinksTree() {
591 return new HtmlTree(HtmlTag.CODE);
592 }
594 /**
595 * Get the summary table tree for the given class.
596 *
597 * @param classDoc the class for which the summary table is generated
598 * @return a content tree for the summary table
599 */
600 public Content getSummaryTableTree(ClassDoc classDoc) {
601 return writer.getSummaryTableTree(this, classDoc);
602 }
604 /**
605 * Get the member tree to be documented.
606 *
607 * @param memberTree the content tree of member to be documented
608 * @return a content tree that will be added to the class documentation
609 */
610 public Content getMemberTree(Content memberTree) {
611 return writer.getMemberTree(memberTree);
612 }
614 /**
615 * Get the member tree to be documented.
616 *
617 * @param memberTree the content tree of member to be documented
618 * @param isLastContent true if the content to be added is the last content
619 * @return a content tree that will be added to the class documentation
620 */
621 public Content getMemberTree(Content memberTree, boolean isLastContent) {
622 if (isLastContent)
623 return HtmlTree.UL(HtmlStyle.blockListLast, memberTree);
624 else
625 return HtmlTree.UL(HtmlStyle.blockList, memberTree);
626 }
627 }