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 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 19 | import java.util.Arrays; |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.Collections; |
| 22 | import java.util.Comparator; |
| 23 | import java.util.List; |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 24 | import java.util.regex.Pattern; |
| 25 | import java.util.regex.Matcher; |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 26 | import java.io.File; |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 27 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 28 | import com.google.clearsilver.jsilver.data.Data; |
| 29 | |
| 30 | /** |
| 31 | * Represents a browsable sample code project, with methods for managing |
| 32 | * metadata collection, file output, sorting, etc. |
| 33 | */ |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 34 | public class SampleCode { |
| 35 | String mSource; |
| 36 | String mDest; |
| 37 | String mTitle; |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 38 | String mProjectDir; |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 39 | String mTags; |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 40 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 41 | /** Max size for browseable images/video. If a source file exceeds this size, |
| 42 | * a file is generated with a generic placeholder and the original file is not |
| 43 | * copied to out. |
| 44 | */ |
| 45 | private static final double MAX_FILE_SIZE_BYTES = 2097152; |
| 46 | |
| 47 | /** When full tree nav is enabled, generate an index for every dir |
| 48 | * and linkify the breadcrumb paths in all files. |
| 49 | */ |
| 50 | private static final boolean FULL_TREE_NAVIGATION = false; |
| 51 | |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 52 | public SampleCode(String source, String dest, String title) { |
| 53 | mSource = source; |
| 54 | mTitle = title; |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 55 | mTags = null; |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 56 | |
Dirk Dougherty | 815f934 | 2013-09-12 00:30:28 -0700 | [diff] [blame] | 57 | if (dest != null) { |
| 58 | int len = dest.length(); |
| 59 | if (len > 1 && dest.charAt(len - 1) != '/') { |
| 60 | mDest = dest + '/'; |
| 61 | } else { |
| 62 | mDest = dest; |
| 63 | } |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Iterates a given sample code project gathering metadata for files and building |
| 69 | * a node tree that reflects the project's directory structure. After iterating |
| 70 | * the project, this method adds the project's metadata to jd_lists_unified, |
| 71 | * so that it is accessible for dynamic content and search suggestions. |
| 72 | * |
| 73 | * @param offlineMode Ignored -- offline-docs mode is not currently supported for |
| 74 | * browsable sample code projects. |
| 75 | * @return A root Node for the project containing its metadata and tree structure. |
| 76 | */ |
| 77 | public Node setSamplesTOC(boolean offlineMode) { |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 78 | List<Node> filelist = new ArrayList<Node>(); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 79 | File f = new File(mSource); |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 80 | mProjectDir = f.getName(); |
| 81 | String name = mProjectDir; |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 82 | String mOut = mDest + name; |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 83 | if (!f.isDirectory()) { |
| 84 | System.out.println("-samplecode not a directory: " + mSource); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 85 | return null; |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 86 | } |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 87 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 88 | Data hdf = Doclava.makeHDF(); |
| 89 | setProjectStructure(filelist, f, mDest); |
| 90 | String link = ClearPage.toroot + "samples/" + name + "/index" + Doclava.htmlExtension; |
| 91 | Node rootNode = writeSampleIndexCs(hdf, f, |
| 92 | new Node.Builder().setLabel(mProjectDir).setLink(link).setChildren(filelist).build(),false); |
| 93 | return rootNode; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * For a given sample code project dir, iterate through the project generating |
| 98 | * browsable html for all valid sample code files. After iterating the project |
| 99 | * generate a templated index file to the project output root. |
| 100 | * |
| 101 | * @param offlineMode Ignored -- offline-docs mode is not currently supported for |
| 102 | * browsable sample code projects. |
| 103 | */ |
| 104 | public void writeSamplesFiles(boolean offlineMode) { |
| 105 | List<Node> filelist = new ArrayList<Node>(); |
| 106 | File f = new File(mSource); |
| 107 | mProjectDir = f.getName(); |
| 108 | String name = mProjectDir; |
| 109 | String mOut = mDest + name; |
| 110 | if (!f.isDirectory()) { |
| 111 | System.out.println("-samplecode not a directory: " + mSource); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 112 | } |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 113 | |
| 114 | Data hdf = Doclava.makeHDF(); |
| 115 | if (Doclava.samplesNavTree != null) { |
| 116 | hdf.setValue("samples_toc_tree", Doclava.samplesNavTree.getValue("samples_toc_tree", "")); |
| 117 | } |
| 118 | hdf.setValue("samples", "true"); |
| 119 | hdf.setValue("projectDir", mProjectDir); |
| 120 | writeProjectDirectory(f, mDest, false, hdf, "Files."); |
| 121 | writeProjectStructure(name, hdf); |
| 122 | hdf.removeTree("parentdirs"); |
| 123 | hdf.setValue("parentdirs.0.Name", name); |
| 124 | boolean writeFiles = true; |
| 125 | String link = "samples/" + name + "/index" + Doclava.htmlExtension; |
| 126 | //Write root _index.jd to out and add metadata to Node. |
| 127 | writeSampleIndexCs(hdf, f, |
| 128 | new Node.Builder().setLabel(mProjectDir).setLink(link).build(), true); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 131 | /** |
| 132 | * Given the root Node for a sample code project, iterates through the project |
| 133 | * gathering metadata and project tree structure. Unsupported file types are |
| 134 | * filtered from the project output. The collected project Nodes are appended to |
| 135 | * the root project node. |
| 136 | * |
| 137 | * @param parent The root Node that represents this sample code project. |
| 138 | * @param dir The current dir being processed. |
| 139 | * @param relative Relative path for creating links to this file. |
| 140 | */ |
| 141 | public void setProjectStructure(List<Node> parent, File dir, String relative) { |
| 142 | String name, link; |
| 143 | File[] dirContents = dir.listFiles(); |
| 144 | Arrays.sort(dirContents, byTypeAndName); |
| 145 | for (File f: dirContents) { |
| 146 | name = f.getName(); |
| 147 | if (!isValidFiletype(name)) { |
| 148 | continue; |
| 149 | } |
| 150 | if (f.isFile() && name.contains(".")) { |
| 151 | String path = relative + name; |
| 152 | link = convertExtension(path, Doclava.htmlExtension); |
| 153 | if (inList(path, IMAGES) || inList(path, VIDEOS) || inList(path, TEMPLATED)) { |
| 154 | parent.add(new Node.Builder().setLabel(name).setLink(ClearPage.toroot + link).build()); |
| 155 | } |
| 156 | } else if (f.isDirectory()) { |
| 157 | List<Node> mchildren = new ArrayList<Node>(); |
| 158 | String dirpath = relative + name + "/"; |
| 159 | setProjectStructure(mchildren, f, dirpath); |
| 160 | if (mchildren.size() > 0) { |
| 161 | parent.add(new Node.Builder().setLabel(name).setLink(ClearPage.toroot |
| 162 | + dirpath).setChildren(mchildren).build()); |
| 163 | } |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 166 | } |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 167 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 168 | /** |
| 169 | * Given a root sample code project path, iterates through the project |
| 170 | * setting page metadata to manage html output and writing/copying files to |
| 171 | * the output directory. Source files are templated and images are templated |
| 172 | * and linked to the original image. |
| 173 | * |
| 174 | * @param dir The current dir being processed. |
| 175 | * @param relative Relative path for creating links to this file. |
| 176 | * @param recursed Whether the method is being called recursively. |
| 177 | * @param hdf The data to read/write for files in this project. |
| 178 | * @param newKey Key passed in recursion for managing cs child trees. |
| 179 | */ |
| 180 | public void writeProjectDirectory(File dir, String relative, Boolean recursed, |
| 181 | Data hdf, String newkey) { |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 182 | String name = ""; |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 183 | String link = ""; |
| 184 | String type = ""; |
| 185 | int i = 0; |
| 186 | String expansion = ".Sub."; |
| 187 | String key = newkey; |
| 188 | |
| 189 | if (recursed) { |
| 190 | key = (key + expansion); |
| 191 | } else { |
| 192 | expansion = ""; |
| 193 | } |
| 194 | |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 195 | File[] dirContents = dir.listFiles(); |
| 196 | Arrays.sort(dirContents, byTypeAndName); |
| 197 | for (File f: dirContents) { |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 198 | name = f.getName(); |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 199 | if (!isValidFiletype(name)) { |
| 200 | continue; |
| 201 | } |
| 202 | if (f.isFile() && name.contains(".")) { |
| 203 | String path = relative + name; |
| 204 | type = mapTypes(name); |
| 205 | link = convertExtension(path, Doclava.htmlExtension); |
| 206 | if (inList(path, IMAGES)) { |
| 207 | type = "img"; |
| 208 | if (f.length() < MAX_FILE_SIZE_BYTES) { |
| 209 | ClearPage.copyFile(false, f, path); |
| 210 | writeImageVideoPage(f, convertExtension(path, Doclava.htmlExtension), |
Dirk Dougherty | 3542770 | 2013-11-18 15:22:01 -0800 | [diff] [blame] | 211 | relative, type, true); |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 212 | } else { |
| 213 | writeImageVideoPage(f, convertExtension(path, Doclava.htmlExtension), |
Dirk Dougherty | 3542770 | 2013-11-18 15:22:01 -0800 | [diff] [blame] | 214 | relative, type, false); |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 215 | } |
| 216 | hdf.setValue(key + i + ".Type", "img"); |
| 217 | hdf.setValue(key + i + ".Name", name); |
| 218 | hdf.setValue(key + i + ".Href", link); |
| 219 | hdf.setValue(key + i + ".RelPath", relative); |
| 220 | } else if (inList(path, VIDEOS)) { |
| 221 | type = "video"; |
| 222 | if (f.length() < MAX_FILE_SIZE_BYTES) { |
| 223 | ClearPage.copyFile(false, f, path); |
| 224 | writeImageVideoPage(f, convertExtension(path, Doclava.htmlExtension), |
Dirk Dougherty | 3542770 | 2013-11-18 15:22:01 -0800 | [diff] [blame] | 225 | relative, type, true); |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 226 | } else { |
| 227 | writeImageVideoPage(f, convertExtension(path, Doclava.htmlExtension), |
Dirk Dougherty | 3542770 | 2013-11-18 15:22:01 -0800 | [diff] [blame] | 228 | relative, type, false); |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 229 | } |
| 230 | hdf.setValue(key + i + ".Type", "video"); |
| 231 | hdf.setValue(key + i + ".Name", name); |
| 232 | hdf.setValue(key + i + ".Href", link); |
| 233 | hdf.setValue(key + i + ".RelPath", relative); |
| 234 | } else if (inList(path, TEMPLATED)) { |
| 235 | writePage(f, convertExtension(path, Doclava.htmlExtension), relative, hdf); |
| 236 | hdf.setValue(key + i + ".Type", type); |
| 237 | hdf.setValue(key + i + ".Name", name); |
| 238 | hdf.setValue(key + i + ".Href", link); |
| 239 | hdf.setValue(key + i + ".RelPath", relative); |
| 240 | } |
| 241 | i++; |
| 242 | } else if (f.isDirectory()) { |
| 243 | List<Node> mchildren = new ArrayList<Node>(); |
| 244 | type = "dir"; |
| 245 | String dirpath = relative + name; |
| 246 | link = dirpath + "/index" + Doclava.htmlExtension; |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 247 | String hdfkeyName = (key + i + ".Name"); |
| 248 | String hdfkeyType = (key + i + ".Type"); |
| 249 | String hdfkeyHref = (key + i + ".Href"); |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 250 | hdf.setValue(hdfkeyName, name); |
| 251 | hdf.setValue(hdfkeyType, type); |
| 252 | hdf.setValue(hdfkeyHref, relative + name + "/" + "index" + Doclava.htmlExtension); |
| 253 | writeProjectDirectory(f, relative + name + "/", true, hdf, (key + i)); |
| 254 | i++; |
| 255 | } |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 256 | } |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 257 | |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 258 | setParentDirs(hdf, relative, name, false); |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 259 | //Generate an index.html page for each dir being processed |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 260 | if (FULL_TREE_NAVIGATION) { |
| 261 | ClearPage.write(hdf, "sampleindex.cs", relative + "/index" + Doclava.htmlExtension); |
| 262 | } |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 265 | /** |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 266 | * Processes a templated project index page from _index.jd in a project root. |
| 267 | * Each sample project must have an index, and each index locally defines it's own |
| 268 | * page.tags and sample.group cs vars. This method takes a SC node on input, reads |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 269 | * any local vars from the _index.jd, optionally generates an html file to out, |
| 270 | * then updates the SC node with the page vars and returns it to the caller. |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 271 | * |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 272 | * @param hdf The data source to read/write for this index file. |
| 273 | * @param dir The sample project root directory. |
| 274 | * @param tnode A Node to serve as the project's root node. |
| 275 | * @param writeFiles If true, generates output files only. If false, collects |
| 276 | * metadata only. |
| 277 | * @return The tnode root with any metadata/child Nodes appended. |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 278 | */ |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 279 | public Node writeSampleIndexCs(Data hdf, File dir, Node tnode, boolean writeFiles) { |
| 280 | |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 281 | String filename = dir.getAbsolutePath() + "/_index.jd"; |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 282 | String mGroup = ""; |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 283 | File f = new File(filename); |
| 284 | String rel = dir.getPath(); |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 285 | if (writeFiles) { |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 286 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 287 | hdf.setValue("samples", "true"); |
| 288 | //set any default page variables for root index |
| 289 | hdf.setValue("page.title", mProjectDir); |
| 290 | hdf.setValue("projectDir", mProjectDir); |
| 291 | hdf.setValue("projectTitle", mTitle); |
| 292 | //add the download/project links to the landing pages. |
| 293 | hdf.setValue("samplesProjectIndex", "true"); |
| 294 | if (!f.isFile()) { |
| 295 | //The directory didn't have an _index.jd, so create a stub. |
| 296 | ClearPage.write(hdf, "sampleindex.cs", mDest + "index" + Doclava.htmlExtension); |
| 297 | } else { |
| 298 | DocFile.writePage(filename, rel, mDest + "index" + Doclava.htmlExtension, hdf); |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 299 | } |
| 300 | } else if (f.isFile()) { |
| 301 | //gather metadata for toc and jd_lists_unified |
| 302 | DocFile.getPageMetadata(filename, hdf); |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 303 | mGroup = hdf.getValue("sample.group", ""); |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 304 | if (!"".equals(mGroup)) { |
| 305 | tnode.setGroup(hdf.getValue("sample.group", "")); |
| 306 | } else { |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 307 | //Errors.error(Errors.INVALID_SAMPLE_INDEX, null, "Sample " + mProjectDir |
| 308 | // + ": Root _index.jd must be present and must define sample.group" |
| 309 | // + " tag. Please see ... for details."); |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | return tnode; |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 313 | } |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 314 | |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 315 | /** |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 316 | * Sets metadata for managing html output and generates the project view page |
| 317 | * for a project. |
| 318 | * |
| 319 | * @param dir The project root dir. |
| 320 | * @param hdf The data to read/write for files in this project. |
| 321 | */ |
| 322 | public void writeProjectStructure(String dir, Data hdf) { |
| 323 | hdf.setValue("projectStructure", "true"); |
| 324 | hdf.setValue("projectDir", mProjectDir); |
| 325 | hdf.setValue("page.title", mProjectDir + " Structure"); |
| 326 | hdf.setValue("projectTitle", mTitle); |
| 327 | ClearPage.write(hdf, "sampleindex.cs", mDest + "project" + Doclava.htmlExtension); |
| 328 | hdf.setValue("projectStructure", ""); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Keeps track of each file's parent dirs. Used for generating path breadcrumbs in html. |
| 333 | * |
| 334 | * @param dir The data to read/write for this file. |
| 335 | * @param hdf The relative path for this file, from samples root. |
| 336 | * @param subdir The relative path for this file, from samples root. |
| 337 | * @param name The name of the file (minus extension). |
| 338 | * @param isFile Whether this is a file (not a dir). |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 339 | */ |
| 340 | Data setParentDirs(Data hdf, String subdir, String name, Boolean isFile) { |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 341 | if (FULL_TREE_NAVIGATION) { |
| 342 | hdf.setValue("linkfyPathCrumb", ""); |
| 343 | } |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 344 | int iter; |
| 345 | hdf.removeTree("parentdirs"); |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 346 | String s = subdir; |
| 347 | String urlParts[] = s.split("/"); |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 348 | int n, l = 1; |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 349 | for (iter=1; iter < urlParts.length; iter++) { |
| 350 | n = iter-1; |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 351 | hdf.setValue("parentdirs." + n + ".Name", urlParts[iter]); |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 352 | hdf.setValue("parentdirs." + n + ".Link", subdir + "index" + Doclava.htmlExtension); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 353 | } |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 354 | return hdf; |
| 355 | } |
| 356 | |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 357 | /** |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 358 | * Writes a templated source code file to out. |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 359 | */ |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 360 | public void writePage(File f, String out, String subdir, Data hdf) { |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 361 | String name = f.getName(); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 362 | String path = f.getPath(); |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 363 | String data = SampleTagInfo.readFile(new SourcePositionInfo(path, -1, -1), path, |
| 364 | "sample code", true, true, true, true); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 365 | data = Doclava.escape(data); |
| 366 | |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 367 | String relative = subdir.replaceFirst("samples/", ""); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 368 | setParentDirs(hdf, subdir, name, true); |
| 369 | hdf.setValue("projectTitle", mTitle); |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 370 | hdf.setValue("projectDir", mProjectDir); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 371 | hdf.setValue("page.title", name); |
| 372 | hdf.setValue("subdir", subdir); |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 373 | hdf.setValue("relative", relative); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 374 | hdf.setValue("realFile", name); |
| 375 | hdf.setValue("fileContents", data); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 376 | hdf.setValue("resTag", "sample"); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 377 | |
| 378 | ClearPage.write(hdf, "sample.cs", out); |
| 379 | } |
| 380 | |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 381 | /** |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 382 | * Writes a templated image or video file to out. |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 383 | */ |
Dirk Dougherty | 3542770 | 2013-11-18 15:22:01 -0800 | [diff] [blame] | 384 | public void writeImageVideoPage(File f, String out, String subdir, |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 385 | String resourceType, boolean browsable) { |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 386 | Data hdf = Doclava.makeHDF(); |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 387 | if (Doclava.samplesNavTree != null) { |
| 388 | hdf.setValue("samples_toc_tree", Doclava.samplesNavTree.getValue("samples_toc_tree", "")); |
| 389 | } |
| 390 | hdf.setValue("samples", "true"); |
| 391 | |
Dirk Dougherty | 3542770 | 2013-11-18 15:22:01 -0800 | [diff] [blame] | 392 | String name = f.getName(); |
| 393 | if (!browsable) { |
| 394 | hdf.setValue("noDisplay", "true"); |
| 395 | } |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 396 | setParentDirs(hdf, subdir, name, true); |
Dirk Dougherty | 3542770 | 2013-11-18 15:22:01 -0800 | [diff] [blame] | 397 | hdf.setValue("samples", "true"); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 398 | hdf.setValue("page.title", name); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 399 | hdf.setValue("projectTitle", mTitle); |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 400 | hdf.setValue("projectDir", mProjectDir); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 401 | hdf.setValue("subdir", subdir); |
Dirk Dougherty | 3542770 | 2013-11-18 15:22:01 -0800 | [diff] [blame] | 402 | hdf.setValue("resType", resourceType); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 403 | hdf.setValue("realFile", name); |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 404 | |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 405 | ClearPage.write(hdf, "sample.cs", out); |
| 406 | } |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 407 | |
| 408 | /** |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 409 | * Given a node containing sample code projects and a node containing all valid |
| 410 | * group nodes, extract project nodes from tnode and append them to the group node |
| 411 | * that matches their sample.group metadata. |
| 412 | * |
| 413 | * @param tnode A list of nodes containing sample code projects. |
| 414 | * @param groupnodes A list of nodes that represent the valid sample groups. |
| 415 | * @return The groupnodes list with all projects appended properly to their |
| 416 | * associated sample groups. |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 417 | */ |
Dirk Dougherty | 815f934 | 2013-09-12 00:30:28 -0700 | [diff] [blame] | 418 | public static void writeSamplesNavTree(List<Node> tnode, List<Node> groupnodes) { |
| 419 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 420 | Node node = new Node.Builder().setLabel("Samples").setLink(ClearPage.toroot |
| 421 | + "samples/index" + Doclava.htmlExtension).setChildren(tnode).build(); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 422 | |
Dirk Dougherty | 815f934 | 2013-09-12 00:30:28 -0700 | [diff] [blame] | 423 | if (groupnodes != null) { |
| 424 | for (int i = 0; i < tnode.size(); i++) { |
Dirk Dougherty | 187b985 | 2013-11-23 12:05:36 -0800 | [diff] [blame] | 425 | if (tnode.get(i) != null) { |
| 426 | groupnodes = appendNodeGroups(tnode.get(i), groupnodes); |
| 427 | } |
Dirk Dougherty | 815f934 | 2013-09-12 00:30:28 -0700 | [diff] [blame] | 428 | } |
Dirk Dougherty | 1d8fa8d | 2013-09-12 19:01:02 -0700 | [diff] [blame] | 429 | for (int n = 0; n < groupnodes.size(); n++) { |
| 430 | if (groupnodes.get(n).getChildren() == null) { |
| 431 | groupnodes.remove(n); |
| 432 | n--; |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 433 | } else { |
| 434 | Collections.sort(groupnodes.get(n).getChildren(), byLabel); |
Dirk Dougherty | 1d8fa8d | 2013-09-12 19:01:02 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
Dirk Dougherty | 815f934 | 2013-09-12 00:30:28 -0700 | [diff] [blame] | 437 | node.setChildren(groupnodes); |
| 438 | } |
| 439 | |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 440 | StringBuilder buf = new StringBuilder(); |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 441 | node.renderGroupNodesTOC(buf); |
| 442 | if (Doclava.samplesNavTree != null) { |
| 443 | Doclava.samplesNavTree.setValue("samples_toc_tree", buf.toString()); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 448 | /** |
Dirk Dougherty | 1d8fa8d | 2013-09-12 19:01:02 -0700 | [diff] [blame] | 449 | * For a given project root node, get the group and then iterate the list of valid |
| 450 | * groups looking for a match. If found, append the project to that group node. |
Dirk Dougherty | f9d9cb0 | 2013-09-13 19:45:17 -0700 | [diff] [blame] | 451 | * Samples that reference a valid sample group tag are added to a list for that |
Dirk Dougherty | 1d8fa8d | 2013-09-12 19:01:02 -0700 | [diff] [blame] | 452 | * group. Samples declare a sample.group tag in their _index.jd files. |
Dirk Dougherty | 815f934 | 2013-09-12 00:30:28 -0700 | [diff] [blame] | 453 | */ |
| 454 | private static List<Node> appendNodeGroups(Node gNode, List<Node> groupnodes) { |
| 455 | List<Node> mgrouplist = new ArrayList<Node>(); |
Dirk Dougherty | 1d8fa8d | 2013-09-12 19:01:02 -0700 | [diff] [blame] | 456 | for (int i = 0; i < groupnodes.size(); i++) { |
Dirk Dougherty | 187b985 | 2013-11-23 12:05:36 -0800 | [diff] [blame] | 457 | if (gNode.getGroup().equals(groupnodes.get(i).getLabel())) { |
Dirk Dougherty | 815f934 | 2013-09-12 00:30:28 -0700 | [diff] [blame] | 458 | if (groupnodes.get(i).getChildren() == null) { |
Dirk Dougherty | 1d8fa8d | 2013-09-12 19:01:02 -0700 | [diff] [blame] | 459 | mgrouplist.add(gNode); |
Dirk Dougherty | 815f934 | 2013-09-12 00:30:28 -0700 | [diff] [blame] | 460 | groupnodes.get(i).setChildren(mgrouplist); |
Dirk Dougherty | 1d8fa8d | 2013-09-12 19:01:02 -0700 | [diff] [blame] | 461 | } else { |
| 462 | groupnodes.get(i).getChildren().add(gNode); |
Dirk Dougherty | 815f934 | 2013-09-12 00:30:28 -0700 | [diff] [blame] | 463 | } |
Dirk Dougherty | 815f934 | 2013-09-12 00:30:28 -0700 | [diff] [blame] | 464 | break; |
Dirk Dougherty | 1d8fa8d | 2013-09-12 19:01:02 -0700 | [diff] [blame] | 465 | } |
Dirk Dougherty | 815f934 | 2013-09-12 00:30:28 -0700 | [diff] [blame] | 466 | } |
| 467 | return groupnodes; |
| 468 | } |
| 469 | |
| 470 | /** |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 471 | * Sorts an array of files by type and name (alpha), with manifest always at top. |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 472 | */ |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 473 | Comparator<File> byTypeAndName = new Comparator<File>() { |
| 474 | public int compare (File one, File other) { |
| 475 | if (one.isDirectory() && !other.isDirectory()) { |
| 476 | return 1; |
| 477 | } else if (!one.isDirectory() && other.isDirectory()) { |
| 478 | return -1; |
Dirk Dougherty | 187b985 | 2013-11-23 12:05:36 -0800 | [diff] [blame] | 479 | } else if ("AndroidManifest.xml".equals(one.getName())) { |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 480 | return -1; |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 481 | } else { |
| 482 | return one.compareTo(other); |
| 483 | } |
| 484 | } |
| 485 | }; |
| 486 | |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 487 | /** |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 488 | * Sorts a list of Nodes by label. |
| 489 | */ |
| 490 | public static Comparator<Node> byLabel = new Comparator<Node>() { |
| 491 | public int compare(Node one, Node other) { |
| 492 | return one.getLabel().compareTo(other.getLabel()); |
| 493 | } |
| 494 | }; |
| 495 | |
| 496 | /** |
| 497 | * Concatenates dirs that only hold dirs, to simplify nav tree |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 498 | */ |
Dirk Dougherty | 815f934 | 2013-09-12 00:30:28 -0700 | [diff] [blame] | 499 | public static List<Node> squashNodes(List<Node> tnode) { |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 500 | List<Node> list = tnode; |
| 501 | |
| 502 | for(int i = 0; i < list.size(); ++i) { |
Dirk Dougherty | 187b985 | 2013-11-23 12:05:36 -0800 | [diff] [blame] | 503 | if (("dir".equals(list.get(i).getType())) && |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 504 | (list.size() == 1) && |
| 505 | (list.get(i).getChildren().get(0).getChildren() != null)) { |
| 506 | String thisLabel = list.get(i).getLabel(); |
| 507 | String childLabel = list.get(i).getChildren().get(0).getLabel(); |
| 508 | String newLabel = thisLabel + "/" + childLabel; |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 509 | list.get(i).setLabel(newLabel); |
| 510 | list.get(i).setChildren(list.get(i).getChildren().get(0).getChildren()); |
| 511 | } else { |
| 512 | continue; |
| 513 | } |
| 514 | } |
| 515 | return list; |
| 516 | } |
| 517 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 518 | public static String convertExtension(String s, String ext) { |
| 519 | return s.substring(0, s.lastIndexOf('.')) + ext; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Whitelists of valid image/video and source code types. |
| 524 | */ |
| 525 | public static String[] IMAGES = {".png", ".jpg", ".gif"}; |
| 526 | public static String[] VIDEOS = {".mp4", ".ogv", ".webm"}; |
| 527 | public static String[] TEMPLATED = {".java", ".xml", ".aidl", ".rs",".txt", ".TXT"}; |
| 528 | |
| 529 | public static boolean inList(String s, String[] list) { |
| 530 | for (String t : list) { |
| 531 | if (s.endsWith(t)) { |
| 532 | return true; |
| 533 | } |
| 534 | } |
| 535 | return false; |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Maps filenames to a set of generic types. Used for displaying files/dirs |
| 540 | * in the project view page. |
| 541 | */ |
| 542 | public static String mapTypes(String name) { |
| 543 | String type = name.substring(name.lastIndexOf('.') + 1, name.length()); |
| 544 | if ("xml".equals(type) || "java".equals(type)) { |
| 545 | if ("AndroidManifest.xml".equals(name)) type = "manifest"; |
| 546 | return type; |
| 547 | } else { |
| 548 | return type = "file"; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * Validates a source file from a project against restrictions to determine |
| 554 | * whether to include the file in the browsable project output. |
| 555 | */ |
| 556 | public boolean isValidFiletype(String name) { |
| 557 | if (name.startsWith(".") || |
| 558 | name.startsWith("_") || |
| 559 | "default.properties".equals(name) || |
| 560 | "build.properties".equals(name) || |
| 561 | name.endsWith(".ttf") || |
| 562 | name.endsWith(".gradle") || |
| 563 | name.endsWith(".bat") || |
| 564 | "Android.mk".equals(name)) { |
| 565 | return false; |
| 566 | } else { |
| 567 | return true; |
| 568 | } |
| 569 | } |
| 570 | |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 571 | /** |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 572 | * SampleCode variant of NavTree node. |
| 573 | */ |
| 574 | public static class Node { |
| 575 | private String mLabel; |
| 576 | private String mLink; |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 577 | private String mGroup; // from sample.group in _index.jd |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 578 | private List<Node> mChildren; |
| 579 | private String mType; |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 580 | |
Dirk Dougherty | f9d9cb0 | 2013-09-13 19:45:17 -0700 | [diff] [blame] | 581 | private Node(Builder builder) { |
| 582 | mLabel = builder.mLabel; |
| 583 | mLink = builder.mLink; |
| 584 | mGroup = builder.mGroup; |
Dirk Dougherty | f9d9cb0 | 2013-09-13 19:45:17 -0700 | [diff] [blame] | 585 | mChildren = builder.mChildren; |
| 586 | mType = builder.mType; |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Dirk Dougherty | f9d9cb0 | 2013-09-13 19:45:17 -0700 | [diff] [blame] | 589 | public static class Builder { |
| 590 | private String mLabel, mLink, mGroup, mType; |
Dirk Dougherty | f9d9cb0 | 2013-09-13 19:45:17 -0700 | [diff] [blame] | 591 | private List<Node> mChildren = null; |
| 592 | public Builder setLabel(String mLabel) { this.mLabel = mLabel; return this;} |
| 593 | public Builder setLink(String mLink) { this.mLink = mLink; return this;} |
| 594 | public Builder setGroup(String mGroup) { this.mGroup = mGroup; return this;} |
Dirk Dougherty | f9d9cb0 | 2013-09-13 19:45:17 -0700 | [diff] [blame] | 595 | public Builder setChildren(List<Node> mChildren) { this.mChildren = mChildren; return this;} |
| 596 | public Builder setType(String mType) { this.mType = mType; return this;} |
| 597 | public Node build() {return new Node(this);} |
| 598 | } |
| 599 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 600 | /** |
| 601 | * Renders browsable sample groups and projects to an html list, starting |
| 602 | * from the group nodes and then rendering their project nodes and finally their |
| 603 | * child dirs and files. |
| 604 | */ |
| 605 | void renderGroupNodesTOC(StringBuilder buf) { |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 606 | List<Node> list = mChildren; |
| 607 | if (list == null || list.size() == 0) { |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 608 | return; |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 609 | } else { |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 610 | final int n = list.size(); |
| 611 | for (int i = 0; i < n; i++) { |
| 612 | if (list.get(i).getChildren() == null) { |
| 613 | continue; |
| 614 | } else { |
| 615 | buf.append("<li class=\"nav-section\">"); |
| 616 | buf.append("<div class=\"nav-section-header\">"); |
| 617 | buf.append("<a href=\"" + list.get(i).getLink() + "\" title=\"" |
| 618 | + list.get(i).getLabel() + "\">" |
| 619 | + list.get(i).getLabel() + "</a>"); |
| 620 | buf.append("</div>"); |
| 621 | buf.append("<ul>"); |
| 622 | list.get(i).renderProjectNodesTOC(buf); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 623 | } |
| 624 | } |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 625 | buf.append("</ul>"); |
| 626 | buf.append("</li>"); |
Dirk Dougherty | 1b45d1d | 2010-08-17 17:25:36 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 630 | /** |
| 631 | * Renders a list of sample code projects associated with a group node. |
| 632 | */ |
| 633 | void renderProjectNodesTOC(StringBuilder buf) { |
| 634 | List<Node> list = mChildren; |
| 635 | if (list == null || list.size() == 0) { |
| 636 | return; |
| 637 | } else { |
| 638 | final int n = list.size(); |
| 639 | for (int i = 0; i < n; i++) { |
| 640 | if (list.get(i).getChildren() == null) { |
| 641 | continue; |
| 642 | } else { |
| 643 | buf.append("<li class=\"nav-section\">"); |
| 644 | buf.append("<div class=\"nav-section-header\">"); |
| 645 | buf.append("<a href=\"" + list.get(i).getLink() + "\" title=\"" |
| 646 | + list.get(i).getLabel() + "\">" |
| 647 | + list.get(i).getLabel() + "</a>"); |
| 648 | buf.append("</div>"); |
| 649 | buf.append("<ul>"); |
| 650 | list.get(i).renderChildrenToc(buf); |
| 651 | } |
| 652 | } |
| 653 | buf.append("</ul>"); |
| 654 | buf.append("</li>"); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * Renders child dirs and files associated with a project node. |
| 660 | */ |
| 661 | void renderChildrenToc(StringBuilder buf) { |
| 662 | List<Node> list = mChildren; |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 663 | if (list == null || list.size() == 0) { |
| 664 | buf.append("null"); |
| 665 | } else { |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 666 | final int n = list.size(); |
| 667 | for (int i = 0; i < n; i++) { |
| 668 | if (list.get(i).getChildren() == null) { |
| 669 | buf.append("<li>"); |
| 670 | buf.append("<a href=\"" + list.get(i).getLink() + "\" title=\"" |
| 671 | + list.get(i).getLabel() + "\">" |
| 672 | + list.get(i).getLabel() + "</a>"); |
| 673 | buf.append(" </li>"); |
| 674 | } else { |
| 675 | buf.append("<li class=\"nav-section sticky\">"); |
| 676 | buf.append("<div class=\"nav-section-header empty\">"); |
| 677 | buf.append("<a href=\"#\" onclick=\"return false;\" title=\"" |
| 678 | + list.get(i).getLabel() + "\">" |
| 679 | + list.get(i).getLabel() + "/</a>"); |
| 680 | buf.append("</div>"); |
| 681 | buf.append("<ul>"); |
| 682 | list.get(i).renderChildrenToc(buf); |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 683 | } |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 684 | } |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 685 | buf.append("</ul>"); |
| 686 | buf.append("</li>"); |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 690 | /** |
| 691 | * Node getters and setters |
| 692 | */ |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 693 | public String getLabel() { |
| 694 | return mLabel; |
| 695 | } |
| 696 | |
| 697 | public void setLabel(String label) { |
| 698 | mLabel = label; |
| 699 | } |
| 700 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 701 | public String getLink() { |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 702 | return mLink; |
| 703 | } |
| 704 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 705 | public void setLink(String ref) { |
| 706 | mLink = ref; |
Dirk Dougherty | 25586c0 | 2013-08-30 16:21:15 -0700 | [diff] [blame] | 707 | } |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 708 | |
Dirk Dougherty | 815f934 | 2013-09-12 00:30:28 -0700 | [diff] [blame] | 709 | public String getGroup() { |
| 710 | return mGroup; |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Dirk Dougherty | 815f934 | 2013-09-12 00:30:28 -0700 | [diff] [blame] | 713 | public void setGroup(String group) { |
| 714 | mGroup = group; |
Dirk Dougherty | e52f6d8 | 2013-09-04 17:27:29 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 717 | public List<Node> getChildren() { |
| 718 | return mChildren; |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 721 | public void setChildren(List<Node> node) { |
| 722 | mChildren = node; |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 725 | public String getType() { |
| 726 | return mType; |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 727 | } |
Dirk Dougherty | c11a467 | 2013-08-23 19:14:10 -0700 | [diff] [blame] | 728 | |
Dirk Dougherty | 8a8f791 | 2013-11-26 18:45:31 -0800 | [diff] [blame] | 729 | public void setType(String type) { |
| 730 | mType = type; |
| 731 | } |
Dirk Dougherty | 3542770 | 2013-11-18 15:22:01 -0800 | [diff] [blame] | 732 | } |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 733 | } |