1 package spoon.support; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.FileNotFoundException ; 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.io.Serializable ; 9 import java.util.Map ; 10 import java.util.TreeMap ; 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 30 public class StandardEnvironment implements Serializable , Environment { 31 32 35 public static final String 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 xmlRootFolder; 56 57 61 public StandardEnvironment() { 62 } 63 64 67 public StandardEnvironment(FileGenerator defaultFileGenerator) { 68 this.defaultFileGenerator = defaultFileGenerator; 69 } 70 71 public void debugMessage(String 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 <String , ProcessorProperties> processorProperties = new TreeMap <String , ProcessorProperties>(); 89 90 public ProcessorProperties getProcessorProperties(String processorName) 91 throws FileNotFoundException , IOException { 92 if (processorProperties.containsKey(processorName)) 93 return processorProperties.get(processorName); 94 95 InputStream 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 getPropertyStream(String processorName) 103 throws FileNotFoundException { 104 for (File child : getXmlRootFolder().listFiles()) { 105 if (child.getName().equals(processorName + PROPERTIES_EXT)) { 106 return new FileInputStream (child); 107 } 108 } 109 throw new FileNotFoundException (); 110 } 111 112 116 public File getXmlRootFolder() { 117 if (xmlRootFolder == null) { 118 xmlRootFolder = new File ("."); 119 } 120 return xmlRootFolder; 121 } 122 123 public boolean isDebug() { 124 return debug; 125 } 126 127 132 public boolean isProcessingStopped() { 133 return processingStopped; 134 } 135 136 139 public boolean isVerbose() { 140 return verbose; 141 } 142 143 private void prefix(StringBuffer buffer, Severity severity) { 144 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 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 message) { 173 StringBuffer buffer = new StringBuffer (); 174 175 prefix(buffer, severity); 176 177 buffer.append(message); 179 180 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 message) { 201 StringBuffer buffer = new StringBuffer (); 202 203 prefix(buffer, severity); 204 buffer.append(message); 206 print(buffer, severity); 207 } 208 209 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 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 263 public void setXmlRootFolder(File 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 processorName, 278 ProcessorProperties prop) { 279 processorProperties.put(processorName, prop); 280 } 281 282 public void report(Processor processor, Severity severity, 283 CtElement element, String message, ProblemFixer... fix) { 284 report(processor, severity, element, message); 286 } 287 288 } 289 | Popular Tags |