01: package org.nanocontainer.script.groovy;
02:
03: import java.util.Collections;
04: import java.util.List;
05:
06: import org.codehaus.groovy.control.CompilationFailedException;
07: import org.codehaus.groovy.control.ErrorCollector;
08: import org.codehaus.groovy.control.ProcessingUnit;
09: import org.codehaus.groovy.control.messages.ExceptionMessage;
10: import org.nanocontainer.script.NanoContainerMarkupException;
11:
12: /**
13: * @author Paul Hammant
14: * @version $Revision: 3144 $
15: */
16: public class GroovyCompilationException extends
17: NanoContainerMarkupException {
18: private CompilationFailedException compilationFailedException;
19:
20: public GroovyCompilationException(String message,
21: CompilationFailedException e) {
22: super (message, e);
23: this .compilationFailedException = e;
24: }
25:
26: public String getMessage() {
27: StringBuffer sb = new StringBuffer();
28: sb.append(super .getMessage() + "\n");
29: List errors = getErrors(compilationFailedException);
30: for (int i = 0; i < errors.size(); i++) {
31: Object o = errors.get(i);
32: if (o instanceof ExceptionMessage) {
33: ExceptionMessage em = (ExceptionMessage) o;
34: sb.append(em.getCause().getMessage() + "\n");
35: }
36: }
37: return sb.toString();
38: }
39:
40: /**
41: * Extract errors from groovy exception, coding defensively against
42: * possible null values.
43: * @param e the CompilationFailedException
44: * @return A List of errors
45: */
46: private List getErrors(CompilationFailedException e) {
47: ProcessingUnit unit = e.getUnit();
48: if (unit != null) {
49: ErrorCollector collector = unit.getErrorCollector();
50: if (collector != null) {
51: List errors = collector.getErrors();
52: if (errors != null) {
53: return errors;
54: }
55: }
56: }
57: return Collections.EMPTY_LIST;
58: }
59: }
|