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