Group.java revision 3233:b5d08bc0d224
1/* 2 * Copyright (c) 1998, 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 26package jdk.javadoc.internal.doclets.toolkit.util; 27 28import java.util.*; 29 30import javax.lang.model.element.Element; 31import javax.lang.model.element.PackageElement; 32 33import jdk.javadoc.internal.doclets.toolkit.Configuration; 34 35 36/** 37 * Process and manage grouping of packages, as specified by "-group" option on 38 * the command line. 39 * <p> 40 * For example, if user has used -group option as 41 * -group "Core Packages" "java.*" -group "CORBA Packages" "org.omg.*", then 42 * the packages specified on the command line will be grouped according to their 43 * names starting with either "java." or "org.omg.". All the other packages 44 * which do not fall in the user given groups, are grouped in default group, 45 * named as either "Other Packages" or "Packages" depending upon if "-group" 46 * option used or not at all used respectively. 47 * </p> 48 * <p> 49 * Also the packages are grouped according to the longest possible match of 50 * their names with the grouping information provided. For example, if there 51 * are two groups, like -group "Lang" "java.lang" and -group "Core" "java.*", 52 * will put the package java.lang in the group "Lang" and not in group "Core". 53 * </p> 54 * 55 * <p><b>This is NOT part of any supported API. 56 * If you write code that depends on this, you do so at your own risk. 57 * This code and its internal interfaces are subject to change or 58 * deletion without notice.</b> 59 * 60 * @author Atul M Dambalkar 61 */ 62public class Group { 63 64 /** 65 * Map of regular expressions with the corresponding group name. 66 */ 67 private Map<String,String> regExpGroupMap = new HashMap<>(); 68 69 /** 70 * List of regular expressions sorted according to the length. Regular 71 * expression with longest length will be first in the sorted order. 72 */ 73 private List<String> sortedRegExpList = new ArrayList<>(); 74 75 /** 76 * List of group names in the same order as given on the command line. 77 */ 78 private List<String> groupList = new ArrayList<>(); 79 80 /** 81 * Map of non-regular expressions(possible package names) with the 82 * corresponding group name. 83 */ 84 private Map<String,String> pkgNameGroupMap = new HashMap<>(); 85 86 /** 87 * The global configuration information for this run. 88 */ 89 private final Configuration configuration; 90 91 /** 92 * Since we need to sort the keys in the reverse order(longest key first), 93 * the compare method in the implementing class is doing the reverse 94 * comparison. 95 */ 96 private static class MapKeyComparator implements Comparator<String> { 97 public int compare(String key1, String key2) { 98 return key2.length() - key1.length(); 99 } 100 } 101 102 public Group(Configuration configuration) { 103 this.configuration = configuration; 104 } 105 106 /** 107 * Depending upon the format of the package name provided in the "-group" 108 * option, generate two separate maps. There will be a map for mapping 109 * regular expression(only meta character allowed is '*' and that is at the 110 * end of the regular expression) on to the group name. And another map 111 * for mapping (possible) package names(if the name format doesen't contain 112 * meta character '*', then it is assumed to be a package name) on to the 113 * group name. This will also sort all the regular expressions found in the 114 * reverse order of their lengths, i.e. longest regular expression will be 115 * first in the sorted list. 116 * 117 * @param groupname The name of the group from -group option. 118 * @param pkgNameFormList List of the package name formats. 119 */ 120 public boolean checkPackageGroups(String groupname, String pkgNameFormList) { 121 StringTokenizer strtok = new StringTokenizer(pkgNameFormList, ":"); 122 if (groupList.contains(groupname)) { 123 configuration.message.warning("doclet.Groupname_already_used", groupname); 124 return false; 125 } 126 groupList.add(groupname); 127 while (strtok.hasMoreTokens()) { 128 String id = strtok.nextToken(); 129 if (id.length() == 0) { 130 configuration.message.warning("doclet.Error_in_packagelist", groupname, pkgNameFormList); 131 return false; 132 } 133 if (id.endsWith("*")) { 134 id = id.substring(0, id.length() - 1); 135 if (foundGroupFormat(regExpGroupMap, id)) { 136 return false; 137 } 138 regExpGroupMap.put(id, groupname); 139 sortedRegExpList.add(id); 140 } else { 141 if (foundGroupFormat(pkgNameGroupMap, id)) { 142 return false; 143 } 144 pkgNameGroupMap.put(id, groupname); 145 } 146 } 147 Collections.sort(sortedRegExpList, new MapKeyComparator()); 148 return true; 149 } 150 151 /** 152 * Search if the given map has given the package format. 153 * 154 * @param map Map to be searched. 155 * @param pkgFormat The pacakge format to search. 156 * 157 * @return true if package name format found in the map, else false. 158 */ 159 boolean foundGroupFormat(Map<String,?> map, String pkgFormat) { 160 if (map.containsKey(pkgFormat)) { 161 configuration.message.error("doclet.Same_package_name_used", pkgFormat); 162 return true; 163 } 164 return false; 165 } 166 167 /** 168 * Group the packages according the grouping information provided on the 169 * command line. Given a list of packages, search each package name in 170 * regular expression map as well as package name map to get the 171 * corresponding group name. Create another map with mapping of group name 172 * to the package list, which will fall under the specified group. If any 173 * package doesen't belong to any specified group on the comamnd line, then 174 * a new group named "Other Packages" will be created for it. If there are 175 * no groups found, in other words if "-group" option is not at all used, 176 * then all the packages will be grouped under group "Packages". 177 * 178 * @param packages Packages specified on the command line. 179 */ 180 public Map<String, SortedSet<PackageElement>> groupPackages(Set<PackageElement> packages) { 181 Map<String, SortedSet<PackageElement>> groupPackageMap = new HashMap<>(); 182 String defaultGroupName = 183 (pkgNameGroupMap.isEmpty() && regExpGroupMap.isEmpty())? 184 configuration.message.getText("doclet.Packages") : 185 configuration.message.getText("doclet.Other_Packages"); 186 // if the user has not used the default group name, add it 187 if (!groupList.contains(defaultGroupName)) { 188 groupList.add(defaultGroupName); 189 } 190 for (PackageElement pkg : packages) { 191 String pkgName = pkg.isUnnamed() ? null : configuration.utils.getPackageName(pkg); 192 String groupName = pkg.isUnnamed() ? null : pkgNameGroupMap.get(pkgName); 193 // if this package is not explicitly assigned to a group, 194 // try matching it to group specified by regular expression 195 if (groupName == null) { 196 groupName = regExpGroupName(pkgName); 197 } 198 // if it is in neither group map, put it in the default 199 // group 200 if (groupName == null) { 201 groupName = defaultGroupName; 202 } 203 getPkgList(groupPackageMap, groupName).add(pkg); 204 } 205 return groupPackageMap; 206 } 207 208 /** 209 * Search for package name in the sorted regular expression 210 * list, if found return the group name. If not, return null. 211 * 212 * @param pkgName Name of package to be found in the regular 213 * expression list. 214 */ 215 String regExpGroupName(String pkgName) { 216 for (String regexp : sortedRegExpList) { 217 if (pkgName.startsWith(regexp)) { 218 return regExpGroupMap.get(regexp); 219 } 220 } 221 return null; 222 } 223 224 /** 225 * For the given group name, return the package list, on which it is mapped. 226 * Create a new list, if not found. 227 * 228 * @param map Map to be searched for gorup name. 229 * @param groupname Group name to search. 230 */ 231 SortedSet<PackageElement> getPkgList(Map<String, SortedSet<PackageElement>> map, 232 String groupname) { 233 return map.computeIfAbsent(groupname, g -> new TreeSet<>(configuration.utils.makePackageComparator())); 234 } 235 236 /** 237 * Return the list of groups, in the same order as specified 238 * on the command line. 239 */ 240 public List<String> getGroupList() { 241 return groupList; 242 } 243} 244