Makefile fixup
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / problem / DefaultProblem.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.problem;
12
13 import org.eclipse.jdt.core.compiler.IProblem;
14 import org.eclipse.jdt.internal.compiler.util.Util;
15
16 public class DefaultProblem implements ProblemSeverities, IProblem {
17         
18         private static final String LINE_DELIMITER = System.getProperty("line.separator"); //$NON-NLS-1$
19                 
20         private char[] fileName;
21         private int id;
22         private int startPosition, endPosition, line;
23         private int severity;
24         private String[] arguments;
25         private String message;
26         
27         public DefaultProblem(
28                 char[] originatingFileName,
29                 String message,
30                 int id,
31                 String[] stringArguments,
32                 int severity,
33                 int startPosition,
34                 int endPosition,
35                 int line) {
36
37                 this.fileName = originatingFileName;
38                 this.message = message;
39                 this.id = id;
40                 this.arguments = stringArguments;
41                 this.severity = severity;
42                 this.startPosition = startPosition;
43                 this.endPosition = endPosition;
44                 this.line = line;
45         }
46         public String errorReportSource(char[] unitSource) {
47                 //extra from the source the innacurate     token
48                 //and "highlight" it using some underneath ^^^^^
49                 //put some context around too.
50
51                 //this code assumes that the font used in the console is fixed size
52
53                 //sanity .....
54                 if ((this.startPosition > this.endPosition)
55                         || ((this.startPosition < 0) && (this.endPosition < 0)))
56                         return Util.bind("problem.noSourceInformation"); //$NON-NLS-1$
57
58                 StringBuffer errorBuffer = new StringBuffer(" "); //$NON-NLS-1$
59                 errorBuffer.append(Util.bind("problem.atLine", String.valueOf(this.line))); //$NON-NLS-1$
60                 errorBuffer.append(LINE_DELIMITER).append("\t"); //$NON-NLS-1$
61                 
62                 char c;
63                 final char SPACE = '\u0020';
64                 final char MARK = '^';
65                 final char TAB = '\t';
66                 //the next code tries to underline the token.....
67                 //it assumes (for a good display) that token source does not
68                 //contain any \r \n. This is false on statements ! 
69                 //(the code still works but the display is not optimal !)
70
71                 // expand to line limits
72                 int length = unitSource.length, begin, end;
73                 for (begin = this.startPosition >= length ? length - 1 : this.startPosition; begin > 0; begin--) {
74                         if ((c = unitSource[begin - 1]) == '\n' || c == '\r') break;
75                 }
76                 for (end = this.endPosition >= length ? length - 1 : this.endPosition ; end+1 < length; end++) {
77                         if ((c = unitSource[end + 1]) == '\r' || c == '\n') break;
78                 }
79                 
80                 // trim left and right spaces/tabs
81                 while ((c = unitSource[begin]) == ' ' || c == '\t') begin++;
82                 //while ((c = unitSource[end]) == ' ' || c == '\t') end--; TODO (philippe) should also trim right, but all tests are to be updated
83                 
84                 // copy source
85                 errorBuffer.append(unitSource, begin, end-begin+1);
86                 errorBuffer.append(LINE_DELIMITER).append("\t"); //$NON-NLS-1$
87
88                 // compute underline
89                 for (int i = begin; i <this.startPosition; i++) {
90                         errorBuffer.append((unitSource[i] == TAB) ? TAB : SPACE);
91                 }
92                 for (int i = this.startPosition; i <= (this.endPosition >= length ? length - 1 : this.endPosition); i++) {
93                         errorBuffer.append(MARK);
94                 }
95                 return errorBuffer.toString();
96         }
97
98         /**
99          * Answer back the original arguments recorded into the problem.
100          * @return java.lang.String[]
101          */
102         public String[] getArguments() {
103
104                 return this.arguments;
105         }
106
107         /**
108          * Answer the type of problem.
109          * @see org.eclipse.jdt.core.compiler.IProblem#getID()
110          * @return int
111          */
112         public int getID() {
113
114                 return this.id;
115         }
116
117         /**
118          * Answer a localized, human-readable message string which describes the problem.
119          * @return java.lang.String
120          */
121         public String getMessage() {
122
123                 return this.message;
124         }
125
126         /**
127          * Answer the file name in which the problem was found.
128          * @return char[]
129          */
130         public char[] getOriginatingFileName() {
131
132                 return this.fileName;
133         }
134
135         /**
136          * Answer the end position of the problem (inclusive), or -1 if unknown.
137          * @return int
138          */
139         public int getSourceEnd() {
140
141                 return this.endPosition;
142         }
143
144         /**
145          * Answer the line number in source where the problem begins.
146          * @return int
147          */
148         public int getSourceLineNumber() {
149
150                 return this.line;
151         }
152
153         /**
154          * Answer the start position of the problem (inclusive), or -1 if unknown.
155          * @return int
156          */
157         public int getSourceStart() {
158
159                 return this.startPosition;
160         }
161
162         /*
163          * Helper method: checks the severity to see if the Error bit is set.
164          * @return boolean
165          */
166         public boolean isError() {
167
168                 return (this.severity & ProblemSeverities.Error) != 0;
169         }
170
171         /*
172          * Helper method: checks the severity to see if the Error bit is not set.
173          * @return boolean
174          */
175         public boolean isWarning() {
176
177                 return (this.severity & ProblemSeverities.Error) == 0;
178         }
179
180         public void setOriginatingFileName(char[] fileName) {
181                 this.fileName = fileName;
182         }
183         
184         /**
185          * Set the end position of the problem (inclusive), or -1 if unknown.
186          *
187          * Used for shifting problem positions.
188          * @param sourceEnd the new value of the sourceEnd of the receiver
189          */
190         public void setSourceEnd(int sourceEnd) {
191
192                 this.endPosition = sourceEnd;
193         }
194
195         /**
196          * Set the line number in source where the problem begins.
197          * @param lineNumber the new value of the line number of the receiver
198          */
199         public void setSourceLineNumber(int lineNumber) {
200
201                 this.line = lineNumber;
202         }
203
204         /**
205          * Set the start position of the problem (inclusive), or -1 if unknown.
206          *
207          * Used for shifting problem positions.
208          * @param sourceStart the new value of the source start position of the receiver
209          */
210         public void setSourceStart(int sourceStart) {
211
212                 this.startPosition = sourceStart;
213         }
214
215         public String toString() {
216
217                 String s = "Pb(" + (this.id & IgnoreCategoriesMask) + ") "; //$NON-NLS-1$ //$NON-NLS-2$
218                 if (this.message != null) {
219                         s += this.message;
220                 } else {
221                         if (this.arguments != null)
222                                 for (int i = 0; i < this.arguments.length; i++)
223                                         s += " " + this.arguments[i]; //$NON-NLS-1$
224                 }
225                 return s;
226         }
227 }