1 /*
   2  * Copyright (c) 1997, 2019, 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.util.Arrays;
  29 import java.util.List;
  30 
  31 import javax.lang.model.element.Element;
  32 import javax.lang.model.element.TypeElement;
  33 import javax.lang.model.element.VariableElement;
  34 
  35 import jdk.javadoc.internal.doclets.formats.html.markup.ContentBuilder;
  36 import jdk.javadoc.internal.doclets.formats.html.markup.Entity;
  37 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
  38 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
  39 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
  40 import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
  41 import jdk.javadoc.internal.doclets.formats.html.markup.Table;
  42 import jdk.javadoc.internal.doclets.formats.html.markup.TableHeader;
  43 import jdk.javadoc.internal.doclets.toolkit.Content;
  44 import jdk.javadoc.internal.doclets.toolkit.FieldWriter;
  45 import jdk.javadoc.internal.doclets.toolkit.MemberSummaryWriter;
  46 
  47 /**
  48  * Writes field documentation in HTML format.
  49  *
  50  *  <p><b>This is NOT part of any supported API.
  51  *  If you write code that depends on this, you do so at your own risk.
  52  *  This code and its internal interfaces are subject to change or
  53  *  deletion without notice.</b>
  54  *
  55  * @author Robert Field
  56  * @author Atul M Dambalkar
  57  * @author Jamie Ho (rewrite)
  58  * @author Bhavesh Patel (Modified)
  59  */
  60 public class FieldWriterImpl extends AbstractMemberWriter
  61     implements FieldWriter, MemberSummaryWriter {
  62 
  63     public FieldWriterImpl(SubWriterHolderWriter writer, TypeElement typeElement) {
  64         super(writer, typeElement);
  65     }
  66 
  67     public FieldWriterImpl(SubWriterHolderWriter writer) {
  68         super(writer);
  69     }
  70 
  71     /**
  72      * {@inheritDoc}
  73      */
  74     @Override
  75     public Content getMemberSummaryHeader(TypeElement typeElement,
  76             Content memberSummaryTree) {
  77         memberSummaryTree.add(MarkerComments.START_OF_FIELD_SUMMARY);
  78         Content memberTree = new ContentBuilder();
  79         writer.addSummaryHeader(this, typeElement, memberTree);
  80         return memberTree;
  81     }
  82 
  83     /**
  84      * {@inheritDoc}
  85      */
  86     @Override
  87     public void addMemberTree(Content memberSummaryTree, Content memberTree) {
  88         writer.addMemberTree(HtmlStyle.fieldSummary, memberSummaryTree, memberTree);
  89     }
  90 
  91     /**
  92      * {@inheritDoc}
  93      */
  94     @Override
  95     public Content getFieldDetailsTreeHeader(TypeElement typeElement, Content memberDetailsTree) {
  96         memberDetailsTree.add(MarkerComments.START_OF_FIELD_DETAILS);
  97         Content fieldDetailsTree = new ContentBuilder();
  98         Content heading = HtmlTree.HEADING(Headings.TypeDeclaration.DETAILS_HEADING,
  99                 contents.fieldDetailsLabel);
 100         fieldDetailsTree.add(heading);
 101         fieldDetailsTree.add(links.createAnchor(
 102                 SectionName.FIELD_DETAIL));
 103         return fieldDetailsTree;
 104     }
 105 
 106     /**
 107      * {@inheritDoc}
 108      */
 109     @Override
 110     public Content getFieldDocTreeHeader(VariableElement field, Content fieldDetailsTree) {
 111         Content fieldTree = new ContentBuilder();
 112         Content heading = new HtmlTree(Headings.TypeDeclaration.MEMBER_HEADING);
 113         heading.add(name(field));
 114         fieldTree.add(heading);
 115         fieldTree.add(links.createAnchor(name(field)));
 116         return HtmlTree.SECTION(HtmlStyle.detail, fieldTree);
 117     }
 118 
 119     /**
 120      * {@inheritDoc}
 121      */
 122     @Override
 123     public Content getSignature(VariableElement field) {
 124         Content pre = new HtmlTree(HtmlTag.PRE);
 125         writer.addAnnotationInfo(field, pre);
 126         addModifiers(field, pre);
 127         Content fieldlink = writer.getLink(new LinkInfoImpl(
 128                 configuration, LinkInfoImpl.Kind.MEMBER, field.asType()));
 129         pre.add(fieldlink);
 130         pre.add(" ");
 131         if (configuration.linksource) {
 132             Content fieldName = new StringContent(name(field));
 133             writer.addSrcLink(field, fieldName, pre);
 134         } else {
 135             addName(name(field), pre);
 136         }
 137         return pre;
 138     }
 139 
 140     /**
 141      * {@inheritDoc}
 142      */
 143     @Override
 144     public void addDeprecated(VariableElement field, Content fieldTree) {
 145         addDeprecatedInfo(field, fieldTree);
 146     }
 147 
 148     /**
 149      * {@inheritDoc}
 150      */
 151     @Override
 152     public void addComments(VariableElement field, Content fieldTree) {
 153         if (!utils.getFullBody(field).isEmpty()) {
 154             writer.addInlineComment(field, fieldTree);
 155         }
 156     }
 157 
 158     /**
 159      * {@inheritDoc}
 160      */
 161     @Override
 162     public void addTags(VariableElement field, Content fieldTree) {
 163         writer.addTagsInfo(field, fieldTree);
 164     }
 165 
 166     /**
 167      * {@inheritDoc}
 168      */
 169     @Override
 170     public Content getFieldDetails(Content fieldDetailsTreeHeader, Content fieldDetailsTree) {
 171         Content fieldDetails = new ContentBuilder(fieldDetailsTreeHeader, fieldDetailsTree);
 172         return getMemberTree(HtmlTree.SECTION(HtmlStyle.fieldDetails, fieldDetails));
 173     }
 174 
 175     /**
 176      * {@inheritDoc}
 177      */
 178     @Override
 179     public Content getFieldDoc(Content fieldTree,
 180             boolean isLastContent) {
 181         return getMemberTree(fieldTree, isLastContent);
 182     }
 183 
 184     /**
 185      * {@inheritDoc}
 186      */
 187     @Override
 188     public void addSummaryLabel(Content memberTree) {
 189         Content label = HtmlTree.HEADING(Headings.TypeDeclaration.SUMMARY_HEADING,
 190                 contents.fieldSummaryLabel);
 191         memberTree.add(label);
 192     }
 193 
 194     /**
 195      * {@inheritDoc}
 196      */
 197     @Override
 198     public TableHeader getSummaryTableHeader(Element member) {
 199         return new TableHeader(contents.modifierAndTypeLabel, contents.fieldLabel,
 200                 contents.descriptionLabel);
 201     }
 202 
 203     @Override
 204     protected Table createSummaryTable() {
 205         List<HtmlStyle> bodyRowStyles = Arrays.asList(HtmlStyle.colFirst, HtmlStyle.colSecond,
 206                 HtmlStyle.colLast);
 207 
 208         return new Table(HtmlStyle.memberSummary)
 209                 .setCaption(contents.fields)
 210                 .setHeader(getSummaryTableHeader(typeElement))
 211                 .setRowScopeColumn(1)
 212                 .setColumnStyles(bodyRowStyles);
 213     }
 214 
 215     /**
 216      * {@inheritDoc}
 217      */
 218     @Override
 219     public void addSummaryAnchor(TypeElement typeElement, Content memberTree) {
 220         memberTree.add(links.createAnchor(
 221                 SectionName.FIELD_SUMMARY));
 222     }
 223 
 224     /**
 225      * {@inheritDoc}
 226      */
 227     @Override
 228     public void addInheritedSummaryAnchor(TypeElement typeElement, Content inheritedTree) {
 229         inheritedTree.add(links.createAnchor(
 230                 SectionName.FIELDS_INHERITANCE, configuration.getClassName(typeElement)));
 231     }
 232 
 233     /**
 234      * {@inheritDoc}
 235      */
 236     @Override
 237     public void addInheritedSummaryLabel(TypeElement typeElement, Content inheritedTree) {
 238         Content classLink = writer.getPreQualifiedClassLink(
 239                 LinkInfoImpl.Kind.MEMBER, typeElement, false);
 240         Content label;
 241         if (configuration.summarizeOverriddenMethods) {
 242             label = new StringContent(utils.isClass(typeElement)
 243                     ? resources.getText("doclet.Fields_Declared_In_Class")
 244                     : resources.getText("doclet.Fields_Declared_In_Interface"));
 245         } else {
 246             label = new StringContent(utils.isClass(typeElement)
 247                     ? resources.getText("doclet.Fields_Inherited_From_Class")
 248                     : resources.getText("doclet.Fields_Inherited_From_Interface"));
 249         }
 250         Content labelHeading = HtmlTree.HEADING(Headings.TypeDeclaration.INHERITED_SUMMARY_HEADING,
 251                 label);
 252         labelHeading.add(Entity.NO_BREAK_SPACE);
 253         labelHeading.add(classLink);
 254         inheritedTree.add(labelHeading);
 255     }
 256 
 257     /**
 258      * {@inheritDoc}
 259      */
 260     @Override
 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, typeElement , member, name(member), false));
 265         Content code = HtmlTree.CODE(memberLink);
 266         tdSummary.add(code);
 267     }
 268 
 269     /**
 270      * {@inheritDoc}
 271      */
 272     @Override
 273     protected void addInheritedSummaryLink(TypeElement typeElement, Element member, Content linksTree) {
 274         linksTree.add(
 275                 writer.getDocLink(LinkInfoImpl.Kind.MEMBER, typeElement, member,
 276                 name(member), false));
 277     }
 278 
 279     /**
 280      * {@inheritDoc}
 281      */
 282     @Override
 283     protected void addSummaryType(Element member, Content tdSummaryType) {
 284         addModifierAndType(member, member.asType(), tdSummaryType);
 285     }
 286 
 287     /**
 288      * {@inheritDoc}
 289      */
 290     @Override
 291     protected Content getDeprecatedLink(Element member) {
 292         String name = utils.getFullyQualifiedName(member) + "." + member.getSimpleName();
 293         return writer.getDocLink(LinkInfoImpl.Kind.MEMBER, member, name);
 294     }
 295 
 296     @Override
 297     public Content getMemberTreeHeader(){
 298         return writer.getMemberTreeHeader();
 299     }
 300 }