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;
13 import net.sourceforge.phpdt.core.compiler.IProblem;
14 import net.sourceforge.phpdt.internal.compiler.util.Util;
16 public class DefaultProblem implements ProblemSeverities, IProblem {
17 private char[] fileName;
21 private int startPosition, endPosition, line;
25 private String[] arguments;
27 private String message;
29 public DefaultProblem(char[] originatingFileName, String message, int id,
30 String[] stringArguments, int severity, int startPosition,
31 int endPosition, int line) {
32 this.fileName = originatingFileName;
33 this.message = message;
35 this.arguments = stringArguments;
36 this.severity = severity;
37 this.startPosition = startPosition;
38 this.endPosition = endPosition;
42 public String errorReportSource(char[] unitSource) {
43 // extra from the source the innacurate token
44 // and "highlight" it using some underneath ^^^^^
45 // put some context around too.
46 // this code assumes that the font used in the console is fixed size
48 if ((startPosition > endPosition)
49 || ((startPosition < 0) && (endPosition < 0)))
50 return Util.bind("problem.noSourceInformation"); //$NON-NLS-1$
51 StringBuffer errorBuffer = new StringBuffer(" "); //$NON-NLS-1$
52 errorBuffer.append(Util.bind("problem.atLine", String.valueOf(line))); //$NON-NLS-1$
53 errorBuffer.append("\n\t"); //$NON-NLS-1$
55 final char SPACE = '\u0020';
56 final char MARK = '^';
57 final char TAB = '\t';
58 // the next code tries to underline the token.....
59 // it assumes (for a good display) that token source does not
60 // contain any \r \n. This is false on statements !
61 // (the code still works but the display is not optimal !)
62 // expand to line limits
63 int length = unitSource.length, begin, end;
64 for (begin = startPosition >= length ? length - 1 : startPosition; begin > 0; begin--) {
65 if ((c = unitSource[begin - 1]) == '\n' || c == '\r')
68 for (end = endPosition >= length ? length - 1 : endPosition; end + 1 < length; end++) {
69 if ((c = unitSource[end + 1]) == '\r' || c == '\n')
72 // trim left and right spaces/tabs
73 while ((c = unitSource[begin]) == ' ' || c == '\t')
75 // while ((c = unitSource[end]) == ' ' || c == '\t') end--; TODO
76 // (philippe) should also trim right, but all tests are to be updated
78 errorBuffer.append(unitSource, begin, end - begin + 1);
79 errorBuffer.append("\n\t"); //$NON-NLS-1$
81 for (int i = begin; i < startPosition; i++) {
82 errorBuffer.append((unitSource[i] == TAB) ? TAB : SPACE);
84 for (int i = startPosition; i <= (endPosition >= length ? length - 1
85 : endPosition); i++) {
86 errorBuffer.append(MARK);
88 return errorBuffer.toString();
91 // public String errorReportSource(ITextEditor textEditor){
92 // //ICompilationUnit compilationUnit) {
93 // //extra from the source the innacurate token
94 // //and "highlight" it using some underneath ^^^^^
95 // //put some context around too.
97 // //this code assumes that the font used in the console is fixed size
100 // if ((startPosition > endPosition)
101 // || ((startPosition <= 0) && (endPosition <= 0)))
102 // return ProjectPrefUtil.bind("problem.noSourceInformation"); //$NON-NLS-1$
104 // final char SPACE = '\u0020';
105 // final char MARK = '^';
106 // final char TAB = '\t';
107 // // char[] source = compilationUnit.getContents();
109 // textEditor.getDocumentProvider().getDocument(null).get().toCharArray();
110 // //the next code tries to underline the token.....
111 // //it assumes (for a good display) that token source does not
112 // //contain any \r \n. This is false on statements !
113 // //(the code still works but the display is not optimal !)
115 // //compute the how-much-char we are displaying around the inaccurate
117 // int begin = startPosition >= source.length ? source.length - 1 :
119 // int relativeStart = 0;
120 // int end = endPosition >= source.length ? source.length - 1 :
122 // int relativeEnd = 0;
123 // label : for (relativeStart = 0;; relativeStart++) {
126 // if ((source[begin - 1] == '\n') || (source[begin - 1] == '\r'))
130 // label : for (relativeEnd = 0;; relativeEnd++) {
131 // if ((end + 1) >= source.length)
133 // if ((source[end + 1] == '\r') || (source[end + 1] == '\n')) {
138 // //extract the message form the source
139 // char[] extract = new char[end - begin + 1];
140 // System.arraycopy(source, begin, extract, 0, extract.length);
142 // //remove all SPACE and TAB that begin the error message...
143 // int trimLeftIndex = 0;
144 // while (((c = extract[trimLeftIndex++]) == TAB) || (c == SPACE)) {
148 // trimLeftIndex - 1,
149 // extract = new char[extract.length - trimLeftIndex + 1],
152 // relativeStart -= trimLeftIndex;
153 // //buffer spaces and tabs in order to reach the error position
155 // char[] underneath = new char[extract.length]; // can't be bigger
156 // for (int i = 0; i <= relativeStart; i++) {
157 // if (extract[i] == TAB) {
158 // underneath[pos++] = TAB;
160 // underneath[pos++] = SPACE;
163 // //mark the error position
164 // for (int i = startPosition;
165 // i <= (endPosition >= source.length ? source.length - 1 : endPosition);
167 // underneath[pos++] = MARK;
168 // //resize underneathto remove 'null' chars
169 // System.arraycopy(underneath, 0, underneath = new char[pos], 0, pos);
171 // return " " + ProjectPrefUtil.bind("problem.atLine", String.valueOf(line))
172 // //$NON-NLS-2$ //$NON-NLS-1$
173 // + "\n\t" + new String(extract) + "\n\t" + new String(underneath);
174 // //$NON-NLS-2$ //$NON-NLS-1$
177 * Answer back the original arguments recorded into the problem.
179 * @return java.lang.String[]
181 public String[] getArguments() {
186 * Answer the type of problem.
188 * @see net.sourceforge.phpdt.core.compiler.IProblem#getID()
196 * Answer a localized, human-readable message string which describes the
199 * @return java.lang.String
201 public String getMessage() {
206 * Answer the file name in which the problem was found.
210 public char[] getOriginatingFileName() {
215 * Answer the end position of the problem (inclusive), or -1 if unknown.
219 public int getSourceEnd() {
224 * Answer the line number in source where the problem begins.
228 public int getSourceLineNumber() {
233 * Answer the start position of the problem (inclusive), or -1 if unknown.
237 public int getSourceStart() {
238 return startPosition;
242 * Helper method: checks the severity to see if the Error bit is set.
245 public boolean isError() {
246 return (severity & ProblemSeverities.Error) != 0;
250 * Helper method: checks the severity to see if the Error bit is not set.
253 public boolean isWarning() {
254 return (severity & ProblemSeverities.Error) == 0;
258 * Set the end position of the problem (inclusive), or -1 if unknown.
260 * Used for shifting problem positions.
263 * the new value of the sourceEnd of the receiver
265 public void setSourceEnd(int sourceEnd) {
266 endPosition = sourceEnd;
270 * Set the line number in source where the problem begins.
273 * the new value of the line number of the receiver
275 public void setSourceLineNumber(int lineNumber) {
280 * Set the start position of the problem (inclusive), or -1 if unknown.
282 * Used for shifting problem positions.
285 * the new value of the source start position of the receiver
287 public void setSourceStart(int sourceStart) {
288 startPosition = sourceStart;
291 public String toString() {
292 String s = "Pb(" + (id & IgnoreCategoriesMask) + ") "; //$NON-NLS-1$ //$NON-NLS-2$
293 if (message != null) {
296 if (arguments != null)
297 for (int i = 0; i < arguments.length; i++)
298 s += " " + arguments[i]; //$NON-NLS-1$