1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.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.env.ICompilationUnit;
15 import net.sourceforge.phpdt.internal.compiler.util.Util;
17 public class DefaultProblem implements ProblemSeverities, IProblem {
19 private char[] fileName;
21 private int startPosition, endPosition, line;
23 private String[] arguments;
24 private String message;
26 public DefaultProblem(
27 char[] originatingFileName,
30 String[] stringArguments,
36 this.fileName = originatingFileName;
37 this.message = message;
39 this.arguments = stringArguments;
40 this.severity = severity;
41 this.startPosition = startPosition;
42 this.endPosition = endPosition;
46 public String errorReportSource(ICompilationUnit compilationUnit) {
47 //extra from the source the innacurate token
48 //and "highlight" it using some underneath ^^^^^
49 //put some context around too.
51 //this code assumes that the font used in the console is fixed size
54 if ((startPosition > endPosition)
55 || ((startPosition <= 0) && (endPosition <= 0)))
56 return Util.bind("problem.noSourceInformation"); //$NON-NLS-1$
58 final char SPACE = '\u0020';
59 final char MARK = '^';
60 final char TAB = '\t';
61 char[] source = compilationUnit.getContents();
62 //the next code tries to underline the token.....
63 //it assumes (for a good display) that token source does not
64 //contain any \r \n. This is false on statements !
65 //(the code still works but the display is not optimal !)
67 //compute the how-much-char we are displaying around the inaccurate token
68 int begin = startPosition >= source.length ? source.length - 1 : startPosition;
69 int relativeStart = 0;
70 int end = endPosition >= source.length ? source.length - 1 : endPosition;
72 label : for (relativeStart = 0;; relativeStart++) {
75 if ((source[begin - 1] == '\n') || (source[begin - 1] == '\r'))
79 label : for (relativeEnd = 0;; relativeEnd++) {
80 if ((end + 1) >= source.length)
82 if ((source[end + 1] == '\r') || (source[end + 1] == '\n')) {
87 //extract the message form the source
88 char[] extract = new char[end - begin + 1];
89 System.arraycopy(source, begin, extract, 0, extract.length);
91 //remove all SPACE and TAB that begin the error message...
92 int trimLeftIndex = 0;
93 while (((c = extract[trimLeftIndex++]) == TAB) || (c == SPACE)) {
98 extract = new char[extract.length - trimLeftIndex + 1],
101 relativeStart -= trimLeftIndex;
102 //buffer spaces and tabs in order to reach the error position
104 char[] underneath = new char[extract.length]; // can't be bigger
105 for (int i = 0; i <= relativeStart; i++) {
106 if (extract[i] == TAB) {
107 underneath[pos++] = TAB;
109 underneath[pos++] = SPACE;
112 //mark the error position
113 for (int i = startPosition;
114 i <= (endPosition >= source.length ? source.length - 1 : endPosition);
116 underneath[pos++] = MARK;
117 //resize underneathto remove 'null' chars
118 System.arraycopy(underneath, 0, underneath = new char[pos], 0, pos);
120 return " " + Util.bind("problem.atLine", String.valueOf(line)) //$NON-NLS-2$ //$NON-NLS-1$
121 + "\n\t" + new String(extract) + "\n\t" + new String(underneath); //$NON-NLS-2$ //$NON-NLS-1$
125 * Answer back the original arguments recorded into the problem.
126 * @return java.lang.String[]
128 public String[] getArguments() {
134 * Answer the type of problem.
135 * @see org.eclipse.jdt.core.compiler.IProblem#getID()
144 * Answer a localized, human-readable message string which describes the problem.
145 * @return java.lang.String
147 public String getMessage() {
153 * Answer the file name in which the problem was found.
156 public char[] getOriginatingFileName() {
162 * Answer the end position of the problem (inclusive), or -1 if unknown.
165 public int getSourceEnd() {
171 * Answer the line number in source where the problem begins.
174 public int getSourceLineNumber() {
180 * Answer the start position of the problem (inclusive), or -1 if unknown.
183 public int getSourceStart() {
185 return startPosition;
189 * Helper method: checks the severity to see if the Error bit is set.
192 public boolean isError() {
194 return (severity & ProblemSeverities.Error) != 0;
198 * Helper method: checks the severity to see if the Error bit is not set.
201 public boolean isWarning() {
203 return (severity & ProblemSeverities.Error) == 0;
207 * Set the end position of the problem (inclusive), or -1 if unknown.
209 * Used for shifting problem positions.
210 * @param sourceEnd the new value of the sourceEnd of the receiver
212 public void setSourceEnd(int sourceEnd) {
214 endPosition = sourceEnd;
218 * Set the line number in source where the problem begins.
219 * @param lineNumber the new value of the line number of the receiver
221 public void setSourceLineNumber(int lineNumber) {
227 * Set the start position of the problem (inclusive), or -1 if unknown.
229 * Used for shifting problem positions.
230 * @param sourceStart the new value of the source start position of the receiver
232 public void setSourceStart(int sourceStart) {
234 startPosition = sourceStart;
237 public String toString() {
239 String s = "Pb(" + (id & IgnoreCategoriesMask) + ") "; //$NON-NLS-1$ //$NON-NLS-2$
240 if (message != null) {
243 if (arguments != null)
244 for (int i = 0; i < arguments.length; i++)
245 s += " " + arguments[i]; //$NON-NLS-1$