KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > groovy > GroovyCompilationException


1 package org.nanocontainer.script.groovy;
2
3 import java.util.Collections JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.codehaus.groovy.control.CompilationFailedException;
7 import org.codehaus.groovy.control.ErrorCollector;
8 import org.codehaus.groovy.control.ProcessingUnit;
9 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 NanoContainerMarkupException {
17     private CompilationFailedException compilationFailedException;
18
19     public GroovyCompilationException(String JavaDoc message, CompilationFailedException e) {
20         super(message,e);
21         this.compilationFailedException = e;
22     }
23
24     public String JavaDoc getMessage() {
25         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
26         sb.append(super.getMessage() + "\n");
27         List JavaDoc errors = getErrors(compilationFailedException);
28         for (int i = 0; i < errors.size(); i++) {
29             Object JavaDoc o = errors.get(i);
30             if (o instanceof ExceptionMessage) {
31                 ExceptionMessage em = (ExceptionMessage) o;
32                 sb.append(em.getCause().getMessage() + "\n");
33             }
34         }
35         return sb.toString();
36     }
37
38     /**
39      * Extract errors from groovy exception, coding defensively against
40      * possible null values.
41      * @param e the CompilationFailedException
42      * @return A List of errors
43      */

44     private List JavaDoc getErrors(CompilationFailedException e) {
45         ProcessingUnit unit = e.getUnit();
46         if ( unit != null ){
47             ErrorCollector collector = unit.getErrorCollector();
48             if ( collector != null ){
49                 List JavaDoc errors = collector.getErrors();
50                 if ( errors != null ){
51                     return errors;
52                 }
53             }
54         }
55         return Collections.EMPTY_LIST;
56     }
57 }
58
Popular Tags