The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | import com.sun.javadoc.*; |
| 18 | import org.clearsilver.HDF; |
| 19 | import org.clearsilver.CS; |
| 20 | import java.io.File; |
| 21 | import java.io.FileInputStream; |
| 22 | import java.io.FileOutputStream; |
| 23 | import java.io.OutputStreamWriter; |
| 24 | import java.io.IOException; |
| 25 | import java.util.ArrayList; |
| 26 | |
| 27 | public class ClearPage |
| 28 | { |
| 29 | /* |
| 30 | public ClearPage() |
| 31 | { |
| 32 | String templ = "templates/index.cs"; |
| 33 | String filename = "docs/index.html"; |
| 34 | |
| 35 | data.setValue("A.B.C", "1"); |
| 36 | data.setValue("A.B.D", "2"); |
| 37 | } |
| 38 | */ |
| 39 | |
| 40 | public static ArrayList<String> hdfFiles = new ArrayList<String>(); |
| 41 | |
| 42 | private static ArrayList<String> mTemplateDirs = new ArrayList<String>(); |
| 43 | private static boolean mTemplateDirSet = false; |
| 44 | |
| 45 | public static String outputDir = "docs"; |
| 46 | public static String htmlDir = null; |
| 47 | public static String toroot = null; |
| 48 | |
| 49 | public static void addTemplateDir(String dir) |
| 50 | { |
| 51 | mTemplateDirSet = true; |
| 52 | mTemplateDirs.add(dir); |
| 53 | |
| 54 | File hdfFile = new File(dir, "data.hdf"); |
| 55 | if (hdfFile.canRead()) { |
| 56 | hdfFiles.add(hdfFile.getPath()); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | private static int countSlashes(String s) |
| 61 | { |
| 62 | final int N = s.length(); |
| 63 | int slashcount = 0; |
| 64 | for (int i=0; i<N; i++) { |
| 65 | if (s.charAt(i) == '/') { |
| 66 | slashcount++; |
| 67 | } |
| 68 | } |
| 69 | return slashcount; |
| 70 | } |
| 71 | |
| 72 | public static void write(HDF data, String templ, String filename) |
| 73 | { |
| 74 | write(data, templ, filename, false); |
| 75 | } |
| 76 | |
| 77 | public static void write(HDF data, String templ, String filename, boolean fullPath) |
| 78 | { |
| 79 | if (htmlDir != null) { |
| 80 | data.setValue("hasindex", "true"); |
| 81 | } |
| 82 | |
| 83 | String toroot; |
| 84 | if (ClearPage.toroot != null) { |
| 85 | toroot = ClearPage.toroot; |
| 86 | } else { |
| 87 | int slashcount = countSlashes(filename); |
| 88 | if (slashcount > 0) { |
| 89 | toroot = ""; |
| 90 | for (int i=0; i<slashcount; i++) { |
| 91 | toroot += "../"; |
| 92 | } |
| 93 | } else { |
| 94 | toroot = "./"; |
| 95 | } |
| 96 | } |
| 97 | data.setValue("toroot", toroot); |
| 98 | |
| 99 | data.setValue("filename", filename); |
| 100 | |
| 101 | if (!fullPath) { |
| 102 | filename = outputDir + "/" + filename; |
| 103 | } |
| 104 | |
| 105 | int i=0; |
| 106 | if (htmlDir != null) { |
| 107 | data.setValue("hdf.loadpaths." + i, htmlDir); |
| 108 | i++; |
| 109 | } |
| 110 | if (mTemplateDirSet) { |
| 111 | for (String dir: mTemplateDirs) { |
| 112 | data.setValue("hdf.loadpaths." + i, dir); |
| 113 | i++; |
| 114 | } |
| 115 | } else { |
| 116 | data.setValue("hdf.loadpaths." + i, "templates"); |
| 117 | } |
| 118 | |
| 119 | CS cs = new CS(data); |
| 120 | cs.parseFile(templ); |
| 121 | |
| 122 | File file = new File(outputFilename(filename)); |
| 123 | |
| 124 | ensureDirectory(file); |
| 125 | |
| 126 | OutputStreamWriter stream = null; |
| 127 | try { |
| 128 | stream = new OutputStreamWriter( |
Dirk Dougherty | 233bc0b | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 129 | new FileOutputStream(file), "UTF-8"); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 130 | String rendered = cs.render(); |
| 131 | stream.write(rendered, 0, rendered.length()); |
| 132 | } |
| 133 | catch (IOException e) { |
| 134 | System.out.println("error: " + e.getMessage() + "; when writing file: " + filename); |
| 135 | } |
| 136 | finally { |
| 137 | if (stream != null) { |
| 138 | try { |
| 139 | stream.close(); |
| 140 | } |
| 141 | catch (IOException e) { |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // recursively create the directories to the output |
| 148 | public static void ensureDirectory(File f) |
| 149 | { |
| 150 | File parent = f.getParentFile(); |
| 151 | if (parent != null) { |
| 152 | parent.mkdirs(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | public static void copyFile(File from, String toPath) |
| 157 | { |
| 158 | File to = new File(outputDir + "/" + toPath); |
| 159 | FileInputStream in; |
| 160 | FileOutputStream out; |
| 161 | try { |
| 162 | if (!from.exists()) { |
| 163 | throw new IOException(); |
| 164 | } |
| 165 | in = new FileInputStream(from); |
| 166 | } |
| 167 | catch (IOException e) { |
| 168 | System.err.println(from.getAbsolutePath() + ": Error opening file"); |
| 169 | return ; |
| 170 | } |
| 171 | ensureDirectory(to); |
| 172 | try { |
| 173 | out = new FileOutputStream(to); |
| 174 | } |
| 175 | catch (IOException e) { |
| 176 | System.err.println(from.getAbsolutePath() + ": Error opening file"); |
| 177 | return ; |
| 178 | } |
| 179 | |
| 180 | long sizel = from.length(); |
| 181 | final int maxsize = 64*1024; |
| 182 | int size = sizel > maxsize ? maxsize : (int)sizel; |
| 183 | byte[] buf = new byte[size]; |
| 184 | while (true) { |
| 185 | try { |
| 186 | size = in.read(buf); |
| 187 | } |
| 188 | catch (IOException e) { |
| 189 | System.err.println(from.getAbsolutePath() |
| 190 | + ": error reading file"); |
| 191 | break; |
| 192 | } |
| 193 | if (size > 0) { |
| 194 | try { |
| 195 | out.write(buf, 0, size); |
| 196 | } |
| 197 | catch (IOException e) { |
| 198 | System.err.println(from.getAbsolutePath() |
| 199 | + ": error writing file"); |
| 200 | } |
| 201 | } else { |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | try { |
| 206 | in.close(); |
| 207 | } |
| 208 | catch (IOException e) { |
| 209 | } |
| 210 | try { |
| 211 | out.close(); |
| 212 | } |
| 213 | catch (IOException e) { |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** Takes a string that ends w/ .html and changes the .html to htmlExtension */ |
| 218 | public static String outputFilename(String htmlFile) { |
| 219 | if (!DroidDoc.htmlExtension.equals(".html") && htmlFile.endsWith(".html")) { |
| 220 | return htmlFile.substring(0, htmlFile.length()-5) + DroidDoc.htmlExtension; |
| 221 | } else { |
| 222 | return htmlFile; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | } |