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; 18 19 import com.google.clearsilver.jsilver.data.Data; 20 21 import java.util.*; 22 import java.io.*; 23 24 25 public class SampleCode { 26 String mSource; 27 String mDest; 28 String mTitle; 29 SampleCode(String source, String dest, String title)30 public SampleCode(String source, String dest, String title) { 31 mSource = source; 32 mTitle = title; 33 int len = dest.length(); 34 if (len > 1 && dest.charAt(len - 1) != '/') { 35 mDest = dest + '/'; 36 } else { 37 mDest = dest; 38 } 39 } 40 write(boolean offlineMode)41 public void write(boolean offlineMode) { 42 File f = new File(mSource); 43 if (!f.isDirectory()) { 44 System.out.println("-samplecode not a directory: " + mSource); 45 return; 46 } 47 48 writeDirectory(f, mDest, offlineMode); 49 } 50 convertExtension(String s, String ext)51 public static String convertExtension(String s, String ext) { 52 return s.substring(0, s.lastIndexOf('.')) + ext; 53 } 54 55 public static String[] IMAGES = {".png", ".jpg", ".gif"}; 56 public static String[] TEMPLATED = {".java", ".xml", ".aidl", ".rs"}; 57 inList(String s, String[] list)58 public static boolean inList(String s, String[] list) { 59 for (String t : list) { 60 if (s.endsWith(t)) { 61 return true; 62 } 63 } 64 return false; 65 } 66 writeDirectory(File dir, String relative)67 public void writeDirectory(File dir, String relative) { 68 writeDirectory(dir, relative, false); 69 } 70 writeDirectory(File dir, String relative, boolean offline)71 public void writeDirectory(File dir, String relative, boolean offline) { 72 TreeSet<String> dirs = new TreeSet<String>(); 73 TreeSet<String> files = new TreeSet<String>(); 74 75 String subdir = relative; // .substring(mDest.length()); 76 77 for (File f : dir.listFiles()) { 78 String name = f.getName(); 79 if (name.startsWith(".") || name.startsWith("_")) { 80 continue; 81 } 82 if (f.isFile()) { 83 String out = relative + name; 84 85 if (inList(out, IMAGES)) { 86 // copied directly 87 ClearPage.copyFile(f, out); 88 writeImagePage(f, convertExtension(out, Doclava.htmlExtension), subdir); 89 files.add(name); 90 } 91 if (inList(out, TEMPLATED)) { 92 // copied and goes through the template 93 ClearPage.copyFile(f, out); 94 writePage(f, convertExtension(out, Doclava.htmlExtension), subdir); 95 files.add(name); 96 } 97 // else ignored 98 } else if (f.isDirectory()) { 99 writeDirectory(f, relative + name + "/", offline); 100 dirs.add(name); 101 } 102 } 103 104 // write the index page 105 int i; 106 107 Data hdf = writeIndex(dir); 108 hdf.setValue("subdir", subdir); 109 i = 0; 110 for (String d : dirs) { 111 hdf.setValue("subdirs." + i + ".name", d); 112 i++; 113 } 114 i = 0; 115 for (String f : files) { 116 hdf.setValue("files." + i + ".name", f); 117 hdf.setValue("files." + i + ".href", convertExtension(f, ".html")); 118 i++; 119 } 120 121 if (!offline) relative = "/" + relative; 122 ClearPage.write(hdf, "sampleindex.cs", relative + "index" + Doclava.htmlExtension); 123 } 124 writeIndex(File dir)125 public Data writeIndex(File dir) { 126 Data hdf = Doclava.makeHDF(); 127 128 hdf.setValue("page.title", dir.getName() + " - " + mTitle); 129 hdf.setValue("projectTitle", mTitle); 130 131 String filename = dir.getPath() + "/_index.html"; 132 String summary = 133 SampleTagInfo.readFile(new SourcePositionInfo(filename, -1, -1), filename, "sample code", 134 true, false, true); 135 136 if (summary == null) { 137 summary = ""; 138 } 139 hdf.setValue("summary", summary); 140 141 return hdf; 142 } 143 writePage(File f, String out, String subdir)144 public void writePage(File f, String out, String subdir) { 145 String name = f.getName(); 146 147 String filename = f.getPath(); 148 String data = 149 SampleTagInfo.readFile(new SourcePositionInfo(filename, -1, -1), filename, "sample code", 150 true, true, true); 151 data = Doclava.escape(data); 152 153 Data hdf = Doclava.makeHDF(); 154 155 hdf.setValue("page.title", name); 156 hdf.setValue("subdir", subdir); 157 hdf.setValue("realFile", name); 158 hdf.setValue("fileContents", data); 159 160 ClearPage.write(hdf, "sample.cs", out); 161 } 162 writeImagePage(File f, String out, String subdir)163 public void writeImagePage(File f, String out, String subdir) { 164 String name = f.getName(); 165 166 String data = "<img src=\"" + name + "\" title=\"" + name + "\" />"; 167 168 Data hdf = Doclava.makeHDF(); 169 170 hdf.setValue("page.title", name); 171 hdf.setValue("subdir", subdir); 172 hdf.setValue("realFile", name); 173 hdf.setValue("fileContents", data); 174 175 ClearPage.write(hdf, "sample.cs", out); 176 } 177 } 178