001 /*
002 * Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package com.sun.tools.doclets.internal.toolkit.util;
027
028 import com.sun.tools.doclets.internal.toolkit.*;
029 import com.sun.javadoc.*;
030 import java.util.*;
031
032 /**
033 * Build the mapping of each Unicode character with it's member lists
034 * containing members names starting with it. Also build a list for all the
035 * Unicode characters which start a member name. Member name is
036 * classkind or field or method or constructor name.
037 *
038 * This code is not part of an API.
039 * It is implementation that is subject to change.
040 * Do not use it as an API
041 *
042 * @since 1.2
043 * @see java.lang.Character
044 * @author Atul M Dambalkar
045 */
046 public class IndexBuilder {
047
048 /**
049 * Mapping of each Unicode Character with the member list containing
050 * members with names starting with it.
051 */
052 private Map<Character,List<Doc>> indexmap = new HashMap<Character,List<Doc>>();
053
054 /**
055 * Don't generate deprecated information if true.
056 */
057 private boolean noDeprecated;
058
059 /**
060 * Build this Index only for classes?
061 */
062 private boolean classesOnly;
063
064 // make ProgramElementDoc[] when new toArray is available
065 protected final Object[] elements;
066
067 /**
068 * A comparator used to sort classes and members.
069 * Note: Maybe this compare code belongs in the tool?
070 */
071 private class DocComparator implements Comparator<Doc> {
072 public int compare(Doc d1, Doc d2) {
073 String doc1 = d1.name();
074 String doc2 = d2.name();
075 int compareResult;
076 if ((compareResult = doc1.compareToIgnoreCase(doc2)) != 0) {
077 return compareResult;
078 } else if (d1 instanceof ProgramElementDoc && d2 instanceof ProgramElementDoc) {
079 doc1 = (((ProgramElementDoc) d1).qualifiedName());
080 doc2 = (((ProgramElementDoc) d2).qualifiedName());
081 return doc1.compareToIgnoreCase(doc2);
082 } else {
083 return 0;
084 }
085 }
086 }
087
088 /**
089 * Constructor. Build the index map.
090 *
091 * @param configuration the current configuration of the doclet.
092 * @param noDeprecated true if -nodeprecated option is used,
093 * false otherwise.
094 */
095 public IndexBuilder(Configuration configuration, boolean noDeprecated) {
096 this(configuration, noDeprecated, false);
097 }
098
099 /**
100 * Constructor. Build the index map.
101 *
102 * @param configuration the current configuration of the doclet.
103 * @param noDeprecated true if -nodeprecated option is used,
104 * false otherwise.
105 * @param classesOnly Include only classes in index.
106 */
107 public IndexBuilder(Configuration configuration, boolean noDeprecated,
108 boolean classesOnly) {
109 if (classesOnly) {
110 configuration.message.notice("doclet.Building_Index_For_All_Classes");
111 } else {
112 configuration.message.notice("doclet.Building_Index");
113 }
114 this.noDeprecated = noDeprecated;
115 this.classesOnly = classesOnly;
116 buildIndexMap(configuration.root);
117 Set<Character> set = indexmap.keySet();
118 elements = set.toArray();
119 Arrays.sort(elements);
120 }
121
122 /**
123 * Sort the index map. Traverse the index map for all it's elements and
124 * sort each element which is a list.
125 */
126 protected void sortIndexMap() {
127 for (Iterator<List<Doc>> it = indexmap.values().iterator(); it.hasNext(); ) {
128 Collections.sort(it.next(), new DocComparator());
129 }
130 }
131
132 /**
133 * Get all the members in all the Packages and all the Classes
134 * given on the command line. Form separate list of those members depending
135 * upon their names.
136 *
137 * @param root Root of the documemt.
138 */
139 protected void buildIndexMap(RootDoc root) {
140 PackageDoc[] packages = root.specifiedPackages();
141 ClassDoc[] classes = root.classes();
142 if (!classesOnly) {
143 if (packages.length == 0) {
144 Set<PackageDoc> set = new HashSet<PackageDoc>();
145 PackageDoc pd;
146 for (int i = 0; i < classes.length; i++) {
147 pd = classes[i].containingPackage();
148 if (pd != null && pd.name().length() > 0) {
149 set.add(pd);
150 }
151 }
152 adjustIndexMap(set.toArray(packages));
153 } else {
154 adjustIndexMap(packages);
155 }
156 }
157 adjustIndexMap(classes);
158 if (!classesOnly) {
159 for (int i = 0; i < classes.length; i++) {
160 if (shouldAddToIndexMap(classes[i])) {
161 putMembersInIndexMap(classes[i]);
162 }
163 }
164 }
165 sortIndexMap();
166 }
167
168 /**
169 * Put all the members(fields, methods and constructors) in the classdoc
170 * to the indexmap.
171 *
172 * @param classdoc ClassDoc whose members will be added to the indexmap.
173 */
174 protected void putMembersInIndexMap(ClassDoc classdoc) {
175 adjustIndexMap(classdoc.fields());
176 adjustIndexMap(classdoc.methods());
177 adjustIndexMap(classdoc.constructors());
178 }
179
180
181 /**
182 * Adjust list of members according to their names. Check the first
183 * character in a member name, and then add the member to a list of members
184 * for that particular unicode character.
185 *
186 * @param elements Array of members.
187 */
188 protected void adjustIndexMap(Doc[] elements) {
189 for (int i = 0; i < elements.length; i++) {
190 if (shouldAddToIndexMap(elements[i])) {
191 String name = elements[i].name();
192 char ch = (name.length()==0)?
193 '*' :
194 Character.toUpperCase(name.charAt(0));
195 Character unicode = new Character(ch);
196 List<Doc> list = indexmap.get(unicode);
197 if (list == null) {
198 list = new ArrayList<Doc>();
199 indexmap.put(unicode, list);
200 }
201 list.add(elements[i]);
202 }
203 }
204 }
205
206 /**
207 * Should this doc element be added to the index map?
208 */
209 protected boolean shouldAddToIndexMap(Doc element) {
210 return !(noDeprecated && element.tags("deprecated").length > 0);
211 }
212
213 /**
214 * Return a map of all the individual member lists with Unicode character.
215 *
216 * @return Map index map.
217 */
218 public Map<Character,List<Doc>> getIndexMap() {
219 return indexmap;
220 }
221
222 /**
223 * Return the sorted list of members, for passed Unicode Character.
224 *
225 * @param index index Unicode character.
226 * @return List member list for specific Unicode character.
227 */
228 public List<Doc> getMemberList(Character index) {
229 return indexmap.get(index);
230 }
231
232 /**
233 * Array of IndexMap keys, Unicode characters.
234 */
235 public Object[] elements() {
236 return elements;
237 }
238 }