Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 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 | |
| 17 | package com.google.doclava; |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 18 | import com.google.clearsilver.jsilver.data.Data; |
| 19 | |
| 20 | import java.util.*; |
| 21 | import java.io.*; |
| 22 | |
| 23 | |
| 24 | public class SampleCode { |
| 25 | String mSource; |
| 26 | String mDest; |
| 27 | String mTitle; |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 28 | String mProjectDir; |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 29 | String mTags; |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 30 | |
| 31 | public SampleCode(String source, String dest, String title) { |
| 32 | mSource = source; |
| 33 | mTitle = title; |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 34 | mTags = null; |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 35 | |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 36 | int len = dest.length(); |
| 37 | if (len > 1 && dest.charAt(len - 1) != '/') { |
| 38 | mDest = dest + '/'; |
| 39 | } else { |
| 40 | mDest = dest; |
| 41 | } |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 42 | |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 43 | |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 44 | //System.out.println("SampleCode init: source: " + mSource); |
| 45 | //System.out.println("SampleCode init: dest: " + mDest); |
| 46 | //System.out.println("SampleCode init: title: " + mTitle); |
| 47 | |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 50 | public Node write(boolean offlineMode) { |
| 51 | List<Node> filelist = new ArrayList<Node>(); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 52 | File f = new File(mSource); |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 53 | mProjectDir = f.getName(); |
| 54 | String name = mProjectDir; |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 55 | String startname = name; |
| 56 | String subdir = mDest; |
| 57 | String mOut = subdir + name; |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 58 | if (!f.isDirectory()) { |
| 59 | System.out.println("-samplecode not a directory: " + mSource); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 60 | return null; |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 61 | } |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 62 | |
| 63 | if (offlineMode) |
| 64 | writeIndexOnly(f, mDest, offlineMode); |
| 65 | else { |
| 66 | Data hdf = Doclava.makeHDF(); |
| 67 | hdf.setValue("samples", "true"); |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 68 | hdf.setValue("projectDir", mProjectDir); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 69 | writeProjectDirectory(filelist, f, mDest, false, hdf, "Files."); |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 70 | writeProjectStructure(name, hdf); |
| 71 | getSummaryFromDir(hdf, f, "Files."); |
| 72 | writeProjectIndex(hdf); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 73 | hdf.removeTree("parentdirs"); |
| 74 | hdf.setValue("parentdirs.0.Name", name); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 75 | // return a root SC node for the sample with children appended |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 76 | return new Node(mProjectDir, "samples/" + startname + "/index.html", mTags, filelist, null); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 77 | } |
| 78 | return null; |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | public static String convertExtension(String s, String ext) { |
| 82 | return s.substring(0, s.lastIndexOf('.')) + ext; |
| 83 | } |
| 84 | |
| 85 | public static String[] IMAGES = {".png", ".jpg", ".gif"}; |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 86 | public static String[] TEMPLATED = {".java", ".xml", ".aidl", ".rs",".txt", ".TXT"}; |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 87 | |
| 88 | public static boolean inList(String s, String[] list) { |
| 89 | for (String t : list) { |
| 90 | if (s.endsWith(t)) { |
| 91 | return true; |
| 92 | } |
| 93 | } |
| 94 | return false; |
| 95 | } |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 96 | |
| 97 | public static String mapTypes(String name) { |
| 98 | String type = name.substring(name.lastIndexOf('.') + 1, name.length()); |
| 99 | if (type.equals("xml") || type.equals("java")) { |
| 100 | if (name.equals("AndroidManifest.xml")) type = "manifest"; |
| 101 | return type; |
| 102 | } else { |
| 103 | return type = "file"; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public void writeProjectDirectory(List<Node> parent, File dir, String relative, Boolean recursed, Data hdf, String newkey) { |
| 108 | TreeSet<String> dirs = new TreeSet<String>(); //dirs for project structure and breadcrumb |
| 109 | TreeSet<String> files = new TreeSet<String>(); //files for project structure and breadcrumb |
| 110 | |
| 111 | String subdir = relative; |
| 112 | String name = ""; |
| 113 | String label = ""; |
| 114 | String link = ""; |
| 115 | String type = ""; |
| 116 | int i = 0; |
| 117 | String expansion = ".Sub."; |
| 118 | String key = newkey; |
| 119 | |
| 120 | if (recursed) { |
| 121 | key = (key + expansion); |
| 122 | } else { |
| 123 | expansion = ""; |
| 124 | } |
| 125 | |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 126 | File[] dirContents = dir.listFiles(); |
| 127 | Arrays.sort(dirContents, byTypeAndName); |
| 128 | for (File f: dirContents) { |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 129 | name = f.getName(); |
| 130 | // don't process certain types of files |
| 131 | if (name.startsWith(".") || |
| 132 | name.startsWith("_") || |
| 133 | name.equals("default.properties") || |
| 134 | name.equals("build.properties") || |
| 135 | name.endsWith(".ttf") || |
| 136 | name.equals("Android.mk")) { |
| 137 | //System.out.println("Invalid File Type, bypassing: " + name); |
| 138 | continue; |
| 139 | } |
| 140 | if (f.isFile() && name.contains(".")){ |
| 141 | String path = relative + name; |
| 142 | type = mapTypes(name); |
| 143 | link = convertExtension(path, ".html"); |
| 144 | hdf.setValue("samples", "true");//dd needed? |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 145 | if (inList(path, IMAGES)) { |
| 146 | // copy these files to output directly |
| 147 | type = "img"; |
| 148 | ClearPage.copyFile(false, f, path); |
| 149 | writeImagePage(f, convertExtension(path, Doclava.htmlExtension), relative); |
| 150 | files.add(name); |
| 151 | hdf.setValue(key + i + ".Type", "img"); |
| 152 | hdf.setValue(key + i + ".Name", name); |
| 153 | hdf.setValue(key + i + ".Href", link); |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 154 | hdf.setValue(key + i + ".RelPath", relative); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 155 | } |
| 156 | if (inList(path, TEMPLATED)) { |
| 157 | // copied and goes through the template |
| 158 | ClearPage.copyFile(false, f, path); |
| 159 | writePage(f, convertExtension(path, Doclava.htmlExtension), relative); |
| 160 | files.add(name); |
| 161 | hdf.setValue(key + i + ".Type", type); |
| 162 | hdf.setValue(key + i + ".Name", name); |
| 163 | hdf.setValue(key + i + ".Href", link); |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 164 | hdf.setValue(key + i + ".RelPath", relative); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 165 | } |
| 166 | // add file to the navtree |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 167 | parent.add(new Node(name, link , null, null, type)); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 168 | i++; |
| 169 | } else if (f.isDirectory()) { |
| 170 | List<Node> mchildren = new ArrayList<Node>(); |
| 171 | type = "dir"; |
| 172 | String dirpath = relative + name; |
| 173 | link = dirpath + "/index.html"; |
| 174 | String hdfkeyName = (key + i + ".Name"); |
| 175 | String hdfkeyType = (key + i + ".Type"); |
| 176 | String hdfkeyHref = (key + i + ".Href"); |
| 177 | hdf.setValue(hdfkeyName, name); |
| 178 | hdf.setValue(hdfkeyType, type); |
| 179 | hdf.setValue(hdfkeyHref, relative + name + "/" + "index.html"); |
| 180 | //System.out.println("Found directory, recursing. Current key: " + hdfkeyName); |
| 181 | writeProjectDirectory(mchildren, f, relative + name + "/", true, hdf, (key + i)); |
| 182 | if (mchildren.size() > 0) { |
| 183 | //dir is processed, now add it to the navtree |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 184 | //don't link sidenav subdirs at this point (but can use "link" to do so) |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 185 | parent.add(new Node(name, null, null, mchildren, type)); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 186 | } |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 187 | dirs.add(name); |
| 188 | |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 189 | i++; |
| 190 | } |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 191 | |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 192 | } |
| 193 | //dd not working yet |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 194 | //Get summary from any _index files in any project dirs (currently disabled) |
| 195 | // getSummaryFromDir(hdf, dir, newkey); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 196 | //If this is an index for the project root (assumed root if split length is 3 (development/samples/nn)), |
| 197 | //then remove the root dir so that it won't appear in the breadcrumb. Else just pass it through to |
| 198 | //setParentDirs as usual. |
| 199 | String mpath = dir + ""; |
| 200 | String sdir[] = mpath.split("/"); |
| 201 | if (sdir.length == 3 ) { |
| 202 | System.out.println("-----------------> this must be the root: [sdir len]" + sdir.length + "[dir]" + dir); |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 203 | hdf.setValue("showProjectPaths","true");//dd remove here? |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 204 | } |
| 205 | setParentDirs(hdf, relative, name, false); |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 206 | //concatenate dirs in the navtree. Comment out or remove to restore |
| 207 | //normal nav tree. |
| 208 | squashNodes(parent); |
Scott Main | e27c950 | 2011-01-19 14:07:39 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 211 | public void writeProjectStructure(String dir, Data hdf) { |
| 212 | //System.out.println(">>-- writing project structure for " + dir ); |
| 213 | hdf.setValue("projectStructure", "true"); |
| 214 | hdf.setValue("projectDir", mProjectDir); |
| 215 | hdf.setValue("page.title", mProjectDir + " Structure"); |
| 216 | hdf.setValue("projectTitle", mTitle); |
| 217 | ClearPage.write(hdf, "sampleindex.cs", mDest + "project" + Doclava.htmlExtension); //write the project.html file |
| 218 | hdf.setValue("projectStructure", ""); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 221 | /** |
| 222 | * Write the project's templated _index.html |
| 223 | */ |
| 224 | public void writeProjectIndex(Data hdf) { |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 225 | //System.out.println(">>-- writing project index for " + mDest ); |
| 226 | hdf.setValue("projectDir", mProjectDir); |
| 227 | hdf.setValue("page.title", mProjectDir + " Sample"); |
| 228 | hdf.setValue("projectTitle", mTitle); |
| 229 | ClearPage.write(hdf, "sampleindex.cs", mDest + "index" + Doclava.htmlExtension); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 232 | /** |
| 233 | * Grab the contents of an _index.html summary from a dir. |
| 234 | */ |
| 235 | public void getSummaryFromDir(Data hdf, File dir, String key) { |
| 236 | //System.out.println("Getting summary for " + dir + "/_index.html"); |
| 237 | hdf.setValue("summary", ""); |
| 238 | hdf.setValue("summaryFlag", ""); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 239 | String filename = dir.getPath() + "/_index.html"; |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 240 | String summary = SampleTagInfo.readFile(new SourcePositionInfo(filename, |
| 241 | -1,-1), filename, "sample code", true, false, false, true); |
| 242 | if (summary != null) { |
| 243 | hdf.setValue(key + "SummaryFlag", "true"); |
| 244 | hdf.setValue("summary", summary); |
| 245 | //set the target for [info] link |
| 246 | //hdf.setValue(key + "Href", dir + "/index.html"); |
| 247 | //return true; |
| 248 | } |
| 249 | } |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 250 | |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 251 | /** |
| 252 | * Keep track of file parents |
| 253 | */ |
| 254 | Data setParentDirs(Data hdf, String subdir, String name, Boolean isFile) { |
| 255 | isFile = false; |
| 256 | int iter; |
| 257 | hdf.removeTree("parentdirs"); |
| 258 | //System.out.println("setParentDirs for " + subdir + name); |
| 259 | String s = subdir; |
| 260 | String urlParts[] = s.split("/"); |
| 261 | int n, l = (isFile)?1:0; |
| 262 | for (iter=2; iter < urlParts.length - l; iter++) { |
| 263 | n = iter-2; |
| 264 | //System.out.println("parentdirs." + n + ".Name == " + urlParts[iter]); |
| 265 | hdf.setValue("parentdirs." + n + ".Name", urlParts[iter]); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 266 | } |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 267 | return hdf; |
| 268 | } |
| 269 | |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 270 | /** |
| 271 | * Write a templated source code file to out. |
| 272 | */ |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 273 | public void writePage(File f, String out, String subdir) { |
| 274 | String name = f.getName(); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 275 | String path = f.getPath(); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 276 | String data = |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 277 | SampleTagInfo.readFile(new SourcePositionInfo(path, -1, -1), path, "sample code", |
| 278 | true, true, true, true); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 279 | data = Doclava.escape(data); |
| 280 | |
| 281 | Data hdf = Doclava.makeHDF(); |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 282 | |
| 283 | String relative = subdir.replaceFirst("samples/", ""); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 284 | hdf.setValue("samples", "true"); |
| 285 | setParentDirs(hdf, subdir, name, true); |
| 286 | hdf.setValue("projectTitle", mTitle); |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 287 | hdf.setValue("projectDir", mProjectDir); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 288 | hdf.setValue("page.title", name); |
| 289 | hdf.setValue("subdir", subdir); |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 290 | hdf.setValue("relative", relative); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 291 | hdf.setValue("realFile", name); |
| 292 | hdf.setValue("fileContents", data); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 293 | hdf.setValue("resTag", "sample"); |
| 294 | hdf.setValue("resType", "Sample Code"); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 295 | |
| 296 | ClearPage.write(hdf, "sample.cs", out); |
| 297 | } |
| 298 | |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 299 | /** |
| 300 | * Write a templated image file to out. |
| 301 | */ |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 302 | public void writeImagePage(File f, String out, String subdir) { |
| 303 | String name = f.getName(); |
| 304 | |
| 305 | String data = "<img src=\"" + name + "\" title=\"" + name + "\" />"; |
| 306 | |
| 307 | Data hdf = Doclava.makeHDF(); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 308 | hdf.setValue("samples", "true"); |
| 309 | setParentDirs(hdf, subdir, name, true); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 310 | hdf.setValue("page.title", name); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 311 | hdf.setValue("projectTitle", mTitle); |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 312 | hdf.setValue("projectDir", mProjectDir); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 313 | hdf.setValue("subdir", subdir); |
| 314 | hdf.setValue("realFile", name); |
| 315 | hdf.setValue("fileContents", data); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 316 | hdf.setValue("resTag", "sample"); |
| 317 | hdf.setValue("resType", "Sample Code"); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 318 | ClearPage.write(hdf, "sample.cs", out); |
| 319 | } |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 320 | |
| 321 | /** |
| 322 | * Render a SC node to a navtree js file. |
| 323 | */ |
| 324 | public static void writeSamplesNavTree(List<Node> tnode) { |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 325 | Node node = new Node("Reference", "packages.html", null, tnode, null); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 326 | |
| 327 | StringBuilder buf = new StringBuilder(); |
| 328 | if (false) { |
| 329 | // if you want a root node |
| 330 | buf.append("["); |
| 331 | node.render(buf); |
| 332 | buf.append("]"); |
| 333 | } else { |
| 334 | // if you don't want a root node |
| 335 | node.renderChildren(buf); |
| 336 | } |
| 337 | |
| 338 | Data data = Doclava.makeHDF(); |
| 339 | data.setValue("reference_tree", buf.toString()); |
| 340 | ClearPage.write(data, "samples_navtree_data.cs", "samples_navtree_data.js"); |
| 341 | } |
| 342 | |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 343 | /** |
| 344 | * Sort by type and name (alpha), with manifest and src always at top. |
| 345 | */ |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 346 | Comparator<File> byTypeAndName = new Comparator<File>() { |
| 347 | public int compare (File one, File other) { |
| 348 | if (one.isDirectory() && !other.isDirectory()) { |
| 349 | return 1; |
| 350 | } else if (!one.isDirectory() && other.isDirectory()) { |
| 351 | return -1; |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 352 | } else if (one.getName().equals("AndroidManifest.xml")) { |
| 353 | return -1; |
| 354 | } else if (one.getName().equals("src")) { |
| 355 | return -1; |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 356 | } else { |
| 357 | return one.compareTo(other); |
| 358 | } |
| 359 | } |
| 360 | }; |
| 361 | |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 362 | /** |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 363 | * Concatenate dirs that only hold dirs to simplify nav tree |
| 364 | */ |
| 365 | public static List<Node> squashNodes(List<Node> tnode){ |
| 366 | List<Node> list = tnode; |
| 367 | |
| 368 | for(int i = 0; i < list.size(); ++i) { |
| 369 | //only squash dirs that contain another dir whose list size is 1 and |
| 370 | //that don't contain endpoints |
| 371 | if ((list.get(i).getType().equals("dir")) && |
| 372 | (list.size() == 1) && |
| 373 | (list.get(i).getChildren().get(0).getChildren() != null)) { |
| 374 | String thisLabel = list.get(i).getLabel(); |
| 375 | String childLabel = list.get(i).getChildren().get(0).getLabel(); |
| 376 | String newLabel = thisLabel + "/" + childLabel; |
| 377 | //Set label of parent and mChildren to those of child-child, skipping |
| 378 | //squashed dir |
| 379 | list.get(i).setLabel(newLabel); |
| 380 | list.get(i).setChildren(list.get(i).getChildren().get(0).getChildren()); |
| 381 | } else { |
| 382 | continue; |
| 383 | } |
| 384 | } |
| 385 | return list; |
| 386 | } |
| 387 | |
| 388 | /** |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 389 | * SampleCode variant of NavTree node. |
| 390 | */ |
| 391 | public static class Node { |
| 392 | private String mLabel; |
| 393 | private String mLink; |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 394 | private String mTags; |
| 395 | private List<Node> mChildren; |
| 396 | private String mType; |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 397 | |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 398 | Node(String label, String link, String tags, List<Node> children, String type) { |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 399 | mLabel = label; |
| 400 | mLink = link; |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 401 | mTags = tags; |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 402 | mChildren = children; |
| 403 | mType = type; |
| 404 | } |
| 405 | |
| 406 | static void renderString(StringBuilder buf, String s) { |
| 407 | if (s == null) { |
| 408 | buf.append("null"); |
| 409 | } else { |
| 410 | buf.append('"'); |
| 411 | final int N = s.length(); |
| 412 | for (int i = 0; i < N; i++) { |
| 413 | char c = s.charAt(i); |
| 414 | if (c >= ' ' && c <= '~' && c != '"' && c != '\\') { |
| 415 | buf.append(c); |
| 416 | } else { |
| 417 | buf.append("\\u"); |
| 418 | for (int j = 0; i < 4; i++) { |
| 419 | char x = (char) (c & 0x000f); |
| 420 | if (x > 10) { |
| 421 | x = (char) (x - 10 + 'a'); |
| 422 | } else { |
| 423 | x = (char) (x + '0'); |
| 424 | } |
| 425 | buf.append(x); |
| 426 | c >>= 4; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | buf.append('"'); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | void renderChildren(StringBuilder buf) { |
| 435 | List<Node> list = mChildren; |
| 436 | if (list == null || list.size() == 0) { |
| 437 | // We output null for no children. That way empty lists here can just |
| 438 | // be a byproduct of how we generate the lists. |
| 439 | buf.append("null"); |
| 440 | } else { |
| 441 | buf.append("[ "); |
| 442 | final int N = list.size(); |
| 443 | for (int i = 0; i < N; i++) { |
| 444 | list.get(i).render(buf); |
| 445 | if (i != N - 1) { |
| 446 | buf.append(", "); |
| 447 | } |
| 448 | } |
| 449 | buf.append(" ]\n"); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void render(StringBuilder buf) { |
| 454 | buf.append("[ "); |
| 455 | renderString(buf, mLabel); |
| 456 | buf.append(", "); |
| 457 | renderString(buf, mLink); |
| 458 | buf.append(", "); |
| 459 | renderChildren(buf); |
| 460 | buf.append(", "); |
| 461 | renderString(buf, mType); |
| 462 | buf.append(" ]"); |
| 463 | } |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame^] | 464 | |
| 465 | public List<Node> getChildren() { |
| 466 | if (mChildren != null) { |
| 467 | return mChildren; |
| 468 | } else { |
| 469 | return null; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | public void setChildren(List<Node> node) { |
| 474 | mChildren = node; |
| 475 | } |
| 476 | |
| 477 | public String getLabel() { |
| 478 | return mLabel; |
| 479 | } |
| 480 | |
| 481 | public void setLabel(String label) { |
| 482 | mLabel = label; |
| 483 | } |
| 484 | |
| 485 | public String getType() { |
| 486 | return mType.toString(); |
| 487 | } |
| 488 | |
| 489 | public String getHref() { |
| 490 | return mLink; |
| 491 | } |
| 492 | |
| 493 | public void setHref(String link) { |
| 494 | mLink = link; |
| 495 | } |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 498 | /** |
| 499 | * @deprecated |
| 500 | */ |
| 501 | public void writeDirectory(File dir, String relative, boolean offline) { |
| 502 | TreeSet<String> dirs = new TreeSet<String>(); |
| 503 | TreeSet<String> files = new TreeSet<String>(); |
| 504 | |
| 505 | String subdir = relative; // .substring(mDest.length()); |
| 506 | |
| 507 | for (File f : dir.listFiles()) { |
| 508 | String name = f.getName(); |
| 509 | if (name.startsWith(".") || name.startsWith("_")) { |
| 510 | continue; |
| 511 | } |
| 512 | if (f.isFile()) { |
| 513 | String out = relative + name; |
| 514 | if (inList(out, IMAGES)) { |
| 515 | // copied directly |
| 516 | ClearPage.copyFile(false, f, out); |
| 517 | writeImagePage(f, convertExtension(out, Doclava.htmlExtension), subdir); |
| 518 | files.add(name); |
| 519 | } |
| 520 | if (inList(out, TEMPLATED)) { |
| 521 | // copied and goes through the template |
| 522 | ClearPage.copyFile(false, f, out); |
| 523 | writePage(f, convertExtension(out, Doclava.htmlExtension), subdir); |
| 524 | files.add(name); |
| 525 | |
| 526 | } |
| 527 | // else ignored |
| 528 | } else if (f.isDirectory()) { |
| 529 | writeDirectory(f, relative + name + "/", offline); |
| 530 | dirs.add(name); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | // write the index page |
| 535 | int i; |
| 536 | |
| 537 | Data hdf = writeIndex(dir); |
| 538 | hdf.setValue("subdir", subdir); |
| 539 | i = 0; |
| 540 | for (String d : dirs) { |
| 541 | hdf.setValue("subdirs." + i + ".Name", d); |
| 542 | hdf.setValue("files." + i + ".Href", convertExtension(d, ".html")); |
| 543 | i++; |
| 544 | } |
| 545 | i = 0; |
| 546 | for (String f : files) { |
| 547 | hdf.setValue("files." + i + ".Name", f); |
| 548 | hdf.setValue("files." + i + ".Href", convertExtension(f, ".html")); |
| 549 | i++; |
| 550 | } |
| 551 | |
| 552 | if (!offline) relative = "/" + relative; |
| 553 | ClearPage.write(hdf, "sampleindex.cs", relative + "index" + Doclava.htmlExtension); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * @deprecated |
| 558 | */ |
| 559 | public void writeIndexOnly(File dir, String relative, Boolean offline) { |
| 560 | Data hdf = writeIndex(dir); |
| 561 | if (!offline) relative = "/" + relative; |
| 562 | |
| 563 | System.out.println("writing indexonly at " + relative + "/index" + Doclava.htmlExtension); |
| 564 | ClearPage.write(hdf, "sampleindex.cs", relative + "index" + Doclava.htmlExtension); |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * @deprecated |
| 569 | */ |
| 570 | public Data writeIndex(File dir) { |
| 571 | Data hdf = Doclava.makeHDF(); |
| 572 | hdf.setValue("page.title", dir.getName() + " - " + mTitle); |
| 573 | hdf.setValue("projectTitle", mTitle); |
| 574 | |
| 575 | String filename = dir.getPath() + "/_index.html"; |
| 576 | String summary = |
| 577 | SampleTagInfo.readFile(new SourcePositionInfo(filename, -1, -1), filename, "sample code", |
| 578 | true, false, false, true); |
| 579 | |
| 580 | if (summary == null) { |
| 581 | summary = ""; |
| 582 | } |
| 583 | hdf.setValue("summary", summary); |
| 584 | |
| 585 | return hdf; |
| 586 | } |
| 587 | |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 588 | } |