blob: 4bb0784f4c4b1f2bb11b5b071a7ba639673e9ca8 [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
22import java.io.File;
23import java.io.FileInputStream;
24import java.io.FileOutputStream;
25import java.io.OutputStreamWriter;
26import java.io.IOException;
27import java.util.ArrayList;
28import java.util.List;
29
30public class ClearPage {
31 /*
32 * public ClearPage() { String templ = "templates/index.cs"; String filename = "docs/index.html";
33 *
34 * data.setValue("A.B.C", "1"); data.setValue("A.B.D", "2"); }
35 */
36
37 private static ArrayList<String> mTemplateDirs = new ArrayList<String>();
38 private static boolean mTemplateDirSet = false;
39
40 private static ArrayList<String> mBundledTemplateDirs = new ArrayList<String>();
41
42 public static String outputDir = "docs";
43 public static String htmlDir = null;
44 public static String toroot = null;
45
46 public static void addTemplateDir(String dir) {
47 mTemplateDirSet = true;
48 mTemplateDirs.add(dir);
49 }
50
51 public static List<String> getTemplateDirs() {
52 return mTemplateDirs;
53 }
54
55 public static void addBundledTemplateDir(String dir) {
56 mTemplateDirSet = true;
57 mBundledTemplateDirs.add(dir);
58 }
59
60 public static List<String> getBundledTemplateDirs() {
61 return mBundledTemplateDirs;
62 }
63
64 private static int countSlashes(String s) {
65 final int N = s.length();
66 int slashcount = 0;
67 for (int i = 0; i < N; i++) {
68 if (s.charAt(i) == '/') {
69 slashcount++;
70 }
71 }
72 return slashcount;
73 }
74
75 public static void write(Data data, String templ, String filename, JSilver cs) {
76 write(data, templ, filename, false, cs);
77 }
78
79 public static void write(Data data, String templ, String filename) {
80 write(data, templ, filename, false, Doclava.jSilver);
81 }
82
83 public static void write(Data data, String templ, String filename, boolean fullPath) {
84 write(data, templ, filename, false, Doclava.jSilver);
85 }
86
87 public static void write(Data data, String templ, String filename, boolean fullPath, JSilver cs) {
88 if (htmlDir != null) {
89 data.setValue("hasindex", "true");
90 }
91
92 String toroot;
93 if (ClearPage.toroot != null) {
94 toroot = ClearPage.toroot;
95 } else {
96 int slashcount = countSlashes(filename);
97 if (slashcount > 0) {
98 toroot = "";
99 for (int i = 0; i < slashcount; i++) {
100 toroot += "../";
101 }
102 } else {
103 toroot = "./";
104 }
105 }
106 data.setValue("toroot", toroot);
107
108 data.setValue("filename", filename);
109
110 if (!fullPath) {
111 filename = outputDir + "/" + filename;
112 }
113
114 int i = 0;
115 if (htmlDir != null) {
116 data.setValue("hdf.loadpaths." + i, htmlDir);
117 i++;
118 }
119 if (mTemplateDirSet) {
120 for (String dir : mTemplateDirs) {
121 data.setValue("hdf.loadpaths." + i, dir);
122 i++;
123 }
124 } else {
125 data.setValue("hdf.loadpaths." + i, "templates");
126 }
127
128 File file = new File(outputFilename(filename));
129
130 ensureDirectory(file);
131
132 OutputStreamWriter stream = null;
133 try {
134 stream = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
135 String rendered = cs.render(templ, data);
136 stream.write(rendered, 0, rendered.length());
137 } catch (IOException e) {
138 System.out.println("error: " + e.getMessage() + "; when writing file: " + filename);
139 } finally {
140 if (stream != null) {
141 try {
142 stream.close();
143 } catch (IOException e) {}
144 }
145 }
146 }
147
148 // recursively create the directories to the output
149 public static void ensureDirectory(File f) {
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 File to = new File(outputDir + "/" + toPath);
158 FileInputStream in;
159 FileOutputStream out;
160 try {
161 if (!from.exists()) {
162 throw new IOException();
163 }
164 in = new FileInputStream(from);
165 } catch (IOException e) {
166 System.err.println(from.getAbsolutePath() + ": Error opening file");
167 return;
168 }
169 ensureDirectory(to);
170 try {
171 out = new FileOutputStream(to);
172 } catch (IOException e) {
173 System.err.println(from.getAbsolutePath() + ": Error opening file");
174 return;
175 }
176
177 long sizel = from.length();
178 final int maxsize = 64 * 1024;
179 int size = sizel > maxsize ? maxsize : (int) sizel;
180 byte[] buf = new byte[size];
181 while (true) {
182 try {
183 size = in.read(buf);
184 } catch (IOException e) {
185 System.err.println(from.getAbsolutePath() + ": error reading file");
186 break;
187 }
188 if (size > 0) {
189 try {
190 out.write(buf, 0, size);
191 } catch (IOException e) {
192 System.err.println(from.getAbsolutePath() + ": error writing file");
193 }
194 } else {
195 break;
196 }
197 }
198 try {
199 in.close();
200 } catch (IOException e) {}
201 try {
202 out.close();
203 } catch (IOException e) {}
204 }
205
206 /** Takes a string that ends w/ .html and changes the .html to htmlExtension */
207 public static String outputFilename(String htmlFile) {
208 if (!Doclava.htmlExtension.equals(".html") && htmlFile.endsWith(".html")) {
209 return htmlFile.substring(0, htmlFile.length() - 5) + Doclava.htmlExtension;
210 } else {
211 return htmlFile;
212 }
213 }
214
215}