001    package jp.osdl.jbento2.report;
002    
003    import java.io.BufferedWriter;
004    import java.io.File;
005    import java.io.FileFilter;
006    import java.io.FileWriter;
007    import java.util.ArrayList;
008    import java.util.Collections;
009    import java.util.Comparator;
010    import java.util.HashMap;
011    import java.util.List;
012    import java.util.Map;
013    
014    import org.apache.commons.io.IOUtils;
015    import org.apache.velocity.Template;
016    import org.apache.velocity.VelocityContext;
017    import org.apache.velocity.app.Velocity;
018    
019    public class SummaryBuilder extends AbstractReportBuilder {
020    
021        public SummaryBuilder(File dataDir, File destDir, File destRootDir) {
022            super(dataDir, destDir, destRootDir);
023        }
024    
025        public void execute() throws Exception {
026            buildSummary();
027        }
028    
029        private void buildSummary() throws Exception {
030            Template template = Velocity
031                    .getTemplate("jp/osdl/jbento2/report/result.vm");
032    
033            VelocityContext context = new VelocityContext();
034            context.put("title", "JBento Summary Report");
035            context.put("relativeRoot", getRelativeRoot());
036            context.put("relativePath", getRelativePath());
037    
038            File[] detailDirs = getDataDir().listFiles(new FileFilter() {
039                public boolean accept(File f) {
040                    return FileMatcher.isAnalyzeDir(f);
041                }
042            });
043    
044            List detailList = new ArrayList();
045            for (int i = 0; i < detailDirs.length; i++) {
046                Map item = new HashMap();
047                item.put("name", detailDirs[i].getName());
048                item.put("path", detailDirs[i].getName());
049                detailList.add(item);
050            }
051    
052            Collections.sort(detailList, new Comparator() {
053                public int compare(Object arg0, Object arg1) {
054                    return FileMatcher.compareAnalyzeDir((String) ((Map) arg0)
055                            .get("name"), (String) ((Map) arg1).get("name"));
056                }
057            });
058            context.put("details", detailList);
059    
060            BufferedWriter writer = null;
061            try {
062                writer = new BufferedWriter(new FileWriter(new File(getDestDir(),
063                        "result.html")));
064                template.merge(context, writer);
065                writer.flush();
066            } finally {
067                IOUtils.closeQuietly(writer);
068            }
069        }
070    }