1/*
2 * Copyright (c) 2003, 2017, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.util.EnumSet;
25import java.util.List;
26import java.util.Map;
27import java.util.Set;
28import javax.lang.model.element.Element;
29
30import com.sun.source.doctree.DocTree;
31import com.sun.source.doctree.TextTree;
32import com.sun.source.doctree.UnknownBlockTagTree;
33import com.sun.source.doctree.UnknownInlineTagTree;
34import com.sun.source.util.SimpleDocTreeVisitor;
35import jdk.javadoc.doclet.Taglet;
36import jdk.javadoc.doclet.Taglet.Location;
37import static jdk.javadoc.doclet.Taglet.Location.*;
38
39
40/**
41 * A sample Taglet representing @todo. This tag can be used in any kind of
42 * {@link javax.lang.model.Element}.  It is not an inline tag. The text is displayed
43 * in yellow to remind the developer to perform a task.  For
44 * example, "@todo Fix this!" would be shown as:
45 * <DL>
46 * <DT>
47 * <B>To Do:</B>
48 * <DD><table summary="Summary" cellpadding=2 cellspacing=0><tr><td bgcolor="yellow">Fix this!
49 * </td></tr></table></DD>
50 * </DL>
51 *
52 * @author Jamie Ho
53 * @since 1.4
54 */
55
56public class ToDoTaglet implements Taglet {
57
58    private static final String NAME = "todo";
59    private static final String HEADER = "To Do:";
60
61    /**
62     * Return the name of this custom tag.
63     */
64    public String getName() {
65        return NAME;
66    }
67
68    private final EnumSet<Location> allowedSet = EnumSet.allOf(Location.class);
69
70    @Override
71    public Set<Taglet.Location> getAllowedLocations() {
72        return allowedSet;
73    }
74
75    /**
76     * Will return false since <code>@todo</code>
77     * is not an inline tag.
78     * @return false since <code>@todo</code>
79     * is not an inline tag.
80     */
81
82    public boolean isInlineTag() {
83        return false;
84    }
85
86    /**
87     * Given an array of <code>Tag</code>s representing this custom
88     * tag, return its string representation.
89     * @param tags  the array of <code>DocTree</code>s representing this custom tag.
90     * @param element the declaration to which the enclosing comment belongs
91     */
92    @Override
93    public String toString(List<? extends DocTree> tags, Element element) {
94        if (tags.isEmpty()) {
95            return "";
96        }
97        String result = "\n<DT><B>" + HEADER + "</B><DD>";
98        result += "<table summary=\"Summary\" cellpadding=2 cellspacing=0><tr><td bgcolor=\"yellow\">";
99        for (int i = 0; i < tags.size(); i++) {
100            if (i > 0) {
101                result += ", ";
102            }
103            result += getText(tags.get(i));
104        }
105        return result + "</td></tr></table></DD>\n";
106    }
107
108    static String getText(DocTree dt) {
109        return new SimpleDocTreeVisitor<String, Void>() {
110            @Override
111            public String visitUnknownBlockTag(UnknownBlockTagTree node, Void p) {
112                for (DocTree dt : node.getContent()) {
113                    return dt.accept(this, null);
114                }
115                return "";
116            }
117
118            @Override
119            public String visitUnknownInlineTag(UnknownInlineTagTree node, Void p) {
120                for (DocTree dt : node.getContent()) {
121                    return dt.accept(this, null);
122                }
123                return "";
124            }
125
126            @Override
127            public String visitText(TextTree node, Void p) {
128                return node.getBody();
129            }
130
131            @Override
132            protected String defaultAction(DocTree node, Void p) {
133                return "";
134            }
135
136        }.visit(dt, null);
137    }
138}
139