Mon, 15 Oct 2012 17:07:55 -0700
8000666: javadoc should write directly to Writer instead of composing strings
Reviewed-by: bpatel
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 * <p><b>This is NOT part of any supported API.
40 * If you write code that depends on this, you do so at your own risk.
41 * This code and its internal interfaces are subject to change or
42 * deletion without notice.</b>
43 *
44 * @since 1.2
45 * @see java.lang.Character
46 * @author Atul M Dambalkar
47 */
48 public class IndexBuilder {
50 /**
51 * Mapping of each Unicode Character with the member list containing
52 * members with names starting with it.
53 */
54 private Map<Character,List<Doc>> indexmap = new HashMap<Character,List<Doc>>();
56 /**
57 * Don't generate deprecated information if true.
58 */
59 private boolean noDeprecated;
61 /**
62 * Build this Index only for classes?
63 */
64 private boolean classesOnly;
66 // make ProgramElementDoc[] when new toArray is available
67 protected final Object[] elements;
69 /**
70 * A comparator used to sort classes and members.
71 * Note: Maybe this compare code belongs in the tool?
72 */
73 private class DocComparator implements Comparator<Doc> {
74 public int compare(Doc d1, Doc d2) {
75 String doc1 = d1.name();
76 String doc2 = d2.name();
77 int compareResult;
78 if ((compareResult = doc1.compareToIgnoreCase(doc2)) != 0) {
79 return compareResult;
80 } else if (d1 instanceof ProgramElementDoc && d2 instanceof ProgramElementDoc) {
81 doc1 = (((ProgramElementDoc) d1).qualifiedName());
82 doc2 = (((ProgramElementDoc) d2).qualifiedName());
83 return doc1.compareToIgnoreCase(doc2);
84 } else {
85 return 0;
86 }
87 }
88 }
90 /**
91 * Constructor. Build the index map.
92 *
93 * @param configuration the current configuration of the doclet.
94 * @param noDeprecated true if -nodeprecated option is used,
95 * false otherwise.
96 */
97 public IndexBuilder(Configuration configuration, boolean noDeprecated) {
98 this(configuration, noDeprecated, false);
99 }
101 /**
102 * Constructor. Build the index map.
103 *
104 * @param configuration the current configuration of the doclet.
105 * @param noDeprecated true if -nodeprecated option is used,
106 * false otherwise.
107 * @param classesOnly Include only classes in index.
108 */
109 public IndexBuilder(Configuration configuration, boolean noDeprecated,
110 boolean classesOnly) {
111 if (classesOnly) {
112 configuration.message.notice("doclet.Building_Index_For_All_Classes");
113 } else {
114 configuration.message.notice("doclet.Building_Index");
115 }
116 this.noDeprecated = noDeprecated;
117 this.classesOnly = classesOnly;
118 buildIndexMap(configuration.root);
119 Set<Character> set = indexmap.keySet();
120 elements = set.toArray();
121 Arrays.sort(elements);
122 }
124 /**
125 * Sort the index map. Traverse the index map for all it's elements and
126 * sort each element which is a list.
127 */
128 protected void sortIndexMap() {
129 for (Iterator<List<Doc>> it = indexmap.values().iterator(); it.hasNext(); ) {
130 Collections.sort(it.next(), new DocComparator());
131 }
132 }
134 /**
135 * Get all the members in all the Packages and all the Classes
136 * given on the command line. Form separate list of those members depending
137 * upon their names.
138 *
139 * @param root Root of the documemt.
140 */
141 protected void buildIndexMap(RootDoc root) {
142 PackageDoc[] packages = root.specifiedPackages();
143 ClassDoc[] classes = root.classes();
144 if (!classesOnly) {
145 if (packages.length == 0) {
146 Set<PackageDoc> set = new HashSet<PackageDoc>();
147 PackageDoc pd;
148 for (int i = 0; i < classes.length; i++) {
149 pd = classes[i].containingPackage();
150 if (pd != null && pd.name().length() > 0) {
151 set.add(pd);
152 }
153 }
154 adjustIndexMap(set.toArray(packages));
155 } else {
156 adjustIndexMap(packages);
157 }
158 }
159 adjustIndexMap(classes);
160 if (!classesOnly) {
161 for (int i = 0; i < classes.length; i++) {
162 if (shouldAddToIndexMap(classes[i])) {
163 putMembersInIndexMap(classes[i]);
164 }
165 }
166 }
167 sortIndexMap();
168 }
170 /**
171 * Put all the members(fields, methods and constructors) in the classdoc
172 * to the indexmap.
173 *
174 * @param classdoc ClassDoc whose members will be added to the indexmap.
175 */
176 protected void putMembersInIndexMap(ClassDoc classdoc) {
177 adjustIndexMap(classdoc.fields());
178 adjustIndexMap(classdoc.methods());
179 adjustIndexMap(classdoc.constructors());
180 }
183 /**
184 * Adjust list of members according to their names. Check the first
185 * character in a member name, and then add the member to a list of members
186 * for that particular unicode character.
187 *
188 * @param elements Array of members.
189 */
190 protected void adjustIndexMap(Doc[] elements) {
191 for (int i = 0; i < elements.length; i++) {
192 if (shouldAddToIndexMap(elements[i])) {
193 String name = elements[i].name();
194 char ch = (name.length()==0)?
195 '*' :
196 Character.toUpperCase(name.charAt(0));
197 Character unicode = new Character(ch);
198 List<Doc> list = indexmap.get(unicode);
199 if (list == null) {
200 list = new ArrayList<Doc>();
201 indexmap.put(unicode, list);
202 }
203 list.add(elements[i]);
204 }
205 }
206 }
208 /**
209 * Should this doc element be added to the index map?
210 */
211 protected boolean shouldAddToIndexMap(Doc element) {
212 if (element instanceof PackageDoc)
213 // Do not add to index map if -nodeprecated option is set and the
214 // package is marked as deprecated.
215 return !(noDeprecated && Util.isDeprecated(element));
216 else
217 // Do not add to index map if -nodeprecated option is set and if the
218 // Doc is marked as deprecated or the containing package is marked as
219 // deprecated.
220 return !(noDeprecated &&
221 (Util.isDeprecated(element) ||
222 Util.isDeprecated(((ProgramElementDoc)element).containingPackage())));
223 }
225 /**
226 * Return a map of all the individual member lists with Unicode character.
227 *
228 * @return Map index map.
229 */
230 public Map<Character,List<Doc>> getIndexMap() {
231 return indexmap;
232 }
234 /**
235 * Return the sorted list of members, for passed Unicode Character.
236 *
237 * @param index index Unicode character.
238 * @return List member list for specific Unicode character.
239 */
240 public List<Doc> getMemberList(Character index) {
241 return indexmap.get(index);
242 }
244 /**
245 * Array of IndexMap keys, Unicode characters.
246 */
247 public Object[] elements() {
248 return elements;
249 }
250 }