1e7169ed5e1a7c669ce2943d73a388500813ac3e
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / problem / DefaultProblem.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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 net.sourceforge.phpdt.internal.compiler.problem;
12
13 import net.sourceforge.phpdt.core.compiler.IProblem;
14 import net.sourceforge.phpdt.internal.compiler.util.Util;
15
16 import org.eclipse.ui.texteditor.ITextEditor;
17
18
19 public class DefaultProblem implements ProblemSeverities, IProblem {
20                 
21         private char[] fileName;
22         private int id;
23         private int startPosition, endPosition, line;
24         private int severity;
25         private String[] arguments;
26         private String message;
27         
28         public DefaultProblem(
29                 char[] originatingFileName,
30                 String message,
31                 int id,
32                 String[] stringArguments,
33                 int severity,
34                 int startPosition,
35                 int endPosition,
36                 int line) {
37
38                 this.fileName = originatingFileName;
39                 this.message = message;
40                 this.id = id;
41                 this.arguments = stringArguments;
42                 this.severity = severity;
43                 this.startPosition = startPosition;
44                 this.endPosition = endPosition;
45                 this.line = line;
46         }
47
48         public String errorReportSource(ITextEditor textEditor){ //ICompilationUnit compilationUnit) {
49                 //extra from the source the innacurate     token
50                 //and "highlight" it using some underneath ^^^^^
51                 //put some context around too.
52
53                 //this code assumes that the font used in the console is fixed size
54
55                 //sanity .....
56                 if ((startPosition > endPosition)
57                         || ((startPosition <= 0) && (endPosition <= 0)))
58                         return Util.bind("problem.noSourceInformation"); //$NON-NLS-1$
59
60                 final char SPACE = '\u0020';
61                 final char MARK = '^';
62                 final char TAB = '\t';
63 //              char[] source = compilationUnit.getContents();
64                 char[] source = textEditor.getDocumentProvider().getDocument(null).get().toCharArray();
65                 //the next code tries to underline the token.....
66                 //it assumes (for a good display) that token source does not
67                 //contain any \r \n. This is false on statements ! 
68                 //(the code still works but the display is not optimal !)
69
70                 //compute the how-much-char we are displaying around the inaccurate token
71                 int begin = startPosition >= source.length ? source.length - 1 : startPosition;
72                 int relativeStart = 0;
73                 int end = endPosition >= source.length ? source.length - 1 : endPosition;
74                 int relativeEnd = 0;
75                 label : for (relativeStart = 0;; relativeStart++) {
76                         if (begin == 0)
77                                 break label;
78                         if ((source[begin - 1] == '\n') || (source[begin - 1] == '\r'))
79                                 break label;
80                         begin--;
81                 }
82                 label : for (relativeEnd = 0;; relativeEnd++) {
83                         if ((end + 1) >= source.length)
84                                 break label;
85                         if ((source[end + 1] == '\r') || (source[end + 1] == '\n')) {
86                                 break label;
87                         }
88                         end++;
89                 }
90                 //extract the message form the source
91                 char[] extract = new char[end - begin + 1];
92                 System.arraycopy(source, begin, extract, 0, extract.length);
93                 char c;
94                 //remove all SPACE and TAB that begin the error message...
95                 int trimLeftIndex = 0;
96                 while (((c = extract[trimLeftIndex++]) == TAB) || (c == SPACE)) {
97                 };
98                 System.arraycopy(
99                         extract,
100                         trimLeftIndex - 1,
101                         extract = new char[extract.length - trimLeftIndex + 1],
102                         0,
103                         extract.length);
104                 relativeStart -= trimLeftIndex;
105                 //buffer spaces and tabs in order to reach the error position
106                 int pos = 0;
107                 char[] underneath = new char[extract.length]; // can't be bigger
108                 for (int i = 0; i <= relativeStart; i++) {
109                         if (extract[i] == TAB) {
110                                 underneath[pos++] = TAB;
111                         } else {
112                                 underneath[pos++] = SPACE;
113                         }
114                 }
115                 //mark the error position
116                 for (int i = startPosition;
117                         i <= (endPosition >= source.length ? source.length - 1 : endPosition);
118                         i++)
119                         underneath[pos++] = MARK;
120                 //resize underneathto remove 'null' chars
121                 System.arraycopy(underneath, 0, underneath = new char[pos], 0, pos);
122
123                 return " " + Util.bind("problem.atLine", String.valueOf(line))  //$NON-NLS-2$ //$NON-NLS-1$
124                         + "\n\t" + new String(extract) + "\n\t" + new String(underneath); //$NON-NLS-2$ //$NON-NLS-1$
125         }
126
127         /**
128          * Answer back the original arguments recorded into the problem.
129          * @return java.lang.String[]
130          */
131         public String[] getArguments() {
132
133                 return arguments;
134         }
135
136         /**
137          * Answer the type of problem.
138          * @see org.eclipse.jdt.core.compiler.IProblem#getID()
139          * @return int
140          */
141         public int getID() {
142
143                 return id;
144         }
145
146         /**
147          * Answer a localized, human-readable message string which describes the problem.
148          * @return java.lang.String
149          */
150         public String getMessage() {
151
152                 return message;
153         }
154
155         /**
156          * Answer the file name in which the problem was found.
157          * @return char[]
158          */
159         public char[] getOriginatingFileName() {
160
161                 return fileName;
162         }
163
164         /**
165          * Answer the end position of the problem (inclusive), or -1 if unknown.
166          * @return int
167          */
168         public int getSourceEnd() {
169
170                 return endPosition;
171         }
172
173         /**
174          * Answer the line number in source where the problem begins.
175          * @return int
176          */
177         public int getSourceLineNumber() {
178
179                 return line;
180         }
181
182         /**
183          * Answer the start position of the problem (inclusive), or -1 if unknown.
184          * @return int
185          */
186         public int getSourceStart() {
187
188                 return startPosition;
189         }
190
191         /*
192          * Helper method: checks the severity to see if the Error bit is set.
193          * @return boolean
194          */
195         public boolean isError() {
196
197                 return (severity & ProblemSeverities.Error) != 0;
198         }
199
200         /*
201          * Helper method: checks the severity to see if the Error bit is not set.
202          * @return boolean
203          */
204         public boolean isWarning() {
205
206                 return (severity & ProblemSeverities.Error) == 0;
207         }
208
209         /**
210          * Set the end position of the problem (inclusive), or -1 if unknown.
211          *
212          * Used for shifting problem positions.
213          * @param sourceEnd the new value of the sourceEnd of the receiver
214          */
215         public void setSourceEnd(int sourceEnd) {
216
217                 endPosition = sourceEnd;
218         }
219
220         /**
221          * Set the line number in source where the problem begins.
222          * @param lineNumber the new value of the line number of the receiver
223          */
224         public void setSourceLineNumber(int lineNumber) {
225
226                 line = lineNumber;
227         }
228
229         /**
230          * Set the start position of the problem (inclusive), or -1 if unknown.
231          *
232          * Used for shifting problem positions.
233          * @param sourceStart the new value of the source start position of the receiver
234          */
235         public void setSourceStart(int sourceStart) {
236
237                 startPosition = sourceStart;
238         }
239
240         public String toString() {
241
242                 String s = "Pb(" + (id & IgnoreCategoriesMask) + ") "; //$NON-NLS-1$ //$NON-NLS-2$
243                 if (message != null) {
244                         s += message;
245                 } else {
246                         if (arguments != null)
247                                 for (int i = 0; i < arguments.length; i++)
248                                         s += " " + arguments[i]; //$NON-NLS-1$
249                 }
250                 return s;
251         }
252 }