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

Tue, 09 Oct 2012 19:31:58 -0700

author
jjg
date
Tue, 09 Oct 2012 19:31:58 -0700
changeset 1358
fc123bdeddb8
parent 1357
c75be5bc5283
child 1359
25e14ad23cef
permissions
-rw-r--r--

8000208: fix langtools javadoc comment issues
Reviewed-by: bpatel, mcimadamore

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

mercurial