View Javadoc

1   package org.codehaus.groovy.control.messages;
2   
3   import org.codehaus.groovy.control.Janitor;
4   import org.codehaus.groovy.control.ProcessingUnit;
5   import org.codehaus.groovy.control.SourceUnit;
6   import org.codehaus.groovy.syntax.SyntaxException;
7   
8   import java.io.PrintWriter;
9   
10  
11  /***
12   * A class for error messages produced by the parser system.
13   *
14   * @author <a href="mailto:cpoirier@dreaming.org">Chris Poirier</a>
15   * @version $Id: SyntaxErrorMessage.java,v 1.2 2004/12/07 21:53:00 spullara Exp $
16   */
17  
18  public class SyntaxErrorMessage extends Message {
19      protected SyntaxException cause = null;
20  
21      public SyntaxErrorMessage(SyntaxException cause) {
22          this.cause = cause;
23      }
24  
25  
26      /***
27       * Returns the underlying SyntaxException.
28       */
29  
30      public SyntaxException getCause() {
31          return this.cause;
32      }
33  
34  
35      /***
36       * Writes out a nicely formatted summary of the syntax error.
37       */
38  
39      public void write(PrintWriter output, ProcessingUnit context, Janitor janitor) {
40          SourceUnit source = (SourceUnit) context;   // This is reliably true
41  
42          String name = source.getName();
43          int line = getCause().getStartLine();
44          int column = getCause().getStartColumn();
45          String sample = source.getSample(line, column, janitor);
46  
47          output.print(name + ": " + line + ": " + getCause().getMessage());
48          if (sample != null) {
49              output.println();
50              output.print(source.getSample(line, column, janitor));
51          }
52      }
53  
54  
55  }
56  
57  
58