Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: feb9070503cec2907e589ab5c2d705cafede4041 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.internal.core.index.sourceindexer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.filetype.ICFileType;
import org.eclipse.cdt.core.filetype.ICFileTypeConstants;
import org.eclipse.cdt.core.model.ICModelMarker;
import org.eclipse.cdt.core.search.ICSearchConstants;
import org.eclipse.cdt.internal.core.Util;
import org.eclipse.cdt.internal.core.index.IIndexer;
import org.eclipse.cdt.internal.core.index.IIndexerOutput;
import org.eclipse.cdt.internal.core.search.indexing.IndexManager;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;

public abstract class AbstractIndexer implements IIndexer, ICSearchConstants {
	
	public static boolean VERBOSE = false;
	public static boolean TIMING = false;
	
	protected IIndexerOutput output;

	//Index Markers
	private int problemMarkersEnabled = 0;
	private Map problemsMap = null;
	protected static final String INDEXER_MARKER_PREFIX = Util.bind("indexerMarker.prefix" ) + " "; //$NON-NLS-1$ //$NON-NLS-2$
    protected static final String INDEXER_MARKER_ORIGINATOR =  ICModelMarker.INDEXER_MARKER + ".originator";  //$NON-NLS-1$
    private static final String INDEXER_MARKER_PROCESSING = Util.bind( "indexerMarker.processing" ); //$NON-NLS-1$
	protected IFile resourceFile;
	
	public AbstractIndexer() {
		super();
	}
	
	public static void verbose(String log) {
	  System.out.println("(" + Thread.currentThread() + ") " + log); //$NON-NLS-1$//$NON-NLS-2$
	}
	
	public IIndexerOutput getOutput() {
	    return output;
	}
	    
	  
	public Map getProblemsMap() {
		return problemsMap;
	}

	/**
	 * @see IIndexer#index(IFile document, IIndexerOutput output)
	 */
	public void index(IFile file, IIndexerOutput output) throws IOException {
		this.output = output;
		if (shouldIndex(this.getResourceFile())) indexFile(file);
	} 
	
	protected abstract void indexFile(IFile file) throws IOException;
	
	/**
	 * @param fileToBeIndexed
	 * @see IIndexer#shouldIndex(IFile file)
	 */
	public boolean shouldIndex(IFile fileToBeIndexed) {
		if (fileToBeIndexed != null){
			ICFileType type = CCorePlugin.getDefault().getFileType(fileToBeIndexed.getProject(),fileToBeIndexed.getName());
			if (type.isSource() || type.isHeader()){
			  String id = type.getId();
			  if (id.equals(ICFileTypeConstants.FT_C_SOURCE) ||
			  	  id.equals(ICFileTypeConstants.FT_CXX_SOURCE) ||
				  id.equals(ICFileTypeConstants.FT_C_HEADER) ||
				  id.equals(ICFileTypeConstants.FT_CXX_HEADER))
			  	return true;
			}
		}
		
		return false;
	}

    protected abstract class Problem {
        public IResource resource;
        public IResource originator;
        public Problem (IResource resource, IResource originator) {
            this.resource = resource;
            this.originator = originator;
        }
		/**
		 * Method to actually add/remove problem markers 
		 */
		abstract public void run();
    }

	protected class FileInfoMarker extends Problem {
		private String message;
		public FileInfoMarker(IResource resource, IResource originator, String message) {
			super(resource, originator);
			this.message = message;
		}

