1 /*
   2  * Copyright (c) 2002, 2014, 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     private final Utils utils;
  54 
  55     /**
  56      * Constructor
  57      */
  58     public MetaKeywords(Configuration configuration) {
  59         this.configuration = configuration;
  60         this.utils = configuration.utils;
  61     }
  62 
  63     /**
  64      * Returns an array of strings where each element
  65      * is a class, method or field name.  This array is
  66      * used to create one meta keyword tag for each element.
  67      * Method parameter lists are converted to "()" and
  68      * overloads are combined.
  69      *
  70      * Constructors are not included because they have the same
  71      * name as the class, which is already included.
  72      * Nested class members are not included because their
  73      * definitions are on separate pages.
  74      */
  75     public String[] getMetaKeywords(ClassDoc classdoc) {
  76         ArrayList<String> results = new ArrayList<>();
  77 
  78         // Add field and method keywords only if -keywords option is used
  79         if( configuration.keywords ) {
  80             results.addAll(getClassKeyword(classdoc));
  81             results.addAll(getMemberKeywords(classdoc.fields()));
  82             results.addAll(getMemberKeywords(classdoc.methods()));
  83         }
  84         return results.toArray(new String[]{});
  85     }
  86 
  87     /**
  88      * Get the current class for a meta tag keyword, as the first
  89      * and only element of an array list.
  90      */
  91     protected ArrayList<String> getClassKeyword(ClassDoc classdoc) {
  92         String cltypelower = classdoc.isInterface() ? "interface" : "class";
  93         ArrayList<String> metakeywords = new ArrayList<>(1);
  94         metakeywords.add(classdoc.qualifiedName() + " " + cltypelower);
  95         return metakeywords;
  96     }
  97 
  98     /**
  99      * Get the package keywords.
 100      */
 101     public String[] getMetaKeywords(PackageDoc packageDoc) {
 102         if( configuration.keywords ) {
 103             String pkgName = utils.getPackageName(packageDoc);
 104             return new String[] { pkgName + " " + "package" };
 105         } else {
 106             return new String[] {};
 107         }
 108     }
 109 
 110     /**
 111      * Get the profile keywords.
 112      *
 113      * @param profile the profile being documented
 114      */
 115     public String[] getMetaKeywords(Profile profile) {
 116         if( configuration.keywords ) {
 117             String profileName = profile.name;
 118             return new String[] { profileName + " " + "profile" };
 119         } else {
 120             return new String[] {};
 121         }
 122     }
 123 
 124     /**
 125      * Get the overview keywords.
 126      */
 127     public String[] getOverviewMetaKeywords(String title, String docTitle) {
 128         if( configuration.keywords ) {
 129             String windowOverview = configuration.getText(title);
 130             String[] metakeywords = { windowOverview };
 131             if (docTitle.length() > 0 ) {
 132                 metakeywords[0] += ", " + docTitle;
 133             }
 134             return metakeywords;
 135         } else {
 136             return new String[] {};
 137         }
 138     }
 139 
 140     /**
 141      * Get members for meta tag keywords as an array,
 142      * where each member name is a string element of the array.
 143      * The parameter lists are not included in the keywords;
 144      * therefore all overloaded methods are combined.<br>
 145      * Example: getValue(Object) is returned in array as getValue()
 146      *
 147      * @param memberdocs  array of MemberDoc objects to be added to keywords
 148      */
 149     protected ArrayList<String> getMemberKeywords(MemberDoc[] memberdocs) {
 150         ArrayList<String> results = new ArrayList<>();
 151         String membername;
 152         for (MemberDoc memberdoc : memberdocs) {
 153             membername = memberdoc.name() + (memberdoc.isMethod() ? "()" : "");
 154             if (!results.contains(membername)) {
 155                 results.add(membername);
 156             }
 157         }
 158         return results;
 159     }
 160 }