langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/MetaKeywords.java
author jjg
Tue, 09 Oct 2012 19:10:00 -0700
changeset 14258 8d2148961366
parent 5520 86e4b9a9da40
child 14260 727a84636f12
permissions -rw-r--r--
8000663: clean up langtools imports Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
     2
 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1475
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1475
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1475
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1475
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1475
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.doclets.internal.toolkit.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    28
import java.util.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    29
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.javadoc.*;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    31
import com.sun.tools.doclets.internal.toolkit.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 * Provides methods for creating an array of class, method and
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 * field names to be included as meta keywords in the HTML header
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * of class pages.  These keywords improve search results
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * on browsers that look for keywords.
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * This code is not part of an API.
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * It is implementation that is subject to change.
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * Do not use it as an API
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * @author Doug Kramer
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
public class MetaKeywords {
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
     * The global configuration information for this run.
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    private final Configuration configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
     * Constructor
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
     */
1475
19c0851667ca 6748541: javadoc should be reusable
jjg
parents: 1264
diff changeset
    55
    public MetaKeywords(Configuration configuration) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        this.configuration = configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
     * Returns an array of strings where each element
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     * is a class, method or field name.  This array is
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     * used to create one meta keyword tag for each element.
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * Method parameter lists are converted to "()" and
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     * overloads are combined.
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     * Constructors are not included because they have the same
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     * name as the class, which is already included.
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     * Nested class members are not included because their
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     * definitions are on separate pages.
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    public String[] getMetaKeywords(ClassDoc classdoc) {
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    72
        ArrayList<String> results = new ArrayList<String>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        // Add field and method keywords only if -keywords option is used
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        if( configuration.keywords ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
            results.addAll(getClassKeyword(classdoc));
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
            results.addAll(getMemberKeywords(classdoc.fields()));
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            results.addAll(getMemberKeywords(classdoc.methods()));
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        }
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    80
        return results.toArray(new String[]{});
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     * Get the current class for a meta tag keyword, as the first
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     * and only element of an array list.
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     */
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    87
    protected ArrayList<String> getClassKeyword(ClassDoc classdoc) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        String cltypelower = classdoc.isInterface() ? "interface" : "class";
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    89
        ArrayList<String> metakeywords = new ArrayList<String>(1);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        metakeywords.add(classdoc.qualifiedName() + " " + cltypelower);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        return metakeywords;
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     * Get the package keywords.
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    public String[] getMetaKeywords(PackageDoc packageDoc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        if( configuration.keywords ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            String pkgName = Util.getPackageName(packageDoc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            return new String[] { pkgName + " " + "package" };
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            return new String[] {};
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     * Get the overview keywords.
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    public String[] getOverviewMetaKeywords(String title, String docTitle) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        if( configuration.keywords ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            String windowOverview = configuration.getText(title);
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            String[] metakeywords = { windowOverview };
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            if (docTitle.length() > 0 ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                metakeywords[0] += ", " + docTitle;
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            return metakeywords;
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            return new String[] {};
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
     * Get members for meta tag keywords as an array,
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     * where each member name is a string element of the array.
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     * The parameter lists are not included in the keywords;
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     * therefore all overloaded methods are combined.<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     * Example: getValue(Object) is returned in array as getValue()
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
     * @param memberdocs  array of MemberDoc objects to be added to keywords
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
     */
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   131
    protected ArrayList<String> getMemberKeywords(MemberDoc[] memberdocs) {
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   132
        ArrayList<String> results = new ArrayList<String>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        String membername;
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        for (int i=0; i < memberdocs.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            membername = memberdocs[i].name()
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
                             + (memberdocs[i].isMethod() ? "()" : "");
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            if ( ! results.contains(membername) ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                results.add(membername);
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        return results;
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
}