home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / tooldocs / javadoc / source / standard / Group.java < prev    next >
Encoding:
Java Source  |  1999-09-19  |  10.2 KB  |  273 lines

  1. /*
  2.  * @(#)Group.java    1.6 98/09/09
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package com.sun.tools.doclets.standard;
  16.  
  17. import com.sun.tools.doclets.*;
  18. import com.sun.javadoc.*;
  19. import java.util.*;
  20.  
  21. /**
  22.  * Process and manage grouping of packages, as specified by "-group" option on
  23.  * the command line. 
  24.  * <p>
  25.  * For example, if user has used "-group option as
  26.  * -group "Core Packages" "java.*" -group "CORBA Packages" "org.omg.*", then  
  27.  * the packages specified on the command line will be grouped according to their
  28.  * names starting with either "java." or "org.omg.". All the other packages 
  29.  * which do not fall in the user given groups, are grouped in default group,
  30.  * named as either "Other Packages" or "Packages" depending upon if "-group" 
  31.  * option used or not at all used respectively. 
  32.  * </p>
  33.  * <p> 
  34.  * Also the packages are grouped according to the longest possible match of
  35.  * their names with the grouping information provided. For example, if there
  36.  * are two groups, like -group "Lang" "java.lang" and -group "Core" "java.*", 
  37.  * will put the package java.lang in the group "Lang" and not in group "Core".
  38.  * </p>
  39.  *
  40.  * @author Atul M Dambalkar
  41.  */
  42. public class Group {
  43.  
  44.     /**
  45.      * Map of regular expressions with the corresponding group name.
  46.      */
  47.     private static Map regExpGroupMap = new HashMap();
  48.  
  49.     /**
  50.      * List of regular expressions sorted according to the length. Regular 
  51.      * expression with longest length will be first in the sorted order.
  52.      */
  53.     private static List sortedRegExpList = new ArrayList();
  54.  
  55.     /**
  56.      * List of group names in the same order as given on the command line.
  57.      */
  58.     private static List groupList = new ArrayList();
  59.  
  60.     /**
  61.      * Map of non-regular expressions(possible package names) with the 
  62.      * corresponding group name.
  63.      */
  64.     private static Map pkgNameGroupMap = new HashMap();
  65.  
  66.     /**
  67.      * Since we need to sort the keys in the reverse order(longest key first),
  68.      * the compare method in the implementing class is doing the reverse
  69.      * comparison.
  70.      */
  71.     private static class MapKeyComparator implements Comparator {
  72.         public int compare(Object key1, Object key2) {
  73.             return ((String)key2).length() - ((String)key1).length();
  74.         }
  75.     }
  76.      
  77.     /**       
  78.      * Depending upon the format of the package name provided in the "-group"
  79.      * option, generate two separate maps. There will be a map for mapping  
  80.      * regular expression(only meta character allowed is '*' and that is at the
  81.      * end of the regular expression) on to the group name. And another map
  82.      * for mapping (possible) package names(if the name format doesen't contain
  83.      * meta character '*', then it is assumed to be a package name) on to the 
  84.      * group name. This will also sort all the regular expressions found in the 
  85.      * reverse order of their lengths, i.e. longest regular expression will be
  86.      * first in the sorted list.
  87.      *
  88.      * @param groupname       The name of the group from -group option.
  89.      * @param pkgNameFormList List of the package name formats.
  90.      * @param reporter        Error reporter object.
  91.      */
  92.     public static boolean checkPackageGroups(String groupname, 
  93.                                              String pkgNameFormList,
  94.                                              DocErrorReporter reporter) {
  95.         StringTokenizer strtok = new StringTokenizer(pkgNameFormList, ":");
  96.         if (groupList.contains(groupname)) {
  97.             reporter.printError(getText("doclet.Groupname_already_used", 
  98.                                          groupname));
  99.             return false;
  100.         }
  101.         groupList.add(groupname);
  102.         while (strtok.hasMoreTokens()) {
  103.             String id = strtok.nextToken();
  104.             if (id.length() == 0) {
  105.                 reporter.printError(getText("doclet.Error_in_packagelist", 
  106.                                              groupname, pkgNameFormList));
  107.                 return false;
  108.             }
  109.             if (id.endsWith("*")) {
  110.                 id = id.substring(0, id.length() - 1);
  111.                 if (foundGroupFormat(regExpGroupMap, id, reporter)) {
  112.                     return false;
  113.                 } 
  114.                 regExpGroupMap.put(id, groupname);
  115.                 sortedRegExpList.add(id);
  116.             } else {
  117.                 if (foundGroupFormat(pkgNameGroupMap, id, reporter)) {
  118.                     return false;
  119.                 } 
  120.                 pkgNameGroupMap.put(id, groupname);
  121.             }
  122.         }
  123.         Collections.sort(sortedRegExpList, new MapKeyComparator());
  124.         return true;
  125.     }
  126.  
  127.     /**
  128.      * Search if the given map has given the package format.
  129.      *
  130.      * @return true if package name format found in the map, else false.
  131.      * @param map Map to be searched.
  132.      * @param pkgFormat The pacakge format to search.
  133.      * @param reporter Error reporter object.
  134.      */
  135.     static boolean foundGroupFormat(Map map, String pkgFormat,
  136.                                     DocErrorReporter reporter) {
  137.         if (map.containsKey(pkgFormat)) { 
  138.             reporter.printError(getText("doclet.Same_package_name_used", 
  139.                                          pkgFormat));
  140.             return true;
  141.         }
  142.         return false;
  143.     }
  144.  
  145.     /** 
  146.      * This is needed since Arrays.asList() raises
  147.      * UnSupportedOperationException.
  148.      */
  149.     static List asList(Object[] arr) {
  150.         List list = new ArrayList();
  151.         for (int i = 0; i < arr.length; i++) {
  152.             list.add(arr[i]);
  153.         }
  154.         return list;
  155.     }
  156.  
  157.     /**
  158.      * Group the packages according the grouping information provided on the
  159.      * command line. Given a list of packages, search each package name in 
  160.      * regular expression map as well as package name map to get the 
  161.      * corresponding group name. Create another map with mapping of group name
  162.      * to the package list, which will fall under the specified group. If any
  163.      * package doesen't belong to any specified group on the comamnd line, then 
  164.      * a new group named "Other Packages" will be created for it. If there are 
  165.      * no groups found, in other words if "-group" option is not at all used, 
  166.      * then all the packages will be grouped under group "Packages".
  167.      * 
  168.      * @param packages Packages specified on the command line.
  169.      */
  170.     public static Map groupPackages(PackageDoc[] packages) {
  171.         Map groupPackageMap = new HashMap();
  172.         String defaultGroupName = 
  173.             (pkgNameGroupMap.isEmpty() && regExpGroupMap.isEmpty())? 
  174.                 getText("doclet.Packages") :
  175.                 getText("doclet.Other_Packages");
  176.         // if the user has not used the default group name, add it
  177.         if (!groupList.contains(defaultGroupName)) {
  178.             groupList.add(defaultGroupName);
  179.         }
  180.         for (int i = 0; i < packages.length; i++) {
  181.             PackageDoc pkg = packages[i];
  182.             String pkgName = pkg.name();
  183.             String groupName = (String)pkgNameGroupMap.get(pkgName);
  184.             // if this package is not explictly assigned to a group,
  185.             // try matching it to group specified by regular expression 
  186.             if (groupName == null) {
  187.                 groupName = regExpGroupName(pkgName);
  188.             }
  189.             // if it is in neither group map, put it in the default
  190.             // group
  191.             if (groupName == null) {
  192.                 groupName = defaultGroupName;
  193.             }
  194.             getPkgList(groupPackageMap, groupName).add(pkg);
  195.         }
  196.         return groupPackageMap;
  197.     }
  198.      
  199.     /**
  200.      * Search for package name in the sorted regular expression 
  201.      * list, if found return the group name.  If not, return null.
  202.      * 
  203.      * @param pkgName Name of package to be found in the regular 
  204.      * expression list.
  205.      */
  206.     static String regExpGroupName(String pkgName) { 
  207.         for (int j = 0; j < sortedRegExpList.size(); j++) {
  208.             String regexp = (String)sortedRegExpList.get(j); 
  209.             if (pkgName.startsWith(regexp)) {
  210.                 return (String)regExpGroupMap.get(regexp);
  211.             } 
  212.         }
  213.         return null;
  214.     }
  215.  
  216.     /**
  217.      * For the given group name, return the package list, on which it is mapped.
  218.      * Create a new list, if not found.
  219.      * 
  220.      * @param map Map to be searched for gorup name.
  221.      * @param groupname Group name to search.
  222.      */
  223.     static List getPkgList(Map map, String groupname) {
  224.         List list = (List)map.get(groupname);
  225.         if (list == null) {
  226.             list = new ArrayList();
  227.             map.put(groupname, list);
  228.         }
  229.         return list;
  230.     }
  231.  
  232.     /**
  233.      * Return the list of groups, in the same order as specified on the command 
  234.      * line.
  235.      */
  236.     public static List getGroupList() {
  237.         return groupList;
  238.     }
  239.     
  240.     /**                 
  241.      * Retireve the message string from the resource bundle.
  242.      *
  243.      * @param text Key for the resource message string.
  244.      * @param arg1 Argument to be substituted in the resource message string.
  245.      * @param arg2 Argument to be substituted in the resource message string.
  246.      */
  247.     private static String getText(String text, String arg1, String arg2) {
  248.         return Standard.configuration().standardmessage.getText(text, 
  249.                                                                 arg1, arg2);
  250.     }   
  251.  
  252.     /**                 
  253.      * Retireve the message string from the resource bundle.
  254.      *
  255.      * @param text Key for the resource message string.
  256.      * @param arg  Argument to be substituted in the resource message string.
  257.      */
  258.     private static String getText(String text, String arg) {
  259.         return Standard.configuration().standardmessage.getText(text, arg);
  260.     }   
  261.  
  262.     /**                 
  263.      * Retireve the message string from the resource bundle.
  264.      *
  265.      * @param text Key for the resource message string.
  266.      */
  267.     private static String getText(String text) {
  268.         return Standard.configuration().standardmessage.getText(text);
  269.     }   
  270.         
  271.  
  272.