Tue, 16 Oct 2012 21:03:36 -0700
8000673: remove dead code from HtmlWriter and subtypes
Reviewed-by: bpatel
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 boolean printedSummaryHeader = false;
53 protected final SubWriterHolderWriter writer;
54 protected final ClassDoc classdoc;
55 public final boolean nodepr;
57 public AbstractMemberWriter(SubWriterHolderWriter writer,
58 ClassDoc classdoc) {
59 this.writer = writer;
60 this.nodepr = configuration().nodeprecated;
61 this.classdoc = classdoc;
62 }
64 public AbstractMemberWriter(SubWriterHolderWriter writer) {
65 this(writer, null);
66 }
68 /*** abstracts ***/
70 /**
71 * Add the summary label for the member.
72 *
73 * @param memberTree the content tree to which the label will be added
74 */
75 public abstract void addSummaryLabel(Content memberTree);
77 /**
78 * Get the summary for the member summary table.
79 *
80 * @return a string for the table summary
81 */
82 public abstract String getTableSummary();
84 /**
85 * Get the caption for the member summary table.
86 *
87 * @return a string for the table caption
88 */
89 public abstract String getCaption();
91 /**
92 * Get the summary table header for the member.
93 *
94 * @param member the member to be documented
95 * @return the summary table header
96 */
97 public abstract String[] getSummaryTableHeader(ProgramElementDoc member);
99 /**
100 * Add inherited summary lable for the member.
101 *
102 * @param cd the class doc to which to link to
103 * @param inheritedTree the content tree to which the inherited summary label will be added
104 */
105 public abstract void addInheritedSummaryLabel(ClassDoc cd, Content inheritedTree);
107 /**
108 * Add the anchor for the summary section of the member.
109 *
110 * @param cd the class doc to be documented
111 * @param memberTree the content tree to which the summary anchor will be added
112 */
113 public abstract void addSummaryAnchor(ClassDoc cd, Content memberTree);
115 /**
116 * Add the anchor for the inherited summary section of the member.
117 *
118 * @param cd the class doc to be documented
119 * @param inheritedTree the content tree to which the inherited summary anchor will be added
120 */
121 public abstract void addInheritedSummaryAnchor(ClassDoc cd, Content inheritedTree);
123 /**
124 * Add the summary type for the member.
125 *
126 * @param member the member to be documented
127 * @param tdSummaryType the content tree to which the type will be added
128 */
129 protected abstract void addSummaryType(ProgramElementDoc member,
130 Content tdSummaryType);
132 /**
133 * Add the summary link for the member.
134 *
135 * @param cd the class doc to be documented
136 * @param member the member to be documented
137 * @param tdSummary the content tree to which the link will be added
138 */
139 protected void addSummaryLink(ClassDoc cd, ProgramElementDoc member,
140 Content tdSummary) {
141 addSummaryLink(LinkInfoImpl.CONTEXT_MEMBER, cd, member, tdSummary);
142 }
144 /**
145 * Add the summary link for the member.
146 *
147 * @param context the id of the context where the link will be printed
148 * @param cd the class doc to be documented
149 * @param member the member to be documented
150 * @param tdSummary the content tree to which the summary link will be added
151 */
152 protected abstract void addSummaryLink(int context,
153 ClassDoc cd, ProgramElementDoc member, Content tdSummary);
155 /**
156 * Add the inherited summary link for the member.
157 *
158 * @param cd the class doc to be documented
159 * @param member the member to be documented
160 * @param linksTree the content tree to which the inherited summary link will be added
161 */
162 protected abstract void addInheritedSummaryLink(ClassDoc cd,
163 ProgramElementDoc member, Content linksTree);
165 /**
166 * Get the deprecated link.
167 *
168 * @param member the member being linked to
169 * @return a content tree representing the link
170 */
171 protected abstract Content getDeprecatedLink(ProgramElementDoc member);
173 /**
174 * Get the navigation summary link.
175 *
176 * @param cd the class doc to be documented
177 * @param link true if its a link else the label to be printed
178 * @return a content tree for the navigation summary link.
179 */
180 protected abstract Content getNavSummaryLink(ClassDoc cd, boolean link);
182 /**
183 * Add the navigation detail link.
184 *
185 * @param link true if its a link else the label to be printed
186 * @param liNav the content tree to which the navigation detail link will be added
187 */
188 protected abstract void addNavDetailLink(boolean link, Content liNav);
190 /**
191 * Add the member name to the content tree and modifies the display length.
192 *
193 * @param name the member name to be added to the content tree.
194 * @param htmltree the content tree to which the name will be added.
195 */
196 protected void addName(String name, Content htmltree) {
197 htmltree.addContent(name);
198 writer.displayLength += name.length();
199 }
201 /**
202 * Return a string describing the access modifier flags.
203 * Don't include native or synchronized.
204 *
205 * The modifier names are returned in canonical order, as
206 * specified by <em>The Java Language Specification</em>.
207 */
208 protected String modifierString(MemberDoc member) {
209 int ms = member.modifierSpecifier();
210 int no = Modifier.NATIVE | Modifier.SYNCHRONIZED;
211 return Modifier.toString(ms & ~no);
212 }
214 protected String typeString(MemberDoc member) {
215 String type = "";
216 if (member instanceof MethodDoc) {
217 type = ((MethodDoc)member).returnType().toString();
218 } else if (member instanceof FieldDoc) {
219 type = ((FieldDoc)member).type().toString();
220 }
221 return type;
222 }
224 /**
225 * Add the modifier for the member.
226 *
227 * @param member the member for which teh modifier will be added.
228 * @param htmltree the content tree to which the modifier information will be added.
229 */
230 protected void addModifiers(MemberDoc member, Content htmltree) {
231 String mod = modifierString(member);
232 // According to JLS, we should not be showing public modifier for
233 // interface methods.
234 if ((member.isField() || member.isMethod()) &&
235 writer instanceof ClassWriterImpl &&
236 ((ClassWriterImpl) writer).getClassDoc().isInterface()) {
237 mod = Util.replaceText(mod, "public", "").trim();
238 }
239 if(mod.length() > 0) {
240 htmltree.addContent(mod);
241 htmltree.addContent(writer.getSpace());
242 }
243 }
245 protected String makeSpace(int len) {
246 if (len <= 0) {
247 return "";
248 }
249 StringBuilder sb = new StringBuilder(len);
250 for(int i = 0; i < len; i++) {
251 sb.append(' ');
252 }
253 return sb.toString();
254 }
256 /**
257 * Add the modifier and type for the member in the member summary.
258 *
259 * @param member the member to add the type for
260 * @param type the type to add
261 * @param tdSummaryType the content tree to which the modified and type will be added
262 */
263 protected void addModifierAndType(ProgramElementDoc member, Type type,
264 Content tdSummaryType) {
265 HtmlTree code = new HtmlTree(HtmlTag.CODE);
266 addModifier(member, code);
267 if (type == null) {
268 if (member.isClass()) {
269 code.addContent("class");
270 } else {
271 code.addContent("interface");
272 }
273 code.addContent(writer.getSpace());
274 } else {
275 if (member instanceof ExecutableMemberDoc &&
276 ((ExecutableMemberDoc) member).typeParameters().length > 0) {
277 //Code to avoid ugly wrapping in member summary table.
278 int displayLength = ((AbstractExecutableMemberWriter) this).addTypeParameters(
279 (ExecutableMemberDoc) member, code);
280 if (displayLength > 10) {
281 code.addContent(new HtmlTree(HtmlTag.BR));
282 }
283 code.addContent(new RawHtml(
284 writer.getLink(new LinkInfoImpl(
285 LinkInfoImpl.CONTEXT_SUMMARY_RETURN_TYPE, type))));
286 } else {
287 code.addContent(new RawHtml(
288 writer.getLink(new LinkInfoImpl(
289 LinkInfoImpl.CONTEXT_SUMMARY_RETURN_TYPE, type))));
290 }
292 }
293 tdSummaryType.addContent(code);
294 }
296 /**
297 * Add the modifier for the member.
298 *
299 * @param member the member to add the type for
300 * @param code the content tree to which the modified will be added
301 */
302 private void addModifier(ProgramElementDoc member, Content code) {
303 if (member.isProtected()) {
304 code.addContent("protected ");
305 } else if (member.isPrivate()) {
306 code.addContent("private ");
307 } else if (!member.isPublic()) { // Package private
308 code.addContent(configuration().getText("doclet.Package_private"));
309 code.addContent(" ");
310 }
311 if (member.isMethod() && ((MethodDoc)member).isAbstract()) {
312 code.addContent("abstract ");
313 }
314 if (member.isStatic()) {
315 code.addContent("static ");
316 }
317 }
319 /**
320 * Add the deprecated information for the given member.
321 *
322 * @param member the member being documented.
323 * @param contentTree the content tree to which the deprecated information will be added.
324 */
325 protected void addDeprecatedInfo(ProgramElementDoc member, Content contentTree) {
326 String output = (new DeprecatedTaglet()).getTagletOutput(member,
327 writer.getTagletWriterInstance(false)).toString().trim();
328 if (!output.isEmpty()) {
329 Content deprecatedContent = new RawHtml(output);
330 Content div = HtmlTree.DIV(HtmlStyle.block, deprecatedContent);
331 contentTree.addContent(div);
332 }
333 }
335 /**
336 * Add the comment for the given member.
337 *
338 * @param member the member being documented.
339 * @param htmltree the content tree to which the comment will be added.
340 */
341 protected void addComment(ProgramElementDoc member, Content htmltree) {
342 if (member.inlineTags().length > 0) {
343 writer.addInlineComment(member, htmltree);
344 }
345 }
347 protected String name(ProgramElementDoc member) {
348 return member.name();
349 }
351 /**
352 * Get the header for the section.
353 *
354 * @param member the member being documented.
355 * @return a header content for the section.
356 */
357 protected Content getHead(MemberDoc member) {
358 Content memberContent = new RawHtml(member.name());
359 Content heading = HtmlTree.HEADING(HtmlConstants.MEMBER_HEADING, memberContent);
360 return heading;
361 }
363 /**
364 * Return true if the given <code>ProgramElement</code> is inherited
365 * by the class that is being documented.
366 *
367 * @param ped The <code>ProgramElement</code> being checked.
368 * return true if the <code>ProgramElement</code> is being inherited and
369 * false otherwise.
370 */
371 protected boolean isInherited(ProgramElementDoc ped){
372 if(ped.isPrivate() || (ped.isPackagePrivate() &&
373 ! ped.containingPackage().equals(classdoc.containingPackage()))){
374 return false;
375 }
376 return true;
377 }
379 /**
380 * Add deprecated information to the documentation tree
381 *
382 * @param deprmembers list of deprecated members
383 * @param headingKey the caption for the deprecated members table
384 * @param tableSummary the summary for the deprecated members table
385 * @param tableHeader table headers for the deprecated members table
386 * @param contentTree the content tree to which the deprecated members table will be added
387 */
388 protected void addDeprecatedAPI(List<Doc> deprmembers, String headingKey,
389 String tableSummary, String[] tableHeader, Content contentTree) {
390 if (deprmembers.size() > 0) {
391 Content table = HtmlTree.TABLE(0, 3, 0, tableSummary,
392 writer.getTableCaption(configuration().getText(headingKey)));
393 table.addContent(writer.getSummaryTableHeader(tableHeader, "col"));
394 Content tbody = new HtmlTree(HtmlTag.TBODY);
395 for (int i = 0; i < deprmembers.size(); i++) {
396 ProgramElementDoc member =(ProgramElementDoc)deprmembers.get(i);
397 HtmlTree td = HtmlTree.TD(HtmlStyle.colOne, getDeprecatedLink(member));
398 if (member.tags("deprecated").length > 0)
399 writer.addInlineDeprecatedComment(member,
400 member.tags("deprecated")[0], td);
401 HtmlTree tr = HtmlTree.TR(td);
402 if (i%2 == 0)
403 tr.addStyle(HtmlStyle.altColor);
404 else
405 tr.addStyle(HtmlStyle.rowColor);
406 tbody.addContent(tr);
407 }
408 table.addContent(tbody);
409 Content li = HtmlTree.LI(HtmlStyle.blockList, table);
410 Content ul = HtmlTree.UL(HtmlStyle.blockList, li);
411 contentTree.addContent(ul);
412 }
413 }
415 /**
416 * Add use information to the documentation tree.
417 *
418 * @param mems list of program elements for which the use information will be added
419 * @param heading the section heading
420 * @param tableSummary the summary for the use table
421 * @param contentTree the content tree to which the use information will be added
422 */
423 protected void addUseInfo(List<? extends ProgramElementDoc> mems,
424 String heading, String tableSummary, Content contentTree) {
425 if (mems == null) {
426 return;
427 }
428 List<? extends ProgramElementDoc> members = mems;
429 boolean printedUseTableHeader = false;
430 if (members.size() > 0) {
431 Content table = HtmlTree.TABLE(0, 3, 0, tableSummary,
432 writer.getTableCaption(heading));
433 Content tbody = new HtmlTree(HtmlTag.TBODY);
434 Iterator<? extends ProgramElementDoc> it = members.iterator();
435 for (int i = 0; it.hasNext(); i++) {
436 ProgramElementDoc pgmdoc = it.next();
437 ClassDoc cd = pgmdoc.containingClass();
438 if (!printedUseTableHeader) {
439 table.addContent(writer.getSummaryTableHeader(
440 this.getSummaryTableHeader(pgmdoc), "col"));
441 printedUseTableHeader = true;
442 }
443 HtmlTree tr = new HtmlTree(HtmlTag.TR);
444 if (i % 2 == 0) {
445 tr.addStyle(HtmlStyle.altColor);
446 } else {
447 tr.addStyle(HtmlStyle.rowColor);
448 }
449 HtmlTree tdFirst = new HtmlTree(HtmlTag.TD);
450 tdFirst.addStyle(HtmlStyle.colFirst);
451 writer.addSummaryType(this, pgmdoc, tdFirst);
452 tr.addContent(tdFirst);
453 HtmlTree tdLast = new HtmlTree(HtmlTag.TD);
454 tdLast.addStyle(HtmlStyle.colLast);
455 if (cd != null && !(pgmdoc instanceof ConstructorDoc)
456 && !(pgmdoc instanceof ClassDoc)) {
457 HtmlTree name = new HtmlTree(HtmlTag.SPAN);
458 name.addStyle(HtmlStyle.strong);
459 name.addContent(cd.name() + ".");
460 tdLast.addContent(name);
461 }
462 addSummaryLink(pgmdoc instanceof ClassDoc ?
463 LinkInfoImpl.CONTEXT_CLASS_USE : LinkInfoImpl.CONTEXT_MEMBER,
464 cd, pgmdoc, tdLast);
465 writer.addSummaryLinkComment(this, pgmdoc, tdLast);
466 tr.addContent(tdLast);
467 tbody.addContent(tr);
468 }
469 table.addContent(tbody);
470 contentTree.addContent(table);
471 }
472 }
474 /**
475 * Add the navigation detail link.
476 *
477 * @param members the members to be linked
478 * @param liNav the content tree to which the navigation detail link will be added
479 */
480 protected void addNavDetailLink(List<?> members, Content liNav) {
481 addNavDetailLink(members.size() > 0 ? true : false, liNav);
482 }
484 /**
485 * Add the navigation summary link.
486 *
487 * @param members members to be linked
488 * @param visibleMemberMap the visible inherited members map
489 * @param liNav the content tree to which the navigation summary link will be added
490 */
491 protected void addNavSummaryLink(List<?> members,
492 VisibleMemberMap visibleMemberMap, Content liNav) {
493 if (members.size() > 0) {
494 liNav.addContent(getNavSummaryLink(null, true));
495 return;
496 }
497 ClassDoc icd = classdoc.superclass();
498 while (icd != null) {
499 List<?> inhmembers = visibleMemberMap.getMembersFor(icd);
500 if (inhmembers.size() > 0) {
501 liNav.addContent(getNavSummaryLink(icd, true));
502 return;
503 }
504 icd = icd.superclass();
505 }
506 liNav.addContent(getNavSummaryLink(null, false));
507 }
509 protected void serialWarning(SourcePosition pos, String key, String a1, String a2) {
510 if (configuration().serialwarn) {
511 ConfigurationImpl.getInstance().getDocletSpecificMsg().warning(pos, key, a1, a2);
512 }
513 }
515 public ProgramElementDoc[] eligibleMembers(ProgramElementDoc[] members) {
516 return nodepr? Util.excludeDeprecatedMembers(members): members;
517 }
519 public ConfigurationImpl configuration() {
520 return writer.configuration;
521 }
523 /**
524 * Add the member summary for the given class.
525 *
526 * @param classDoc the class that is being documented
527 * @param member the member being documented
528 * @param firstSentenceTags the first sentence tags to be added to the summary
529 * @param tableTree the content tree to which the documentation will be added
530 * @param counter the counter for determing style for the table row
531 */
532 public void addMemberSummary(ClassDoc classDoc, ProgramElementDoc member,
533 Tag[] firstSentenceTags, Content tableTree, int counter) {
534 HtmlTree tdSummaryType = new HtmlTree(HtmlTag.TD);
535 tdSummaryType.addStyle(HtmlStyle.colFirst);
536 writer.addSummaryType(this, member, tdSummaryType);
537 HtmlTree tdSummary = new HtmlTree(HtmlTag.TD);
538 setSummaryColumnStyle(tdSummary);
539 addSummaryLink(classDoc, member, tdSummary);
540 writer.addSummaryLinkComment(this, member, firstSentenceTags, tdSummary);
541 HtmlTree tr = HtmlTree.TR(tdSummaryType);
542 tr.addContent(tdSummary);
543 if (counter%2 == 0)
544 tr.addStyle(HtmlStyle.altColor);
545 else
546 tr.addStyle(HtmlStyle.rowColor);
547 tableTree.addContent(tr);
548 }
550 /**
551 * Set the style for the summary column.
552 *
553 * @param tdTree the column for which the style will be set
554 */
555 public void setSummaryColumnStyle(HtmlTree tdTree) {
556 tdTree.addStyle(HtmlStyle.colLast);
557 }
559 /**
560 * Add inherited member summary for the given class and member.
561 *
562 * @param classDoc the class the inherited member belongs to
563 * @param nestedClass the inherited member that is summarized
564 * @param isFirst true if this is the first member in the list
565 * @param isLast true if this is the last member in the list
566 * @param linksTree the content tree to which the summary will be added
567 */
568 public void addInheritedMemberSummary(ClassDoc classDoc,
569 ProgramElementDoc nestedClass, boolean isFirst, boolean isLast,
570 Content linksTree) {
571 writer.addInheritedMemberSummary(this, classDoc, nestedClass, isFirst,
572 linksTree);
573 }
575 /**
576 * Get the inherited summary header for the given class.
577 *
578 * @param classDoc the class the inherited member belongs to
579 * @return a content tree for the inherited summary header
580 */
581 public Content getInheritedSummaryHeader(ClassDoc classDoc) {
582 Content inheritedTree = writer.getMemberTreeHeader();
583 writer.addInheritedSummaryHeader(this, classDoc, inheritedTree);
584 return inheritedTree;
585 }
587 /**
588 * Get the inherited summary links tree.
589 *
590 * @return a content tree for the inherited summary links
591 */
592 public Content getInheritedSummaryLinksTree() {
593 return new HtmlTree(HtmlTag.CODE);
594 }
596 /**
597 * Get the summary table tree for the given class.
598 *
599 * @param classDoc the class for which the summary table is generated
600 * @return a content tree for the summary table
601 */
602 public Content getSummaryTableTree(ClassDoc classDoc) {
603 return writer.getSummaryTableTree(this, classDoc);
604 }
606 /**
607 * Get the member tree to be documented.
608 *
609 * @param memberTree the content tree of member to be documented
610 * @return a content tree that will be added to the class documentation
611 */
612 public Content getMemberTree(Content memberTree) {
613 return writer.getMemberTree(memberTree);
614 }
616 /**
617 * Get the member tree to be documented.
618 *
619 * @param memberTree the content tree of member to be documented
620 * @param isLastContent true if the content to be added is the last content
621 * @return a content tree that will be added to the class documentation
622 */
623 public Content getMemberTree(Content memberTree, boolean isLastContent) {
624 if (isLastContent)
625 return HtmlTree.UL(HtmlStyle.blockListLast, memberTree);
626 else
627 return HtmlTree.UL(HtmlStyle.blockList, memberTree);
628 }
629 }