KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > incava > analysis > Report


1 package org.incava.analysis;
2
3 import java.io.*;
4 import java.util.Iterator JavaDoc;
5 import java.util.Set JavaDoc;
6 import java.util.TreeSet JavaDoc;
7 import net.sourceforge.pmd.ast.Token;
8
9
10 /**
11  * Reports errors (violations), in a format that is determined by the subclass.
12  */

13 public abstract class Report
14 {
15     /**
16      * The file to which this report currently applies. By default, this is '-',
17      * denoting standard output.
18      */

19     protected String JavaDoc fileName = "-";
20
21     /**
22      * The writer to which this report sends output.
23      */

24     private Writer writer;
25
26     /**
27      * The set of violations, which are maintained in sorted order.
28      */

29     private Set JavaDoc violations = new TreeSet JavaDoc();
30
31     /**
32      * Creates a report for the given writer.
33      *
34      * @param writer The writer associated with this report.
35      */

36     public Report(Writer writer)
37     {
38         this.writer = writer;
39     }
40
41     /**
42      * Creates a report for the given output stream.
43      *
44      * @param os The output stream associated with this report.
45      */

46     public Report(OutputStream os)
47     {
48         this(new OutputStreamWriter(os));
49     }
50
51     /**
52      * Creates a report for the given writer, and a string source.
53      *
54      * @param writer The writer associated with this report.
55      * @param source The source code to which this report applies.
56      */

57     public Report(Writer writer, String JavaDoc source)
58     {
59         this(writer);
60
61         reset(source);
62     }
63
64     /**
65      * Creates a report for the given writer, and a file source.
66      *
67      * @param writer The writer associated with this report.
68      * @param file The file, containing source code, to which this report applies.
69      */

70     public Report(Writer writer, File file)
71     {
72         this(writer);
73         
74         reset(file);
75     }
76
77     /**
78      * Creates a report for the given output stream, and string source.
79      *
80      * @param os The output stream associated with this report.
81      * @param source The source code to which this report applies.
82      */

83     public Report(OutputStream os, String JavaDoc source)
84     {
85         this(os);
86         
87         reset(source);
88     }
89
90     /**
91      * Creates a report for the given output stream, and file.
92      *
93      * @param os The output stream associated with this report.
94      * @param file The file, containing source code, to which this report applies.
95      */

96     public Report(OutputStream os, File file)
97     {
98         this(os);
99         
100         reset(file);
101     }
102     
103     /**
104      * Associates the given file with the list of violations, including that are
105      * adding to this report later, i.e., prior to <code>flush</code>.
106      *
107      * @param file The file associated with the set of violations.
108      */

109     public void reset(File file)
110     {
111         tr.Ace.log("file", file);
112         
113         try {
114             fileName = file.getCanonicalPath();
115         }
116         catch (IOException ioe) {
117         }
118     }
119
120     /**
121      * Associates the given string source with the list of violations, including
122      * that are adding to this report later, i.e., prior to <code>flush</code>.
123      *
124      * @param source The source code associated with the set of violations.
125      */

126     public void reset(String JavaDoc source)
127     {
128         tr.Ace.log("source", source);
129         
130         fileName = "-";
131     }
132
133     /**
134      * Writes all violations, and clears the list.
135      */

136     public void flush()
137     {
138         try {
139             tr.Ace.stack("flushing");
140             
141             Iterator JavaDoc it = violations.iterator();
142             while (it.hasNext()) {
143                 Object JavaDoc obj = it.next();
144                 Violation v = (Violation)obj;
145                 String JavaDoc str = toString(v);
146                 tr.Ace.log("v", v);
147                 tr.Ace.log("str", str);
148                 writer.write(str);
149             }
150             // we can't close STDOUT
151
writer.flush();
152             // writer.close();
153
}
154         catch (IOException ioe) {
155         }
156         violations = new TreeSet JavaDoc();
157     }
158
159     /**
160      * Adds the given violation.
161      *
162      * @param v The violation being added.
163      */

164     public void addViolation(Violation v)
165     {
166         tr.Ace.stack("v", v);
167         violations.add(v);
168     }
169
170     /**
171      * Exists only for testing.
172      */

173     public Set JavaDoc getViolations()
174     {
175         return violations;
176     }
177     
178     /**
179      * Returns a string representing the given violation, consistent with the
180      * format of the Report subclass.
181      *
182      * @param violation The violation to represent as a string.
183      */

184     protected abstract String JavaDoc toString(Violation violation);
185
186     /**
187      * Sends the given string to the writer associated with this Report.
188      *
189      * @param str The string to be written.
190      */

191     protected void write(String JavaDoc str)
192     {
193         tr.Ace.log("str", str);
194         try {
195             writer.write(str);
196         }
197         catch (IOException ioe) {
198         }
199     }
200 }
201
Popular Tags