Tue, 09 Oct 2012 19:10:00 -0700
8000663: clean up langtools imports
Reviewed-by: darcy
1 /*
2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.doclets.internal.toolkit.util;
28 import java.util.*;
30 import com.sun.javadoc.*;
31 import com.sun.tools.doclets.internal.toolkit.*;
33 /**
34 * Build the mapping of each Unicode character with it's member lists
35 * containing members names starting with it. Also build a list for all the
36 * Unicode characters which start a member name. Member name is
37 * classkind or field or method or constructor name.
38 *
39 * This code is not part of an API.
40 * It is implementation that is subject to change.
41 * Do not use it as an API
42 *
43 * @since 1.2
44 * @see java.lang.Character
45 * @author Atul M Dambalkar
46 */
47 public class IndexBuilder {
49 /**
50 * Mapping of each Unicode Character with the member list containing
51 * members with names starting with it.
52 */
53 private Map<Character,List<Doc>> indexmap = new HashMap<Character,List<Doc>>();
55 /**
56 * Don't generate deprecated information if true.
57 */
58 private boolean noDeprecated;
60 /**
61 * Build this Index only for classes?
62 */
63 private boolean classesOnly;
65 // make ProgramElementDoc[] when new toArray is available
66 protected final Object[] elements;
68 /**
69 * A comparator used to sort classes and members.
70 * Note: Maybe this compare code belongs in the tool?
71 */
72 private class DocComparator implements Comparator<Doc> {
73 public int compare(Doc d1, Doc d2) {
74 String doc1 = d1.name();
75 String doc2 = d2.name();
76 int compareResult;
77 if ((compareResult = doc1.compareToIgnoreCase(doc2)) != 0) {
78 return compareResult;
79 } else if (d1 instanceof ProgramElementDoc && d2 instanceof ProgramElementDoc) {
80 doc1 = (((ProgramElementDoc) d1).qualifiedName());
81 doc2 = (((ProgramElementDoc) d2).qualifiedName());
82 return doc1.compareToIgnoreCase(doc2);
83 } else {
84 return 0;
85 }
86 }
87 }
89 /**
90 * Constructor. Build the index map.
91 *
92 * @param configuration the current configuration of the doclet.
93 * @param noDeprecated true if -nodeprecated option is used,
94 * false otherwise.
95 */
96 public IndexBuilder(Configuration configuration, boolean noDeprecated) {
97 this(configuration, noDeprecated, false);
98 }
100 /**
101 * Constructor. Build the index map.
102 *
103 * @param configuration the current configuration of the doclet.
104 * @param noDeprecated true if -nodeprecated option is used,
105 * false otherwise.
106 * @param classesOnly Include only classes in index.
107 */
108 public IndexBuilder(Configuration configuration, boolean noDeprecated,
109 boolean classesOnly) {
110 if (classesOnly) {
111 configuration.message.notice("doclet.Building_Index_For_All_Classes");
112 } else {
113 configuration.message.notice("doclet.Building_Index");
114 }
115 this.noDeprecated = noDeprecated;
116 this.classesOnly = classesOnly;
117 buildIndexMap(configuration.root);
118 Set<Character> set = indexmap.keySet();
119 elements = set.toArray();
120 Arrays.sort(elements);
121 }
123 /**
124 * Sort the index map. Traverse the index map for all it's elements and
125 * sort each element which is a list.
126 */
127 protected void sortIndexMap() {
128 for (Iterator<List<Doc>> it = indexmap.values().iterator(); it.hasNext(); ) {
129 Collections.sort(it.next(), new DocComparator());
130 }
131 }
133 /**
134 * Get all the members in all the Packages and all the Classes
135 * given on the command line. Form separate list of those members depending
136 * upon their names.
137 *
138 * @param root Root of the documemt.
139 */
140 protected void buildIndexMap(RootDoc root) {
141 PackageDoc[] packages = root.specifiedPackages();
142 ClassDoc[] classes = root.classes();
143 if (!classesOnly) {
144 if (packages.length == 0) {
145 Set<PackageDoc> set = new HashSet<PackageDoc>();
146 PackageDoc pd;
147 for (int i = 0; i < classes.length; i++) {
148 pd = classes[i].containingPackage();
149 if (pd != null && pd.name().length() > 0) {
150 set.add(pd);
151 }
152 }
153 adjustIndexMap(set.toArray(packages));
154 } else {
155 adjustIndexMap(packages);
156 }
157 }
158 adjustIndexMap(classes);
159 if (!classesOnly) {
160 for (int i = 0; i < classes.length; i++) {
161 if (shouldAddToIndexMap(classes[i])) {
162 putMembersInIndexMap(classes[i]);
163 }
164 }
165 }
166 sortIndexMap();
167 }
169 /**
170 * Put all the members(fields, methods and constructors) in the classdoc
171 * to the indexmap.
172 *
173 * @param classdoc ClassDoc whose members will be added to the indexmap.
174 */
175 protected void putMembersInIndexMap(ClassDoc classdoc) {
176 adjustIndexMap(classdoc.fields());
177 adjustIndexMap(classdoc.methods());
178 adjustIndexMap(classdoc.constructors());
179 }
182 /**
183 * Adjust list of members according to their names. Check the first
184 * character in a member name, and then add the member to a list of members
185 * for that particular unicode character.
186 *
187 * @param elements Array of members.
188 */
189 protected void adjustIndexMap(Doc[] elements) {
190 for (int i = 0; i < elements.length; i++) {
191 if (shouldAddToIndexMap(elements[i])) {
192 String name = elements[i].name();
193 char ch = (name.length()==0)?
194 '*' :
195 Character.toUpperCase(name.charAt(0));
196 Character unicode = new Character(ch);
197 List<Doc> list = indexmap.get(unicode);
198 if (list == null) {
199 list = new ArrayList<Doc>();
200 indexmap.put(unicode, list);
201 }
202 list.add(elements[i]);
203 }
204 }
205 }
207 /**
208 * Should this doc element be added to the index map?
209 */
210 protected boolean shouldAddToIndexMap(Doc element) {
211 if (element instanceof PackageDoc)
212 // Do not add to index map if -nodeprecated option is set and the
213 // package is marked as deprecated.
214 return !(noDeprecated && Util.isDeprecated(element));
215 else
216 // Do not add to index map if -nodeprecated option is set and if the
217 // Doc is marked as deprecated or the containing package is marked as
218 // deprecated.
219 return !(noDeprecated &&
220 (Util.isDeprecated(element) ||
221 Util.isDeprecated(((ProgramElementDoc)element).containingPackage())));
222 }
224 /**
225 * Return a map of all the individual member lists with Unicode character.
226 *
227 * @return Map index map.
228 */
229 public Map<Character,List<Doc>> getIndexMap() {
230 return indexmap;
231 }
233 /**
234 * Return the sorted list of members, for passed Unicode Character.
235 *
236 * @param index index Unicode character.
237 * @return List member list for specific Unicode character.
238 */
239 public List<Doc> getMemberList(Character index) {
240 return indexmap.get(index);
241 }
243 /**
244 * Array of IndexMap keys, Unicode characters.
245 */
246 public Object[] elements() {
247 return elements;
248 }
249 }