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