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