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 } | 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 } |