Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 import java.util.HashMap;
     18 import java.util.TreeSet;
     19 import java.util.Set;
     20 import org.clearsilver.HDF;
     21 
     22 public class Hierarchy
     23 {
     24     public static void makeHierarchy(HDF hdf, ClassInfo[] classes)
     25     {
     26         HashMap<String,TreeSet<String>> nodes
     27                                     = new HashMap<String,TreeSet<String>>();
     28 
     29         for (ClassInfo cl: classes) {
     30             String name = cl.qualifiedName();
     31 
     32             TreeSet<String> me = nodes.get(name);
     33             if (me == null) {
     34                 me = new TreeSet<String>();
     35                 nodes.put(name, me);
     36             }
     37 
     38             ClassInfo superclass = cl.superclass();
     39             String sname = superclass != null
     40                                     ? superclass.qualifiedName() : null;
     41             if (sname != null) {
     42                 TreeSet<String> s = nodes.get(sname);
     43                 if (s == null) {
     44                     s = new TreeSet<String>();
     45                     nodes.put(sname, s);
     46                 }
     47                 s.add(name);
     48             }
     49         }
     50 
     51         /*
     52         Set<String> keys = nodes.keySet();
     53         for (String n: keys) {
     54             System.out.println("class: " + n);
     55 
     56             TreeSet<String> values = nodes.get(n);
     57             for (String v: values) {
     58                 System.out.println("       - " + v);
     59             }
     60         }
     61         */
     62 
     63         int depth = depth(nodes, "java.lang.Object");
     64 
     65         hdf.setValue("classes.0", "");
     66         hdf.setValue("colspan", "" + depth);
     67 
     68         recurse(nodes, "java.lang.Object", hdf.getObj("classes.0"),depth,depth);
     69 
     70         if (false) {
     71             Set<String> keys = nodes.keySet();
     72             if (keys.size() > 0) {
     73                 System.err.println("The following classes are hidden but"
     74                         + " are superclasses of not-hidden classes");
     75                 for (String n: keys) {
     76                     System.err.println("  " + n);
     77                 }
     78             }
     79         }
     80     }
     81 
     82     private static int depth(HashMap<String,TreeSet<String>> nodes,
     83                                 String name)
     84     {
     85         int d = 0;
     86         TreeSet<String> derived = nodes.get(name);
     87         if (derived != null && derived.size() > 0) {
     88             for (String s: derived) {
     89                 int n = depth(nodes, s);
     90                 if (n > d) {
     91                     d = n;
     92                 }
     93             }
     94         }
     95         return d + 1;
     96     }
     97 
     98     private static boolean exists(ClassInfo cl)
     99     {
    100         return cl != null && !cl.isHidden() && cl.isIncluded();
    101     }
    102 
    103     private static void recurse(HashMap<String,TreeSet<String>> nodes,
    104                                 String name, HDF hdf,
    105                                 int totalDepth, int remainingDepth)
    106     {
    107         int i;
    108 
    109         hdf.setValue("indent", "" + (totalDepth-remainingDepth-1));
    110         hdf.setValue("colspan", "" + remainingDepth);
    111 
    112         ClassInfo cl = Converter.obtainClass(name);
    113 
    114         hdf.setValue("class.label", cl.name());
    115         hdf.setValue("class.qualified", cl.qualifiedName());
    116         if (cl.checkLevel()) {
    117             hdf.setValue("class.link", cl.htmlPage());
    118         }
    119 
    120         if (exists(cl)) {
    121             hdf.setValue("exists", "1");
    122         }
    123 
    124         i = 0;
    125         for (ClassInfo iface: cl.interfaces()) {
    126             hdf.setValue("interfaces." + i + ".class.label", iface.name());
    127             hdf.setValue("interfaces." + i + ".class.qualified", iface.qualifiedName());
    128             if (iface.checkLevel()) {
    129                 hdf.setValue("interfaces." + i + ".class.link", iface.htmlPage());
    130             }
    131             if (exists(cl)) {
    132                 hdf.setValue("interfaces." + i + ".exists", "1");
    133             }
    134             i++;
    135         }
    136 
    137         TreeSet<String> derived = nodes.get(name);
    138         if (derived != null && derived.size() > 0) {
    139             hdf.setValue("derived", "");
    140             HDF children = hdf.getObj("derived");
    141             i = 0;
    142             remainingDepth--;
    143             for (String s: derived) {
    144                 String index = "" + i;
    145                 children.setValue(index, "");
    146                 recurse(nodes, s, children.getObj(index), totalDepth,
    147                         remainingDepth);
    148                 i++;
    149             }
    150         }
    151 
    152         nodes.remove(name);
    153     }
    154 }
    155 
    156