Added mapped path field
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / problem / ProblemHandler.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.problem;
12
13 import net.sourceforge.phpdt.core.compiler.IProblem;
14 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
15 import net.sourceforge.phpdt.internal.compiler.IErrorHandlingPolicy;
16 import net.sourceforge.phpdt.internal.compiler.IProblemFactory;
17 import net.sourceforge.phpdt.internal.compiler.impl.ReferenceContext;
18
19
20 /*
21  * Compiler error handler, responsible to determine whether
22  * a problem is actually a warning or an error; also will
23  * decide whether the compilation task can be processed further or not.
24  *
25  * Behavior : will request its current policy if need to stop on
26  *      first error, and if should proceed (persist) with problems.
27  */
28
29 public class ProblemHandler implements ProblemSeverities {
30
31         public final static String[] NoArgument = new String[0];
32         
33         final public IErrorHandlingPolicy policy;
34         public final IProblemFactory problemFactory;
35 //      public final CompilerOptions options;
36 /*
37  * Problem handler can be supplied with a policy to specify
38  * its behavior in error handling. Also see static methods for
39  * built-in policies.
40  *
41  */
42 public ProblemHandler(IErrorHandlingPolicy policy, IProblemFactory problemFactory) {
43 //CompilerOptions options, IProblemFactory problemFactory) {
44         this.policy = policy;
45         this.problemFactory = problemFactory;
46 //      this.options = options;
47 }
48 /*
49  * Given the current configuration, answers which category the problem
50  * falls into:
51  *              Error | Warning | Ignore
52  */
53 public int computeSeverity(int problemId){
54         
55         return Error; // by default all problems are errors
56 }
57 public IProblem createProblem(
58         char[] fileName, 
59         int problemId, 
60         String[] problemArguments, 
61         String[] messageArguments,
62         int severity, 
63         int problemStartPosition, 
64         int problemEndPosition, 
65         int lineNumber,
66         ReferenceContext referenceContext,
67         CompilationResult unitResult) {
68
69         return problemFactory.createProblem(
70                 fileName, 
71                 problemId, 
72                 problemArguments, 
73                 messageArguments,
74                 severity, 
75                 problemStartPosition, 
76                 problemEndPosition, 
77                 lineNumber); 
78 }
79 public void handle(
80         int problemId, 
81         String[] problemArguments, 
82         String[] messageArguments,
83         int severity, 
84         int problemStartPosition, 
85         int problemEndPosition, 
86         ReferenceContext referenceContext, 
87         CompilationResult unitResult) {
88
89         if (severity == Ignore)
90                 return;
91
92         // if no reference context, we need to abort from the current compilation process
93         if (referenceContext == null) {
94                 if ((severity & Error) != 0) { // non reportable error is fatal
95                         throw new AbortCompilation(problemId, problemArguments, messageArguments);
96                 } else {
97                         return; // ignore non reportable warning
98                 }
99         }
100
101         IProblem problem = 
102                 this.createProblem(
103                         unitResult.getFileName(), 
104                         problemId, 
105                         problemArguments, 
106                         messageArguments,
107                         severity, 
108                         problemStartPosition, 
109                         problemEndPosition, 
110                         problemStartPosition >= 0
111                                 ? searchLineNumber(unitResult.lineSeparatorPositions, problemStartPosition)
112                                 : 0,
113                         referenceContext,
114                         unitResult); 
115         if (problem == null) return; // problem couldn't be created, ignore
116         
117         switch (severity & Error) {
118                 case Error :
119                         this.record(problem, unitResult, referenceContext);
120                         referenceContext.tagAsHavingErrors();
121
122                         // should abort ?
123                         int abortLevel;
124                         if ((abortLevel = 
125                                 (policy.stopOnFirstError() ? AbortCompilation : severity & Abort)) != 0) {
126
127                                 referenceContext.abort(abortLevel);
128                         }
129                         break;
130                 case Warning :
131                         this.record(problem, unitResult, referenceContext);
132                         break;
133         }
134 }
135 /**
136  * Standard problem handling API, the actual severity (warning/error/ignore) is deducted
137  * from the problem ID and the current compiler options.
138  */
139 public void handle(
140         int problemId, 
141         String[] problemArguments, 
142         String[] messageArguments,
143         int problemStartPosition, 
144         int problemEndPosition, 
145         ReferenceContext referenceContext, 
146         CompilationResult unitResult) {
147
148         this.handle(
149                 problemId,
150                 problemArguments,
151                 messageArguments,
152                 this.computeSeverity(problemId), // severity inferred using the ID
153                 problemStartPosition,
154                 problemEndPosition,
155                 referenceContext,
156                 unitResult);
157 }
158 public void record(IProblem problem, CompilationResult unitResult, ReferenceContext referenceContext) {
159         unitResult.record(problem, referenceContext);
160 }
161 /**
162  * Search the line number corresponding to a specific position
163  *
164  * @param methodBinding org.eclipse.jdt.internal.compiler.nameloopkup.SyntheticAccessMethodBinding
165  */
166 public static final int searchLineNumber(int[] startLineIndexes, int position) {
167         if (startLineIndexes == null)
168                 return 1;
169         int length = startLineIndexes.length;
170         if (length == 0)
171                 return 1;
172         int g = 0, d = length - 1;
173         int m = 0;
174         while (g <= d) {
175                 m = (g + d) /2;
176                 if (position < startLineIndexes[m]) {
177                         d = m-1;
178                 } else if (position > startLineIndexes[m]) {
179                         g = m+1;
180                 } else {
181                         return m + 1;
182                 }
183         }
184         if (position < startLineIndexes[m]) {
185                 return m+1;
186         }
187         return m+2;
188 }
189 }