1 package org.ozoneDB.tools.OPP.message; 9 10 import org.ozoneDB.tools.OPP.message.MessageWriter; 11 12 import java.text.Format ; 13 import java.text.NumberFormat ; 14 import java.util.Stack ; 15 16 public class SummaryMessageWriterDecorator implements MessageWriter { 17 private class Summary { 18 private int warnings; 19 private int errors; 20 private long startTime; 21 private String object = ""; 22 23 public int getWarnings() { 24 return warnings; 25 } 26 27 public int getErrors() { 28 return errors; 29 } 30 31 public long getStartTime() { 32 return startTime; 33 } 34 35 public String getObject() { 36 return object; 37 } 38 39 public void incWarnings() { 40 warnings++; 41 } 42 43 public void incErrors() { 44 errors++; 45 } 46 47 public Summary(String object) { 48 startTime = System.currentTimeMillis(); 49 this.object = object; 50 } 51 52 public long getTotalTime() { 53 return System.currentTimeMillis() - startTime; 54 } 55 56 public void addSummary(Summary summary) { 57 if (summary != null) { 58 warnings += summary.warnings; 59 errors += summary.errors; 60 } 61 } 62 } 63 64 private MessageWriter listener; 65 private Stack summaries = new Stack (); 66 private Summary currentSummary; 67 private StringBuffer indent = new StringBuffer (); 68 69 private void updateIndent(int count) { 70 indent.delete(0, indent.length()); 71 for (int i = 0; i < count; i++) { 72 indent.append(" "); 73 } 74 } 75 76 public SummaryMessageWriterDecorator(MessageWriter listener) { 77 this.listener = listener; 78 } 79 80 public void startGeneration(String object) { 81 info("Begin " + object + "..."); 82 currentSummary = new Summary(object); 83 summaries.push(currentSummary); 84 updateIndent(summaries.size()); 85 } 86 87 public void error(String message) { 88 currentSummary.incErrors(); 89 listener.error(indent + message); 90 } 91 92 public void warning(String message) { 93 currentSummary.incWarnings(); 94 listener.warning(indent + message); 95 } 96 97 public void warning(String filename, int row, String message) { 98 currentSummary.incWarnings(); 99 listener.warning(indent + filename, row, message); 100 } 101 102 public void info(String message) { 103 listener.info(indent + message); 104 } 105 106 public void debug(String message) { 107 listener.debug(indent + message); 108 } 109 110 public void endGeneration() { 111 Summary tmpSummary = (Summary) summaries.pop(); 112 tmpSummary.addSummary(currentSummary); 113 currentSummary = tmpSummary; 114 updateIndent(summaries.size()); 115 if (summaries.size() == 0) { 116 double generationTime = currentSummary.getTotalTime(); 117 generationTime /= 1000; 118 Format fmt = NumberFormat.getInstance(); 119 info("End " + currentSummary.getObject() + " generated in " + fmt.format(new Double (generationTime)) + " seconds."); 120 info("Generation completed with " + currentSummary.getWarnings() + " warnings and " + currentSummary.getErrors() + " errors"); 121 } else { 122 info("End " + currentSummary.object); 123 } 124 } 125 } 126 | Popular Tags |