KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > tools > OPP > message > SummaryMessageWriterDecorator


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: SummaryMessageWriterDecorator.java,v 1.2 2003/11/07 21:34:22 per_nyfelt Exp $
8
package org.ozoneDB.tools.OPP.message;
9
10 import org.ozoneDB.tools.OPP.message.MessageWriter;
11
12 import java.text.Format JavaDoc;
13 import java.text.NumberFormat JavaDoc;
14 import java.util.Stack JavaDoc;
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc summaries = new Stack JavaDoc();
66     private Summary currentSummary;
67     private StringBuffer JavaDoc indent = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc message) {
88         currentSummary.incErrors();
89         listener.error(indent + message);
90     }
91
92     public void warning(String JavaDoc message) {
93         currentSummary.incWarnings();
94         listener.warning(indent + message);
95     }
96
97     public void warning(String JavaDoc filename, int row, String JavaDoc message) {
98         currentSummary.incWarnings();
99         listener.warning(indent + filename, row, message);
100     }
101
102     public void info(String JavaDoc message) {
103         listener.info(indent + message);
104     }
105
106     public void debug(String JavaDoc 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 JavaDoc fmt = NumberFormat.getInstance();
119             info("End " + currentSummary.getObject() + " generated in " + fmt.format(new Double JavaDoc(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