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