MetaKeywords.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (config) 2002, 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
26package jdk.javadoc.internal.doclets.toolkit.util;
27
28import java.util.*;
29
30import javax.lang.model.element.Element;
31import javax.lang.model.element.ModuleElement;
32import javax.lang.model.element.PackageElement;
33import javax.lang.model.element.TypeElement;
34
35import jdk.javadoc.internal.doclets.toolkit.Configuration;
36
37/**
38 * Provides methods for creating an array of class, method and
39 * field names to be included as meta keywords in the HTML header
40 * of class pages.  These keywords improve search results
41 * on browsers that look for keywords.
42 *
43 *  <p><b>This is NOT part of any supported API.
44 *  If you write code that depends on this, you do so at your own risk.
45 *  This code and its internal interfaces are subject to change or
46 *  deletion without notice.</b>
47 *
48 * @author Doug Kramer
49 */
50public class MetaKeywords {
51
52    /**
53     * The global configuration information for this run.
54     */
55    private final Configuration config;
56
57    /**
58     * Constructor
59     */
60    public MetaKeywords(Configuration configuration) {
61        config = configuration;
62    }
63
64    /**
65     * Returns an array of strings where each element
66     * is a class, method or field name.  This array is
67     * used to create one meta keyword tag for each element.
68     * Method parameter lists are converted to "()" and
69     * overloads are combined.
70     *
71     * Constructors are not included because they have the same
72     * name as the class, which is already included.
73     * Nested class members are not included because their
74     * definitions are on separate pages.
75     */
76    public List<String> getMetaKeywords(TypeElement typeElement) {
77        ArrayList<String> results = new ArrayList<>();
78
79        // Add field and method keywords only if -keywords option is used
80        if (config.keywords) {
81            results.addAll(getClassKeyword(typeElement));
82            results.addAll(getMemberKeywords(config.utils.getFields(typeElement)));
83            results.addAll(getMemberKeywords(config.utils.getMethods(typeElement)));
84        }
85        ((ArrayList)results).trimToSize();
86        return results;
87    }
88
89    /**
90     * Get the current class for a meta tag keyword, as the first
91     * and only element of an array list.
92     */
93    protected List<String> getClassKeyword(TypeElement typeElement) {
94        ArrayList<String> metakeywords = new ArrayList<>(1);
95        String cltypelower = config.utils.isInterface(typeElement) ? "interface" : "class";
96        metakeywords.add(config.utils.getFullyQualifiedName(typeElement) + " " + cltypelower);
97        return metakeywords;
98    }
99
100    /**
101     * Get the package keywords.
102     */
103    public List<String> getMetaKeywords(PackageElement packageElement) {
104        List<String> result = new ArrayList<>(1);
105        if (config.keywords) {
106            String pkgName = config.utils.getPackageName(packageElement);
107            result.add(pkgName + " " + "package");
108        }
109        return result;
110    }
111
112    /**
113     * Get the module keywords.
114     *
115     * @param mdle the module being documented
116     */
117    public List<String> getMetaKeywordsForModule(ModuleElement mdle) {
118        if (config.keywords) {
119            return Arrays.asList(mdle.getQualifiedName() + " " + "module");
120        } else {
121            return Collections.emptyList();
122        }
123    }
124
125    /**
126     * Get the overview keywords.
127     */
128    public List<String> getOverviewMetaKeywords(String title, String docTitle) {
129         List<String> result = new ArrayList<>(1);
130        if (config.keywords) {
131            String windowOverview = config.getText(title);
132            if (docTitle.length() > 0) {
133                result.add(windowOverview + ", " + docTitle);
134            } else {
135                result.add(windowOverview);
136            }
137        }
138        return result;
139    }
140
141    /**
142     * Get members for meta tag keywords as an array,
143     * where each member name is a string element of the array.
144     * The parameter lists are not included in the keywords;
145     * therefore all overloaded methods are combined.<br>
146     * Example: getValue(Object) is returned in array as getValue()
147     *
148     * @param members  array of members to be added to keywords
149     */
150    protected List<String> getMemberKeywords(List<? extends Element> members) {
151        ArrayList<String> results = new ArrayList<>();
152        for (Element member : members) {
153            String membername = config.utils.isMethod(member)
154                    ? config.utils.getSimpleName(member) + "()"
155                    : config.utils.getSimpleName(member);
156            if (!results.contains(membername)) {
157                results.add(membername);
158            }
159        }
160        ((ArrayList)results).trimToSize();
161        return results;
162    }
163}
164