		public void run() {
	        try {
	            IMarker[] markers = resource.findMarkers(ICModelMarker.INDEXER_MARKER, true, IResource.DEPTH_ZERO);
	        
	            boolean newProblem = true;
	           
	            if (markers.length > 0) {
	                IMarker tempMarker = null;
	                String tempMsgString = null;

	                for (int i=0; i<markers.length; i++) {
	                    tempMarker = markers[i];
	                    tempMsgString = (String) tempMarker.getAttribute(IMarker.MESSAGE);
	                    if (tempMsgString.equalsIgnoreCase( message )) {
	                        newProblem = false;
	                        break;
	                    }
	                }
	            }
	  
	            if (newProblem){
	                IMarker marker = resource.createMarker(ICModelMarker.INDEXER_MARKER);
	                marker.setAttribute(IMarker.MESSAGE, message); 
	                marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO);
	            }
	        } catch (CoreException e) {}
		}
		
	}
    /**
     * @param file
     * @param message
     */
    protected void addInfoMarker(IFile tempFile, String message) {
        Problem tempProblem = new FileInfoMarker(tempFile, tempFile, message);
        if (getProblemsMap().containsKey(tempFile)) {
            List list = (List) getProblemsMap().get(tempFile);
            list.add(tempProblem);
        } else {
            List list = new ArrayList();
            list.add(new RemoveMarkerProblem(tempFile, getResourceFile()));  //remove existing markers
            list.add(tempProblem);
			getProblemsMap().put(tempFile, list);
        }
    }

    protected class RemoveMarkerProblem extends Problem {
        public RemoveMarkerProblem(IFile file, IFile orig) {
            super(file, orig);
        }

		public void run() {
	        if (originator == null) {
	            //remove all markers
	            try {
	                resource.deleteMarkers(ICModelMarker.INDEXER_MARKER, true, IResource.DEPTH_INFINITE);
	            } catch (CoreException e) {
	            }
	            return;
	        }
	        // else remove only those markers with matching originator
	        IMarker[] markers;
	        try {
	            markers = resource.findMarkers(ICModelMarker.INDEXER_MARKER, true, IResource.DEPTH_INFINITE);
	        } catch (CoreException e1) {
	            return;
	        }
	        String origPath = originator.getFullPath().toString();
	        IMarker mark = null;
	        String orig = null;
	        for (int i = 0; i < markers.length; i++) {
	            mark = markers[ i ];
	            try {
	                orig = (String) mark.getAttribute(INDEXER_MARKER_ORIGINATOR);
	                if( orig != null && orig.equals(origPath )) {
	                    mark.delete();
	                }
	            } catch (CoreException e) {
	            }
	        }
		}
    }

    // Problem markers ******************************
    
    
    public boolean areProblemMarkersEnabled(){
        return problemMarkersEnabled != 0;
    }
    public int getProblemMarkersEnabled() {
        return problemMarkersEnabled;
    }
    
    public void setProblemMarkersEnabled(int value) {
        if (value != 0) {
            problemsMap = new HashMap();
        }
        this.problemMarkersEnabled = value;
    }
    
//    abstract public void generateMarkerProblem(Problem problem);

    public void requestRemoveMarkers(IFile resource, IFile originator ){
        if (!areProblemMarkersEnabled())
            return;
        
        Problem prob = new RemoveMarkerProblem(resource, originator);
        
        //a remove request will erase any previous requests for this resource
        if( problemsMap.containsKey(resource) ){
            List list = (List) problemsMap.get(resource);
            list.clear();
            list.add(prob);
        } else {
            List list = new ArrayList();
            list.add(prob);
            problemsMap.put(resource, list);
        }
    }
    
    public void reportProblems() {
        if (!areProblemMarkersEnabled())
            return;
        
        Iterator i = problemsMap.keySet().iterator();
        
        while (i.hasNext()){
            IFile resource = (IFile) i.next();
            List problemList = (List) problemsMap.get(resource);

            //only bother scheduling a job if we have problems to add or remove
            if (problemList.size() <= 1) {
                IMarker [] marker;
                try {
                    marker = resource.findMarkers( ICModelMarker.INDEXER_MARKER, true, IResource.DEPTH_ZERO);
                } catch (CoreException e) {
                    continue;
                }
                if( marker.length == 0 )
                    continue;
            }
            String jobName = INDEXER_MARKER_PROCESSING;
            jobName += " ("; //$NON-NLS-1$
            jobName += resource.getFullPath();
            jobName += ')';
            
            ProcessMarkersJob job = new ProcessMarkersJob(resource, problemList, jobName);
            
            IndexManager indexManager = CCorePlugin.getDefault().getCoreModel().getIndexManager();
            IProgressMonitor group = indexManager.getIndexJobProgressGroup();
            
            job.setRule(resource);
            if (group != null)
                job.setProgressGroup(group, 0);
            job.setPriority(Job.DECORATE);
            job.schedule();
        }
    }
    
    private class ProcessMarkersJob extends Job {
        protected final List problems;
        private final IFile resource;
        public ProcessMarkersJob(IFile resource, List problems, String name) {
            super(name);
            this.problems = problems;
            this.resource = resource;
        }

        protected IStatus run(IProgressMonitor monitor) {
            IWorkspaceRunnable job = new IWorkspaceRunnable( ) {
                public void run(IProgressMonitor monitor) {
                    processMarkers( problems );
                }
            };
            try {
                CCorePlugin.getWorkspace().run(job, resource, 0, null);
            } catch (CoreException e) {
            }
            return Status.OK_STATUS;
        }
    }
    
    protected void processMarkers(List problemsList) {
        Iterator i = problemsList.iterator();
        while (i.hasNext()) {
            Problem prob = (Problem) i.next();
			prob.run();
        }
    }
	public void run() {
		// TODO Auto-generated method stub
		
	}

	public IFile getResourceFile() {
	    return resourceFile;
	}

}

Back to the top