KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > env > MessagerImpl


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * tyeung@bea.com - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.apt.core.internal.env;
13
14 import com.sun.mirror.apt.Messager;
15 import com.sun.mirror.util.SourcePosition;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.jdt.apt.core.internal.util.SourcePositionImpl;
19 import org.eclipse.jdt.apt.core.util.EclipseMessager;
20 import org.eclipse.jdt.core.dom.ASTNode;
21 import org.eclipse.jdt.core.dom.CompilationUnit;
22
23
24 public class MessagerImpl implements Messager, EclipseMessager
25 {
26     public static enum Severity {
27         ERROR,
28         WARNING,
29         INFO
30     }
31     private final AbstractCompilationEnv _env;
32
33     MessagerImpl(AbstractCompilationEnv env){
34         _env = env;
35     }
36     
37     public void printError(SourcePosition pos, String JavaDoc msg, String JavaDoc[] arguments)
38     {
39         if( pos == null )
40             printError(msg);
41         else if( pos instanceof SourcePositionImpl )
42             print((SourcePositionImpl)pos, Severity.ERROR, msg, arguments);
43         else
44             print(pos, Severity.ERROR, msg, arguments);
45     }
46     
47     public void printError(ASTNode node, String JavaDoc msg)
48     {
49         if( node == null )
50             throw new IllegalArgumentException JavaDoc("'node' cannot be null"); //$NON-NLS-1$
51
final int start = node.getStartPosition();
52         // The only time you get a dom AST node is when you are processing in a per-file mode.
53
// _env.getAstCompilationUnit() && _env.getFile() will return an non-null value.
54
int line = _env.getAstCompilationUnit().getLineNumber(start);
55         if( line < 1 )
56             line = 1;
57         _env.addMessage(_env.getFile(), start, node.getLength() + start, Severity.ERROR, msg, line, null );
58     }
59
60     public void printError(String JavaDoc msg)
61     {
62         print(Severity.ERROR, msg, null);
63     }
64
65     public void printNotice(SourcePosition pos, String JavaDoc msg, String JavaDoc[] arguments)
66     {
67         if( pos instanceof SourcePositionImpl )
68             print((SourcePositionImpl)pos, Severity.INFO, msg, arguments);
69         else if (pos == null )
70             printNotice(msg);
71         else
72             print(pos, Severity.INFO, msg, arguments);
73     }
74     
75     public void printNotice(ASTNode node, String JavaDoc msg)
76     {
77         if( node == null )
78             throw new IllegalArgumentException JavaDoc("'node' cannot be null"); //$NON-NLS-1$
79
final int start = node.getStartPosition();
80         // The only time you get a dom AST node is when you are processing in a per-file mode.
81
// _env.getAstCompilationUnit() && _env.getFile() will return an non-null value.
82
int line = _env.getAstCompilationUnit().getLineNumber(start);
83         if( line < 1 )
84             line = 1;
85         _env.addMessage(_env.getFile(), start, node.getLength() + start, Severity.INFO, msg, line, null );
86     }
87
88     public void printNotice(String JavaDoc msg)
89     {
90        print(Severity.INFO, msg, null);
91     }
92
93     public void printWarning(SourcePosition pos, String JavaDoc msg, String JavaDoc[] arguments)
94     {
95         if( pos instanceof SourcePositionImpl )
96             print((SourcePositionImpl)pos, Severity.WARNING, msg, arguments);
97         else if (pos == null )
98             printWarning(msg);
99         else
100             print(pos, Severity.WARNING, msg, arguments);
101     }
102     
103     public void printWarning(ASTNode node, String JavaDoc msg)
104     {
105         if( node == null )
106             throw new IllegalArgumentException JavaDoc("'node' cannot be null"); //$NON-NLS-1$
107
final int start = node.getStartPosition();
108         // The only time you get a dom AST node is when you are processing in a per-file mode.
109
// _env.getAstCompilationUnit() && _env.getFile() will return an non-null value.
110
int line = _env.getAstCompilationUnit().getLineNumber(start);
111         if( line < 1 )
112             line = 1;
113         _env.addMessage(_env.getFile(), start, node.getLength() + start, Severity.WARNING, msg, line, null);
114     }
115
116     public void printWarning(String JavaDoc msg)
117     {
118         print(Severity.WARNING, msg, null);
119     }
120     
121     public void printError(SourcePosition pos, String JavaDoc msg) {
122         printError(pos, msg, null);
123     }
124
125     public void printWarning(SourcePosition pos, String JavaDoc msg) {
126         printWarning(pos, msg, null);
127     }
128
129     public void printNotice(SourcePosition pos, String JavaDoc msg) {
130         printNotice(pos, msg, null);
131     }
132     
133     public void printFixableError(SourcePosition pos, String JavaDoc msg, String JavaDoc pluginId, String JavaDoc errorId) {
134         if (pluginId == null) {
135             throw new IllegalArgumentException JavaDoc("pluginId cannot be null"); //$NON-NLS-1$
136
}
137         if (errorId == null) {
138             throw new IllegalArgumentException JavaDoc("errorId cannot be null"); //$NON-NLS-1$
139
}
140         printError(pos, msg, new String JavaDoc[] {pluginId, errorId});
141     }
142     
143     public void printFixableWarning(SourcePosition pos, String JavaDoc msg, String JavaDoc pluginId, String JavaDoc errorId) {
144         if (pluginId == null) {
145             throw new IllegalArgumentException JavaDoc("pluginId cannot be null"); //$NON-NLS-1$
146
}
147         if (errorId == null) {
148             throw new IllegalArgumentException JavaDoc("errorId cannot be null"); //$NON-NLS-1$
149
}
150         printWarning(pos, msg, new String JavaDoc[] {pluginId, errorId});
151     }
152     
153     public void printFixableNotice(SourcePosition pos, String JavaDoc msg, String JavaDoc pluginId, String JavaDoc errorId) {
154         if (pluginId == null) {
155             throw new IllegalArgumentException JavaDoc("pluginId cannot be null"); //$NON-NLS-1$
156
}
157         if (errorId == null) {
158             throw new IllegalArgumentException JavaDoc("errorId cannot be null"); //$NON-NLS-1$
159
}
160         printNotice(pos, msg, new String JavaDoc[] {pluginId, errorId});
161     }
162     
163     public void printFixableError(String JavaDoc msg, String JavaDoc pluginId, String JavaDoc errorId) {
164         if (pluginId == null) {
165             throw new IllegalArgumentException JavaDoc("pluginId cannot be null"); //$NON-NLS-1$
166
}
167         if (errorId == null) {
168             throw new IllegalArgumentException JavaDoc("errorId cannot be null"); //$NON-NLS-1$
169
}
170         print(Severity.ERROR, msg, new String JavaDoc[] {pluginId, errorId});
171     }
172     
173     public void printFixableWarning(String JavaDoc msg, String JavaDoc pluginId, String JavaDoc errorId) {
174         if (pluginId == null) {
175             throw new IllegalArgumentException JavaDoc("pluginId cannot be null"); //$NON-NLS-1$
176
}
177         if (errorId == null) {
178             throw new IllegalArgumentException JavaDoc("errorId cannot be null"); //$NON-NLS-1$
179
}
180         print(Severity.WARNING, msg, new String JavaDoc[] {pluginId, errorId});
181     }
182     
183     public void printFixableNotice(String JavaDoc msg, String JavaDoc pluginId, String JavaDoc errorId) {
184         if (pluginId == null) {
185             throw new IllegalArgumentException JavaDoc("pluginId cannot be null"); //$NON-NLS-1$
186
}
187         if (errorId == null) {
188             throw new IllegalArgumentException JavaDoc("errorId cannot be null"); //$NON-NLS-1$
189
}
190         print(Severity.INFO, msg, new String JavaDoc[] {pluginId, errorId});
191     }
192   
193     private void print(SourcePositionImpl pos,
194                        Severity severity,
195                        String JavaDoc msg,
196                        String JavaDoc[] arguments)
197     {
198         final IFile resource = pos.getResource();
199         if( resource == null ){
200             throw new IllegalStateException JavaDoc("missing resource"); //$NON-NLS-1$
201
}
202         else{
203           _env.addMessage(resource, pos.getStartingOffset(), pos.getEndingOffset(),
204                           severity, msg, pos.line(), arguments);
205         }
206     }
207     
208     private void print(SourcePosition pos,
209                        Severity severity,
210                        String JavaDoc msg,
211                        String JavaDoc[] arguments)
212     {
213         final java.io.File JavaDoc file = pos.file();
214         IFile resource = null;
215         if( file != null ){
216             final String JavaDoc projAbsPath = _env.getProject().getLocation().toOSString();
217             final String JavaDoc fileAbsPath = file.getAbsolutePath();
218             final String JavaDoc fileRelPath = fileAbsPath.substring(projAbsPath.length());
219             resource = _env.getProject().getFile(fileRelPath);
220             if( !resource.exists() )
221                 resource = null;
222         }
223          
224         int offset = -1;
225         if( resource != null ){
226             final CompilationUnit unit = _env.getASTFrom(resource);
227             if( unit != null )
228                 offset = unit.getPosition( pos.line(), pos.column() );
229         }
230         _env.addMessage(resource, offset, -1, severity, msg, pos.line(), arguments );
231     }
232
233     private void print(Severity severity, String JavaDoc msg, String JavaDoc[] arguments)
234     {
235         _env.addMessage(null, -1, -1, severity, msg, 1, arguments );
236         
237     }
238   
239 }
240
Popular Tags