1 /*
   2  * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.javadoc.internal.doclets.formats.html;
  27 
  28 import java.io.*;
  29 
  30 import java.util.Arrays;
  31 import java.util.List;
  32 
  33 import javax.lang.model.element.Element;
  34 import javax.lang.model.element.ExecutableElement;
  35 import javax.lang.model.element.TypeElement;
  36 import javax.lang.model.type.TypeMirror;
  37 
  38 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlConstants;
  39 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
  40 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
  41 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
  42 import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
  43 import jdk.javadoc.internal.doclets.toolkit.AnnotationTypeFieldWriter;
  44 import jdk.javadoc.internal.doclets.toolkit.Content;
  45 import jdk.javadoc.internal.doclets.toolkit.MemberSummaryWriter;
  46 
  47 
  48 /**
  49  * Writes annotation type field documentation in HTML format.
  50  *
  51  *  <p><b>This is NOT part of any supported API.
  52  *  If you write code that depends on this, you do so at your own risk.
  53  *  This code and its internal interfaces are subject to change or
  54  *  deletion without notice.</b>
  55  *
  56  * @author Bhavesh Patel
  57  */
  58 public class AnnotationTypeFieldWriterImpl extends AbstractMemberWriter
  59     implements AnnotationTypeFieldWriter, MemberSummaryWriter {
  60 
  61     /**
  62      * Construct a new AnnotationTypeFieldWriterImpl.
  63      *
  64      * @param writer         the writer that will write the output.
  65      * @param annotationType the AnnotationType that holds this member.
  66      */
  67     public AnnotationTypeFieldWriterImpl(SubWriterHolderWriter writer,
  68             TypeElement annotationType) {
  69         super(writer, annotationType);
  70     }
  71 
  72     /**
  73      * {@inheritDoc}
  74      */
  75     public Content getMemberSummaryHeader(TypeElement typeElement,
  76             Content memberSummaryTree) {
  77         memberSummaryTree.addContent(
  78                 HtmlConstants.START_OF_ANNOTATION_TYPE_FIELD_SUMMARY);
  79         Content memberTree = writer.getMemberTreeHeader();
  80         writer.addSummaryHeader(this, typeElement, memberTree);
  81         return memberTree;
  82     }
  83 
  84     /**
  85      * {@inheritDoc}
  86      */
  87     public Content getMemberTreeHeader() {
  88         return writer.getMemberTreeHeader();
  89     }
  90 
  91     /**
  92      * {@inheritDoc}
  93      */
  94     public void addMemberTree(Content memberSummaryTree, Content memberTree) {
  95         writer.addMemberTree(memberSummaryTree, memberTree);
  96     }
  97 
  98     /**
  99      * {@inheritDoc}
 100      */
 101     public void addAnnotationFieldDetailsMarker(Content memberDetails) {
 102         memberDetails.addContent(HtmlConstants.START_OF_ANNOTATION_TYPE_FIELD_DETAILS);
 103     }
 104 
 105     /**
 106      * {@inheritDoc}
 107      */
 108     public void addAnnotationDetailsTreeHeader(TypeElement typeElement,
 109             Content memberDetailsTree) {
 110         if (!writer.printedAnnotationFieldHeading) {
 111             memberDetailsTree.addContent(writer.getMarkerAnchor(
 112                     SectionName.ANNOTATION_TYPE_FIELD_DETAIL));
 113             Content heading = HtmlTree.HEADING(HtmlConstants.DETAILS_HEADING,
 114                     writer.fieldDetailsLabel);
 115             memberDetailsTree.addContent(heading);
 116             writer.printedAnnotationFieldHeading = true;
 117         }
 118     }
 119 
 120     /**
 121      * {@inheritDoc}
 122      */
 123     public Content getAnnotationDocTreeHeader(Element member,
 124             Content annotationDetailsTree) {
 125         annotationDetailsTree.addContent(
 126                 writer.getMarkerAnchor(name(member)));
 127         Content annotationDocTree = writer.getMemberTreeHeader();
 128         Content heading = new HtmlTree(HtmlConstants.MEMBER_HEADING);
 129         heading.addContent(name(member));
 130         annotationDocTree.addContent(heading);
 131         return annotationDocTree;
 132     }
 133 
 134     /**
 135      * {@inheritDoc}
 136      */
 137     public Content getSignature(Element member) {
 138         Content pre = new HtmlTree(HtmlTag.PRE);
 139         writer.addAnnotationInfo(member, pre);
 140         addModifiers(member, pre);
 141         Content link =
 142                 writer.getLink(new LinkInfoImpl(configuration,
 143                         LinkInfoImpl.Kind.MEMBER, getType(member)));
 144         pre.addContent(link);
 145         pre.addContent(writer.getSpace());
 146         if (configuration.linksource) {
 147             Content memberName = new StringContent(name(member));
 148             writer.addSrcLink(member, memberName, pre);
 149         } else {
 150             addName(name(member), pre);
 151         }
 152         return pre;
 153     }
 154 
 155     /**
 156      * {@inheritDoc}
 157      */
 158     public void addDeprecated(Element member, Content annotationDocTree) {
 159         addDeprecatedInfo(member, annotationDocTree);
 160     }
 161 
 162     /**
 163      * {@inheritDoc}
 164      */
 165     public void addComments(Element member, Content annotationDocTree) {
 166         addComment(member, annotationDocTree);
 167     }
 168 
 169     /**
 170      * {@inheritDoc}
 171      */
 172     public void addTags(Element member, Content annotationDocTree) {
 173         writer.addTagsInfo(member, annotationDocTree);
 174     }
 175 
 176     /**
 177      * {@inheritDoc}
 178      */
 179     public Content getAnnotationDetails(Content annotationDetailsTree) {
 180         if (configuration.allowTag(HtmlTag.SECTION)) {
 181             HtmlTree htmlTree = HtmlTree.SECTION(getMemberTree(annotationDetailsTree));
 182             return htmlTree;
 183         }
 184         return getMemberTree(annotationDetailsTree);
 185     }
 186 
 187     /**
 188      * {@inheritDoc}
 189      */
 190     public Content getAnnotationDoc(Content annotationDocTree,
 191             boolean isLastContent) {
 192         return getMemberTree(annotationDocTree, isLastContent);
 193     }
 194 
 195     /**
 196      * Close the writer.
 197      */
 198     public void close() throws IOException {
 199         writer.close();
 200     }
 201 
 202     /**
 203      * {@inheritDoc}
 204      */
 205     public void addSummaryLabel(Content memberTree) {
 206         Content label = HtmlTree.HEADING(HtmlConstants.SUMMARY_HEADING,
 207                 writer.getResource("doclet.Field_Summary"));
 208         memberTree.addContent(label);
 209     }
 210 
 211     /**
 212      * {@inheritDoc}
 213      */
 214     public String getTableSummary() {
 215         return configuration.getText("doclet.Member_Table_Summary",
 216                 configuration.getText("doclet.Field_Summary"),
 217                 configuration.getText("doclet.fields"));
 218     }
 219 
 220     /**
 221      * {@inheritDoc}
 222      */
 223     public Content getCaption() {
 224         return configuration.getResource("doclet.Fields");
 225     }
 226 
 227     /**
 228      * {@inheritDoc}
 229      */
 230     public List<String> getSummaryTableHeader(Element member) {
 231         List<String> header = Arrays.asList(writer.getModifierTypeHeader(),
 232                 configuration.getText("doclet.0_and_1",
 233                         configuration.getText("doclet.Fields"),
 234                         configuration.getText("doclet.Description")));
 235         return header;
 236     }
 237 
 238     /**
 239      * {@inheritDoc}
 240      */
 241     public void addSummaryAnchor(TypeElement typeElement, Content memberTree) {
 242         memberTree.addContent(writer.getMarkerAnchor(
 243                 SectionName.ANNOTATION_TYPE_FIELD_SUMMARY));
 244     }
 245 
 246     /**
 247      * {@inheritDoc}
 248      */
 249     public void addInheritedSummaryAnchor(TypeElement typeElement, Content inheritedTree) {
 250     }
 251 
 252     /**
 253      * {@inheritDoc}
 254      */
 255     public void addInheritedSummaryLabel(TypeElement typeElement, Content inheritedTree) {
 256     }
 257 
 258     /**
 259      * {@inheritDoc}
 260      */
 261     protected void addSummaryLink(LinkInfoImpl.Kind context, TypeElement typeElement, Element member,
 262             Content tdSummary) {
 263         Content memberLink = HtmlTree.SPAN(HtmlStyle.memberNameLink,
 264                 writer.getDocLink(context, member, name(member), false));
 265         Content code = HtmlTree.CODE(memberLink);
 266         tdSummary.addContent(code);
 267     }
 268 
 269     /**
 270      * {@inheritDoc}
 271      */
 272     protected void addInheritedSummaryLink(TypeElement typeElement,
 273             Element member, Content linksTree) {
 274         //Not applicable.
 275     }
 276 
 277     /**
 278      * {@inheritDoc}
 279      */
 280     protected void addSummaryType(Element member, Content tdSummaryType) {
 281         addModifierAndType(member, getType(member), tdSummaryType);
 282     }
 283 
 284     /**
 285      * {@inheritDoc}
 286      */
 287     protected Content getDeprecatedLink(Element member) {
 288         return writer.getDocLink(LinkInfoImpl.Kind.MEMBER,
 289                 member, utils.getFullyQualifiedName(member));
 290     }
 291 
 292     /**
 293      * {@inheritDoc}
 294      */
 295     protected Content getNavSummaryLink(TypeElement typeElement, boolean link) {
 296         if (link) {
 297             return writer.getHyperLink(
 298                     SectionName.ANNOTATION_TYPE_FIELD_SUMMARY,
 299                     writer.getResource("doclet.navField"));
 300         } else {
 301             return writer.getResource("doclet.navField");
 302         }
 303     }
 304 
 305     /**
 306      * {@inheritDoc}
 307      */
 308     protected void addNavDetailLink(boolean link, Content liNav) {
 309         if (link) {
 310             liNav.addContent(writer.getHyperLink(
 311                     SectionName.ANNOTATION_TYPE_FIELD_DETAIL,
 312                     writer.getResource("doclet.navField")));
 313         } else {
 314             liNav.addContent(writer.getResource("doclet.navField"));
 315         }
 316     }
 317     private TypeMirror getType(Element member) {
 318         if (utils.isConstructor(member))
 319             return null;
 320         if (utils.isExecutableElement(member))
 321             return utils.getReturnType((ExecutableElement)member);
 322         return member.asType();
 323     }
 324 }