001 package dk.deepthought.sidious.util;
002 
003 import java.io.BufferedWriter;
004 import java.io.File;
005 import java.io.FileWriter;
006 import java.io.IOException;
007 import java.util.Calendar;
008 
009 /**
010  @author Deepthought
011  
012  */
013 public class HtmlIndexMaker {
014 
015     private StringBuffer buf = new StringBuffer();
016 
017     public void makeIndex(String src, String dist) {
018         System.out.println("Generating index file for directory: " + src);
019         prepareHTML();
020         scanDir(new File(src));
021         finishHTML();
022         try {
023             print(dist);
024         catch (IOException e) {
025             e.printStackTrace();
026         }
027     }
028 
029     void scanDir(File dir) {
030         File[] files = dir.listFiles();
031         for (File file : files) {
032             if (file.isDirectory()) {
033                 scanDir(file);
034             }
035             if (file.isFile()) {
036                 boolean all = file.getName().equalsIgnoreCase("all.html");
037                 boolean analyze = file.getName().equalsIgnoreCase(
038                         "analyse.html");
039                 boolean index = file.getName().equalsIgnoreCase("index.html");
040                 boolean fileOK = !all && !index && !analyze;
041                 if (file.getName().endsWith("html"&& fileOK) {
042                     indexHTML(file);
043                 }
044             }
045         }
046     }
047 
048     void indexHTML(File file) {
049         String absolut = file.getPath();
050         absolut = absolut.replaceAll("\\\\""/");
051         String name = file.getName();
052         int i = absolut.lastIndexOf("dk");
053         String real = absolut.substring(i, absolut.length());
054         String qualifiedJavaName = real.replaceAll("/"".").substring(0, real.length()-5);
055         buf.append("<li><a href=" + real + ">" + qualifiedJavaName + "</a></li>\n");
056     }
057 
058     void prepareHTML() {
059         buf.append("<html>\n");
060         buf.append("<head><title>Repository of sidious</title></head>\n");
061         buf.append("<body>\n");
062         buf
063                 .append("<h2>This is a print of the source code of the sidious project</h2>\n");
064         buf.append("<ul>\n");
065     }
066 
067     void finishHTML() {
068         buf.append("</ul>\n");
069         Calendar cal = Calendar.getInstance();
070         String minut = "";
071         int calMinut = cal.get(Calendar.MINUTE);
072         if (calMinut < 10) {
073             minut = "0" + calMinut;
074         else {
075             minut = "" + calMinut;
076         }
077         String date = cal.get(Calendar.YEAR"-"
078                 (cal.get(Calendar.MONTH1"-" + cal.get(Calendar.DATE)
079                 " " + cal.get(Calendar.HOUR_OF_DAY":" + minut;
080 //        buf.append("<p>Print taken: " + date + "</p>\n");
081         buf.append("</body>\n");
082         buf.append("</html>\n");
083     }
084 
085     public void print(String filenamethrows IOException {
086         BufferedWriter buffer = new BufferedWriter(new FileWriter(new File(
087                 filename)));
088         String retur = buf.toString();
089         buffer.write(retur);
090         buffer.flush();
091         buffer.close();
092     }
093 
094     public static void main(String[] args) {
095         String src = args[0];
096         String dist = args[1];
097         new HtmlIndexMaker().makeIndex(src, dist);
098     }
099 
100 }