1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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 org.eclipse.jdt.internal.compiler.problem;
13 import org.eclipse.jdt.core.compiler.IProblem;
14 import org.eclipse.jdt.internal.compiler.util.Util;
16 public class DefaultProblem implements ProblemSeverities, IProblem {
18 private static final String LINE_DELIMITER = System.getProperty("line.separator"); //$NON-NLS-1$
20 private char[] fileName;
22 private int startPosition, endPosition, line;
24 private String[] arguments;
25 private String message;
27 public DefaultProblem(
28 char[] originatingFileName,
31 String[] stringArguments,
37 this.fileName = originatingFileName;
38 this.message = message;
40 this.arguments = stringArguments;
41 this.severity = severity;
42 this.startPosition = startPosition;
43 this.endPosition = endPosition;
46 public String errorReportSource(char[] unitSource) {
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 ((this.startPosition > this.endPosition)
55 || ((this.startPosition < 0) && (this.endPosition < 0)))
56 return Util.bind("problem.noSourceInformation"); //$NON-NLS-1$
58 StringBuffer errorBuffer = new StringBuffer(" "); //$NON-NLS-1$
59 errorBuffer.append(Util.bind("problem.atLine", String.valueOf(this.line))); //$NON-NLS-1$
60 errorBuffer.append(LINE_DELIMITER).append("\t"); //$NON-NLS-1$
63 final char SPACE = '\u0020';
64 final char MARK = '^';
65 final char TAB = '\t';
66 //the next code tries to underline the token.....
67 //it assumes (for a good display) that token source does not
68 //contain any \r \n. This is false on statements !
69 //(the code still works but the display is not optimal !)
71 // expand to line limits
72 int length = unitSource.length, begin, end;
73 for (begin = this.startPosition >= length ? length - 1 : this.startPosition; begin > 0; begin--) {
74 if ((c = unitSource[begin - 1]) == '\n' || c == '\r') break;
76 for (end = this.endPosition >= length ? length - 1 : this.endPosition ; end+1 < length; end++) {
77 if ((c = unitSource[end + 1]) == '\r' || c == '\n') break;
80 // trim left and right spaces/tabs
81 while ((c = unitSource[begin]) == ' ' || c == '\t') begin++;
82 //while ((c = unitSource[end]) == ' ' || c == '\t') end--; TODO (philippe) should also trim right, but all tests are to be updated
85 errorBuffer.append(unitSource, begin, end-begin+1);
86 errorBuffer.append(LINE_DELIMITER).append("\t"); //$NON-NLS-1$
89 for (int i = begin; i <this.startPosition; i++) {
90 errorBuffer.append((unitSource[i] == TAB) ? TAB : SPACE);
92 for (int i = this.startPosition; i <= (this.endPosition >= length ? length - 1 : this.endPosition); i++) {
93 errorBuffer.append(MARK);
95 return errorBuffer.toString();
99 * Answer back the original arguments recorded into the problem.
100 * @return java.lang.String[]
102 public String[] getArguments() {
104 return this.arguments;
108 * Answer the type of problem.
109 * @see org.eclipse.jdt.core.compiler.IProblem#getID()
118 * Answer a localized, human-readable message string which describes the problem.
119 * @return java.lang.String
121 public String getMessage() {
127 * Answer the file name in which the problem was found.
130 public char[] getOriginatingFileName() {
132 return this.fileName;
136 * Answer the end position of the problem (inclusive), or -1 if unknown.
139 public int getSourceEnd() {
141 return this.endPosition;
145 * Answer the line number in source where the problem begins.
148 public int getSourceLineNumber() {
154 * Answer the start position of the problem (inclusive), or -1 if unknown.
157 public int getSourceStart() {
159 return this.startPosition;
163 * Helper method: checks the severity to see if the Error bit is set.
166 public boolean isError() {
168 return (this.severity & ProblemSeverities.Error) != 0;
172 * Helper method: checks the severity to see if the Error bit is not set.
175 public boolean isWarning() {
177 return (this.severity & ProblemSeverities.Error) == 0;
180 public void setOriginatingFileName(char[] fileName) {
181 this.fileName = fileName;
185 * Set the end position of the problem (inclusive), or -1 if unknown.
187 * Used for shifting problem positions.
188 * @param sourceEnd the new value of the sourceEnd of the receiver
190 public void setSourceEnd(int sourceEnd) {
192 this.endPosition = sourceEnd;
196 * Set the line number in source where the problem begins.
197 * @param lineNumber the new value of the line number of the receiver
199 public void setSourceLineNumber(int lineNumber) {
201 this.line = lineNumber;
205 * Set the start position of the problem (inclusive), or -1 if unknown.
207 * Used for shifting problem positions.
208 * @param sourceStart the new value of the source start position of the receiver
210 public void setSourceStart(int sourceStart) {
212 this.startPosition = sourceStart;
215 public String toString() {
217 String s = "Pb(" + (this.id & IgnoreCategoriesMask) + ") "; //$NON-NLS-1$ //$NON-NLS-2$
218 if (this.message != null) {
221 if (this.arguments != null)
222 for (int i = 0; i < this.arguments.length; i++)
223 s += " " + this.arguments[i]; //$NON-NLS-1$