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

Sun, 24 Feb 2013 11:36:58 -0800

author
jjg
date
Sun, 24 Feb 2013 11:36:58 -0800
changeset 1606
ccbe7ffdd867
parent 1359
25e14ad23cef
child 2525
2eb010b6cb22
permissions
-rw-r--r--

7112427: The doclet needs to be able to generate JavaFX documentation.
Reviewed-by: jjg
Contributed-by: jan.valenta@oracle.com

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

mercurial