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