NavTree.java revision 03fdd1f95e8785c57eeabcec94e5f91ddf4a6ad4
1/* 2 * Copyright (C) 2010 Google Inc. 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 17package com.google.doclava; 18 19import com.google.clearsilver.jsilver.data.Data; 20 21import java.util.ArrayList; 22import java.util.List; 23import java.util.SortedMap; 24import java.util.TreeMap; 25 26public class NavTree { 27 28 public static void writeNavTree(String dir, String refPrefix) { 29 List<Node> children = new ArrayList<Node>(); 30 for (PackageInfo pkg : Doclava.choosePackages()) { 31 children.add(makePackageNode(pkg)); 32 } 33 Node node = new Node("Reference", dir + refPrefix + "packages.html", children, null); 34 35 StringBuilder buf = new StringBuilder(); 36 if (false) { 37 // if you want a root node 38 buf.append("["); 39 node.render(buf); 40 buf.append("]"); 41 } else { 42 // if you don't want a root node 43 node.renderChildren(buf); 44 } 45 46 Data data = Doclava.makeHDF(); 47 data.setValue("reference_tree", buf.toString()); 48 if (refPrefix == "gms-"){ 49 ClearPage.write(data, "gms_navtree_data.cs", "gms_navtree_data.js"); 50 } else if (refPrefix == "gcm-"){ 51 ClearPage.write(data, "gcm_navtree_data.cs", "gcm_navtree_data.js"); 52 } else { 53 ClearPage.write(data, "navtree_data.cs", "navtree_data.js"); 54 } 55 } 56 57 /** 58 * Write the YAML formatted navigation tree. 59 * @see "http://yaml.org/" 60 */ 61 public static void writeYamlTree(String dir, String fileName){ 62 Data data = Doclava.makeHDF(); 63 ClassInfo[] classes = Converter.rootClasses(); 64 65 SortedMap<String, Object> sorted = new TreeMap<String, Object>(); 66 for (ClassInfo cl : classes) { 67 if (cl.isHiddenOrRemoved()) { 68 continue; 69 } 70 sorted.put(cl.qualifiedName(), cl); 71 72 PackageInfo pkg = cl.containingPackage(); 73 String name; 74 if (pkg == null) { 75 name = ""; 76 } else { 77 name = pkg.name(); 78 } 79 sorted.put(name, pkg); 80 } 81 82 data = makeYamlHDF(sorted, "docs.pages", data); 83 if ((Doclava.USE_DEVSITE_LOCALE_OUTPUT_PATHS) && (Doclava.testSupportRef)) { 84 dir = Doclava.ensureSlash(dir) + Doclava.testSupportPath; 85 } else if ((Doclava.USE_DEVSITE_LOCALE_OUTPUT_PATHS) && (Doclava.wearableSupportRef)) { 86 dir = Doclava.ensureSlash(dir) + Doclava.wearableSupportPath; 87 } 88 ClearPage.write(data, "yaml_navtree.cs", Doclava.ensureSlash(dir) + fileName); 89 } 90 91 public static Data makeYamlHDF(SortedMap<String, Object> sorted, String base, Data data) { 92 93 String key = "docs.pages."; 94 int i = 0; 95 for (String s : sorted.keySet()) { 96 Object o = sorted.get(s); 97 98 if (o instanceof PackageInfo) { 99 PackageInfo pkg = (PackageInfo) o; 100 101 data.setValue("docs.pages." + i + ".id", "" + i); 102 data.setValue("docs.pages." + i + ".label", pkg.name()); 103 data.setValue("docs.pages." + i + ".shortname", "API"); 104 data.setValue("docs.pages." + i + ".apilevel", pkg.getSince()); 105 data.setValue("docs.pages." + i + ".link", pkg.htmlPage()); 106 data.setValue("docs.pages." + i + ".type", "package"); 107 } else if (o instanceof ClassInfo) { 108 ClassInfo cl = (ClassInfo) o; 109 110 // skip classes that are the child of another class, recursion will handle those. 111 if (cl.containingClass() == null){ 112 113 data.setValue("docs.pages." + i + ".id", "" + i); 114 data = makeYamlHDF(cl, "docs.pages."+i, data); 115 } 116 } 117 118 i++; 119 } 120 return data; 121 } 122 123 public static Data makeYamlHDF(ClassInfo cl, String base, Data data) { 124 data.setValue(base + ".label", cl.name()); 125 data.setValue(base + ".shortname", cl.name().substring(cl.name().lastIndexOf(".")+1)); 126 data.setValue(base + ".link", cl.htmlPage()); 127 data.setValue(base + ".type", cl.kind()); 128 129 if (cl.innerClasses().size() > 0){ 130 int j = 0; 131 for (ClassInfo cl2 : cl.innerClasses()){ 132 data = makeYamlHDF(cl2, base + ".children." + j, data); 133 j++; 134 } 135 } 136 137 return data; 138 } 139 private static Node makePackageNode(PackageInfo pkg) { 140 List<Node> children = new ArrayList<Node>(); 141 142 addClassNodes(children, "Annotations", pkg.annotations()); 143 addClassNodes(children, "Interfaces", pkg.interfaces()); 144 addClassNodes(children, "Classes", pkg.ordinaryClasses()); 145 addClassNodes(children, "Enums", pkg.enums()); 146 addClassNodes(children, "Exceptions", pkg.exceptions()); 147 addClassNodes(children, "Errors", pkg.errors()); 148 149 return new Node(pkg.name(), pkg.htmlPage(), children, pkg.getSince()); 150 } 151 152 private static void addClassNodes(List<Node> parent, String label, ClassInfo[] classes) { 153 List<Node> children = new ArrayList<Node>(); 154 155 for (ClassInfo cl : classes) { 156 if (cl.checkLevel()) { 157 children.add(new Node(cl.name(), cl.htmlPage(), null, cl.getSince())); 158 } 159 } 160 161 if (children.size() > 0) { 162 parent.add(new Node(label, null, children, null)); 163 } 164 } 165 166 private static class Node { 167 private String mLabel; 168 private String mLink; 169 List<Node> mChildren; 170 private String mSince; 171 172 Node(String label, String link, List<Node> children, String since) { 173 mLabel = label; 174 mLink = link; 175 mChildren = children; 176 mSince = since; 177 } 178 179 static void renderString(StringBuilder buf, String s) { 180 if (s == null) { 181 buf.append("null"); 182 } else { 183 buf.append('"'); 184 final int N = s.length(); 185 for (int i = 0; i < N; i++) { 186 char c = s.charAt(i); 187 if (c >= ' ' && c <= '~' && c != '"' && c != '\\') { 188 buf.append(c); 189 } else { 190 buf.append("\\u"); 191 for (int j = 0; i < 4; i++) { 192 char x = (char) (c & 0x000f); 193 if (x >= 10) { 194 x = (char) (x - 10 + 'a'); 195 } else { 196 x = (char) (x + '0'); 197 } 198 buf.append(x); 199 c >>= 4; 200 } 201 } 202 } 203 buf.append('"'); 204 } 205 } 206 207 void renderChildren(StringBuilder buf) { 208 List<Node> list = mChildren; 209 if (list == null || list.size() == 0) { 210 // We output null for no children. That way empty lists here can just 211 // be a byproduct of how we generate the lists. 212 buf.append("null"); 213 } else { 214 buf.append("[ "); 215 final int N = list.size(); 216 for (int i = 0; i < N; i++) { 217 list.get(i).render(buf); 218 if (i != N - 1) { 219 buf.append(", "); 220 } 221 } 222 buf.append(" ]\n"); 223 } 224 } 225 226 void render(StringBuilder buf) { 227 buf.append("[ "); 228 renderString(buf, mLabel); 229 buf.append(", "); 230 renderString(buf, mLink); 231 buf.append(", "); 232 renderChildren(buf); 233 buf.append(", "); 234 renderString(buf, mSince); 235 buf.append(" ]"); 236 } 237 } 238} 239