1 /*
2  * Copyright (c) 2003, 2020, 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.toolkit.taglets;
27 
28 import java.util.Set;
29 import javax.lang.model.element.Element;
30 
31 import com.sun.source.doctree.DocTree;
32 import com.sun.source.doctree.UnknownBlockTagTree;
33 import jdk.javadoc.doclet.Taglet.Location;
34 import jdk.javadoc.internal.doclets.toolkit.Content;
35 
36 /**
37  * A base class that implements the {@link Taglet} interface.
38  *
39  *  <p><b>This is NOT part of any supported API.
40  *  If you write code that depends on this, you do so at your own risk.
41  *  This code and its internal interfaces are subject to change or
42  *  deletion without notice.</b>
43  */
44 public class BaseTaglet implements Taglet {
45 
46     protected final DocTree.Kind tagKind;
47     protected final String name;
48     private final boolean inline;
49     private final Set<Location> sites;
50 
BaseTaglet(DocTree.Kind tagKind, boolean inline, Set<Location> sites)51     BaseTaglet(DocTree.Kind tagKind, boolean inline, Set<Location> sites) {
52         this(tagKind.tagName, tagKind, inline, sites);
53     }
54 
BaseTaglet(String name, boolean inline, Set<Location> sites)55     BaseTaglet(String name, boolean inline, Set<Location> sites) {
56         this(name, inline ? DocTree.Kind.UNKNOWN_INLINE_TAG : DocTree.Kind.UNKNOWN_BLOCK_TAG, inline, sites);
57     }
58 
BaseTaglet(String name, DocTree.Kind tagKind, boolean inline, Set<Location> sites)59     private BaseTaglet(String name, DocTree.Kind tagKind, boolean inline, Set<Location> sites) {
60         this.name = name;
61         this.tagKind = tagKind;
62         this.inline = inline;
63         this.sites = sites;
64     }
65 
66     @Override
getAllowedLocations()67     public Set<Location> getAllowedLocations() {
68         return sites;
69     }
70 
71     @Override
inField()72     public final boolean inField() {
73         return sites.contains(Location.FIELD);
74     }
75 
76     @Override
inConstructor()77     public final boolean inConstructor() {
78         return sites.contains(Location.CONSTRUCTOR);
79     }
80 
81     @Override
inMethod()82     public final boolean inMethod() {
83         return sites.contains(Location.METHOD);
84     }
85 
86     @Override
inOverview()87     public final boolean inOverview() {
88         return sites.contains(Location.OVERVIEW);
89     }
90 
91     @Override
inModule()92     public final boolean inModule() {
93         return sites.contains(Location.MODULE);
94     }
95 
96     @Override
inPackage()97     public final boolean inPackage() {
98         return sites.contains(Location.PACKAGE);
99     }
100 
101     @Override
inType()102     public final boolean inType() {
103         return sites.contains(Location.TYPE);
104     }
105 
106     @Override
isInlineTag()107     public final boolean isInlineTag() {
108         return inline;
109     }
110 
111     @Override
getName()112     public String getName() {
113         return name;
114     }
115 
116     /**
117      * Returns the kind of trees recognized by this taglet.
118      *
119      * @return the kind of trees recognized by this taglet
120      */
getTagKind()121     public DocTree.Kind getTagKind() {
122         return tagKind;
123     }
124 
125     /**
126      * Returns whether or not this taglet accepts a {@code DocTree} node.
127      * The taglet accepts a tree node if it has the same kind, and
128      * if the kind is {@code UNKNOWN_BLOCK_TAG} the same tag name.
129      *
130      * @param tree the tree node
131      * @return {@code true} if this taglet accepts this tree node
132      */
accepts(DocTree tree)133     public boolean accepts(DocTree tree) {
134         return (tree.getKind() == DocTree.Kind.UNKNOWN_BLOCK_TAG
135                     && tagKind == DocTree.Kind.UNKNOWN_BLOCK_TAG)
136                 ? ((UnknownBlockTagTree) tree).getTagName().equals(name)
137                 : tree.getKind() == tagKind;
138     }
139 
140     /**
141      * {@inheritDoc}
142      *
143      * @implSpec This implementation throws {@link UnsupportedTagletOperationException}.
144      */
145     @Override
getInlineTagOutput(Element element, DocTree tag, TagletWriter writer)146     public Content getInlineTagOutput(Element element, DocTree tag, TagletWriter writer) {
147         throw new UnsupportedTagletOperationException("Method not supported in taglet " + getName() + ".");
148     }
149 
150     /**
151      * {@inheritDoc}
152      *
153      * @implSpec This implementation throws {@link UnsupportedTagletOperationException}
154      */
155     @Override
getAllBlockTagOutput(Element holder, TagletWriter writer)156     public Content getAllBlockTagOutput(Element holder, TagletWriter writer) {
157         throw new UnsupportedTagletOperationException("Method not supported in taglet " + getName() + ".");
158     }
159 }
160