duke@1: /*
jjg@1357: * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1: *
duke@1: * This code is free software; you can redistribute it and/or modify it
duke@1: * under the terms of the GNU General Public License version 2 only, as
ohair@554: * published by the Free Software Foundation. Oracle designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
ohair@554: * by Oracle in the LICENSE file that accompanied this code.
duke@1: *
duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1: * version 2 for more details (a copy is included in the LICENSE file that
duke@1: * accompanied this code).
duke@1: *
duke@1: * You should have received a copy of the GNU General Public License version
duke@1: * 2 along with this work; if not, write to the Free Software Foundation,
duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1: *
ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554: * or visit www.oracle.com if you need additional information or have any
ohair@554: * questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.doclets.internal.toolkit.util;
duke@1:
jjg@1357: import java.util.*;
jjg@1357:
jjg@1357: import com.sun.javadoc.*;
duke@1: import com.sun.tools.doclets.internal.toolkit.*;
duke@1:
duke@1: /**
duke@1: * Build the mapping of each Unicode character with it's member lists
duke@1: * containing members names starting with it. Also build a list for all the
duke@1: * Unicode characters which start a member name. Member name is
duke@1: * classkind or field or method or constructor name.
duke@1: *
jjg@1359: *
This is NOT part of any supported API.
jjg@1359: * If you write code that depends on this, you do so at your own risk.
jjg@1359: * This code and its internal interfaces are subject to change or
jjg@1359: * deletion without notice.
duke@1: *
duke@1: * @since 1.2
duke@1: * @see java.lang.Character
duke@1: * @author Atul M Dambalkar
duke@1: */
duke@1: public class IndexBuilder {
duke@1:
duke@1: /**
duke@1: * Mapping of each Unicode Character with the member list containing
duke@1: * members with names starting with it.
duke@1: */
jjg@74: private Map> indexmap = new HashMap>();
duke@1:
duke@1: /**
duke@1: * Don't generate deprecated information if true.
duke@1: */
duke@1: private boolean noDeprecated;
duke@1:
duke@1: /**
duke@1: * Build this Index only for classes?
duke@1: */
duke@1: private boolean classesOnly;
duke@1:
duke@1: // make ProgramElementDoc[] when new toArray is available
duke@1: protected final Object[] elements;
duke@1:
duke@1: /**
duke@1: * A comparator used to sort classes and members.
duke@1: * Note: Maybe this compare code belongs in the tool?
duke@1: */
jjg@74: private class DocComparator implements Comparator {
jjg@74: public int compare(Doc d1, Doc d2) {
jjg@74: String doc1 = d1.name();
jjg@74: String doc2 = d2.name();
duke@1: int compareResult;
duke@1: if ((compareResult = doc1.compareToIgnoreCase(doc2)) != 0) {
duke@1: return compareResult;
duke@1: } else if (d1 instanceof ProgramElementDoc && d2 instanceof ProgramElementDoc) {
duke@1: doc1 = (((ProgramElementDoc) d1).qualifiedName());
duke@1: doc2 = (((ProgramElementDoc) d2).qualifiedName());
duke@1: return doc1.compareToIgnoreCase(doc2);
duke@1: } else {
duke@1: return 0;
duke@1: }
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Constructor. Build the index map.
duke@1: *
duke@1: * @param configuration the current configuration of the doclet.
duke@1: * @param noDeprecated true if -nodeprecated option is used,
duke@1: * false otherwise.
duke@1: */
duke@1: public IndexBuilder(Configuration configuration, boolean noDeprecated) {
duke@1: this(configuration, noDeprecated, false);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Constructor. Build the index map.
duke@1: *
duke@1: * @param configuration the current configuration of the doclet.
duke@1: * @param noDeprecated true if -nodeprecated option is used,
duke@1: * false otherwise.
duke@1: * @param classesOnly Include only classes in index.
duke@1: */
duke@1: public IndexBuilder(Configuration configuration, boolean noDeprecated,
duke@1: boolean classesOnly) {
duke@1: if (classesOnly) {
duke@1: configuration.message.notice("doclet.Building_Index_For_All_Classes");
duke@1: } else {
duke@1: configuration.message.notice("doclet.Building_Index");
duke@1: }
duke@1: this.noDeprecated = noDeprecated;
duke@1: this.classesOnly = classesOnly;
duke@1: buildIndexMap(configuration.root);
mcimadamore@184: Set set = indexmap.keySet();
duke@1: elements = set.toArray();
duke@1: Arrays.sort(elements);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Sort the index map. Traverse the index map for all it's elements and
duke@1: * sort each element which is a list.
duke@1: */
duke@1: protected void sortIndexMap() {
jjg@74: for (Iterator> it = indexmap.values().iterator(); it.hasNext(); ) {
jjg@74: Collections.sort(it.next(), new DocComparator());
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Get all the members in all the Packages and all the Classes
duke@1: * given on the command line. Form separate list of those members depending
duke@1: * upon their names.
duke@1: *
duke@1: * @param root Root of the documemt.
duke@1: */
duke@1: protected void buildIndexMap(RootDoc root) {
duke@1: PackageDoc[] packages = root.specifiedPackages();
duke@1: ClassDoc[] classes = root.classes();
duke@1: if (!classesOnly) {
duke@1: if (packages.length == 0) {
jjg@74: Set set = new HashSet();
duke@1: PackageDoc pd;
duke@1: for (int i = 0; i < classes.length; i++) {
duke@1: pd = classes[i].containingPackage();
duke@1: if (pd != null && pd.name().length() > 0) {
duke@1: set.add(pd);
duke@1: }
duke@1: }
jjg@74: adjustIndexMap(set.toArray(packages));
duke@1: } else {
duke@1: adjustIndexMap(packages);
duke@1: }
duke@1: }
duke@1: adjustIndexMap(classes);
duke@1: if (!classesOnly) {
duke@1: for (int i = 0; i < classes.length; i++) {
duke@1: if (shouldAddToIndexMap(classes[i])) {
duke@1: putMembersInIndexMap(classes[i]);
duke@1: }
duke@1: }
duke@1: }
duke@1: sortIndexMap();
duke@1: }
duke@1:
duke@1: /**
duke@1: * Put all the members(fields, methods and constructors) in the classdoc
duke@1: * to the indexmap.
duke@1: *
duke@1: * @param classdoc ClassDoc whose members will be added to the indexmap.
duke@1: */
duke@1: protected void putMembersInIndexMap(ClassDoc classdoc) {
duke@1: adjustIndexMap(classdoc.fields());
duke@1: adjustIndexMap(classdoc.methods());
duke@1: adjustIndexMap(classdoc.constructors());
duke@1: }
duke@1:
duke@1:
duke@1: /**
duke@1: * Adjust list of members according to their names. Check the first
duke@1: * character in a member name, and then add the member to a list of members
duke@1: * for that particular unicode character.
duke@1: *
duke@1: * @param elements Array of members.
duke@1: */
duke@1: protected void adjustIndexMap(Doc[] elements) {
duke@1: for (int i = 0; i < elements.length; i++) {
duke@1: if (shouldAddToIndexMap(elements[i])) {
duke@1: String name = elements[i].name();
duke@1: char ch = (name.length()==0)?
duke@1: '*' :
duke@1: Character.toUpperCase(name.charAt(0));
duke@1: Character unicode = new Character(ch);
jjg@74: List list = indexmap.get(unicode);
duke@1: if (list == null) {
jjg@74: list = new ArrayList();
duke@1: indexmap.put(unicode, list);
duke@1: }
duke@1: list.add(elements[i]);
duke@1: }
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Should this doc element be added to the index map?
duke@1: */
duke@1: protected boolean shouldAddToIndexMap(Doc element) {
bpatel@995: if (element instanceof PackageDoc)
bpatel@995: // Do not add to index map if -nodeprecated option is set and the
bpatel@995: // package is marked as deprecated.
bpatel@995: return !(noDeprecated && Util.isDeprecated(element));
bpatel@995: else
bpatel@995: // Do not add to index map if -nodeprecated option is set and if the
bpatel@995: // Doc is marked as deprecated or the containing package is marked as
bpatel@995: // deprecated.
bpatel@995: return !(noDeprecated &&
bpatel@995: (Util.isDeprecated(element) ||
bpatel@995: Util.isDeprecated(((ProgramElementDoc)element).containingPackage())));
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return a map of all the individual member lists with Unicode character.
duke@1: *
duke@1: * @return Map index map.
duke@1: */
mcimadamore@184: public Map> getIndexMap() {
duke@1: return indexmap;
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return the sorted list of members, for passed Unicode Character.
duke@1: *
duke@1: * @param index index Unicode character.
duke@1: * @return List member list for specific Unicode character.
duke@1: */
mcimadamore@184: public List getMemberList(Character index) {
mcimadamore@184: return indexmap.get(index);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Array of IndexMap keys, Unicode characters.
duke@1: */
duke@1: public Object[] elements() {
duke@1: return elements;
duke@1: }
duke@1: }