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

  1. /*
  2.  * @(#)IndexBuilder.java    1.12 98/09/22
  3.  *
  4.  * Copyright 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;
  16.  
  17. import com.sun.javadoc.*;
  18. import java.io.*;
  19. import java.lang.*;
  20. import java.util.*;
  21.  
  22. /**
  23.  * Build the mapping of each Unicode character with it's member lists
  24.  * containing members names starting with it. Also build a list for all the
  25.  * Unicode characters which start a member name. Member name is
  26.  * classkind or field or method or constructor name.
  27.  *
  28.  * @since JDK1.2
  29.  * @see java.lang.Character
  30.  * @author Atul M Dambalkar
  31.  */
  32. public class IndexBuilder {
  33.  
  34.     /**
  35.      * Mapping of each Unicode Character with the member list containing
  36.      * members with names starting with it.
  37.      */
  38.     private Map indexmap = new HashMap();
  39.  
  40.     /**
  41.      * Don't generate deprecated information if true.
  42.      */
  43.     private boolean noDeprecated;
  44.  
  45.     /**
  46.      * Build this Index only for classes?
  47.      */
  48.     private boolean classesOnly;
  49.  
  50.     // make ProgramElementDoc[] when new toArray is available
  51.     protected final Object[] elements;  
  52.  
  53.     /**
  54.      * Constructor. Build the index map.
  55.      *
  56.      * @param root Root of the Tree.
  57.      * @param noDeprecated true if -nodeprecated option is used,
  58.      * false otherwise.
  59.      */
  60.     public IndexBuilder(RootDoc root, boolean noDeprecated) {
  61.         this(root, noDeprecated, false);
  62.     }
  63.  
  64.     /**
  65.      * Constructor. Build the index map.
  66.      *
  67.      * @param root Root of the Tree.
  68.      * @param noDeprecated true if -nodeprecated option is used,
  69.      * false otherwise.
  70.      * @param classesOnly Include only classes in index.
  71.      */
  72.     public IndexBuilder(RootDoc root, boolean noDeprecated, 
  73.                         boolean classesOnly) {
  74.         if (classesOnly) {
  75.             Configuration.message.
  76.                               notice("doclet.Building_Index_For_All_Classes");
  77.         } else {
  78.             Configuration.message.notice("doclet.Building_Index");
  79.         }
  80.         this.noDeprecated = noDeprecated;
  81.         this.classesOnly = classesOnly;
  82.         buildIndexMap(root);
  83.         Set set = indexmap.keySet();
  84.         elements =  set.toArray();
  85.         Arrays.sort(elements);
  86.     }
  87.  
  88.     /**
  89.      * Sort the index map. Traverse the index map for all it's elements and
  90.      * sort each element which is a list.
  91.      */
  92.     protected void sortIndexMap() {
  93.         for (Iterator it = indexmap.values().iterator(); it.hasNext(); ) {
  94.             Collections.sort((List)it.next());
  95.         } 
  96.     }
  97.  
  98.     /**
  99.      * Get all the members in all the Packages and all the Classes 
  100.      * given on the command line. Form separate list of those members depending
  101.      * upon their names.
  102.      *
  103.      * @param root Root of the documemt.
  104.      */
  105.     protected void buildIndexMap(RootDoc root)  {
  106.         PackageDoc[] packages = root.specifiedPackages();
  107.         ClassDoc[] classes = root.classes();
  108.         List list = new ArrayList();
  109.         if (!classesOnly) {
  110.             adjustIndexMap(packages);
  111.         }
  112.         adjustIndexMap(classes);
  113.         if (!classesOnly) {
  114.             for (int i = 0; i < classes.length; i++) {
  115.                 if (shouldAddToIndexMap(classes[i])) {
  116.                     putMembersInIndexMap(classes[i]);
  117.                 }
  118.             }
  119.         }
  120.         sortIndexMap();
  121.     }
  122.  
  123.     /**
  124.      * Put all the members(fields, methods and constructors) in the classdoc
  125.      * to the indexmap. 
  126.      *
  127.      * @param classdoc ClassDoc whose members will be added to the indexmap.
  128.      */
  129.     protected void putMembersInIndexMap(ClassDoc classdoc) {
  130.         adjustIndexMap(classdoc.fields());
  131.         adjustIndexMap(classdoc.methods());
  132.         adjustIndexMap(classdoc.constructors());
  133.     }
  134.         
  135.  
  136.     /**
  137.      * Adjust list of members according to their names. Check the first
  138.      * character in a member name, and then add the member to a list of members
  139.      * for that particular unicode character.
  140.      * 
  141.      * @param elements Array of members.
  142.      */
  143.     protected void adjustIndexMap(Doc[] elements) {
  144.         for (int i = 0; i < elements.length; i++) {
  145.             if (shouldAddToIndexMap(elements[i])) {
  146.                 String name = elements[i].name();
  147.                 char ch = (name.length()==0)? 
  148.                     '*' : 
  149.                     Character.toUpperCase(name.charAt(0));
  150.                 Character unicode = new Character(ch);
  151.                 List list = (List)indexmap.get(unicode); 
  152.                 if (list == null) {
  153.                     list = new ArrayList();
  154.                     indexmap.put(unicode, list); 
  155.                 }     
  156.                 list.add(elements[i]);
  157.             }
  158.         }
  159.     }
  160.     
  161.     /**
  162.      * Should this doc element be added to the index map?
  163.      */ 
  164.     protected boolean shouldAddToIndexMap(Doc element) {
  165.         return !(noDeprecated && element.tags("deprecated").length > 0);
  166.     }
  167.  
  168.     /**
  169.      * Return a map of all the individual member lists with Unicode character.
  170.      *
  171.      * @return Map index map.
  172.      */ 
  173.     public Map getIndexMap() {
  174.         return indexmap;
  175.     }
  176.  
  177.     /**
  178.      * Return the sorted list of members, for passed Unicode Character. 
  179.      *
  180.      * @param index index Unicode character.
  181.      * @return List member list for specific Unicode character.
  182.      */
  183.     public List getMemberList(Character index) {
  184.         return (List)indexmap.get(index);
  185.     }
  186.  
  187.     /**
  188.      * Array of IndexMap keys, Unicode characters.
  189.      */
  190.     public Object[] elements() {
  191.         return elements;
  192.     }
  193. }
  194.