1 46 47 package org.codehaus.groovy.control; 48 49 import java.io.PrintWriter ; 50 import java.util.Iterator ; 51 import java.util.LinkedList ; 52 import java.util.List ; 53 54 import org.codehaus.groovy.control.messages.ExceptionMessage; 55 import org.codehaus.groovy.control.messages.LocatedMessage; 56 import org.codehaus.groovy.control.messages.Message; 57 import org.codehaus.groovy.control.messages.SyntaxErrorMessage; 58 import org.codehaus.groovy.control.messages.WarningMessage; 59 import org.codehaus.groovy.syntax.CSTNode; 60 import org.codehaus.groovy.syntax.SyntaxException; 61 62 71 public class ErrorCollector { 72 73 76 protected LinkedList warnings; 77 80 protected LinkedList errors; 81 84 protected CompilerConfiguration configuration; 85 86 89 public ErrorCollector(CompilerConfiguration configuration) { 90 this.warnings = null; 91 this.errors = null; 92 93 this.configuration = configuration; 94 } 95 96 public void addCollectorContents(ErrorCollector er) { 97 if (er.errors!=null) { 98 if (errors==null) { 99 errors = er.errors; 100 } else { 101 errors.addAll(errors); 102 } 103 } 104 if (er.warnings!=null) { 105 if (warnings==null) { 106 warnings = er.warnings; 107 } else { 108 warnings.addAll(warnings); 109 } 110 } 111 } 112 113 114 115 118 public void addErrorAndContinue(Message message) { 119 if (this.errors == null) { 120 this.errors = new LinkedList (); 121 } 122 123 this.errors.add(message); 124 } 125 126 129 public void addError(Message message) throws CompilationFailedException { 130 addErrorAndContinue(message); 131 132 if (errors!=null && this.errors.size() >= configuration.getTolerance()) { 133 failIfErrors(); 134 } 135 } 136 137 141 public void addError(Message message, boolean fatal) throws CompilationFailedException { 142 if (fatal) { 143 addFatalError(message); 144 } 145 else { 146 addError(message); 147 } 148 } 149 150 151 154 public void addError(SyntaxException error, SourceUnit source) throws CompilationFailedException { 155 addError(Message.create(error, source), error.isFatal()); 156 } 157 158 159 162 public void addError(String text, CSTNode context, SourceUnit source) throws CompilationFailedException { 163 addError(new LocatedMessage(text, context, source)); 164 } 165 166 167 171 public void addFatalError(Message message) throws CompilationFailedException { 172 addError(message); 173 failIfErrors(); 174 } 175 176 177 public void addException(Exception cause, SourceUnit source) throws CompilationFailedException { 178 addError(new ExceptionMessage(cause,configuration.getDebug(),source)); 179 failIfErrors(); 180 } 181 182 185 public boolean hasErrors() { 186 return this.errors != null; 187 } 188 189 192 public boolean hasWarnings() { 193 return this.warnings != null; 194 } 195 196 199 public List getWarnings() { 200 return this.warnings; 201 } 202 203 206 public List getErrors() { 207 return this.errors; 208 } 209 210 213 public int getWarningCount() { 214 return ((this.warnings == null) ? 0 : this.warnings.size()); 215 } 216 217 220 public int getErrorCount() { 221 return ((this.errors == null) ? 0 : this.errors.size()); 222 } 223 224 227 public WarningMessage getWarning(int index) { 228 if (index < getWarningCount()) { 229 return (WarningMessage) this.warnings.get(index); 230 } 231 return null; 232 } 233 234 237 public Message getError(int index) { 238 if (index < getErrorCount()) { 239 return (Message) this.errors.get(index); 240 } 241 return null; 242 } 243 244 247 public Message getLastError() { 248 return (Message) this.errors.getLast(); 249 } 250 251 255 public SyntaxException getSyntaxError(int index) { 256 SyntaxException exception = null; 257 258 Message message = getError(index); 259 if (message != null && message instanceof SyntaxErrorMessage) { 260 exception = ((SyntaxErrorMessage) message).getCause(); 261 } 262 return exception; 263 } 264 265 269 public Exception getException(int index) { 270 Exception exception = null; 271 272 Message message = getError(index); 273 if (message != null) { 274 if (message instanceof ExceptionMessage) { 275 exception = ((ExceptionMessage) message).getCause(); 276 } 277 else if (message instanceof SyntaxErrorMessage) { 278 exception = ((SyntaxErrorMessage) message).getCause(); 279 } 280 } 281 return exception; 282 } 283 284 287 public void addWarning(WarningMessage message) { 288 if (message.isRelevant(configuration.getWarningLevel())) { 289 if (this.warnings == null) { 290 this.warnings = new LinkedList (); 291 } 292 293 this.warnings.add(message); 294 } 295 } 296 297 298 302 public void addWarning(int importance, String text, CSTNode context, SourceUnit source) { 303 if (WarningMessage.isRelevant(importance, configuration.getWarningLevel())) { 304 addWarning(new WarningMessage(importance, text, context, source)); 305 } 306 } 307 308 309 313 public void addWarning(int importance, String text, Object data, CSTNode context, SourceUnit source) { 314 if (WarningMessage.isRelevant(importance, configuration.getWarningLevel())) { 315 addWarning(new WarningMessage(importance, text, data, context, source)); 316 } 317 } 318 319 320 324 protected void failIfErrors() throws CompilationFailedException { 325 if (hasErrors()) throw new MultipleCompilationErrorsException(this); 326 } 327 328 331 332 335 public void write(PrintWriter writer, Janitor janitor) { 336 if (this.warnings != null) { 337 Iterator iterator = this.warnings.iterator(); 338 while (iterator.hasNext()) { 339 WarningMessage warning = (WarningMessage) iterator.next(); 340 warning.write(writer, janitor); 341 } 342 343 writer.println(); 344 writer.print(warnings.size()); 345 writer.print(" Warning"); 346 if (warnings.size()>1) writer.print("s"); 347 writer.println(); 348 349 this.warnings = null; 350 } 351 352 if (this.errors != null) { 353 Iterator iterator = this.errors.iterator(); 354 while (iterator.hasNext()) { 355 Message message = (Message) iterator.next(); 356 message.write(writer, janitor); 357 358 if (configuration.getDebug() && (message instanceof SyntaxErrorMessage)) { 359 SyntaxErrorMessage sem = (SyntaxErrorMessage) message; 360 SyntaxException se = sem.getCause(); 361 se.printStackTrace(writer); 362 } 363 } 364 365 writer.println(); 366 writer.print(errors.size()); 367 writer.print(" Error"); 368 if (errors.size()>1) writer.print("s"); 369 writer.println(); 370 } 371 } 372 373 } 374 | Popular Tags |