src/share/classes/com/sun/tools/doclets/internal/toolkit/util/Group.java

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 74
5a9172b251dd
child 140
22c4c1143a3a
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

duke@1 1 /*
xdono@117 2 * Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.doclets.internal.toolkit.util;
duke@1 27
duke@1 28 import com.sun.tools.doclets.internal.toolkit.*;
duke@1 29 import com.sun.javadoc.*;
duke@1 30 import java.util.*;
duke@1 31
duke@1 32 /**
duke@1 33 * Process and manage grouping of packages, as specified by "-group" option on
duke@1 34 * the command line.
duke@1 35 * <p>
duke@1 36 * For example, if user has used -group option as
duke@1 37 * -group "Core Packages" "java.*" -group "CORBA Packages" "org.omg.*", then
duke@1 38 * the packages specified on the command line will be grouped according to their
duke@1 39 * names starting with either "java." or "org.omg.". All the other packages
duke@1 40 * which do not fall in the user given groups, are grouped in default group,
duke@1 41 * named as either "Other Packages" or "Packages" depending upon if "-group"
duke@1 42 * option used or not at all used respectively.
duke@1 43 * </p>
duke@1 44 * <p>
duke@1 45 * Also the packages are grouped according to the longest possible match of
duke@1 46 * their names with the grouping information provided. For example, if there
duke@1 47 * are two groups, like -group "Lang" "java.lang" and -group "Core" "java.*",
duke@1 48 * will put the package java.lang in the group "Lang" and not in group "Core".
duke@1 49 * </p>
duke@1 50 *
duke@1 51 * This code is not part of an API.
duke@1 52 * It is implementation that is subject to change.
duke@1 53 * Do not use it as an API
duke@1 54 *
duke@1 55 * @author Atul M Dambalkar
duke@1 56 */
duke@1 57 public class Group {
duke@1 58
duke@1 59 private static Group instance;
duke@1 60
duke@1 61 /**
duke@1 62 * Map of regular expressions with the corresponding group name.
duke@1 63 */
jjg@74 64 private Map<String,String> regExpGroupMap = new HashMap<String,String>();
duke@1 65
duke@1 66 /**
duke@1 67 * List of regular expressions sorted according to the length. Regular
duke@1 68 * expression with longest length will be first in the sorted order.
duke@1 69 */
jjg@74 70 private List<String> sortedRegExpList = new ArrayList<String>();
duke@1 71
duke@1 72 /**
duke@1 73 * List of group names in the same order as given on the command line.
duke@1 74 */
jjg@74 75 private List<String> groupList = new ArrayList<String>();
duke@1 76
duke@1 77 /**
duke@1 78 * Map of non-regular expressions(possible package names) with the
duke@1 79 * corresponding group name.
duke@1 80 */
jjg@74 81 private Map<String,String> pkgNameGroupMap = new HashMap<String,String>();
duke@1 82
duke@1 83 /**
duke@1 84 * The global configuration information for this run.
duke@1 85 */
duke@1 86 private final Configuration configuration;
duke@1 87
duke@1 88 /**
duke@1 89 * Since we need to sort the keys in the reverse order(longest key first),
duke@1 90 * the compare method in the implementing class is doing the reverse
duke@1 91 * comparison.
duke@1 92 */
jjg@74 93 private static class MapKeyComparator implements Comparator<String> {
jjg@74 94 public int compare(String key1, String key2) {
jjg@74 95 return key2.length() - key1.length();
duke@1 96 }
duke@1 97 }
duke@1 98
duke@1 99 private Group(Configuration configuration) {
duke@1 100 this.configuration = configuration;
duke@1 101 }
duke@1 102
duke@1 103 public static Group getInstance(Configuration configuration) {
duke@1 104 if (instance == null) {
duke@1 105 instance = new Group(configuration);
duke@1 106 }
duke@1 107 return instance;
duke@1 108 }
duke@1 109
duke@1 110 /**
duke@1 111 * Depending upon the format of the package name provided in the "-group"
duke@1 112 * option, generate two separate maps. There will be a map for mapping
duke@1 113 * regular expression(only meta character allowed is '*' and that is at the
duke@1 114 * end of the regular expression) on to the group name. And another map
duke@1 115 * for mapping (possible) package names(if the name format doesen't contain
duke@1 116 * meta character '*', then it is assumed to be a package name) on to the
duke@1 117 * group name. This will also sort all the regular expressions found in the
duke@1 118 * reverse order of their lengths, i.e. longest regular expression will be
duke@1 119 * first in the sorted list.
duke@1 120 *
duke@1 121 * @param groupname The name of the group from -group option.
duke@1 122 * @param pkgNameFormList List of the package name formats.
duke@1 123 */
duke@1 124 public boolean checkPackageGroups(String groupname,
duke@1 125 String pkgNameFormList) {
duke@1 126 StringTokenizer strtok = new StringTokenizer(pkgNameFormList, ":");
duke@1 127 if (groupList.contains(groupname)) {
duke@1 128 configuration.message.warning("doclet.Groupname_already_used", groupname);
duke@1 129 return false;
duke@1 130 }
duke@1 131 groupList.add(groupname);
duke@1 132 while (strtok.hasMoreTokens()) {
duke@1 133 String id = strtok.nextToken();
duke@1 134 if (id.length() == 0) {
duke@1 135 configuration.message.warning("doclet.Error_in_packagelist", groupname, pkgNameFormList);
duke@1 136 return false;
duke@1 137 }
duke@1 138 if (id.endsWith("*")) {
duke@1 139 id = id.substring(0, id.length() - 1);
duke@1 140 if (foundGroupFormat(regExpGroupMap, id)) {
duke@1 141 return false;
duke@1 142 }
duke@1 143 regExpGroupMap.put(id, groupname);
duke@1 144 sortedRegExpList.add(id);
duke@1 145 } else {
duke@1 146 if (foundGroupFormat(pkgNameGroupMap, id)) {
duke@1 147 return false;
duke@1 148 }
duke@1 149 pkgNameGroupMap.put(id, groupname);
duke@1 150 }
duke@1 151 }
duke@1 152 Collections.sort(sortedRegExpList, new MapKeyComparator());
duke@1 153 return true;
duke@1 154 }
duke@1 155
duke@1 156 /**
duke@1 157 * Search if the given map has given the package format.
duke@1 158 *
duke@1 159 * @param map Map to be searched.
duke@1 160 * @param pkgFormat The pacakge format to search.
duke@1 161 *
duke@1 162 * @return true if package name format found in the map, else false.
duke@1 163 */
duke@1 164 boolean foundGroupFormat(Map map, String pkgFormat) {
duke@1 165 if (map.containsKey(pkgFormat)) {
duke@1 166 configuration.message.error("doclet.Same_package_name_used", pkgFormat);
duke@1 167 return true;
duke@1 168 }
duke@1 169 return false;
duke@1 170 }
duke@1 171
duke@1 172 /**
duke@1 173 * Group the packages according the grouping information provided on the
duke@1 174 * command line. Given a list of packages, search each package name in
duke@1 175 * regular expression map as well as package name map to get the
duke@1 176 * corresponding group name. Create another map with mapping of group name
duke@1 177 * to the package list, which will fall under the specified group. If any
duke@1 178 * package doesen't belong to any specified group on the comamnd line, then
duke@1 179 * a new group named "Other Packages" will be created for it. If there are
duke@1 180 * no groups found, in other words if "-group" option is not at all used,
duke@1 181 * then all the packages will be grouped under group "Packages".
duke@1 182 *
duke@1 183 * @param packages Packages specified on the command line.
duke@1 184 */
jjg@74 185 public Map<String,List<PackageDoc>> groupPackages(PackageDoc[] packages) {
jjg@74 186 Map<String,List<PackageDoc>> groupPackageMap = new HashMap<String,List<PackageDoc>>();
duke@1 187 String defaultGroupName =
duke@1 188 (pkgNameGroupMap.isEmpty() && regExpGroupMap.isEmpty())?
duke@1 189 configuration.message.getText("doclet.Packages") :
duke@1 190 configuration.message.getText("doclet.Other_Packages");
duke@1 191 // if the user has not used the default group name, add it
duke@1 192 if (!groupList.contains(defaultGroupName)) {
duke@1 193 groupList.add(defaultGroupName);
duke@1 194 }
duke@1 195 for (int i = 0; i < packages.length; i++) {
duke@1 196 PackageDoc pkg = packages[i];
duke@1 197 String pkgName = pkg.name();
jjg@74 198 String groupName = pkgNameGroupMap.get(pkgName);
duke@1 199 // if this package is not explicitly assigned to a group,
duke@1 200 // try matching it to group specified by regular expression
duke@1 201 if (groupName == null) {
duke@1 202 groupName = regExpGroupName(pkgName);
duke@1 203 }
duke@1 204 // if it is in neither group map, put it in the default
duke@1 205 // group
duke@1 206 if (groupName == null) {
duke@1 207 groupName = defaultGroupName;
duke@1 208 }
duke@1 209 getPkgList(groupPackageMap, groupName).add(pkg);
duke@1 210 }
duke@1 211 return groupPackageMap;
duke@1 212 }
duke@1 213
duke@1 214 /**
duke@1 215 * Search for package name in the sorted regular expression
duke@1 216 * list, if found return the group name. If not, return null.
duke@1 217 *
duke@1 218 * @param pkgName Name of package to be found in the regular
duke@1 219 * expression list.
duke@1 220 */
duke@1 221 String regExpGroupName(String pkgName) {
duke@1 222 for (int j = 0; j < sortedRegExpList.size(); j++) {
jjg@74 223 String regexp = sortedRegExpList.get(j);
duke@1 224 if (pkgName.startsWith(regexp)) {
jjg@74 225 return regExpGroupMap.get(regexp);
duke@1 226 }
duke@1 227 }
duke@1 228 return null;
duke@1 229 }
duke@1 230
duke@1 231 /**
duke@1 232 * For the given group name, return the package list, on which it is mapped.
duke@1 233 * Create a new list, if not found.
duke@1 234 *
duke@1 235 * @param map Map to be searched for gorup name.
duke@1 236 * @param groupname Group name to search.
duke@1 237 */
jjg@74 238 List<PackageDoc> getPkgList(Map<String,List<PackageDoc>> map, String groupname) {
jjg@74 239 List<PackageDoc> list = map.get(groupname);
duke@1 240 if (list == null) {
jjg@74 241 list = new ArrayList<PackageDoc>();
duke@1 242 map.put(groupname, list);
duke@1 243 }
duke@1 244 return list;
duke@1 245 }
duke@1 246
duke@1 247 /**
duke@1 248 * Return the list of groups, in the same order as specified
duke@1 249 * on the command line.
duke@1 250 */
duke@1 251 public List getGroupList() {
duke@1 252 return groupList;
duke@1 253 }
duke@1 254 }

mercurial