blob: a112b7f53a24c73f4bc729c7abdce43e03c1611b [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";
Bill Napier4bac50a2010-08-25 18:17:16 -070043 public static List<String> htmlDirs = new ArrayList<String>();
Ben Dodson920dbbb2010-08-04 15:21:06 -070044 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) {
Bill Napier4bac50a2010-08-25 18:17:16 -070088 if (!htmlDirs.isEmpty()) {
Ben Dodson920dbbb2010-08-04 15:21:06 -070089 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;
Bill Napier4bac50a2010-08-25 18:17:16 -0700115 if (!htmlDirs.isEmpty()) {
116 for (String dir : htmlDirs) {
117 data.setValue("hdf.loadpaths." + i, dir);
118 i++;
119 }
Ben Dodson920dbbb2010-08-04 15:21:06 -0700120 }
121 if (mTemplateDirSet) {
122 for (String dir : mTemplateDirs) {
123 data.setValue("hdf.loadpaths." + i, dir);
124 i++;
125 }
126 } else {
127 data.setValue("hdf.loadpaths." + i, "templates");
128 }
129
130 File file = new File(outputFilename(filename));
131
132 ensureDirectory(file);
133
134 OutputStreamWriter stream = null;
135 try {
136 stream = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
137 String rendered = cs.render(templ, data);
138 stream.write(rendered, 0, rendered.length());
139 } catch (IOException e) {
140 System.out.println("error: " + e.getMessage() + "; when writing file: " + filename);
141 } finally {
142 if (stream != null) {
143 try {
144 stream.close();
145 } catch (IOException e) {}
146 }
147 }
148 }
149
150 // recursively create the directories to the output
151 public static void ensureDirectory(File f) {
152 File parent = f.getParentFile();
153 if (parent != null) {
154 parent.mkdirs();
155 }
156 }
157
158 public static void copyFile(File from, String toPath) {
159 File to = new File(outputDir + "/" + toPath);
160 FileInputStream in;
161 FileOutputStream out;
162 try {
163 if (!from.exists()) {
164 throw new IOException();
165 }
166 in = new FileInputStream(from);
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 } catch (IOException e) {
175 System.err.println(from.getAbsolutePath() + ": Error opening file");
176 return;
177 }
178
179 long sizel = from.length();
180 final int maxsize = 64 * 1024;
181 int size = sizel > maxsize ? maxsize : (int) sizel;
182 byte[] buf = new byte[size];
183 while (true) {
184 try {
185 size = in.read(buf);
186 } catch (IOException e) {
187 System.err.println(from.getAbsolutePath() + ": error reading file");
188 break;
189 }
190 if (size > 0) {
191 try {
192 out.write(buf, 0, size);
193 } catch (IOException e) {
194 System.err.println(from.getAbsolutePath() + ": error writing file");
195 }
196 } else {
197 break;
198 }
199 }
200 try {
201 in.close();
202 } catch (IOException e) {}
203 try {
204 out.close();
205 } catch (IOException e) {}
206 }
207
208 /** Takes a string that ends w/ .html and changes the .html to htmlExtension */
209 public static String outputFilename(String htmlFile) {
210 if (!Doclava.htmlExtension.equals(".html") && htmlFile.endsWith(".html")) {
211 return htmlFile.substring(0, htmlFile.length() - 5) + Doclava.htmlExtension;
212 } else {
213 return htmlFile;
214 }
215 }
216
217}