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

changeset 1
9a66ca7c79fa
child 74
5a9172b251dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/IndexBuilder.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,238 @@
     1.4 +/*
     1.5 + * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.doclets.internal.toolkit.util;
    1.30 +
    1.31 +import com.sun.tools.doclets.internal.toolkit.*;
    1.32 +import com.sun.javadoc.*;
    1.33 +import java.util.*;
    1.34 +
    1.35 +/**
    1.36 + * Build the mapping of each Unicode character with it's member lists
    1.37 + * containing members names starting with it. Also build a list for all the
    1.38 + * Unicode characters which start a member name. Member name is
    1.39 + * classkind or field or method or constructor name.
    1.40 + *
    1.41 + * This code is not part of an API.
    1.42 + * It is implementation that is subject to change.
    1.43 + * Do not use it as an API
    1.44 + *
    1.45 + * @since 1.2
    1.46 + * @see java.lang.Character
    1.47 + * @author Atul M Dambalkar
    1.48 + */
    1.49 +public class IndexBuilder {
    1.50 +
    1.51 +    /**
    1.52 +     * Mapping of each Unicode Character with the member list containing
    1.53 +     * members with names starting with it.
    1.54 +     */
    1.55 +    private Map indexmap = new HashMap();
    1.56 +
    1.57 +    /**
    1.58 +     * Don't generate deprecated information if true.
    1.59 +     */
    1.60 +    private boolean noDeprecated;
    1.61 +
    1.62 +    /**
    1.63 +     * Build this Index only for classes?
    1.64 +     */
    1.65 +    private boolean classesOnly;
    1.66 +
    1.67 +    // make ProgramElementDoc[] when new toArray is available
    1.68 +    protected final Object[] elements;
    1.69 +
    1.70 +    /**
    1.71 +     * A comparator used to sort classes and members.
    1.72 +     * Note:  Maybe this compare code belongs in the tool?
    1.73 +     */
    1.74 +    private class DocComparator implements Comparator {
    1.75 +        public int compare(Object d1, Object d2) {
    1.76 +            String doc1 = (((Doc) d1).name());
    1.77 +            String doc2 = (((Doc) d2).name());
    1.78 +            int compareResult;
    1.79 +            if ((compareResult = doc1.compareToIgnoreCase(doc2)) != 0) {
    1.80 +                return compareResult;
    1.81 +            } else if (d1 instanceof ProgramElementDoc && d2 instanceof ProgramElementDoc) {
    1.82 +                 doc1 = (((ProgramElementDoc) d1).qualifiedName());
    1.83 +                 doc2 = (((ProgramElementDoc) d2).qualifiedName());
    1.84 +                 return doc1.compareToIgnoreCase(doc2);
    1.85 +            } else {
    1.86 +                return 0;
    1.87 +            }
    1.88 +        }
    1.89 +    }
    1.90 +
    1.91 +    /**
    1.92 +     * Constructor. Build the index map.
    1.93 +     *
    1.94 +     * @param configuration the current configuration of the doclet.
    1.95 +     * @param noDeprecated  true if -nodeprecated option is used,
    1.96 +     *                      false otherwise.
    1.97 +     */
    1.98 +    public IndexBuilder(Configuration configuration, boolean noDeprecated) {
    1.99 +        this(configuration, noDeprecated, false);
   1.100 +    }
   1.101 +
   1.102 +    /**
   1.103 +     * Constructor. Build the index map.
   1.104 +     *
   1.105 +     * @param configuration the current configuration of the doclet.
   1.106 +     * @param noDeprecated  true if -nodeprecated option is used,
   1.107 +     *                      false otherwise.
   1.108 +     * @param classesOnly   Include only classes in index.
   1.109 +     */
   1.110 +    public IndexBuilder(Configuration configuration, boolean noDeprecated,
   1.111 +                        boolean classesOnly) {
   1.112 +        if (classesOnly) {
   1.113 +            configuration.message.notice("doclet.Building_Index_For_All_Classes");
   1.114 +        } else {
   1.115 +            configuration.message.notice("doclet.Building_Index");
   1.116 +        }
   1.117 +        this.noDeprecated = noDeprecated;
   1.118 +        this.classesOnly = classesOnly;
   1.119 +        buildIndexMap(configuration.root);
   1.120 +        Set set = indexmap.keySet();
   1.121 +        elements =  set.toArray();
   1.122 +        Arrays.sort(elements);
   1.123 +    }
   1.124 +
   1.125 +    /**
   1.126 +     * Sort the index map. Traverse the index map for all it's elements and
   1.127 +     * sort each element which is a list.
   1.128 +     */
   1.129 +    protected void sortIndexMap() {
   1.130 +        for (Iterator it = indexmap.values().iterator(); it.hasNext(); ) {
   1.131 +            Collections.sort((List)it.next(), new DocComparator());
   1.132 +        }
   1.133 +    }
   1.134 +
   1.135 +    /**
   1.136 +     * Get all the members in all the Packages and all the Classes
   1.137 +     * given on the command line. Form separate list of those members depending
   1.138 +     * upon their names.
   1.139 +     *
   1.140 +     * @param root Root of the documemt.
   1.141 +     */
   1.142 +    protected void buildIndexMap(RootDoc root)  {
   1.143 +        PackageDoc[] packages = root.specifiedPackages();
   1.144 +        ClassDoc[] classes = root.classes();
   1.145 +        if (!classesOnly) {
   1.146 +            if (packages.length == 0) {
   1.147 +                Set set = new HashSet();
   1.148 +                PackageDoc pd;
   1.149 +                for (int i = 0; i < classes.length; i++) {
   1.150 +                    pd = classes[i].containingPackage();
   1.151 +                    if (pd != null && pd.name().length() > 0) {
   1.152 +                        set.add(pd);
   1.153 +                    }
   1.154 +                }
   1.155 +                adjustIndexMap((PackageDoc[]) set.toArray(packages));
   1.156 +            } else {
   1.157 +                adjustIndexMap(packages);
   1.158 +            }
   1.159 +        }
   1.160 +        adjustIndexMap(classes);
   1.161 +        if (!classesOnly) {
   1.162 +            for (int i = 0; i < classes.length; i++) {
   1.163 +                if (shouldAddToIndexMap(classes[i])) {
   1.164 +                    putMembersInIndexMap(classes[i]);
   1.165 +                }
   1.166 +            }
   1.167 +        }
   1.168 +        sortIndexMap();
   1.169 +    }
   1.170 +
   1.171 +    /**
   1.172 +     * Put all the members(fields, methods and constructors) in the classdoc
   1.173 +     * to the indexmap.
   1.174 +     *
   1.175 +     * @param classdoc ClassDoc whose members will be added to the indexmap.
   1.176 +     */
   1.177 +    protected void putMembersInIndexMap(ClassDoc classdoc) {
   1.178 +        adjustIndexMap(classdoc.fields());
   1.179 +        adjustIndexMap(classdoc.methods());
   1.180 +        adjustIndexMap(classdoc.constructors());
   1.181 +    }
   1.182 +
   1.183 +
   1.184 +    /**
   1.185 +     * Adjust list of members according to their names. Check the first
   1.186 +     * character in a member name, and then add the member to a list of members
   1.187 +     * for that particular unicode character.
   1.188 +     *
   1.189 +     * @param elements Array of members.
   1.190 +     */
   1.191 +    protected void adjustIndexMap(Doc[] elements) {
   1.192 +        for (int i = 0; i < elements.length; i++) {
   1.193 +            if (shouldAddToIndexMap(elements[i])) {
   1.194 +                String name = elements[i].name();
   1.195 +                char ch = (name.length()==0)?
   1.196 +                    '*' :
   1.197 +                    Character.toUpperCase(name.charAt(0));
   1.198 +                Character unicode = new Character(ch);
   1.199 +                List list = (List)indexmap.get(unicode);
   1.200 +                if (list == null) {
   1.201 +                    list = new ArrayList();
   1.202 +                    indexmap.put(unicode, list);
   1.203 +                }
   1.204 +                list.add(elements[i]);
   1.205 +            }
   1.206 +        }
   1.207 +    }
   1.208 +
   1.209 +    /**
   1.210 +     * Should this doc element be added to the index map?
   1.211 +     */
   1.212 +    protected boolean shouldAddToIndexMap(Doc element) {
   1.213 +        return !(noDeprecated && element.tags("deprecated").length > 0);
   1.214 +    }
   1.215 +
   1.216 +    /**
   1.217 +     * Return a map of all the individual member lists with Unicode character.
   1.218 +     *
   1.219 +     * @return Map index map.
   1.220 +     */
   1.221 +    public Map getIndexMap() {
   1.222 +        return indexmap;
   1.223 +    }
   1.224 +
   1.225 +    /**
   1.226 +     * Return the sorted list of members, for passed Unicode Character.
   1.227 +     *
   1.228 +     * @param index index Unicode character.
   1.229 +     * @return List member list for specific Unicode character.
   1.230 +     */
   1.231 +    public List getMemberList(Character index) {
   1.232 +        return (List)indexmap.get(index);
   1.233 +    }
   1.234 +
   1.235 +    /**
   1.236 +     * Array of IndexMap keys, Unicode characters.
   1.237 +     */
   1.238 +    public Object[] elements() {
   1.239 +        return elements;
   1.240 +    }
   1.241 +}

mercurial