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

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 184
905e151a185a
child 1357
c75be5bc5283
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

duke@1 1 /*
ohair@554 2 * Copyright (c) 1998, 2008, 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
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 /**
duke@1 60 * Map of regular expressions with the corresponding group name.
duke@1 61 */
jjg@74 62 private Map<String,String> regExpGroupMap = new HashMap<String,String>();
duke@1 63
duke@1 64 /**
duke@1 65 * List of regular expressions sorted according to the length. Regular
duke@1 66 * expression with longest length will be first in the sorted order.
duke@1 67 */
jjg@74 68 private List<String> sortedRegExpList = new ArrayList<String>();
duke@1 69
duke@1 70 /**
duke@1 71 * List of group names in the same order as given on the command line.
duke@1 72 */
jjg@74 73 private List<String> groupList = new ArrayList<String>();
duke@1 74
duke@1 75 /**
duke@1 76 * Map of non-regular expressions(possible package names) with the
duke@1 77 * corresponding group name.
duke@1 78 */
jjg@74 79 private Map<String,String> pkgNameGroupMap = new HashMap<String,String>();
duke@1 80
duke@1 81 /**
duke@1 82 * The global configuration information for this run.
duke@1 83 */
duke@1 84 private final Configuration configuration;
duke@1 85
duke@1 86 /**
duke@1 87 * Since we need to sort the keys in the reverse order(longest key first),
duke@1 88 * the compare method in the implementing class is doing the reverse
duke@1 89 * comparison.
duke@1 90 */
jjg@74 91 private static class MapKeyComparator implements Comparator<String> {
jjg@74 92 public int compare(String key1, String key2) {
jjg@74 93 return key2.length() - key1.length();
duke@1 94 }
duke@1 95 }
duke@1 96
jjg@140 97 public Group(Configuration configuration) {
duke@1 98 this.configuration = configuration;
duke@1 99 }
duke@1 100
duke@1 101 /**
duke@1 102 * Depending upon the format of the package name provided in the "-group"
duke@1 103 * option, generate two separate maps. There will be a map for mapping
duke@1 104 * regular expression(only meta character allowed is '*' and that is at the
duke@1 105 * end of the regular expression) on to the group name. And another map
duke@1 106 * for mapping (possible) package names(if the name format doesen't contain
duke@1 107 * meta character '*', then it is assumed to be a package name) on to the
duke@1 108 * group name. This will also sort all the regular expressions found in the
duke@1 109 * reverse order of their lengths, i.e. longest regular expression will be
duke@1 110 * first in the sorted list.
duke@1 111 *
duke@1 112 * @param groupname The name of the group from -group option.
duke@1 113 * @param pkgNameFormList List of the package name formats.
duke@1 114 */
duke@1 115 public boolean checkPackageGroups(String groupname,
duke@1 116 String pkgNameFormList) {
duke@1 117 StringTokenizer strtok = new StringTokenizer(pkgNameFormList, ":");
duke@1 118 if (groupList.contains(groupname)) {
duke@1 119 configuration.message.warning("doclet.Groupname_already_used", groupname);
duke@1 120 return false;
duke@1 121 }
duke@1 122 groupList.add(groupname);
duke@1 123 while (strtok.hasMoreTokens()) {
duke@1 124 String id = strtok.nextToken();
duke@1 125 if (id.length() == 0) {
duke@1 126 configuration.message.warning("doclet.Error_in_packagelist", groupname, pkgNameFormList);
duke@1 127 return false;
duke@1 128 }
duke@1 129 if (id.endsWith("*")) {
duke@1 130 id = id.substring(0, id.length() - 1);
duke@1 131 if (foundGroupFormat(regExpGroupMap, id)) {
duke@1 132 return false;
duke@1 133 }
duke@1 134 regExpGroupMap.put(id, groupname);
duke@1 135 sortedRegExpList.add(id);
duke@1 136 } else {
duke@1 137 if (foundGroupFormat(pkgNameGroupMap, id)) {
duke@1 138 return false;
duke@1 139 }
duke@1 140 pkgNameGroupMap.put(id, groupname);
duke@1 141 }
duke@1 142 }
duke@1 143 Collections.sort(sortedRegExpList, new MapKeyComparator());
duke@1 144 return true;
duke@1 145 }
duke@1 146
duke@1 147 /**
duke@1 148 * Search if the given map has given the package format.
duke@1 149 *
duke@1 150 * @param map Map to be searched.
duke@1 151 * @param pkgFormat The pacakge format to search.
duke@1 152 *
duke@1 153 * @return true if package name format found in the map, else false.
duke@1 154 */
mcimadamore@184 155 boolean foundGroupFormat(Map<String,?> map, String pkgFormat) {
duke@1 156 if (map.containsKey(pkgFormat)) {
duke@1 157 configuration.message.error("doclet.Same_package_name_used", pkgFormat);
duke@1 158 return true;
duke@1 159 }
duke@1 160 return false;
duke@1 161 }
duke@1 162
duke@1 163 /**
duke@1 164 * Group the packages according the grouping information provided on the
duke@1 165 * command line. Given a list of packages, search each package name in
duke@1 166 * regular expression map as well as package name map to get the
duke@1 167 * corresponding group name. Create another map with mapping of group name
duke@1 168 * to the package list, which will fall under the specified group. If any
duke@1 169 * package doesen't belong to any specified group on the comamnd line, then
duke@1 170 * a new group named "Other Packages" will be created for it. If there are
duke@1 171 * no groups found, in other words if "-group" option is not at all used,
duke@1 172 * then all the packages will be grouped under group "Packages".
duke@1 173 *
duke@1 174 * @param packages Packages specified on the command line.
duke@1 175 */
jjg@74 176 public Map<String,List<PackageDoc>> groupPackages(PackageDoc[] packages) {
jjg@74 177 Map<String,List<PackageDoc>> groupPackageMap = new HashMap<String,List<PackageDoc>>();
duke@1 178 String defaultGroupName =
duke@1 179 (pkgNameGroupMap.isEmpty() && regExpGroupMap.isEmpty())?
duke@1 180 configuration.message.getText("doclet.Packages") :
duke@1 181 configuration.message.getText("doclet.Other_Packages");
duke@1 182 // if the user has not used the default group name, add it
duke@1 183 if (!groupList.contains(defaultGroupName)) {
duke@1 184 groupList.add(defaultGroupName);
duke@1 185 }
duke@1 186 for (int i = 0; i < packages.length; i++) {
duke@1 187 PackageDoc pkg = packages[i];
duke@1 188 String pkgName = pkg.name();
jjg@74 189 String groupName = pkgNameGroupMap.get(pkgName);
duke@1 190 // if this package is not explicitly assigned to a group,
duke@1 191 // try matching it to group specified by regular expression
duke@1 192 if (groupName == null) {
duke@1 193 groupName = regExpGroupName(pkgName);
duke@1 194 }
duke@1 195 // if it is in neither group map, put it in the default
duke@1 196 // group
duke@1 197 if (groupName == null) {
duke@1 198 groupName = defaultGroupName;
duke@1 199 }
duke@1 200 getPkgList(groupPackageMap, groupName).add(pkg);
duke@1 201 }
duke@1 202 return groupPackageMap;
duke@1 203 }
duke@1 204
duke@1 205 /**
duke@1 206 * Search for package name in the sorted regular expression
duke@1 207 * list, if found return the group name. If not, return null.
duke@1 208 *
duke@1 209 * @param pkgName Name of package to be found in the regular
duke@1 210 * expression list.
duke@1 211 */
duke@1 212 String regExpGroupName(String pkgName) {
duke@1 213 for (int j = 0; j < sortedRegExpList.size(); j++) {
jjg@74 214 String regexp = sortedRegExpList.get(j);
duke@1 215 if (pkgName.startsWith(regexp)) {
jjg@74 216 return regExpGroupMap.get(regexp);
duke@1 217 }
duke@1 218 }
duke@1 219 return null;
duke@1 220 }
duke@1 221
duke@1 222 /**
duke@1 223 * For the given group name, return the package list, on which it is mapped.
duke@1 224 * Create a new list, if not found.
duke@1 225 *
duke@1 226 * @param map Map to be searched for gorup name.
duke@1 227 * @param groupname Group name to search.
duke@1 228 */
jjg@74 229 List<PackageDoc> getPkgList(Map<String,List<PackageDoc>> map, String groupname) {
jjg@74 230 List<PackageDoc> list = map.get(groupname);
duke@1 231 if (list == null) {
jjg@74 232 list = new ArrayList<PackageDoc>();
duke@1 233 map.put(groupname, list);
duke@1 234 }
duke@1 235 return list;
duke@1 236 }
duke@1 237
duke@1 238 /**
duke@1 239 * Return the list of groups, in the same order as specified
duke@1 240 * on the command line.
duke@1 241 */
mcimadamore@184 242 public List<String> getGroupList() {
duke@1 243 return groupList;
duke@1 244 }
duke@1 245 }

mercurial