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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial