summaryrefslogtreecommitdiff
path: root/core/source/org/openoffice/ide/eclipse/core/builders/IdlcErrorReader.java
blob: 707d2c15e1190b784eb258313884c92e10680b0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*************************************************************************
 *
 * $RCSfile: IdlcErrorReader.java,v $
 *
 * $Revision: 1.7 $
 *
 * last change: $Author: cedricbosdo $ $Date: 2007/11/25 20:32:27 $
 *
 * The Contents of this file are made available subject to the terms of
 * the GNU Lesser General Public License Version 2.1
 *
 * Sun Microsystems Inc., October, 2000
 *
 *
 * GNU Lesser General Public License Version 2.1
 * =============================================
 * Copyright 2000 by Sun Microsystems, Inc.
 * 901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 * 
 * The Initial Developer of the Original Code is: Sun Microsystems, Inc..
 *
 * Copyright: 2002 by Sun Microsystems, Inc.
 *
 * All Rights Reserved.
 *
 * Contributor(s): Cedric Bosdonnat
 *
 *
 ************************************************************************/
package org.openoffice.ide.eclipse.core.builders;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.openoffice.ide.eclipse.core.PluginLogger;

/**
 * Class reading the idlc error output to transform the errors into markers.
 * 
 * @author cedricbosdo
 *
 */
public class IdlcErrorReader {
    
    /**
     * Include error regular expression.
     * 
     * <p><em>cpp: &lt;file&gt;:&lt;line number&gt; some text Could not find 
     * include file &lt;missing include&gt;</em></p>
     */
    private static final String R_IDLCPP_ERROR = "cpp: (\\S+):([0-9]+)(.*:[0-9]+)? (.*)"; //$NON-NLS-1$
    
    /**
     * Syntax error expression.
     *  
     * <p><em>&lt;file&gt; (&lt;line number&gt;) : &lt;message&gt;</em></p>
     */
    private static final String R_IDLC_ERROR  = "(.*)\\(([0-9]+)\\) : (WARNING, )?(.*)"; //$NON-NLS-1$

    private static final int IDLC_ERROR_MESSAGE_GROUP = 3;

    private static final int IDLCPP_INCLUDE_PATH_GROUP = 4;

    private static final int IDLCPP_OPTIONAL_GROUP = 3;
    
    /**
     * Stream from which the reader extract the errors.
     */
    private LineNumberReader mReader;
    private InputStreamReader mIn;
    
    /**
     * File which compilation has been asked.
     */
    private IFile mCompiledFile;
    
    /**
     * Constructor.
     * 
     * @param pStream the error stream to read
     * @param pFile the built IDL file
     */
    public IdlcErrorReader (InputStream pStream, IFile pFile) {
        mIn = new InputStreamReader(pStream);
        mReader = new LineNumberReader(mIn);
        mCompiledFile = pFile;
    }
    
    /**
     * Computes the error into IDL markers.
     */
    public void readErrors() {
        
        try {
            // Cleans the idlc error previously added
            mCompiledFile.deleteMarkers(
                    IMarker.PROBLEM, true, 
                    IResource.DEPTH_INFINITE);
            
            // Read each line until the stream end (null line)
            String line = mReader.readLine();
            
            // Mark only the file errors. The changed files will be compiled too
            
            while (null != line) {
                
                // Handle the error line
                IMarker marker = analyseIdlcppError(line);
                
                if (null == marker) {
                    marker = analyseIdlcError(line);
                } 
                
                if (null == marker) {
                    PluginLogger.debug("Error line: " + line); //$NON-NLS-1$
                    
                } else {
                    
                    // Keep only the markers for the errors concerning the 
                    // file which is compiled
                    if (IResource.FILE == marker.getResource().getType() && 
                            !marker.getResource().getProjectRelativePath().
                            toString().equals(
                                    mCompiledFile.getProjectRelativePath().
                                    toString())) {
                        marker.delete();
                    }
                }
                
                line = mReader.readLine();
            }
        } catch (IOException e) {
            PluginLogger.error(
                Messages.getString("IdlcErrorReader.ErrorReadingError"), e); //$NON-NLS-1$
        } catch (CoreException e) {
            PluginLogger.error(
                    Messages.getString("IdlcErrorReader.MarkerCreationError") //$NON-NLS-1$
                        + mCompiledFile.getProjectRelativePath().toString(), e);
        } finally {
            try { 
                mReader.close(); 
                mIn.close();
            } catch (IOException e) {
            }
        }
    }
    
