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

changeset 1
9a66ca7c79fa
child 74
5a9172b251dd
equal deleted inserted replaced
-1:000000000000 1:9a66ca7c79fa
1 /*
2 * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package com.sun.tools.doclets.internal.toolkit.util;
27
28 import com.sun.tools.doclets.internal.toolkit.*;
29 import com.sun.javadoc.*;
30 import java.util.*;
31
32 /**
33 * Build the mapping of each Unicode character with it's member lists
34 * containing members names starting with it. Also build a list for all the
35 * Unicode characters which start a member name. Member name is
36 * classkind or field or method or constructor name.
37 *
38 * This code is not part of an API.
39 * It is implementation that is subject to change.
40 * Do not use it as an API
41 *
42 * @since 1.2
43 * @see java.lang.Character
44 * @author Atul M Dambalkar
45 */
46 public class IndexBuilder {
47
48 /**
49 * Mapping of each Unicode Character with the member list containing
50 * members with names starting with it.
51 */
52 private Map indexmap = new HashMap();
53
54 /**
55 * Don't generate deprecated information if true.
56 */
57 private boolean noDeprecated;
58
59 /**
60 * Build this Index only for classes?
61 */
62 private boolean classesOnly;
63
64 // make ProgramElementDoc[] when new toArray is available
65 protected final Object[] elements;
66
67 /**
68 * A comparator used to sort classes and members.
69 * Note: Maybe this compare code belongs in the tool?
70 */
71 private class DocComparator implements Comparator {
72 public int compare(Object d1, Object d2) {
73 String doc1 = (((Doc) d1).name());
74 String doc2 = (((Doc) d2).name());
75 int compareResult;
76 if ((compareResult = doc1.compareToIgnoreCase(doc2)) != 0) {
77 return compareResult;
78 } else if (d1 instanceof ProgramElementDoc && d2 instanceof ProgramElementDoc) {
79 doc1 = (((ProgramElementDoc) d1).qualifiedName());
80 doc2 = (((ProgramElementDoc) d2).qualifiedName());
81 return doc1.compareToIgnoreCase(doc2);
82 } else {
83 return 0;
84 }
85 }
86 }
87
88 /**
89 * Constructor. Build the index map.
90 *
91 * @param configuration the current configuration of the doclet.
92 * @param noDeprecated true if -nodeprecated option is used,
93 * false otherwise.
94 */
95 public IndexBuilder(Configuration configuration, boolean noDeprecated) {
96 this(configuration, noDeprecated, false);
97 }
98
99 /**
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 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 it = indexmap.values().iterator(); it.hasNext(); ) {
128 Collections.sort((List)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 set = new HashSet();
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((PackageDoc[]) 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 list = (List)indexmap.get(unicode);
197 if (list == null) {
198 list = new ArrayList();
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 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 getMemberList(Character index) {
229 return (List)indexmap.get(index);
230 }
231
232 /**
233 * Array of IndexMap keys, Unicode characters.
234 */
235 public Object[] elements() {
236 return elements;
237 }
238 }

mercurial