1 | package dk.deepthought.sidious.util; |
2 | |
3 | import java.io.BufferedWriter; |
4 | import java.io.File; |
5 | import java.io.FileWriter; |
6 | import java.io.IOException; |
7 | import java.util.Calendar; |
8 | |
9 | /** |
10 | * @author Deepthought |
11 | * |
12 | */ |
13 | public class HtmlIndexMaker { |
14 | |
15 | private StringBuffer buf = new StringBuffer(); |
16 | |
17 | public void makeIndex(String src, String dist) { |
18 | System.out.println("Generating index file for directory: " + src); |
19 | prepareHTML(); |
20 | scanDir(new File(src)); |
21 | finishHTML(); |
22 | try { |
23 | print(dist); |
24 | } catch (IOException e) { |
25 | e.printStackTrace(); |
26 | } |
27 | } |
28 | |
29 | void scanDir(File dir) { |
30 | File[] files = dir.listFiles(); |
31 | for (File file : files) { |
32 | if (file.isDirectory()) { |
33 | scanDir(file); |
34 | } |
35 | if (file.isFile()) { |
36 | boolean all = file.getName().equalsIgnoreCase("all.html"); |
37 | boolean analyze = file.getName().equalsIgnoreCase( |
38 | "analyse.html"); |
39 | boolean index = file.getName().equalsIgnoreCase("index.html"); |
40 | boolean fileOK = !all && !index && !analyze; |
41 | if (file.getName().endsWith("html") && fileOK) { |
42 | indexHTML(file); |
43 | } |
44 | } |
45 | } |
46 | } |
47 | |
48 | void indexHTML(File file) { |
49 | String absolut = file.getPath(); |
50 | absolut = absolut.replaceAll("\\\\", "/"); |
51 | String name = file.getName(); |
52 | int i = absolut.lastIndexOf("dk"); |
53 | String real = absolut.substring(i, absolut.length()); |
54 | String qualifiedJavaName = real.replaceAll("/", ".").substring(0, real.length()-5); |
55 | buf.append("<li><a href=" + real + ">" + qualifiedJavaName + "</a></li>\n"); |
56 | } |
57 | |
58 | void prepareHTML() { |
59 | buf.append("<html>\n"); |
60 | buf.append("<head><title>Repository of sidious</title></head>\n"); |
61 | buf.append("<body>\n"); |
62 | buf |
63 | .append("<h2>This is a print of the source code of the sidious project</h2>\n"); |
64 | buf.append("<ul>\n"); |
65 | } |
66 | |
67 | void finishHTML() { |
68 | buf.append("</ul>\n"); |
69 | Calendar cal = Calendar.getInstance(); |
70 | String minut = ""; |
71 | int calMinut = cal.get(Calendar.MINUTE); |
72 | if (calMinut < 10) { |
73 | minut = "0" + calMinut; |
74 | } else { |
75 | minut = "" + calMinut; |
76 | } |
77 | String date = cal.get(Calendar.YEAR) + "-" |
78 | + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DATE) |
79 | + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + minut; |
80 | // buf.append("<p>Print taken: " + date + "</p>\n"); |
81 | buf.append("</body>\n"); |
82 | buf.append("</html>\n"); |
83 | } |
84 | |
85 | public void print(String filename) throws IOException { |
86 | BufferedWriter buffer = new BufferedWriter(new FileWriter(new File( |
87 | filename))); |
88 | String retur = buf.toString(); |
89 | buffer.write(retur); |
90 | buffer.flush(); |
91 | buffer.close(); |
92 | } |
93 | |
94 | public static void main(String[] args) { |
95 | String src = args[0]; |
96 | String dist = args[1]; |
97 | new HtmlIndexMaker().makeIndex(src, dist); |
98 | } |
99 | |
100 | } |