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