• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.JSilver;
20 import com.google.clearsilver.jsilver.data.Data;
21 
22 import java.io.BufferedWriter;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.OutputStreamWriter;
28 import java.io.Writer;
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 public 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";
45   public static List<String> htmlDirs = new ArrayList<String>();
46   public static String toroot = null;
47 
addTemplateDir(String dir)48   public static void addTemplateDir(String dir) {
49     mTemplateDirSet = true;
50     mTemplateDirs.add(dir);
51   }
52 
getTemplateDirs()53   public static List<String> getTemplateDirs() {
54     return mTemplateDirs;
55   }
56 
addBundledTemplateDir(String dir)57   public static void addBundledTemplateDir(String dir) {
58     mTemplateDirSet = true;
59     mBundledTemplateDirs.add(dir);
60   }
61 
getBundledTemplateDirs()62   public static List<String> getBundledTemplateDirs() {
63     return mBundledTemplateDirs;
64   }
65 
countSlashes(String s)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 
write(Data data, String templ, String filename, JSilver cs)77   public static void write(Data data, String templ, String filename, JSilver cs) {
78     write(data, templ, filename, false, cs);
79   }
80 
write(Data data, String templ, String filename)81   public static void write(Data data, String templ, String filename) {
82     write(data, templ, filename, false, Doclava.jSilver);
83   }
84 
write(Data data, String templ, String filename, boolean fullPath)85   public static void write(Data data, String templ, String filename, boolean fullPath) {
86     write(data, templ, filename, false, Doclava.jSilver);
87   }
88 
write(Data data, String templ, String filename, boolean fullPath, JSilver cs)89   public static void write(Data data, String templ, String filename, boolean fullPath, JSilver cs) {
90     if (!htmlDirs.isEmpty()) {
91       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;
117     if (!htmlDirs.isEmpty()) {
118         for (String dir : htmlDirs) {
119           data.setValue("hdf.loadpaths." + i, dir);
120           i++;
121         }
122     }
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);
135 
136     Writer stream = null;
137     try {
138       stream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
139       String rendered = cs.render(templ, data);
140       stream.write(rendered, 0, rendered.length());
141     } catch (IOException e) {
142       System.out.println("error: " + e.getMessage() + "; when writing file: " + filename);
143     } finally {
144       if (stream != null) {
145         try {
146           stream.close();
147         } catch (IOException e) {}
148       }
149     }
150   }
151 
152   // recursively create the directories to the output
ensureDirectory(File f)153   public static void ensureDirectory(File f) {
154     File parent = f.getParentFile();
155     if (parent != null) {
156       parent.mkdirs();
157     }
158   }
159 
copyFile(File from, String toPath)160   public static void copyFile(File from, String toPath) {
161     File to = new File(outputDir + "/" + toPath);
162     FileInputStream in;
163     FileOutputStream out;
164     try {
165       if (!from.exists()) {
166         throw new IOException();
167       }
168       in = new FileInputStream(from);
169     } catch (IOException e) {
170       System.err.println(from.getAbsolutePath() + ": Error opening file");
171       return;
172     }
173     ensureDirectory(to);
174     try {
175       out = new FileOutputStream(to);
176     } catch (IOException e) {
177       System.err.println(from.getAbsolutePath() + ": Error opening file");
178       return;
179     }
180 
181     long sizel = from.length();
182     final int maxsize = 64 * 1024;
183     int size = sizel > maxsize ? maxsize : (int) sizel;
184     byte[] buf = new byte[size];
185     while (true) {
186       try {
187         size = in.read(buf);
188       } catch (IOException e) {
189         System.err.println(from.getAbsolutePath() + ": error reading file");
190         break;
191       }
192       if (size > 0) {
193         try {
194           out.write(buf, 0, size);
195         } catch (IOException e) {
196           System.err.println(from.getAbsolutePath() + ": error writing file");
197         }
198       } else {
199         break;
200       }
201     }
202     try {
203       in.close();
204     } catch (IOException e) {}
205     try {
206       out.close();
207     } catch (IOException e) {}
208   }
209 
210   /** Takes a string that ends w/ .html and changes the .html to htmlExtension */
outputFilename(String htmlFile)211   public static String outputFilename(String htmlFile) {
212     if (!Doclava.htmlExtension.equals(".html") && htmlFile.endsWith(".html")) {
213       return htmlFile.substring(0, htmlFile.length() - 5) + Doclava.htmlExtension;
214     } else {
215       return htmlFile;
216     }
217   }
218 
219 }
220