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 import org.eclipse.ui.texteditor.ITextEditor;
 
  19 public class DefaultProblem implements ProblemSeverities, IProblem {
 
  21         private char[] fileName;
 
  23         private int startPosition, endPosition, line;
 
  25         private String[] arguments;
 
  26         private String message;
 
  28         public DefaultProblem(
 
  29                 char[] originatingFileName,
 
  32                 String[] stringArguments,
 
  38                 this.fileName = originatingFileName;
 
  39                 this.message = message;
 
  41                 this.arguments = stringArguments;
 
  42                 this.severity = severity;
 
  43                 this.startPosition = startPosition;
 
  44                 this.endPosition = endPosition;
 
  48         public String errorReportSource(ITextEditor textEditor){ //ICompilationUnit compilationUnit) {
 
  49                 //extra from the source the innacurate     token
 
  50                 //and "highlight" it using some underneath ^^^^^
 
  51                 //put some context around too.
 
  53                 //this code assumes that the font used in the console is fixed size
 
  56                 if ((startPosition > endPosition)
 
  57                         || ((startPosition <= 0) && (endPosition <= 0)))
 
  58                         return Util.bind("problem.noSourceInformation"); //$NON-NLS-1$
 
  60                 final char SPACE = '\u0020';
 
  61                 final char MARK = '^';
 
  62                 final char TAB = '\t';
 
  63 //              char[] source = compilationUnit.getContents();
 
  64                 char[] source = textEditor.getDocumentProvider().getDocument(null).get().toCharArray();
 
  65                 //the next code tries to underline the token.....
 
  66                 //it assumes (for a good display) that token source does not
 
  67                 //contain any \r \n. This is false on statements ! 
 
  68                 //(the code still works but the display is not optimal !)
 
  70                 //compute the how-much-char we are displaying around the inaccurate token
 
  71                 int begin = startPosition >= source.length ? source.length - 1 : startPosition;
 
  72                 int relativeStart = 0;
 
  73                 int end = endPosition >= source.length ? source.length - 1 : endPosition;
 
  75                 label : for (relativeStart = 0;; relativeStart++) {
 
  78                         if ((source[begin - 1] == '\n') || (source[begin - 1] == '\r'))
 
  82                 label : for (relativeEnd = 0;; relativeEnd++) {
 
  83                         if ((end + 1) >= source.length)
 
  85                         if ((source[end + 1] == '\r') || (source[end + 1] == '\n')) {
 
  90                 //extract the message form the source
 
  91                 char[] extract = new char[end - begin + 1];
 
  92                 System.arraycopy(source, begin, extract, 0, extract.length);
 
  94                 //remove all SPACE and TAB that begin the error message...
 
  95                 int trimLeftIndex = 0;
 
  96                 while (((c = extract[trimLeftIndex++]) == TAB) || (c == SPACE)) {
 
 101                         extract = new char[extract.length - trimLeftIndex + 1],
 
 104                 relativeStart -= trimLeftIndex;
 
 105                 //buffer spaces and tabs in order to reach the error position
 
 107                 char[] underneath = new char[extract.length]; // can't be bigger
 
 108                 for (int i = 0; i <= relativeStart; i++) {
 
 109                         if (extract[i] == TAB) {
 
 110                                 underneath[pos++] = TAB;
 
 112                                 underneath[pos++] = SPACE;
 
 115                 //mark the error position
 
 116                 for (int i = startPosition;
 
 117                         i <= (endPosition >= source.length ? source.length - 1 : endPosition);
 
 119                         underneath[pos++] = MARK;
 
 120                 //resize underneathto remove 'null' chars
 
 121                 System.arraycopy(underneath, 0, underneath = new char[pos], 0, pos);
 
 123                 return " " + Util.bind("problem.atLine", String.valueOf(line))  //$NON-NLS-2$ //$NON-NLS-1$
 
 124                         + "\n\t" + new String(extract) + "\n\t" + new String(underneath); //$NON-NLS-2$ //$NON-NLS-1$
 
 128          * Answer back the original arguments recorded into the problem.
 
 129          * @return java.lang.String[]
 
 131         public String[] getArguments() {
 
 137          * Answer the type of problem.
 
 138          * @see org.eclipse.jdt.core.compiler.IProblem#getID()
 
 147          * Answer a localized, human-readable message string which describes the problem.
 
 148          * @return java.lang.String
 
 150         public String getMessage() {
 
 156          * Answer the file name in which the problem was found.
 
 159         public char[] getOriginatingFileName() {
 
 165          * Answer the end position of the problem (inclusive), or -1 if unknown.
 
 168         public int getSourceEnd() {
 
 174          * Answer the line number in source where the problem begins.
 
 177         public int getSourceLineNumber() {
 
 183          * Answer the start position of the problem (inclusive), or -1 if unknown.
 
 186         public int getSourceStart() {
 
 188                 return startPosition;
 
 192          * Helper method: checks the severity to see if the Error bit is set.
 
 195         public boolean isError() {
 
 197                 return (severity & ProblemSeverities.Error) != 0;
 
 201          * Helper method: checks the severity to see if the Error bit is not set.
 
 204         public boolean isWarning() {
 
 206                 return (severity & ProblemSeverities.Error) == 0;
 
 210          * Set the end position of the problem (inclusive), or -1 if unknown.
 
 212          * Used for shifting problem positions.
 
 213          * @param sourceEnd the new value of the sourceEnd of the receiver
 
 215         public void setSourceEnd(int sourceEnd) {
 
 217                 endPosition = sourceEnd;
 
 221          * Set the line number in source where the problem begins.
 
 222          * @param lineNumber the new value of the line number of the receiver
 
 224         public void setSourceLineNumber(int lineNumber) {
 
 230          * Set the start position of the problem (inclusive), or -1 if unknown.
 
 232          * Used for shifting problem positions.
 
 233          * @param sourceStart the new value of the source start position of the receiver
 
 235         public void setSourceStart(int sourceStart) {
 
 237                 startPosition = sourceStart;
 
 240         public String toString() {
 
 242                 String s = "Pb(" + (id & IgnoreCategoriesMask) + ") "; //$NON-NLS-1$ //$NON-NLS-2$
 
 243                 if (message != null) {
 
 246                         if (arguments != null)
 
 247                                 for (int i = 0; i < arguments.length; i++)
 
 248                                         s += " " + arguments[i]; //$NON-NLS-1$