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

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 554
9d9f26857129
child 1359
25e14ad23cef
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

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

mercurial