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.janino.Location;
22  import org.codehaus.janino.Scanner.ScanException;
23  import org.codehaus.janino.Parser.ParseException;
24  
25  /**
26   * Janino version of a CompilationProblem
27   * 
28   * @author tcurdt
29   */
30  public final class JaninoCompilationProblem implements CompilationProblem {
31  
32      private final Location location;
33      private final String fileName;
34      private final String message;
35      private final boolean error;
36  
37      public JaninoCompilationProblem(final ParseException pParseException) {
38          this(pParseException.getLocation(), pParseException.getMessage(), true);
39      }
40  
41      public JaninoCompilationProblem(final ScanException pScanException) {
42          this(pScanException.getLocation(), pScanException.getMessage(), true);
43      }
44  
45      public JaninoCompilationProblem(final Location pLocation, final String pMessage, final boolean pError) {
46        this(pLocation.getFileName(), pLocation, pMessage, pError);
47      }
48  
49      public JaninoCompilationProblem(final String pFilename, final String pMessage, final boolean pError) {
50          this(pFilename, null, pMessage, pError);
51      }
52  
53      public JaninoCompilationProblem(final String pFilename, final Location pLocation, final String pMessage, final boolean pError) {
54          location = pLocation;
55          fileName = pFilename;
56          message = pMessage;
57          error = pError;
58      }
59  
60      public boolean isError() {
61          return error;
62      }
63  
64      public String getFileName() {
65          return fileName;
66      }
67  
68      public int getStartLine() {
69          if (location == null) {
70              return 0;
71          }
72          return location.getLineNumber();
73      }
74  
75      public int getStartColumn() {
76          if (location == null) {
77              return 0;
78          }
79          return location.getColumnNumber();
80      }
81  
82      public int getEndLine() {
83          return getStartLine();
84      }
85  
86      public int getEndColumn() {
87          return getStartColumn();
88      }
89  
90      public String getMessage() {
91          return message;
92      }
93  
94      public String toString() {
95          final StringBuffer sb = new StringBuffer();
96          sb.append(getFileName()).append(" (");
97          sb.append(getStartLine());
98          sb.append(":");
99          sb.append(getStartColumn());
100         sb.append(") : ");
101         sb.append(getMessage());
102         return sb.toString();
103     }
104 
105 }