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
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 import org.eclipse.ui.texteditor.ITextEditor;
15 public class DefaultProblem implements ProblemSeverities, IProblem {
16 private char[] fileName;
18 private int startPosition, endPosition, line;
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;
28 this.arguments = stringArguments;
29 this.severity = severity;
30 this.startPosition = startPosition;
31 this.endPosition = endPosition;
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
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$
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')
60 for (end = endPosition >= length ? length - 1 : endPosition; end + 1 < length; end++) {
61 if ((c = unitSource[end + 1]) == '\r' || c == '\n')
64 // trim left and right spaces/tabs
65 while ((c = unitSource[begin]) == ' ' || c == '\t')
67 //while ((c = unitSource[end]) == ' ' || c == '\t') end--; TODO
68 // (philippe) should also trim right, but all tests are to be updated
70 errorBuffer.append(unitSource, begin, end - begin + 1);
71 errorBuffer.append("\n\t"); //$NON-NLS-1$
73 for (int i = begin; i < startPosition; i++) {
74 errorBuffer.append((unitSource[i] == TAB) ? TAB : SPACE);
76 for (int i = startPosition; i <= (endPosition >= length
78 : endPosition); i++) {
79 errorBuffer.append(MARK);
81 return errorBuffer.toString();
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.
89 // //this code assumes that the font used in the console is fixed size
92 // if ((startPosition > endPosition)
93 // || ((startPosition <= 0) && (endPosition <= 0)))
94 // return Util.bind("problem.noSourceInformation"); //$NON-NLS-1$
96 // final char SPACE = '\u0020';
97 // final char MARK = '^';
98 // final char TAB = '\t';
99 //// char[] source = compilationUnit.getContents();
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 !)
107 // //compute the how-much-char we are displaying around the inaccurate
109 // int begin = startPosition >= source.length ? source.length - 1 :
111 // int relativeStart = 0;
112 // int end = endPosition >= source.length ? source.length - 1 :
114 // int relativeEnd = 0;
115 // label : for (relativeStart = 0;; relativeStart++) {
118 // if ((source[begin - 1] == '\n') || (source[begin - 1] == '\r'))
122 // label : for (relativeEnd = 0;; relativeEnd++) {
123 // if ((end + 1) >= source.length)
125 // if ((source[end + 1] == '\r') || (source[end + 1] == '\n')) {
130 // //extract the message form the source
131 // char[] extract = new char[end - begin + 1];
132 // System.arraycopy(source, begin, extract, 0, extract.length);
134 // //remove all SPACE and TAB that begin the error message...
135 // int trimLeftIndex = 0;
136 // while (((c = extract[trimLeftIndex++]) == TAB) || (c == SPACE)) {
140 // trimLeftIndex - 1,
141 // extract = new char[extract.length - trimLeftIndex + 1],
144 // relativeStart -= trimLeftIndex;
145 // //buffer spaces and tabs in order to reach the error position
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;
152 // underneath[pos++] = SPACE;
155 // //mark the error position
156 // for (int i = startPosition;
157 // i <= (endPosition >= source.length ? source.length - 1 : endPosition);
159 // underneath[pos++] = MARK;
160 // //resize underneathto remove 'null' chars
161 // System.arraycopy(underneath, 0, underneath = new char[pos], 0, pos);
163 // return " " + Util.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$
169 * Answer back the original arguments recorded into the problem.
171 * @return java.lang.String[]
173 public String[] getArguments() {
177 * Answer the type of problem.
179 * @see org.eclipse.jdt.core.compiler.IProblem#getID()
186 * Answer a localized, human-readable message string which describes the
189 * @return java.lang.String
191 public String getMessage() {
195 * Answer the file name in which the problem was found.
199 public char[] getOriginatingFileName() {
203 * Answer the end position of the problem (inclusive), or -1 if unknown.
207 public int getSourceEnd() {
211 * Answer the line number in source where the problem begins.
215 public int getSourceLineNumber() {
219 * Answer the start position of the problem (inclusive), or -1 if unknown.
223 public int getSourceStart() {
224 return startPosition;
227 * Helper method: checks the severity to see if the Error bit is set.
230 public boolean isError() {
231 return (severity & ProblemSeverities.Error) != 0;
234 * Helper method: checks the severity to see if the Error bit is not set.
237 public boolean isWarning() {
238 return (severity & ProblemSeverities.Error) == 0;
241 * Set the end position of the problem (inclusive), or -1 if unknown.
243 * Used for shifting problem positions.
246 * the new value of the sourceEnd of the receiver
248 public void setSourceEnd(int sourceEnd) {
249 endPosition = sourceEnd;
252 * Set the line number in source where the problem begins.
255 * the new value of the line number of the receiver
257 public void setSourceLineNumber(int lineNumber) {
261 * Set the start position of the problem (inclusive), or -1 if unknown.
263 * Used for shifting problem positions.
266 * the new value of the source start position of the receiver
268 public void setSourceStart(int sourceStart) {
269 startPosition = sourceStart;
271 public String toString() {
272 String s = "Pb(" + (id & IgnoreCategoriesMask) + ") "; //$NON-NLS-1$ //$NON-NLS-2$
273 if (message != null) {
276 if (arguments != null)
277 for (int i = 0; i < arguments.length; i++)
278 s += " " + arguments[i]; //$NON-NLS-1$