001 package com.hammurapi.review.report.html;
002
003 import java.io.File;
004 import java.util.HashMap;
005 import java.util.Map;
006 import java.util.logging.Level;
007 import java.util.logging.Logger;
008
009 import com.hammurapi.config.ObjectDefinition;
010 import com.hammurapi.config.bootstrap.ConfigurationException;
011 import com.hammurapi.config.runtime.ConfigurationContext;
012 import com.hammurapi.render.RenderingConstants;
013 import com.hammurapi.render.RenderingException;
014 import com.hammurapi.render.ReportGenerator;
015 import com.hammurapi.review.Module;
016 import com.hammurapi.review.ObservationSink;
017 import com.hammurapi.review.Reporter;
018 import com.hammurapi.review.ReviewException;
019 import com.hammurapi.review.Summary;
020
021 /**
022 * This class produces review report in HTML format.
023 * @author Pavel Vlasov
024 *
025 */
026 public class HtmlReporter implements Reporter<Object>, com.hammurapi.config.runtime.Component<ObjectDefinition> {
027 private static final Logger logger = Logger.getLogger(HtmlReporter.class.getName());
028
029 private Summary summary;
030 private boolean http;
031 private File outputDir;
032 private String title;
033 private long start;
034
035 private ConfigurationContext<ObjectDefinition> context;
036
037 /**
038 * @param http True indicates that report will be served over http and AJAX can be used.
039 */
040 public HtmlReporter(File outputDir, String title, boolean http) {
041 summary = new Summary();
042 this.outputDir = outputDir;
043 this.title = title;
044 this.http = http;
045 this.start = System.currentTimeMillis();
046 File[] children = outputDir.listFiles();
047 if (children!=null) {
048 for (File child: children) {
049 delete(child);
050 }
051 }
052 }
053
054 private void delete(File file) {
055 if (file.isDirectory()) {
056 for (File child: file.listFiles()) {
057 delete(child);
058 }
059
060 }
061 file.delete();
062 }
063
064 @Override
065 public void close() throws ReviewException {
066 ReportGenerator reportGenerator = new ReportGenerator();
067 reportGenerator.setContext(context);
068 Map<String, Object> env = new HashMap<String, Object>();
069 env.put(RenderingConstants.RENDER_START, start);
070 reportGenerator.setEnvironment(env);
071 try {
072 reportGenerator.generate(summary, outputDir, title, http);
073 } catch (RenderingException e) {
074 logger.log(Level.SEVERE, "Failed to render report: "+e, e);
075 throw new ReviewException(e);
076 }
077 }
078
079 @Override
080 public synchronized ObservationSink createObservationSink(Module module) throws ReviewException {
081 return summary.getObservationSink(module);
082 }
083
084 @Override
085 public void onException(Exception e) {
086 summary.onException(e);
087 }
088
089 @Override
090 public void init(ConfigurationContext<ObjectDefinition> context) throws ConfigurationException {
091 this.context = context;
092 }
093
094 }