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

Mon, 15 Oct 2012 17:07:55 -0700

author
jjg
date
Mon, 15 Oct 2012 17:07:55 -0700
changeset 1364
8db45b13526e
parent 1359
25e14ad23cef
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8000666: javadoc should write directly to Writer instead of composing strings
Reviewed-by: bpatel

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 *
jjg@1359 52 * <p><b>This is NOT part of any supported API.
jjg@1359 53 * If you write code that depends on this, you do so at your own risk.
jjg@1359 54 * This code and its internal interfaces are subject to change or
jjg@1359 55 * deletion without notice.</b>
duke@1 56 *
duke@1 57 * @author Atul M Dambalkar
duke@1 58 */
duke@1 59 public class Group {
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
jjg@140 99 public Group(Configuration configuration) {
duke@1 100 this.configuration = configuration;
duke@1 101 }
duke@1 102
duke@1 103 /**
duke@1 104 * Depending upon the format of the package name provided in the "-group"
duke@1 105 * option, generate two separate maps. There will be a map for mapping
duke@1 106 * regular expression(only meta character allowed is '*' and that is at the
duke@1 107 * end of the regular expression) on to the group name. And another map
duke@1 108 * for mapping (possible) package names(if the name format doesen't contain
duke@1 109 * meta character '*', then it is assumed to be a package name) on to the
duke@1 110 * group name. This will also sort all the regular expressions found in the
duke@1 111 * reverse order of their lengths, i.e. longest regular expression will be
duke@1 112 * first in the sorted list.
duke@1 113 *
duke@1 114 * @param groupname The name of the group from -group option.
duke@1 115 * @param pkgNameFormList List of the package name formats.
duke@1 116 */
duke@1 117 public boolean checkPackageGroups(String groupname,
duke@1 118 String pkgNameFormList) {
duke@1 119 StringTokenizer strtok = new StringTokenizer(pkgNameFormList, ":");
duke@1 120 if (groupList.contains(groupname)) {
duke@1 121 configuration.message.warning("doclet.Groupname_already_used", groupname);
duke@1 122 return false;
duke@1 123 }
duke@1 124 groupList.add(groupname);
duke@1 125 while (strtok.hasMoreTokens()) {
duke@1 126 String id = strtok.nextToken();
duke@1 127 if (id.length() == 0) {
duke@1 128 configuration.message.warning("doclet.Error_in_packagelist", groupname, pkgNameFormList);
duke@1 129 return false;
duke@1 130 }
duke@1 131 if (id.endsWith("*")) {
duke@1 132 id = id.substring(0, id.length() - 1);
duke@1 133 if (foundGroupFormat(regExpGroupMap, id)) {
duke@1 134 return false;
duke@1 135 }
duke@1 136 regExpGroupMap.put(id, groupname);
duke@1 137 sortedRegExpList.add(id);
duke@1 138 } else {
duke@1 139 if (foundGroupFormat(pkgNameGroupMap, id)) {
duke@1 140 return false;
duke@1 141 }
duke@1 142 pkgNameGroupMap.put(id, groupname);
duke@1 143 }
duke@1 144 }
duke@1 145 Collections.sort(sortedRegExpList, new MapKeyComparator());
duke@1 146 return true;
duke@1 147 }
duke@1 148
duke@1 149 /**
duke@1 150 * Search if the given map has given the package format.
duke@1 151 *
duke@1 152 * @param map Map to be searched.
duke@1 153 * @param pkgFormat The pacakge format to search.
duke@1 154 *
duke@1 155 * @return true if package name format found in the map, else false.
duke@1 156 */
mcimadamore@184 157 boolean foundGroupFormat(Map<String,?> map, String pkgFormat) {
duke@1 158 if (map.containsKey(pkgFormat)) {
duke@1 159 configuration.message.error("doclet.Same_package_name_used", pkgFormat);
duke@1 160 return true;
duke@1 161 }
duke@1 162 return false;
duke@1 163 }
duke@1 164
duke@1 165 /**
duke@1 166 * Group the packages according the grouping information provided on the
duke@1 167 * command line. Given a list of packages, search each package name in
duke@1 168 * regular expression map as well as package name map to get the
duke@1 169 * corresponding group name. Create another map with mapping of group name
duke@1 170 * to the package list, which will fall under the specified group. If any
duke@1 171 * package doesen't belong to any specified group on the comamnd line, then
duke@1 172 * a new group named "Other Packages" will be created for it. If there are
duke@1 173 * no groups found, in other words if "-group" option is not at all used,
duke@1 174 * then all the packages will be grouped under group "Packages".
duke@1 175 *
duke@1 176 * @param packages Packages specified on the command line.
duke@1 177 */
jjg@74 178 public Map<String,List<PackageDoc>> groupPackages(PackageDoc[] packages) {
jjg@74 179 Map<String,List<PackageDoc>> groupPackageMap = new HashMap<String,List<PackageDoc>>();
duke@1 180 String defaultGroupName =
duke@1 181 (pkgNameGroupMap.isEmpty() && regExpGroupMap.isEmpty())?
duke@1 182 configuration.message.getText("doclet.Packages") :
duke@1 183 configuration.message.getText("doclet.Other_Packages");
duke@1 184 // if the user has not used the default group name, add it
duke@1 185 if (!groupList.contains(defaultGroupName)) {
duke@1 186 groupList.add(defaultGroupName);
duke@1 187 }
duke@1 188 for (int i = 0; i < packages.length; i++) {
duke@1 189 PackageDoc pkg = packages[i];
duke@1 190 String pkgName = pkg.name();
jjg@74 191 String groupName = pkgNameGroupMap.get(pkgName);
duke@1 192 // if this package is not explicitly assigned to a group,
duke@1 193 // try matching it to group specified by regular expression
duke@1 194 if (groupName == null) {
duke@1 195 groupName = regExpGroupName(pkgName);
duke@1 196 }
duke@1 197 // if it is in neither group map, put it in the default
duke@1 198 // group
duke@1 199 if (groupName == null) {
duke@1 200 groupName = defaultGroupName;
duke@1 201 }
duke@1 202 getPkgList(groupPackageMap, groupName).add(pkg);
duke@1 203 }
duke@1 204 return groupPackageMap;
duke@1 205 }
duke@1 206
duke@1 207 /**
duke@1 208 * Search for package name in the sorted regular expression
duke@1 209 * list, if found return the group name. If not, return null.
duke@1 210 *
duke@1 211 * @param pkgName Name of package to be found in the regular
duke@1 212 * expression list.
duke@1 213 */
duke@1 214 String regExpGroupName(String pkgName) {
duke@1 215 for (int j = 0; j < sortedRegExpList.size(); j++) {
jjg@74 216 String regexp = sortedRegExpList.get(j);
duke@1 217 if (pkgName.startsWith(regexp)) {
jjg@74 218 return regExpGroupMap.get(regexp);
duke@1 219 }
duke@1 220 }
duke@1 221 return null;
duke@1 222 }
duke@1 223
duke@1 224 /**
duke@1 225 * For the given group name, return the package list, on which it is mapped.
duke@1 226 * Create a new list, if not found.
duke@1 227 *
duke@1 228 * @param map Map to be searched for gorup name.
duke@1 229 * @param groupname Group name to search.
duke@1 230 */
jjg@74 231 List<PackageDoc> getPkgList(Map<String,List<PackageDoc>> map, String groupname) {
jjg@74 232 List<PackageDoc> list = map.get(groupname);
duke@1 233 if (list == null) {
jjg@74 234 list = new ArrayList<PackageDoc>();
duke@1 235 map.put(groupname, list);
duke@1 236 }
duke@1 237 return list;
duke@1 238 }
duke@1 239
duke@1 240 /**
duke@1 241 * Return the list of groups, in the same order as specified
duke@1 242 * on the command line.
duke@1 243 */
mcimadamore@184 244 public List<String> getGroupList() {
duke@1 245 return groupList;
duke@1 246 }
duke@1 247 }

mercurial