    /**
     * <p>Method that analyzes the error line and return the appropriate 
     * marker if it is possible.</p>
     * 
     * @param pLine error line to analyse 
     * @return the corresponding marker if the line is an <code>idlc</code> error line. 
     *             <code>null</code> if there were problems by creating the 
     *             marker or if the line isn't an <code>idlc</code> error line.
     */
    private IMarker analyseIdlcError(String pLine) {
        IMarker marker = null;
        
        Pattern pSyntax = Pattern.compile(R_IDLC_ERROR); 
        Matcher mSyntax = pSyntax.matcher(pLine);
        
        if (!pLine.startsWith("idlc:") && mSyntax.matches()) { //$NON-NLS-1$
            IProject project = mCompiledFile.getProject();
            
            boolean error = false;
            if (null == mSyntax.group(IDLC_ERROR_MESSAGE_GROUP)) {
                error = true;
            }
            
            // HELP the groups are indexed from 1. 0 is the whole string
            String filePath = mSyntax.group(1);
            int lineNo = Integer.parseInt(mSyntax.group(2));
            String message = mSyntax.group(mSyntax.groupCount());
            
            // Get a handle on the bad file
            // Create a project relative path
            if (filePath.startsWith(project.getLocation().toOSString())) {
                int pos = project.getLocation().toOSString().length();
                filePath = filePath.substring(pos);
            }
            
            IFile file = project.getFile(filePath);
            try {
                int severity  = IMarker.SEVERITY_WARNING;
                if (error) {
                    severity = IMarker.SEVERITY_ERROR; 
                }
                
                marker = file.createMarker(IMarker.PROBLEM);
                marker.setAttribute(IMarker.SEVERITY, 
                            severity);
                marker.setAttribute(IMarker.MESSAGE, message);
                marker.setAttribute(IMarker.LINE_NUMBER, lineNo);
                marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_NORMAL);
                
                // HELP To print an icon in the editor vertical and overview rulers
                // Set the CHAR_START and CHAR_END attributes. They are relative to the
                // beginning or the end of the document.
                
                // HELP Without specifying the CHAR END and START, the line is well placed
                // But afterwards, pay attention when the marker should be located under 
                // the bad words
                
                // Try to find the line or word that causes the error
//                Map positions = getWrongWord(lineNo, message);
//                
//                marker.setAttribute(IMarker.CHAR_START, ((Integer)positions.get(IMarker.CHAR_START)).intValue());
//                marker.setAttribute(IMarker.CHAR_END,  ((Integer)positions.get(IMarker.CHAR_END)).intValue());
                
            } catch (CoreException e) {
                PluginLogger.error(
                        Messages.getString("IdlcErrorReader.MarkerCreationError") //$NON-NLS-1$
                            + file.getProjectRelativePath().toString(), e);
            }
        }
        
        return marker;
    }
    
    /**
     * <p>Method that analyzes the IDLC preprocessor error line and return the appropriate 
     * marker if it is possible.</p>
     * 
     * @param pLine error line to analyse 
     * @return the corresponding marker if the line is an <code>idlc</code> error line. 
     *             <code>null</code> if there were problems by creating the 
     *             marker or if the line isn't an <code>idlc</code> error line.
     */
    private IMarker analyseIdlcppError(String pLine) {
        IMarker marker = null;
        
        Pattern pInclude = Pattern.compile(R_IDLCPP_ERROR);
        Matcher mInclude = pInclude.matcher(pLine);
        
        if (mInclude.matches()) {
            IProject project = mCompiledFile.getProject();

            String errorFilePath = mInclude.group(1);
            int lineNo = Integer.parseInt(mInclude.group(2));
            String badIncludePath = mInclude.group(IDLCPP_INCLUDE_PATH_GROUP);
            
            if (null == mInclude.group(IDLCPP_OPTIONAL_GROUP)) {
            
                IFile errorFile;
                
                if (errorFilePath.startsWith(".")) { //$NON-NLS-1$
                    // A project local file, that means that the error is in a dependent file
                    errorFile = project.getFile(errorFilePath);
                    
                } else {
                    // The error is in the file which was asked for compilation
                    errorFile = mCompiledFile;    
                }
                
                String message = "idlcpp error: " + badIncludePath; //$NON-NLS-1$
                
                try {
                    marker = errorFile.createMarker(IMarker.PROBLEM);
                    marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
                    marker.setAttribute(IMarker.MESSAGE, message);
                    marker.setAttribute(IMarker.LINE_NUMBER, lineNo);
                    marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
                    
                    // Try to find the line or Word that causes the error
                    Map<String, Integer> positions = getWrongWord(lineNo, message);
                    
                    marker.setAttribute(IMarker.CHAR_START,
                            positions.get(IMarker.CHAR_START).intValue());
                    marker.setAttribute(IMarker.CHAR_END,  
                            positions.get(IMarker.CHAR_END).intValue());
                    
                } catch (CoreException e) {
                    // Nothing to do. Do not create noise in the logs
                }    
            }
        }
        
        return marker;
    }
    
    /**
     * Computes the offset of the first and last characters of the word
     * to underline.
     * 
     * @param pLine the number of the line where the error is located
     * @param pMessage the error message
     * 
     * @return a map containing the offset of the first and last characters of the
     *          word causing the error. 
     */
    private Map<String, Integer> getWrongWord(int pLine, String pMessage) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        int start = 0;
        int end = 0;
        
        try {
            
            // Get the line offset.
            LineNumberReader fileReader = new LineNumberReader(
                    new InputStreamReader(mCompiledFile.getContents()));
            int offset = 0;
            
            for (int i = 0, length = pLine; i < length; i++) {
                String tmpLine = fileReader.readLine();
                offset += tmpLine.length();
            }
            
            // TODO Get the character offset in the line
            // The last character should be found using the next blank.
            
            start = offset;
            end = offset;
        } catch (Exception e) {
            // Nothing to report: the marker will be bad placed perhaps...
        }
        
        // Create the map content
        map.put(IMarker.CHAR_START, new Integer(start));
        map.put(IMarker.CHAR_END, new Integer(end));
        
        return map;
    }
    
}