blob: 0df647d8a6e2ffd249a8048f6c5c17373814225d [file] [log] [blame]
Ben Dodson920dbbb2010-08-04 15:21:06 -07001/*
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
17package com.google.doclava;
18
19import com.google.clearsilver.jsilver.JSilver;
20import com.google.clearsilver.jsilver.data.Data;
21
Andrew Sappersteinf959ed12011-06-23 10:24:40 -070022import java.io.BufferedWriter;
Ben Dodson920dbbb2010-08-04 15:21:06 -070023import java.io.File;
24import java.io.FileInputStream;
25import java.io.FileOutputStream;
Ben Dodson920dbbb2010-08-04 15:21:06 -070026import java.io.IOException;
Andrew Sappersteinf959ed12011-06-23 10:24:40 -070027import java.io.OutputStreamWriter;
28import java.io.Writer;
Ben Dodson920dbbb2010-08-04 15:21:06 -070029import java.util.ArrayList;
30import java.util.List;
31
32public class ClearPage {
33 /*
34 * public ClearPage() { String templ = "templates/index.cs"; String filename = "docs/index.html";
35 *
36 * data.setValue("A.B.C", "1"); data.setValue("A.B.D", "2"); }
37 */
38
39 private static ArrayList<String> mTemplateDirs = new ArrayList<String>();
40 private static boolean mTemplateDirSet = false;
41
42 private static ArrayList<String> mBundledTemplateDirs = new ArrayList<String>();
43
44 public static String outputDir = "docs";
Bill Napier4bac50a2010-08-25 18:17:16 -070045 public static List<String> htmlDirs = new ArrayList<String>();
Ben Dodson920dbbb2010-08-04 15:21:06 -070046 public static String toroot = null;
47
48 public static void addTemplateDir(String dir) {
49 mTemplateDirSet = true;
50 mTemplateDirs.add(dir);
51 }
52
53 public static List<String> getTemplateDirs() {
54 return mTemplateDirs;
55 }
56
57 public static void addBundledTemplateDir(String dir) {
58 mTemplateDirSet = true;
59 mBundledTemplateDirs.add(dir);
60 }
61
62 public static List<String> getBundledTemplateDirs() {
63 return mBundledTemplateDirs;
64 }
65
66 private static int countSlashes(String s) {
67 final int N = s.length();
68 int slashcount = 0;
69 for (int i = 0; i < N; i++) {
70 if (s.charAt(i) == '/') {
71 slashcount++;
72 }
73 }
74 return slashcount;
75 }
76
77 public static void write(Data data, String templ, String filename, JSilver cs) {
78 write(data, templ, filename, false, cs);
79 }
80
81 public static void write(Data data, String templ, String filename) {
82 write(data, templ, filename, false, Doclava.jSilver);
83 }
84
85 public static void write(Data data, String templ, String filename, boolean fullPath) {
86 write(data, templ, filename, false, Doclava.jSilver);
87 }
88
89 public static void write(Data data, String templ, String filename, boolean fullPath, JSilver cs) {
Bill Napier4bac50a2010-08-25 18:17:16 -070090 if (!htmlDirs.isEmpty()) {
Ben Dodson920dbbb2010-08-04 15:21:06 -070091 data.setValue("hasindex", "true");
92 }
93
94 String toroot;
95 if (ClearPage.toroot != null) {
96 toroot = ClearPage.toroot;
97 } else {
98 int slashcount = countSlashes(filename);
99 if (slashcount > 0) {
100 toroot = "";
101 for (int i = 0; i < slashcount; i++) {
102 toroot += "../";
103 }
104 } else {
105 toroot = "./";
106 }
107 }
108 data.setValue("toroot", toroot);
109
110 data.setValue("filename", filename);
111
112 if (!fullPath) {
113 filename = outputDir + "/" + filename;
114 }
115
116 int i = 0;
Bill Napier4bac50a2010-08-25 18:17:16 -0700117 if (!htmlDirs.isEmpty()) {
118 for (String dir : htmlDirs) {
119 data.setValue("hdf.loadpaths." + i, dir);
120 i++;
121 }
Ben Dodson920dbbb2010-08-04 15:21:06 -0700122 }
123 if (mTemplateDirSet) {
124 for (String dir : mTemplateDirs) {
125 data.setValue("hdf.loadpaths." + i, dir);
126 i++;
127 }
128 } else {
129 data.setValue("hdf.loadpaths." + i, "templates");
130 }
131
132 File file = new File(outputFilename(filename));
133
134 ensureDirectory(file);
Andrew Sappersteinf959ed12011-06-23 10:24:40 -0700135 Writer stream = null;
Ben Dodson920dbbb2010-08-04 15:21:06 -0700136 try {
Andrew Sappersteinf959ed12011-06-23 10:24:40 -0700137 stream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
Ben Dodson920dbbb2010-08-04 15:21:06 -0700138 String rendered = cs.render(templ, data);
139 stream.write(rendered, 0, rendered.length());
140 } catch (IOException e) {
141 System.out.println("error: " + e.getMessage() + "; when writing file: " + filename);
142 } finally {
143 if (stream != null) {
144 try {
145 stream.close();
146 } catch (IOException e) {}
147 }
148 }
149 }
150
151 // recursively create the directories to the output
152 public static void ensureDirectory(File f) {
153 File parent = f.getParentFile();
154 if (parent != null) {
155 parent.mkdirs();
156 }
157 }
158
159 public static void copyFile(File from, String toPath) {
160 File to = new File(outputDir + "/" + toPath);
161 FileInputStream in;
162 FileOutputStream out;
163 try {
164 if (!from.exists()) {
165 throw new IOException();
166 }
167 in = new FileInputStream(from);
168 } catch (IOException e) {
169 System.err.println(from.getAbsolutePath() + ": Error opening file");
170 return;
171 }
172 ensureDirectory(to);
173 try {
174 out = new FileOutputStream(to);
175 } catch (IOException e) {
176 System.err.println(from.getAbsolutePath() + ": Error opening file");
177 return;
178 }
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800179 if ((Doclava.devsite) && (!isValidContentType(toPath, DROIDDOC_VALID_CONTENT_TYPES))) {
180 Errors.error(Errors.INVALID_CONTENT_TYPE, null, "Failed to process " + from
181 + ": Invalid file type. Please move the file to frameworks/base/docs/image_sources/... or docs/downloads/...");
182 return;
183 }
Ben Dodson920dbbb2010-08-04 15:21:06 -0700184
185 long sizel = from.length();
186 final int maxsize = 64 * 1024;
187 int size = sizel > maxsize ? maxsize : (int) sizel;
188 byte[] buf = new byte[size];
189 while (true) {
190 try {
191 size = in.read(buf);
192 } catch (IOException e) {
193 System.err.println(from.getAbsolutePath() + ": error reading file");
194 break;
195 }
196 if (size > 0) {
197 try {
198 out.write(buf, 0, size);
199 } catch (IOException e) {
200 System.err.println(from.getAbsolutePath() + ": error writing file");
201 }
202 } else {
203 break;
204 }
205 }
206 try {
207 in.close();
208 } catch (IOException e) {}
209 try {
210 out.close();
211 } catch (IOException e) {}
212 }
213
214 /** Takes a string that ends w/ .html and changes the .html to htmlExtension */
215 public static String outputFilename(String htmlFile) {
216 if (!Doclava.htmlExtension.equals(".html") && htmlFile.endsWith(".html")) {
217 return htmlFile.substring(0, htmlFile.length() - 5) + Doclava.htmlExtension;
218 } else {
219 return htmlFile;
220 }
221 }
222
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800223 public static String[] DROIDDOC_VALID_CONTENT_TYPES = {".txt", ".css", ".js", ".html", ".ico", ".png", ".jpg", ".gif", ".svg", ".webm", ".ogv","mp4", ".java", ".xml", ".aidl", ".rs",".zip", ".yaml"};
224
225 public static boolean isValidContentType(String s, String[] list) {
226 for (String t : list) {
227 if (s.endsWith(t)) {
228 return true;
229 }
230 }
231 return false;
232 }
Ben Dodson920dbbb2010-08-04 15:21:06 -0700233}