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

Mon, 15 Oct 2012 17:07:55 -0700

author
jjg
date
Mon, 15 Oct 2012 17:07:55 -0700
changeset 1364
8db45b13526e
parent 1359
25e14ad23cef
child 1606
ccbe7ffdd867
permissions
-rw-r--r--

8000666: javadoc should write directly to Writer instead of composing strings
Reviewed-by: bpatel

duke@1 1 /*
jjg@1357 2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.doclets.internal.toolkit.util;
duke@1 27
jjg@1357 28 import java.util.*;
jjg@1357 29
jjg@1357 30 import com.sun.javadoc.*;
duke@1 31 import com.sun.tools.doclets.internal.toolkit.*;
duke@1 32
duke@1 33 /**
duke@1 34 * Build the mapping of each Unicode character with it's member lists
duke@1 35 * containing members names starting with it. Also build a list for all the
duke@1 36 * Unicode characters which start a member name. Member name is
duke@1 37 * classkind or field or method or constructor name.
duke@1 38 *
jjg@1359 39 * <p><b>This is NOT part of any supported API.
jjg@1359 40 * If you write code that depends on this, you do so at your own risk.
jjg@1359 41 * This code and its internal interfaces are subject to change or
jjg@1359 42 * deletion without notice.</b>
duke@1 43 *
duke@1 44 * @since 1.2
duke@1 45 * @see java.lang.Character
duke@1 46 * @author Atul M Dambalkar
duke@1 47 */
duke@1 48 public class IndexBuilder {
duke@1 49
duke@1 50 /**
duke@1 51 * Mapping of each Unicode Character with the member list containing
duke@1 52 * members with names starting with it.
duke@1 53 */
jjg@74 54 private Map<Character,List<Doc>> indexmap = new HashMap<Character,List<Doc>>();
duke@1 55
duke@1 56 /**
duke@1 57 * Don't generate deprecated information if true.
duke@1 58 */
duke@1 59 private boolean noDeprecated;
duke@1 60
duke@1 61 /**
duke@1 62 * Build this Index only for classes?
duke@1 63 */
duke@1 64 private boolean classesOnly;
duke@1 65
duke@1 66 // make ProgramElementDoc[] when new toArray is available
duke@1 67 protected final Object[] elements;
duke@1 68
duke@1 69 /**
duke@1 70 * A comparator used to sort classes and members.
duke@1 71 * Note: Maybe this compare code belongs in the tool?
duke@1 72 */
jjg@74 73 private class DocComparator implements Comparator<Doc> {
jjg@74 74 public int compare(Doc d1, Doc d2) {
jjg@74 75 String doc1 = d1.name();
jjg@74 76 String doc2 = d2.name();
duke@1 77 int compareResult;
duke@1 78 if ((compareResult = doc1.compareToIgnoreCase(doc2)) != 0) {
duke@1 79 return compareResult;
duke@1 80 } else if (d1 instanceof ProgramElementDoc && d2 instanceof ProgramElementDoc) {
duke@1 81 doc1 = (((ProgramElementDoc) d1).qualifiedName());
duke@1 82 doc2 = (((ProgramElementDoc) d2).qualifiedName());
duke@1 83 return doc1.compareToIgnoreCase(doc2);
duke@1 84 } else {
duke@1 85 return 0;
duke@1 86 }
duke@1 87 }
duke@1 88 }
duke@1 89
duke@1 90 /**
duke@1 91 * Constructor. Build the index map.
duke@1 92 *
duke@1 93 * @param configuration the current configuration of the doclet.
duke@1 94 * @param noDeprecated true if -nodeprecated option is used,
duke@1 95 * false otherwise.
duke@1 96 */
duke@1 97 public IndexBuilder(Configuration configuration, boolean noDeprecated) {
duke@1 98 this(configuration, noDeprecated, false);
duke@1 99 }
duke@1 100
duke@1 101 /**
duke@1 102 * Constructor. Build the index map.
duke@1 103 *
duke@1 104 * @param configuration the current configuration of the doclet.
duke@1 105 * @param noDeprecated true if -nodeprecated option is used,
duke@1 106 * false otherwise.
duke@1 107 * @param classesOnly Include only classes in index.
duke@1 108 */
duke@1 109 public IndexBuilder(Configuration configuration, boolean noDeprecated,
duke@1 110 boolean classesOnly) {
duke@1 111 if (classesOnly) {
duke@1 112 configuration.message.notice("doclet.Building_Index_For_All_Classes");
duke@1 113 } else {
duke@1 114 configuration.message.notice("doclet.Building_Index");
duke@1 115 }
duke@1 116 this.noDeprecated = noDeprecated;
duke@1 117 this.classesOnly = classesOnly;
duke@1 118 buildIndexMap(configuration.root);
mcimadamore@184 119 Set<Character> set = indexmap.keySet();
duke@1 120 elements = set.toArray();
duke@1 121 Arrays.sort(elements);
duke@1 122 }
duke@1 123
duke@1 124 /**
duke@1 125 * Sort the index map. Traverse the index map for all it's elements and
duke@1 126 * sort each element which is a list.
duke@1 127 */
duke@1 128 protected void sortIndexMap() {
jjg@74 129 for (Iterator<List<Doc>> it = indexmap.values().iterator(); it.hasNext(); ) {
jjg@74 130 Collections.sort(it.next(), new DocComparator());
duke@1 131 }
duke@1 132 }
duke@1 133
duke@1 134 /**
duke@1 135 * Get all the members in all the Packages and all the Classes
duke@1 136 * given on the command line. Form separate list of those members depending
duke@1 137 * upon their names.
duke@1 138 *
duke@1 139 * @param root Root of the documemt.
duke@1 140 */
duke@1 141 protected void buildIndexMap(RootDoc root) {
duke@1 142 PackageDoc[] packages = root.specifiedPackages();
duke@1 143 ClassDoc[] classes = root.classes();
duke@1 144 if (!classesOnly) {
duke@1 145 if (packages.length == 0) {
jjg@74 146 Set<PackageDoc> set = new HashSet<PackageDoc>();
duke@1 147 PackageDoc pd;
duke@1 148 for (int i = 0; i < classes.length; i++) {
duke@1 149 pd = classes[i].containingPackage();
duke@1 150 if (pd != null && pd.name().length() > 0) {
duke@1 151 set.add(pd);
duke@1 152 }
duke@1 153 }
jjg@74 154 adjustIndexMap(set.toArray(packages));
duke@1 155 } else {
duke@1 156 adjustIndexMap(packages);
duke@1 157 }
duke@1 158 }
duke@1 159 adjustIndexMap(classes);
duke@1 160 if (!classesOnly) {
duke@1 161 for (int i = 0; i < classes.length; i++) {
duke@1 162 if (shouldAddToIndexMap(classes[i])) {
duke@1 163 putMembersInIndexMap(classes[i]);
duke@1 164 }
duke@1 165 }
duke@1 166 }
duke@1 167 sortIndexMap();
duke@1 168 }
duke@1 169
duke@1 170 /**
duke@1 171 * Put all the members(fields, methods and constructors) in the classdoc
duke@1 172 * to the indexmap.
duke@1 173 *
duke@1 174 * @param classdoc ClassDoc whose members will be added to the indexmap.
duke@1 175 */
duke@1 176 protected void putMembersInIndexMap(ClassDoc classdoc) {
duke@1 177 adjustIndexMap(classdoc.fields());
duke@1 178 adjustIndexMap(classdoc.methods());
duke@1 179 adjustIndexMap(classdoc.constructors());
duke@1 180 }
duke@1 181
duke@1 182
duke@1 183 /**
duke@1 184 * Adjust list of members according to their names. Check the first
duke@1 185 * character in a member name, and then add the member to a list of members
duke@1 186 * for that particular unicode character.
duke@1 187 *
duke@1 188 * @param elements Array of members.
duke@1 189 */
duke@1 190 protected void adjustIndexMap(Doc[] elements) {
duke@1 191 for (int i = 0; i < elements.length; i++) {
duke@1 192 if (shouldAddToIndexMap(elements[i])) {
duke@1 193 String name = elements[i].name();
duke@1 194 char ch = (name.length()==0)?
duke@1 195 '*' :
duke@1 196 Character.toUpperCase(name.charAt(0));
duke@1 197 Character unicode = new Character(ch);
jjg@74 198 List<Doc> list = indexmap.get(unicode);
duke@1 199 if (list == null) {
jjg@74 200 list = new ArrayList<Doc>();
duke@1 201 indexmap.put(unicode, list);
duke@1 202 }
duke@1 203 list.add(elements[i]);
duke@1 204 }
duke@1 205 }
duke@1 206 }
duke@1 207
duke@1 208 /**
duke@1 209 * Should this doc element be added to the index map?
duke@1 210 */
duke@1 211 protected boolean shouldAddToIndexMap(Doc element) {
bpatel@995 212 if (element instanceof PackageDoc)
bpatel@995 213 // Do not add to index map if -nodeprecated option is set and the
bpatel@995 214 // package is marked as deprecated.
bpatel@995 215 return !(noDeprecated && Util.isDeprecated(element));
bpatel@995 216 else
bpatel@995 217 // Do not add to index map if -nodeprecated option is set and if the
bpatel@995 218 // Doc is marked as deprecated or the containing package is marked as
bpatel@995 219 // deprecated.
bpatel@995 220 return !(noDeprecated &&
bpatel@995 221 (Util.isDeprecated(element) ||
bpatel@995 222 Util.isDeprecated(((ProgramElementDoc)element).containingPackage())));
duke@1 223 }
duke@1 224
duke@1 225 /**
duke@1 226 * Return a map of all the individual member lists with Unicode character.
duke@1 227 *
duke@1 228 * @return Map index map.
duke@1 229 */
mcimadamore@184 230 public Map<Character,List<Doc>> getIndexMap() {
duke@1 231 return indexmap;
duke@1 232 }
duke@1 233
duke@1 234 /**
duke@1 235 * Return the sorted list of members, for passed Unicode Character.
duke@1 236 *
duke@1 237 * @param index index Unicode character.
duke@1 238 * @return List member list for specific Unicode character.
duke@1 239 */
mcimadamore@184 240 public List<Doc> getMemberList(Character index) {
mcimadamore@184 241 return indexmap.get(index);
duke@1 242 }
duke@1 243
duke@1 244 /**
duke@1 245 * Array of IndexMap keys, Unicode characters.
duke@1 246 */
duke@1 247 public Object[] elements() {
duke@1 248 return elements;
duke@1 249 }
duke@1 250 }

mercurial