blob: 9eef0ceacc0b73ce5dbc8a461400aaaccd4992b6 [file] [log] [blame]
The Android Open Source Project88b60792009-03-03 19:28:42 -08001/*
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
17import org.clearsilver.HDF;
18
19import java.util.ArrayList;
20
21public class NavTree {
22
23 public static void writeNavTree(String dir) {
24 ArrayList<Node> children = new ArrayList();
25 for (PackageInfo pkg: DroidDoc.choosePackages()) {
26 children.add(makePackageNode(pkg));
27 }
28 Node node = new Node("Reference", dir + "packages.html", children);
29
30 StringBuilder buf = new StringBuilder();
31 if (false) {
32 // if you want a root node
33 buf.append("[");
34 node.render(buf);
35 buf.append("]");
36 } else {
37 // if you don't want a root node
38 node.renderChildren(buf);
39 }
40
41 HDF data = DroidDoc.makeHDF();
42 data.setValue("reference_tree", buf.toString());
43 ClearPage.write(data, "navtree_data.cs", "navtree_data.js");
44 }
45
46 private static Node makePackageNode(PackageInfo pkg) {
47 ArrayList<Node> children = new ArrayList();
48
49 children.add(new Node("Description", pkg.fullDescriptionHtmlPage(), null));
50
51 addClassNodes(children, "Interfaces", pkg.interfaces());
52 addClassNodes(children, "Classes", pkg.ordinaryClasses());
53 addClassNodes(children, "Enums", pkg.enums());
54 addClassNodes(children, "Exceptions", pkg.exceptions());
55 addClassNodes(children, "Errors", pkg.errors());
56
57 return new Node(pkg.name(), pkg.htmlPage(), children);
58 }
59
60 private static void addClassNodes(ArrayList<Node> parent, String label, ClassInfo[] classes) {
61 ArrayList<Node> children = new ArrayList();
62
63 for (ClassInfo cl: classes) {
64 if (cl.checkLevel()) {
65 children.add(new Node(cl.name(), cl.htmlPage(), null));
66 }
67 }
68
69 if (children.size() > 0) {
70 parent.add(new Node(label, null, children));
71 }
72 }
73
74 private static class Node {
75 private String mLabel;
76 private String mLink;
77 ArrayList<Node> mChildren;
78
79 Node(String label, String link, ArrayList<Node> children) {
80 mLabel = label;
81 mLink = link;
82 mChildren = children;
83 }
84
85 static void renderString(StringBuilder buf, String s) {
86 if (s == null) {
87 buf.append("null");
88 } else {
89 buf.append('"');
90 final int N = s.length();
91 for (int i=0; i<N; i++) {
92 char c = s.charAt(i);
93 if (c >= ' ' && c <= '~' && c != '"' && c != '\\') {
94 buf.append(c);
95 } else {
96 buf.append("\\u");
97 for (int j=0; i<4; i++) {
98 char x = (char)(c & 0x000f);
99 if (x > 10) {
100 x = (char)(x - 10 + 'a');
101 } else {
102 x = (char)(x + '0');
103 }
104 buf.append(x);
105 c >>= 4;
106 }
107 }
108 }
109 buf.append('"');
110 }
111 }
112
113 void renderChildren(StringBuilder buf) {
114 ArrayList<Node> list = mChildren;
115 if (list == null || list.size() == 0) {
116 // We output null for no children. That way empty lists here can just
117 // be a byproduct of how we generate the lists.
118 buf.append("null");
119 } else {
120 buf.append("[ ");
121 final int N = list.size();
122 for (int i=0; i<N; i++) {
123 list.get(i).render(buf);
124 if (i != N-1) {
125 buf.append(", ");
126 }
127 }
128 buf.append(" ]\n");
129 }
130 }
131
132 void render(StringBuilder buf) {
133 buf.append("[ ");
134 renderString(buf, mLabel);
135 buf.append(", ");
136 renderString(buf, mLink);
137 buf.append(", ");
138 renderChildren(buf);
139 buf.append(" ]");
140 }
141 }
142}