001/*
002 * Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package org.jdrupes.mdoclet.internal.doclets.formats.html;
027
028import java.util.*;
029
030import javax.lang.model.element.TypeElement;
031import javax.lang.model.element.VariableElement;
032import javax.lang.model.type.TypeMirror;
033
034import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.HtmlStyle;
035import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.HtmlTree;
036import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.TagName;
037import org.jdrupes.mdoclet.internal.doclets.formats.html.markup.Text;
038import org.jdrupes.mdoclet.internal.doclets.toolkit.Content;
039import org.jdrupes.mdoclet.internal.doclets.toolkit.SerializedFormWriter;
040
041import com.sun.source.doctree.DocTree;
042
043import com.sun.source.doctree.SerialFieldTree;
044import com.sun.source.doctree.SerialTree;
045
046/**
047 * Generate serialized form for serializable fields.
048 * Documentation denoted by the tags <code>serial</code> and
049 * <code>serialField</code> is processed.
050 */
051public class HtmlSerialFieldWriter extends FieldWriterImpl
052        implements SerializedFormWriter.SerialFieldWriter {
053
054    public HtmlSerialFieldWriter(SubWriterHolderWriter writer,
055            TypeElement typeElement) {
056        super(writer, typeElement);
057    }
058
059    public SortedSet<VariableElement> members(TypeElement te) {
060        return utils.serializableFields(te);
061    }
062
063    @Override
064    public Content getSerializableFieldsHeader() {
065        return HtmlTree.UL(HtmlStyle.blockList);
066    }
067
068    @Override
069    public Content getFieldsContentHeader(boolean isLastContent) {
070        return new HtmlTree(TagName.LI).setStyle(HtmlStyle.blockList);
071    }
072
073    @Override
074    public Content getSerializableFields(String heading, Content source) {
075        var section = HtmlTree.SECTION(HtmlStyle.detail);
076        if (!source.isEmpty()) {
077            Content headingContent = Text.of(heading);
078            var serialHeading = HtmlTree.HEADING(
079                Headings.SerializedForm.CLASS_SUBHEADING, headingContent);
080            section.add(serialHeading);
081            section.add(source);
082        }
083        return HtmlTree.LI(section);
084    }
085
086    @Override
087    public void addMemberHeader(TypeMirror fieldType, String fieldName,
088            Content content) {
089        Content nameContent = Text.of(fieldName);
090        var heading = HtmlTree.HEADING(Headings.SerializedForm.MEMBER_HEADING,
091            nameContent);
092        content.add(heading);
093        var pre = new HtmlTree(TagName.PRE);
094        Content fieldContent = writer.getLink(new HtmlLinkInfo(
095            configuration, HtmlLinkInfo.Kind.LINK_TYPE_PARAMS_AND_BOUNDS,
096            fieldType));
097        pre.add(fieldContent);
098        pre.add(" ");
099        pre.add(fieldName);
100        content.add(pre);
101    }
102
103    /**
104     * Add the deprecated information for this member.
105     *
106     * @param field the field to document.
107     * @param content the content to which the deprecated info will be added
108     */
109    @Override
110    public void addMemberDeprecatedInfo(VariableElement field,
111            Content content) {
112        addDeprecatedInfo(field, content);
113    }
114
115    /**
116     * Add the description text for this member.
117     *
118     * @param field the field to document.
119     * @param content the content to which the deprecated info will be added
120     */
121    @Override
122    public void addMemberDescription(VariableElement field, Content content) {
123        if (!utils.getFullBody(field).isEmpty()) {
124            writer.addInlineComment(field, content);
125        }
126        List<? extends SerialTree> tags = utils.getSerialTrees(field);
127        if (!tags.isEmpty() && !tags.get(0).getDescription().isEmpty()) {
128            writer.addInlineComment(field, tags.get(0), content);
129        }
130    }
131
132    /**
133     * Add the description text for this member represented by the tag.
134     *
135     * @param serialFieldTag the field to document (represented by tag)
136     * @param content the content to which the deprecated info will be added
137     */
138    @Override
139    public void addMemberDescription(VariableElement field,
140            SerialFieldTree serialFieldTag, Content content) {
141        List<? extends DocTree> description = serialFieldTag.getDescription();
142        if (!description.isEmpty()) {
143            Content serialFieldContent = writer.commentTagsToContent(field,
144                description,
145                new TagletWriterImpl.Context(false, false));
146            var div = HtmlTree.DIV(HtmlStyle.block, serialFieldContent);
147            content.add(div);
148        }
149    }
150
151    /**
152     * Add the tag information for this member.
153     *
154     * @param field the field to document.
155     * @param content the content to which the member tags info will be added
156     */
157    @Override
158    public void addMemberTags(VariableElement field, Content content) {
159        Content tagContent = writer.getBlockTagOutput(field);
160        if (!tagContent.isEmpty()) {
161            var dl = HtmlTree.DL(HtmlStyle.notes);
162            dl.add(tagContent);
163            content.add(dl);
164        }
165    }
166
167    /**
168     * Check to see if overview details should be printed. If
169     * nocomment option set or if there is no text to be printed
170     * for deprecation info, comment or tags, do not print overview details.
171     *
172     * @param field the field to check overview details for.
173     * @return true if overview details need to be printed
174     */
175    @Override
176    public boolean shouldPrintOverview(VariableElement field) {
177        if (!options.noComment()) {
178            if (!utils.getFullBody(field).isEmpty() ||
179                writer.hasSerializationOverviewTags(field))
180                return true;
181        }
182        if (utils.isDeprecated(field))
183            return true;
184        return false;
185    }
186}