1577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite/*
2577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite * Copyright (C) 2016 Google Inc.
3577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite *
4577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite * Licensed under the Apache License, Version 2.0 (the "License");
5577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite * you may not use this file except in compliance with the License.
6577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite * You may obtain a copy of the License at
7577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite *
8577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite * http://www.apache.org/licenses/LICENSE-2.0
9577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite *
10577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite * Unless required by applicable law or agreed to in writing, software
11577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite * distributed under the License is distributed on an "AS IS" BASIS,
12577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite * See the License for the specific language governing permissions and
14577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite * limitations under the License.
15577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite */
16577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
17577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waitepackage com.google.doclava;
18577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
19577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waiteimport com.google.clearsilver.jsilver.data.Data;
20577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
21577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waiteimport java.util.ArrayList;
22577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
23577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite/**
24577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite* Class for writing a JSON dictionary of Android package/class/member info that can be used for
25577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite* dereferencing javadoc style {@link} tags.
26577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite*/
27577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
28577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waitepublic class AtLinksNavTree {
29577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
30577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  /**
31577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * Write a JSON dictionary of Android package/class/member info. The hierarchy will follow this
32577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * format: package name -> class name -> member name and signature -> parent package.class name
33577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * if the member was inherited (or an empty string if the member was not inherited).
34577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *
35577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * <p>For example:
36577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * <pre>{
37577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *   "android": {
38577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *     "Manifest": {
39577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *       "Manifest()": "",
40577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *       "clone()": "java.lang.Object",
41577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *       "equals(java.lang.Object)": "java.lang.Object",
42577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *       ...
43577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *     },
44577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *     ...
45577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *   },
46577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *   ...
47577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * }</pre>
48577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *
49577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * @param dir The directory path to prepend to the output path if the generated navtree is for
50a3e957579348ffd28a77fccd281cb7b37507169dScott Main  *        one the a supplemental library references (such as the wearable support library)
51577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  */
52577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  public static void writeAtLinksNavTree(String dir) {
53577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    StringBuilder buf = new StringBuilder();
54577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
55577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    buf.append("{");
56577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    addPackages(buf, Doclava.choosePackages());
57577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    buf.append("\n}");
58577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
59577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    Data data = Doclava.makeHDF();
60577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    data.setValue("navtree", buf.toString());
61577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
62577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    String output_path;
63a3e957579348ffd28a77fccd281cb7b37507169dScott Main    if (Doclava.USE_DEVSITE_LOCALE_OUTPUT_PATHS && (Doclava.libraryRoot != null)) {
64a3e957579348ffd28a77fccd281cb7b37507169dScott Main      output_path = dir + Doclava.libraryRoot + "at_links_navtree.json";
65577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    } else {
66577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite      output_path = "at_links_navtree.json";
67577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    }
68577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
69577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    ClearPage.write(data, "at_links_navtree.cs", output_path);
70577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  }
71577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
72577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  /**
73577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * Append the provided string builder with the navtree info for the provided list of packages.
74577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *
75577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * @param buf The string builder to append to.
76577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * @param packages A list of PackageInfo objects. Navtree info for each package will be appended
77577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *        to the provided string builder.
78577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  */
79577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  private static void addPackages(StringBuilder buf, PackageInfo[] packages) {
80577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    boolean is_first_package = true;
81577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    for (PackageInfo pkg : Doclava.choosePackages()) {
8262d562a0a14d8e644a5a1990fca955da19595675Elliot Waite      if (!pkg.name().contains(".internal.")) {
8362d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        if (!is_first_package) {
8462d562a0a14d8e644a5a1990fca955da19595675Elliot Waite          buf.append(",");
8562d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        }
8662d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        buf.append("\n  \"" + pkg.name() + "\": {");
8762d562a0a14d8e644a5a1990fca955da19595675Elliot Waite
8862d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        boolean is_first_class = true;
8962d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        is_first_class = addClasses(buf, pkg.annotations(), is_first_class);
9062d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        is_first_class = addClasses(buf, pkg.interfaces(), is_first_class);
9162d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        is_first_class = addClasses(buf, pkg.ordinaryClasses(), is_first_class);
9262d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        is_first_class = addClasses(buf, pkg.enums(), is_first_class);
9362d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        is_first_class = addClasses(buf, pkg.exceptions(), is_first_class);
9462d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        addClasses(buf, pkg.errors(), is_first_class);
9562d562a0a14d8e644a5a1990fca955da19595675Elliot Waite
9662d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        buf.append("\n  }");
9762d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        is_first_package = false;
98577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite      }
99577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    }
100577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  }
101577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
102577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  /**
103577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * Append the provided string builder with the navtree info for the provided list of classes.
104577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *
105577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * @param buf The string builder to append to.
106577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * @param classes A list of ClassInfo objects. Navtree info for each class will be appended
107577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *        to the provided string builder.
108577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * @param is_first_class True if this is the first child class listed under the parent package.
109577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  */
110577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  private static boolean addClasses(StringBuilder buf, ClassInfo[] classes,
111577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite      boolean is_first_class) {
112577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    for (ClassInfo cl : classes) {
11362d562a0a14d8e644a5a1990fca955da19595675Elliot Waite      if (!is_first_class) {
11462d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        buf.append(",");
11562d562a0a14d8e644a5a1990fca955da19595675Elliot Waite      }
11662d562a0a14d8e644a5a1990fca955da19595675Elliot Waite      buf.append("\n    \"" + cl.name() + "\": {");
117577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
11862d562a0a14d8e644a5a1990fca955da19595675Elliot Waite      boolean is_first_member = true;
11962d562a0a14d8e644a5a1990fca955da19595675Elliot Waite      is_first_member = addFields(buf, cl.fields(), is_first_member, cl);
12062d562a0a14d8e644a5a1990fca955da19595675Elliot Waite      is_first_member = addMethods(buf, cl.constructors(), is_first_member, cl);
12162d562a0a14d8e644a5a1990fca955da19595675Elliot Waite      addMethods(buf, cl.methods(), is_first_member, cl);
122577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
12362d562a0a14d8e644a5a1990fca955da19595675Elliot Waite      buf.append("\n    }");
12462d562a0a14d8e644a5a1990fca955da19595675Elliot Waite      is_first_class = false;
125577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    }
126577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    return is_first_class;
127577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  }
128577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
129577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  /**
130577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * Append the provided string builder with the navtree info for the provided list of fields.
131577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *
132577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * @param buf The string builder to append to.
133577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * @param fields A list of FieldInfo objects. Navtree info for each field will be appended
134577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *        to the provided string builder.
135577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * @param is_first_member True if this is the first child member listed under the parent class.
136577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * @param cl The ClassInfo object for the parent class of this field.
137577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  */
138577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  private static boolean addFields(StringBuilder buf, ArrayList<FieldInfo> fields,
139577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite      boolean is_first_member, ClassInfo cl) {
140577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    for (FieldInfo field : fields) {
14162d562a0a14d8e644a5a1990fca955da19595675Elliot Waite      if (!field.containingClass().qualifiedName().contains(".internal.")) {
14262d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        if (!is_first_member) {
14362d562a0a14d8e644a5a1990fca955da19595675Elliot Waite          buf.append(",");
14462d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        }
14562d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        buf.append("\n      \"" + field.name() + "\": \"");
14662d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        if (!field.containingClass().qualifiedName().equals(cl.qualifiedName())) {
14762d562a0a14d8e644a5a1990fca955da19595675Elliot Waite          buf.append(field.containingClass().qualifiedName());
14862d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        }
14962d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        buf.append("\"");
15062d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        is_first_member = false;
151577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite      }
152577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    }
153577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    return is_first_member;
154577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  }
155577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
156577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  /**
157577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * Append the provided string builder with the navtree info for the provided list of methods.
158577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *
159577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * @param buf The string builder to append to.
160577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * @param methods A list of MethodInfo objects. Navtree info for each method will be appended
161577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  *        to the provided string builder.
162577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * @param is_first_member True if this is the first child member listed under the parent class.
163577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  * @param cl The ClassInfo object for the parent class of this method.
164577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  */
165577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  private static boolean addMethods(StringBuilder buf, ArrayList<MethodInfo> methods,
166577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite      boolean is_first_member, ClassInfo cl) {
167577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    for (MethodInfo method : methods) {
16862d562a0a14d8e644a5a1990fca955da19595675Elliot Waite      if (!method.containingClass().qualifiedName().contains(".internal.")) {
16962d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        if (!is_first_member) {
17062d562a0a14d8e644a5a1990fca955da19595675Elliot Waite          buf.append(",");
17162d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        }
17262d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        buf.append("\n      \"" + method.name() + method.signature() + "\": \"");
17362d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        if (!method.containingClass().qualifiedName().equals(cl.qualifiedName())) {
17462d562a0a14d8e644a5a1990fca955da19595675Elliot Waite          buf.append(method.containingClass().qualifiedName());
17562d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        }
17662d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        buf.append("\"");
17762d562a0a14d8e644a5a1990fca955da19595675Elliot Waite        is_first_member = false;
178577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite      }
179577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    }
180577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite    return is_first_member;
181577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite  }
182577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite
183577bcebdf3f93ab8349a7c1781ff044272fee324Elliot Waite}
184