View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jci.compilers;
19  
20  import org.apache.commons.jci.problems.CompilationProblem;
21  import org.codehaus.groovy.control.messages.ExceptionMessage;
22  import org.codehaus.groovy.control.messages.Message;
23  import org.codehaus.groovy.control.messages.SimpleMessage;
24  import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
25  import org.codehaus.groovy.syntax.SyntaxException;
26  
27  /**
28   * Groovy version of a CompilationProblem
29   * 
30   * @author tcurdt
31   */
32  public final class GroovyCompilationProblem implements CompilationProblem {
33  
34      private final String fileName;
35      private final String message;
36      private final boolean error;
37      private final int startLine;
38      private final int startColumn;
39      private final int endLine;
40      private final int endColumn;
41  
42      public GroovyCompilationProblem(final Message pMessage) {
43          if (pMessage instanceof SimpleMessage) {
44              error = false;
45          } else {
46              error = true;
47          }
48          if (pMessage instanceof SyntaxErrorMessage) {
49              SyntaxErrorMessage syntaxErrorMessage = (SyntaxErrorMessage)pMessage;
50              SyntaxException syntaxException = syntaxErrorMessage.getCause();
51              message = syntaxException.getMessage();
52              fileName = syntaxException.getSourceLocator();
53              // FIXME: getStartLine() vs. getLine()
54              startLine = syntaxException.getStartLine();
55              startColumn = syntaxException.getStartColumn();
56              endLine = syntaxException.getLine();
57              endColumn = syntaxException.getEndColumn();
58          } else {
59              fileName = "";
60              startLine = 0;
61              startColumn = 0;
62              endLine = 0;
63              endColumn = 0;
64              if (pMessage instanceof ExceptionMessage) {
65                  message = ((ExceptionMessage)pMessage).getCause().getMessage();
66              } else if (pMessage instanceof SimpleMessage) {
67                  message = ((SimpleMessage)pMessage).getMessage();
68              } else {
69                  message = pMessage.toString();
70              }
71          }
72      }
73  
74      public boolean isError() {
75          return error;
76      }
77  
78      public String getFileName() {
79          return fileName;
80      }
81  
82      public int getStartLine() {
83          return startLine;
84      }
85  
86      public int getStartColumn() {
87          return startColumn;
88      }
89  
90      public int getEndLine() {
91          return endLine;
92      }
93  
94      public int getEndColumn() {
95          return endColumn;
96      }
97  
98      public String getMessage() {
99          return message;
100     }
101 
102     @Override
103     public String toString() {
104         return getMessage();
105     }
106 
107 }