001: package org.incava.analysis;
002:
003: import java.io.*;
004: import java.util.Iterator;
005: import java.util.Set;
006: import java.util.TreeSet;
007: import net.sourceforge.pmd.ast.Token;
008:
009: /**
010: * Reports errors (violations), in a format that is determined by the subclass.
011: */
012: public abstract class Report {
013: /**
014: * The file to which this report currently applies. By default, this is '-',
015: * denoting standard output.
016: */
017: protected String fileName = "-";
018:
019: /**
020: * The writer to which this report sends output.
021: */
022: private Writer writer;
023:
024: /**
025: * The set of violations, which are maintained in sorted order.
026: */
027: private Set violations = new TreeSet();
028:
029: /**
030: * Creates a report for the given writer.
031: *
032: * @param writer The writer associated with this report.
033: */
034: public Report(Writer writer) {
035: this .writer = writer;
036: }
037:
038: /**
039: * Creates a report for the given output stream.
040: *
041: * @param os The output stream associated with this report.
042: */
043: public Report(OutputStream os) {
044: this (new OutputStreamWriter(os));
045: }
046:
047: /**
048: * Creates a report for the given writer, and a string source.
049: *
050: * @param writer The writer associated with this report.
051: * @param source The source code to which this report applies.
052: */
053: public Report(Writer writer, String source) {
054: this (writer);
055:
056: reset(source);
057: }
058:
059: /**
060: * Creates a report for the given writer, and a file source.
061: *
062: * @param writer The writer associated with this report.
063: * @param file The file, containing source code, to which this report applies.
064: */
065: public Report(Writer writer, File file) {
066: this (writer);
067:
068: reset(file);
069: }
070:
071: /**
072: * Creates a report for the given output stream, and string source.
073: *
074: * @param os The output stream associated with this report.
075: * @param source The source code to which this report applies.
076: */
077: public Report(OutputStream os, String source) {
078: this (os);
079:
080: reset(source);
081: }
082:
083: /**
084: * Creates a report for the given output stream, and file.
085: *
086: * @param os The output stream associated with this report.
087: * @param file The file, containing source code, to which this report applies.
088: */
089: public Report(OutputStream os, File file) {
090: this (os);
091:
092: reset(file);
093: }
094:
095: /**
096: * Associates the given file with the list of violations, including that are
097: * adding to this report later, i.e., prior to <code>flush</code>.
098: *
099: * @param file The file associated with the set of violations.
100: */
101: public void reset(File file) {
102: tr.Ace.log("file", file);
103:
104: try {
105: fileName = file.getCanonicalPath();
106: } catch (IOException ioe) {
107: }
108: }
109:
110: /**
111: * Associates the given string source with the list of violations, including
112: * that are adding to this report later, i.e., prior to <code>flush</code>.
113: *
114: * @param source The source code associated with the set of violations.
115: */
116: public void reset(String source) {
117: tr.Ace.log("source", source);
118:
119: fileName = "-";
120: }
121:
122: /**
123: * Writes all violations, and clears the list.
124: */
125: public void flush() {
126: try {
127: tr.Ace.stack("flushing");
128:
129: Iterator it = violations.iterator();
130: while (it.hasNext()) {
131: Object obj = it.next();
132: Violation v = (Violation) obj;
133: String str = toString(v);
134: tr.Ace.log("v", v);
135: tr.Ace.log("str", str);
136: writer.write(str);
137: }
138: // we can't close STDOUT
139: writer.flush();
140: // writer.close();
141: } catch (IOException ioe) {
142: }
143: violations = new TreeSet();
144: }
145:
146: /**
147: * Adds the given violation.
148: *
149: * @param v The violation being added.
150: */
151: public void addViolation(Violation v) {
152: tr.Ace.stack("v", v);
153: violations.add(v);
154: }
155:
156: /**
157: * Exists only for testing.
158: */
159: public Set getViolations() {
160: return violations;
161: }
162:
163: /**
164: * Returns a string representing the given violation, consistent with the
165: * format of the Report subclass.
166: *
167: * @param violation The violation to represent as a string.
168: */
169: protected abstract String toString(Violation violation);
170:
171: /**
172: * Sends the given string to the writer associated with this Report.
173: *
174: * @param str The string to be written.
175: */
176: protected void write(String str) {
177: tr.Ace.log("str", str);
178: try {
179: writer.write(str);
180: } catch (IOException ioe) {
181: }
182: }
183: }
|