001/* 002 * Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. Oracle designates this 008 * particular file as subject to the "Classpath" exception as provided 009 * by Oracle in the LICENSE file that accompanied this code. 010 * 011 * This code is distributed in the hope that it will be useful, but WITHOUT 012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 014 * version 2 for more details (a copy is included in the LICENSE file that 015 * accompanied this code). 016 * 017 * You should have received a copy of the GNU General Public License version 018 * 2 along with this work; if not, write to the Free Software Foundation, 019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 020 * 021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 022 * or visit www.oracle.com if you need additional information or have any 023 * questions. 024 */ 025 026package org.jdrupes.mdoclet.internal.doclets.toolkit.util; 027 028import java.util.*; 029 030import javax.lang.model.element.Element; 031import javax.lang.model.element.ModuleElement; 032import javax.lang.model.element.PackageElement; 033import javax.lang.model.element.TypeElement; 034 035import org.jdrupes.mdoclet.internal.doclets.toolkit.BaseConfiguration; 036import org.jdrupes.mdoclet.internal.doclets.toolkit.BaseOptions; 037import org.jdrupes.mdoclet.internal.doclets.toolkit.Resources; 038 039/** 040 * Provides methods for creating an array of class, method and 041 * field names to be included as meta keywords in the HTML header 042 * of class pages. These keywords improve search results 043 * on browsers that look for keywords. 044 */ 045public class MetaKeywords { 046 047 private final BaseOptions options; 048 private final Resources resources; 049 private final Utils utils; 050 051 /** 052 * Constructor 053 */ 054 public MetaKeywords(BaseConfiguration configuration) { 055 options = configuration.getOptions(); 056 resources = configuration.getDocResources(); 057 utils = configuration.utils; 058 } 059 060 /** 061 * Returns an array of strings where each element 062 * is a class, method or field name. This array is 063 * used to create one meta keyword tag for each element. 064 * Method parameter lists are converted to "()" and 065 * overloads are combined. 066 * 067 * Constructors are not included because they have the same 068 * name as the class, which is already included. 069 * Nested class members are not included because their 070 * definitions are on separate pages. 071 */ 072 public List<String> getMetaKeywords(TypeElement typeElement) { 073 var results = new ArrayList<String>(); 074 075 // Add field and method keywords only if -keywords option is used 076 if (options.keywords()) { 077 results.addAll(getClassKeyword(typeElement)); 078 results.addAll(getMemberKeywords(utils.getFields(typeElement))); 079 results.addAll(getMemberKeywords(utils.getMethods(typeElement))); 080 } 081 results.trimToSize(); 082 return results; 083 } 084 085 /** 086 * Get the current class for a meta tag keyword, as a singleton list. 087 */ 088 protected List<String> getClassKeyword(TypeElement typeElement) { 089 String cltypelower 090 = utils.isPlainInterface(typeElement) ? "interface" : "class"; 091 return List 092 .of(utils.getFullyQualifiedName(typeElement) + " " + cltypelower); 093 } 094 095 /** 096 * Get the package keywords. 097 */ 098 public List<String> getMetaKeywords(PackageElement packageElement) { 099 if (options.keywords()) { 100 return List 101 .of(utils.getPackageName(packageElement) + " " + "package"); 102 } else { 103 return List.of(); 104 } 105 } 106 107 /** 108 * Get the module keywords. 109 * 110 * @param mdle the module being documented 111 */ 112 public List<String> getMetaKeywordsForModule(ModuleElement mdle) { 113 if (options.keywords()) { 114 return List.of(mdle.getQualifiedName() + " " + "module"); 115 } else { 116 return List.of(); 117 } 118 } 119 120 /** 121 * Get the overview keywords. 122 */ 123 public List<String> getOverviewMetaKeywords(String title, String docTitle) { 124 if (options.keywords()) { 125 String windowOverview = resources.getText(title); 126 if (docTitle.length() > 0) { 127 return List.of(windowOverview + ", " + docTitle); 128 } else { 129 return List.of(windowOverview); 130 } 131 } else { 132 return List.of(); 133 } 134 } 135 136 /** 137 * Get members for meta tag keywords as an array, 138 * where each member name is a string element of the array. 139 * The parameter lists are not included in the keywords; 140 * therefore all overloaded methods are combined.<br> 141 * Example: getValue(Object) is returned in array as getValue() 142 * 143 * @param members array of members to be added to keywords 144 */ 145 protected List<String> getMemberKeywords(List<? extends Element> members) { 146 var results = new ArrayList<String>(); 147 for (Element member : members) { 148 String memberName = utils.isMethod(member) 149 ? utils.getSimpleName(member) + "()" 150 : utils.getSimpleName(member); 151 if (!results.contains(memberName)) { 152 results.add(memberName); 153 } 154 } 155 results.trimToSize(); 156 return results; 157 } 158}