KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > StandardEnvironment


1 package spoon.support;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileNotFoundException JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.io.Serializable JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.TreeMap JavaDoc;
11
12 import spoon.processing.Environment;
13 import spoon.processing.FileGenerator;
14 import spoon.processing.ProblemFixer;
15 import spoon.processing.ProcessingManager;
16 import spoon.processing.Processor;
17 import spoon.processing.ProcessorProperties;
18 import spoon.processing.Severity;
19 import spoon.reflect.Factory;
20 import spoon.reflect.declaration.CtElement;
21 import spoon.reflect.declaration.CtExecutable;
22 import spoon.reflect.declaration.CtSimpleType;
23 import spoon.reflect.declaration.SourcePosition;
24 import spoon.support.processing.XmlProcessorProperties;
25
26 /**
27  * This class implements a simple Spoon environment that reports messages in the
28  * standard output stream (Java-compliant).
29  */

30 public class StandardEnvironment implements Serializable JavaDoc, Environment {
31
32     /**
33      * The processors' properties files extension (.xml)
34      */

35     public static final String JavaDoc PROPERTIES_EXT = ".xml";
36
37     private static final long serialVersionUID = 1L;
38
39     private boolean debug = false;
40
41     private FileGenerator defaultFileGenerator;
42
43     private int errorCount = 0;
44
45     private transient Factory factory;
46
47     ProcessingManager manager;
48
49     private boolean processingStopped = false;
50
51     private boolean verbose = false;
52
53     private int warningCount = 0;
54
55     private File JavaDoc xmlRootFolder;
56
57     /**
58      * Creates a new environment with a <code>null</code> default file
59      * generator.
60      */

61     public StandardEnvironment() {
62     }
63
64     /**
65      * Creates a new environment.
66      */

67     public StandardEnvironment(FileGenerator defaultFileGenerator) {
68         this.defaultFileGenerator = defaultFileGenerator;
69     }
70
71     public void debugMessage(String JavaDoc message) {
72         if (isDebug())
73             System.out.println(message);
74     }
75
76     public FileGenerator<?> getDefaultFileGenerator() {
77         return defaultFileGenerator;
78     }
79
80     public Factory getFactory() {
81         return factory;
82     }
83
84     public ProcessingManager getManager() {
85         return manager;
86     }
87
88     Map JavaDoc<String JavaDoc, ProcessorProperties> processorProperties = new TreeMap JavaDoc<String JavaDoc, ProcessorProperties>();
89
90     public ProcessorProperties getProcessorProperties(String JavaDoc processorName)
91             throws FileNotFoundException JavaDoc, IOException JavaDoc {
92         if (processorProperties.containsKey(processorName))
93             return processorProperties.get(processorName);
94
95         InputStream JavaDoc in = getPropertyStream(processorName);
96         XmlProcessorProperties prop = new XmlProcessorProperties(getFactory(),
97                 processorName, in);
98         processorProperties.put(processorName, prop);
99         return prop;
100     }
101
102     private InputStream JavaDoc getPropertyStream(String JavaDoc processorName)
103             throws FileNotFoundException JavaDoc {
104         for (File JavaDoc child : getXmlRootFolder().listFiles()) {
105             if (child.getName().equals(processorName + PROPERTIES_EXT)) {
106                 return new FileInputStream JavaDoc(child);
107             }
108         }
109         throw new FileNotFoundException JavaDoc();
110     }
111
112     /**
113      * Gets the root folder where the processors' XML configuration files are
114      * located.
115      */

116     public File JavaDoc getXmlRootFolder() {
117         if (xmlRootFolder == null) {
118             xmlRootFolder = new File JavaDoc(".");
119         }
120         return xmlRootFolder;
121     }
122
123     public boolean isDebug() {
124         return debug;
125     }
126
127     /**
128      * Tells if the processing is stopped, generally because one of the
129      * processors called {@link #setProcessingStopped(boolean)} after reporting
130      * an error.
131      */

132     public boolean isProcessingStopped() {
133         return processingStopped;
134     }
135
136     /**
137      * Returns true if Spoon is in verbose mode.
138      */

139     public boolean isVerbose() {
140         return verbose;
141     }
142
143     private void prefix(StringBuffer JavaDoc buffer, Severity severity) {
144         // Prefix message
145
switch (severity) {
146         case ERROR:
147             buffer.append("error : ");
148             errorCount++;
149             break;
150         case WARNING:
151             buffer.append("warning : ");
152             warningCount++;
153             break;
154         case MESSAGE:
155             break;
156         }
157     }
158
159     private void print(StringBuffer JavaDoc buffer, Severity severity) {
160         switch (severity) {
161         case ERROR:
162         case WARNING:
163             System.out.println(buffer.toString());
164             break;
165         default:
166             if (isVerbose())
167                 System.out.println(buffer.toString());
168         }
169     }
170
171     public void report(Processor processor, Severity severity,
172             CtElement element, String JavaDoc message) {
173         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
174
175         prefix(buffer, severity);
176
177         // Adding message
178
buffer.append(message);
179
180         // Add sourceposition (javac format)
181
CtSimpleType<?> type = (element instanceof CtSimpleType) ? (CtSimpleType) element
182                 : element.getParent(CtSimpleType.class);
183         SourcePosition sp = element.getPosition();
184
185         if (sp == null) {
186             buffer.append(" (Unknown Source)");
187         } else {
188             buffer.append(" at " + type.getQualifiedName() + ".");
189             CtExecutable<?> exe = (element instanceof CtExecutable) ? (CtExecutable) element
190                     : element.getParent(CtExecutable.class);
191             if (exe != null)
192                 buffer.append(exe.getSimpleName());
193             buffer.append("(" + sp.getFile().getName() + ":" + sp.getLine()
194                     + ")");
195         }
196
197         print(buffer, severity);
198     }
199
200     public void report(Processor processor, Severity severity, String JavaDoc message) {
201         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
202
203         prefix(buffer, severity);
204         // Adding message
205
buffer.append(message);
206         print(buffer, severity);
207     }
208
209     /**
210      * This method should be called to report the end of the processing.
211      */

212     public void reportEnd() {
213         if (warningCount > 0) {
214             System.out.print(warningCount + " warning");
215             if (warningCount > 1)
216                 System.out.print("s");
217             if (errorCount > 0)
218                 System.out.print("\t");
219         }
220         if (errorCount > 0) {
221             System.out.print(errorCount + " error");
222             if (errorCount > 1)
223                 System.out.print("s");
224         }
225         if ((errorCount + warningCount) > 0)
226             System.out.print("\n");
227     }
228
229     public void reportProgressMessage(String JavaDoc message) {
230         System.out.println(message);
231     }
232
233     public void setDebug(boolean debug) {
234         this.debug = debug;
235     }
236
237     public void setDefaultFileGenerator(
238             FileGenerator<? extends CtElement> defaultFileGenerator) {
239         this.defaultFileGenerator = defaultFileGenerator;
240         defaultFileGenerator.setFactory(getFactory());
241     }
242
243     public void setFactory(Factory factory) {
244         this.factory = factory;
245     }
246
247     public void setManager(ProcessingManager manager) {
248         this.manager = manager;
249     }
250
251     public void setProcessingStopped(boolean processingStopped) {
252         this.processingStopped = processingStopped;
253     }
254
255     public void setVerbose(boolean verbose) {
256         this.verbose = verbose;
257     }
258
259     /**
260      * Sets the root folder where the processors' XML configuration files are
261      * located.
262      */

263     public void setXmlRootFolder(File JavaDoc xmlRootFolder) {
264         this.xmlRootFolder = xmlRootFolder;
265     }
266
267     int complianceLevel = 5;
268
269     public int getComplianceLevel() {
270         return complianceLevel;
271     }
272
273     public void setComplianceLevel(int level) {
274         this.complianceLevel = level;
275     }
276
277     public void setProcessorProperties(String JavaDoc processorName,
278             ProcessorProperties prop) {
279         processorProperties.put(processorName, prop);
280     }
281
282     public void report(Processor processor, Severity severity,
283             CtElement element, String JavaDoc message, ProblemFixer... fix) {
284         // Fix not (yet) used in command-line mode
285
report(processor, severity, element, message);
286     }
287
288 }
289
Popular Tags