~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CProject.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2000, 2011 QNX Software Systems and others.
 
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
 *     QNX Software Systems - Initial API and implementation
 
10
 *     Anton Leherbauer (Wind River Systems)
 
11
 *******************************************************************************/
 
12
package org.eclipse.cdt.internal.core.model;
 
13
 
 
14
 
 
15
import java.io.IOException;
 
16
import java.util.ArrayList;
 
17
import java.util.HashMap;
 
18
import java.util.HashSet;
 
19
import java.util.Iterator;
 
20
import java.util.List;
 
21
import java.util.Map;
 
22
 
 
23
import org.eclipse.cdt.core.CCProjectNature;
 
24
import org.eclipse.cdt.core.CCorePlugin;
 
25
import org.eclipse.cdt.core.CProjectNature;
 
26
import org.eclipse.cdt.core.IBinaryParser;
 
27
import org.eclipse.cdt.core.IBinaryParser.IBinaryArchive;
 
28
import org.eclipse.cdt.core.IBinaryParser.IBinaryFile;
 
29
import org.eclipse.cdt.core.IBinaryParser.IBinaryObject;
 
30
import org.eclipse.cdt.core.model.CModelException;
 
31
import org.eclipse.cdt.core.model.CoreModel;
 
32
import org.eclipse.cdt.core.model.CoreModelUtil;
 
33
import org.eclipse.cdt.core.model.IArchiveContainer;
 
34
import org.eclipse.cdt.core.model.IBinaryContainer;
 
35
import org.eclipse.cdt.core.model.ICContainer;
 
36
import org.eclipse.cdt.core.model.ICElement;
 
37
import org.eclipse.cdt.core.model.ICModelStatusConstants;
 
38
import org.eclipse.cdt.core.model.ICProject;
 
39
import org.eclipse.cdt.core.model.IIncludeEntry;
 
40
import org.eclipse.cdt.core.model.IIncludeReference;
 
41
import org.eclipse.cdt.core.model.ILibraryEntry;
 
42
import org.eclipse.cdt.core.model.ILibraryReference;
 
43
import org.eclipse.cdt.core.model.IOutputEntry;
 
44
import org.eclipse.cdt.core.model.IPathEntry;
 
45
import org.eclipse.cdt.core.model.ISourceEntry;
 
46
import org.eclipse.cdt.core.model.ISourceRoot;
 
47
import org.eclipse.cdt.core.settings.model.CSourceEntry;
 
48
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
 
49
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
 
50
import org.eclipse.cdt.core.settings.model.ICSourceEntry;
 
51
import org.eclipse.cdt.internal.core.settings.model.CProjectDescriptionManager;
 
52
import org.eclipse.cdt.internal.core.util.MementoTokenizer;
 
53
import org.eclipse.core.resources.IFile;
 
54
import org.eclipse.core.resources.IProject;
 
55
import org.eclipse.core.resources.IResource;
 
56
import org.eclipse.core.resources.ProjectScope;
 
57
import org.eclipse.core.runtime.CoreException;
 
58
import org.eclipse.core.runtime.IPath;
 
59
import org.eclipse.core.runtime.IProgressMonitor;
 
60
import org.eclipse.core.runtime.Path;
 
61
import org.eclipse.core.runtime.Preferences;
 
62
import org.eclipse.core.runtime.QualifiedName;
 
63
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
 
64
import org.eclipse.core.runtime.preferences.IScopeContext;
 
65
import org.osgi.service.prefs.BackingStoreException;
 
66
 
 
67
public class CProject extends Openable implements ICProject {
 
68
 
 
69
        private static final String CUSTOM_DEFAULT_OPTION_VALUE = "#\r\n\r#custom-non-empty-default-value#\r\n\r#"; //$NON-NLS-1$
 
70
 
 
71
        public CProject(ICElement parent, IProject project) {
 
72
                super(parent, project, ICElement.C_PROJECT);
 
73
        }
 
74
 
 
75
        public IBinaryContainer getBinaryContainer() throws CModelException {
 
76
                return ((CProjectInfo) getElementInfo()).getBinaryContainer();
 
77
        }
 
78
 
 
79
        public IArchiveContainer getArchiveContainer() throws CModelException {
 
80
                return ((CProjectInfo) getElementInfo()).getArchiveContainer();
 
81
        }
 
82
 
 
83
        public IProject getProject() {
 
84
                return getUnderlyingResource().getProject();
 
85
        }
 
86
 
 
87
        public ICElement findElement(IPath path) throws CModelException {
 
88
                ICElement celem = null;
 
89
                if (path.isAbsolute()) {
 
90
                        celem = CModelManager.getDefault().create(path);
 
91
                } else {
 
92
                        IProject project = getProject();
 
93
                        if (project != null) {
 
94
                                IPath p = project.getFullPath().append(path);
 
95
                                celem = CModelManager.getDefault().create(p);
 
96
                        }
 
97
                }
 
98
                if (celem == null) {
 
99
                        CModelStatus status = new CModelStatus(ICModelStatusConstants.INVALID_PATH, path);
 
100
                        throw new CModelException(status);
 
101
                }
 
102
                return celem;
 
103
        }
 
104
 
 
105
        public static boolean hasCNature(IProject p) {
 
106
                try {
 
107
                        return p.hasNature(CProjectNature.C_NATURE_ID);
 
108
                } catch (CoreException e) {
 
109
                        //throws exception if the project is not open.
 
110
                }
 
111
                return false;
 
112
        }
 
113
 
 
114
        public static boolean hasCCNature(IProject p) {
 
115
                try {
 
116
                        return p.hasNature(CCProjectNature.CC_NATURE_ID);
 
117
                } catch (CoreException e) {
 
118
                        //throws exception if the project is not open.
 
119
                }
 
120
                return false;
 
121
        }
 
122
 
 
123
        private boolean isCProject() {
 
124
                return hasCNature(getProject()) || hasCCNature(getProject());
 
125
        }
 
126
 
 
127
        /**
 
128
         * Returns true if this handle represents the same C project
 
129
         * as the given handle. Two handles represent the same
 
130
         * project if they are identical or if they represent a project with
 
131
         * the same underlying resource and occurrence counts.
 
132
         *
 
133
         * @see CElement#equals(Object)
 
134
         */
 
135
        @Override
 
136
        public boolean equals(Object o) {
 
137
        
 
138
                if (this == o)
 
139
                        return true;
 
140
        
 
141
                if (!(o instanceof CProject))
 
142
                        return false;
 
143
        
 
144
                CProject other = (CProject) o;
 
145
                return getProject().equals(other.getProject());
 
146
        }
 
147
 
 
148
        @Override
 
149
        protected CElementInfo createElementInfo() {
 
150
                return new CProjectInfo(this);
 
151
        }
 
152
 
 
153
        // CHECKPOINT: CProjects will return the hash code of their underlying IProject
 
154
        @Override
 
155
        public int hashCode() {
 
156
                return getProject().hashCode();
 
157
        }
 
158
 
 
159
        public IIncludeReference[] getIncludeReferences() throws CModelException {
 
160
                CProjectInfo pinfo = (CProjectInfo)CModelManager.getDefault().peekAtInfo(this);
 
161
                IIncludeReference[] incRefs = null;
 
162
                if (pinfo != null) {
 
163
                        incRefs = pinfo.incReferences;
 
164
                }
 
165
                if (incRefs == null) {
 
166
                        IPathEntry[] entries = getResolvedPathEntries();
 
167
                        ArrayList<IncludeReference> list = new ArrayList<IncludeReference>(entries.length);
 
168
                        for (IPathEntry entrie : entries) {
 
169
                                if (entrie.getEntryKind() == IPathEntry.CDT_INCLUDE) {
 
170
                                        IIncludeEntry entry = (IIncludeEntry) entrie;
 
171
                                        list.add(new IncludeReference(this, entry));
 
172
                                }
 
173
                        }
 
174
                        incRefs = list.toArray(new IIncludeReference[0]);
 
175
                        if (pinfo != null) {
 
176
                                pinfo.incReferences = incRefs;
 
177
                        }
 
178
                }
 
179
                return incRefs;
 
180
        }
 
181
 
 
182
        public ILibraryReference[] getLibraryReferences() throws CModelException {
 
183
                CProjectInfo pinfo = (CProjectInfo)CModelManager.getDefault().peekAtInfo(this);
 
184
                ILibraryReference[] libRefs = null;
 
185
                if (pinfo != null) {
 
186
                        libRefs = pinfo.libReferences;
 
187
                }
 
188
 
 
189
                if (libRefs == null) {
 
190
                        BinaryParserConfig[] binConfigs = CModelManager.getDefault().getBinaryParser(getProject());
 
191
                        IPathEntry[] entries = getResolvedPathEntries();
 
192
                        ArrayList<ILibraryReference> list = new ArrayList<ILibraryReference>(entries.length);
 
193
                        for (IPathEntry entrie : entries) {
 
194
                                if (entrie.getEntryKind() == IPathEntry.CDT_LIBRARY) {
 
195
                                        ILibraryEntry entry = (ILibraryEntry) entrie;
 
196
                                        ILibraryReference lib = getLibraryReference(this, binConfigs, entry);
 
197
                                        if (lib != null) {
 
198
                                                list.add(lib);
 
199
                                        }
 
200
                                }
 
201
                        }
 
202
                        libRefs = list.toArray(new ILibraryReference[0]);
 
203
                        if (pinfo != null) {
 
204
                                pinfo.libReferences = libRefs;
 
205
                        }
 
206
                }
 
207
                return libRefs;
 
208
        }
 
209
 
 
210
        private static ILibraryReference getLibraryReference(ICProject cproject, BinaryParserConfig[] binConfigs, ILibraryEntry entry) {
 
211
                if (binConfigs == null) {
 
212
                        binConfigs = CModelManager.getDefault().getBinaryParser(cproject.getProject());
 
213
                }
 
214
                ILibraryReference lib = null;
 
215
                if (binConfigs != null) {
 
216
                        for (BinaryParserConfig binConfig : binConfigs) {
 
217
                                IBinaryFile bin;
 
218
                                try {
 
219
                                        IBinaryParser parser = binConfig.getBinaryParser();
 
220
                                        bin = parser.getBinary(entry.getFullLibraryPath());
 
221
                                        if (bin != null) {
 
222
                                                if (bin.getType() == IBinaryFile.ARCHIVE) {
 
223
                                                        lib = new LibraryReferenceArchive(cproject, entry, (IBinaryArchive)bin);
 
224
                                                } else if (bin instanceof IBinaryObject){
 
225
                                                        lib = new LibraryReferenceShared(cproject, entry, (IBinaryObject)bin);
 
226
                                                }
 
227
                                                break;
 
228
                                        }
 
229
                                } catch (IOException e) {
 
230
                                } catch (CoreException e) {
 
231
                                }
 
232
                        }
 
233
                }
 
234
                if (lib == null) {
 
235
                        lib = new LibraryReference(cproject, entry);
 
236
                }
 
237
                return lib;
 
238
        }
 
239
 
 
240
        /**
 
241
         * @see ICProject#getRequiredProjectNames()
 
242
         */
 
243
        public String[] getRequiredProjectNames() throws CModelException {
 
244
                return projectPrerequisites(getResolvedPathEntries());
 
245
        }
 
246
 
 
247
        public String[] projectPrerequisites(IPathEntry[] entries) throws CModelException {
 
248
                return PathEntryManager.getDefault().projectPrerequisites(entries);
 
249
        }
 
250
 
 
251
 
 
252
        /**
 
253
         * @see org.eclipse.cdt.core.model.ICProject#getOption(String, boolean)
 
254
         */
 
255
        public String getOption(String optionName, boolean inheritCCoreOptions) {
 
256
                if (CModelManager.OptionNames.contains(optionName)) {
 
257
                        IEclipsePreferences preferences = getPreferences();
 
258
                        final String cCoreDefault= inheritCCoreOptions ? CCorePlugin.getOption(optionName) : null;
 
259
                        if (preferences == null) {
 
260
                                return cCoreDefault;
 
261
                        }
 
262
                        String value= preferences.get(optionName, cCoreDefault).trim();
 
263
                        return value == null ? null : value.trim();
 
264
                }
 
265
 
 
266
                return null;
 
267
        }
 
268
 
 
269
        /**
 
270
         * @see org.eclipse.cdt.core.model.ICProject#getOptions(boolean)
 
271
         */
 
272
        public Map<String, String> getOptions(boolean inheritCCoreOptions) {
 
273
                // initialize to the defaults from CCorePlugin options pool
 
274
                Map<String, String> options= inheritCCoreOptions ? CCorePlugin.getOptions() : new HashMap<String, String>(5);
 
275
 
 
276
                IEclipsePreferences preferences = getPreferences();
 
277
                if (preferences == null)
 
278
                        return options;
 
279
                HashSet<String> optionNames= CModelManager.OptionNames;
 
280
 
 
281
                // create project options
 
282
                try {
 
283
                        String[] propertyNames= preferences.keys();
 
284
                        for (String propertyName : propertyNames) {
 
285
                                String value= preferences.get(propertyName, null);
 
286
                                if (value != null && optionNames.contains(propertyName)){
 
287
                                        options.put(propertyName, value.trim());
 
288
                                }
 
289
                        }
 
290
                } catch (BackingStoreException e) {
 
291
                        // ignore silently
 
292
                }
 
293
                return options;
 
294
        }
 
295
 
 
296
        /**
 
297
         * @see org.eclipse.cdt.core.model.ICProject#setOption(java.lang.String, java.lang.String)
 
298
         */
 
299
        public void setOption(String optionName, String optionValue) {
 
300
                if (!CModelManager.OptionNames.contains(optionName))
 
301
                        return; // unrecognized option
 
302
 
 
303
                IEclipsePreferences projectPreferences= getPreferences();
 
304
                if (optionValue == null) {
 
305
                        // remove preference
 
306
                        projectPreferences.remove(optionName);
 
307
                } else {
 
308
                        projectPreferences.put(optionName, optionValue);
 
309
                }
 
310
                
 
311
                // Dump changes
 
312
                try {
 
313
                        projectPreferences.flush();
 
314
                } catch (BackingStoreException e) {
 
315
                        // problem with pref store - quietly ignore
 
316
                }
 
317
        }
 
318
 
 
319
        /**
 
320
         * @see org.eclipse.cdt.core.model.ICProject#setOptions(Map)
 
321
         */
 
322
        public void setOptions(Map<String, String> newOptions) {
 
323
                Preferences preferences = new Preferences();
 
324
                setPreferences(preferences); // always reset (26255)
 
325
 
 
326
                if (newOptions != null) {
 
327
                        for (Map.Entry<String, String> e : newOptions.entrySet()) {
 
328
                                String key = e.getKey();
 
329
                                if (!CModelManager.OptionNames.contains(key))
 
330
                                        continue; // unrecognized option
 
331
 
 
332
                                // no filtering for encoding (custom encoding for project is allowed)
 
333
                                String value = e.getValue();
 
334
                                preferences.setDefault(key, CUSTOM_DEFAULT_OPTION_VALUE); // empty string isn't the default (26251)
 
335
                                preferences.setValue(key, value);
 
336
                        }
 
337
                }
 
338
 
 
339
                // persist options
 
340
                savePreferences(preferences);
 
341
        }
 
342
 
 
343
        /**
 
344
         * Returns the project custom preference pool.
 
345
         * Project preferences may include custom encoding.
 
346
         * @return IEclipsePreferences or <code>null</code> if the project
 
347
         *      does not have a C nature.
 
348
         */
 
349
        private IEclipsePreferences getPreferences() {
 
350
                if (!(isCProject())) {
 
351
                        return null;
 
352
                }
 
353
                IScopeContext context= new ProjectScope(getProject());
 
354
                final IEclipsePreferences preferences= context.getNode(CCorePlugin.PLUGIN_ID);
 
355
                return preferences;
 
356
        }
 
357
 
 
358
        /**
 
359
         * Save project custom preferences to persistent properties
 
360
         */
 
361
        private void savePreferences(Preferences preferences) {
 
362
                if (preferences == null)
 
363
                        return;
 
364
                if (!isCProject()) {
 
365
                        return; // ignore
 
366
                }
 
367
                Iterator<String> iter = CModelManager.OptionNames.iterator();
 
368
 
 
369
                while (iter.hasNext()) {
 
370
                        String qualifiedName = iter.next();
 
371
                        String dequalifiedName = qualifiedName.substring(CCorePlugin.PLUGIN_ID.length() + 1);
 
372
                        String value = null;
 
373
 
 
374
                        try {
 
375
                                value = preferences.getString(qualifiedName);
 
376
 
 
377
                                if (value != null && !value.equals(preferences.getDefaultString(qualifiedName))) {
 
378
                                        resource.setPersistentProperty(new QualifiedName(CCorePlugin.PLUGIN_ID, dequalifiedName), value);
 
379
                                } else {
 
380
                                        resource.setPersistentProperty(new QualifiedName(CCorePlugin.PLUGIN_ID, dequalifiedName), null);
 
381
                                }
 
382
                        } catch (CoreException e) {
 
383
                        }
 
384
                }
 
385
        }
 
386
 
 
387
        /*
 
388
         * Set cached preferences, no preferences are saved, only info is updated
 
389
         */
 
390
        private void setPreferences(Preferences preferences) {
 
391
                if (!isCProject()) {
 
392
                        return; // ignore
 
393
                }
 
394
                // Do nothing
 
395
        }
 
396
 
 
397
        /* (non-Javadoc)
 
398
         * @see org.eclipse.cdt.core.model.ICProject#getResolvedCPathEntries()
 
399
         */
 
400
        public IPathEntry[] getResolvedPathEntries() throws CModelException {
 
401
                return CoreModel.getResolvedPathEntries(this);
 
402
        }
 
403
 
 
404
        /* (non-Javadoc)
 
405
         * @see org.eclipse.cdt.core.model.ICProject#getRawCPathEntries()
 
406
         */
 
407
        public IPathEntry[] getRawPathEntries() throws CModelException {
 
408
                return CoreModel.getRawPathEntries(this);
 
409
        }
 
410
 
 
411
        /* (non-Javadoc)
 
412
         * @see org.eclipse.cdt.core.model.ICProject#setRawCPathEntries(org.eclipse.cdt.core.model.IPathEntry[], org.eclipse.core.runtime.IProgressMonitor)
 
413
         */
 
414
        public void setRawPathEntries(IPathEntry[] newEntries, IProgressMonitor monitor) throws CModelException {
 
415
                CoreModel.setRawPathEntries(this, newEntries, monitor);
 
416
        }
 
417
 
 
418
        /* (non-Javadoc)
 
419
         * @see org.eclipse.cdt.core.model.ICProject#getSourceRoot(org.eclipse.cdt.core.model.ISourceEntry)
 
420
         */
 
421
        public ISourceRoot getSourceRoot(ISourceEntry entry) throws CModelException {
 
422
                return getSourceRoot(new CSourceEntry(entry.getPath(), entry.getExclusionPatterns(), 0));
 
423
        }
 
424
 
 
425
        public ISourceRoot getSourceRoot(ICSourceEntry entry) throws CModelException {
 
426
                IPath p = getPath();
 
427
                IPath sp = entry.getFullPath();
 
428
                if (p.isPrefixOf(sp)) {
 
429
                        int count = sp.matchingFirstSegments(p);
 
430
                        sp = sp.removeFirstSegments(count);
 
431
                        IResource res = null;
 
432
                        if (sp.isEmpty()) {
 
433
                                res = getProject();
 
434
                        } else {
 
435
                                res = getProject().findMember(sp);
 
436
                        }
 
437
                        if (res != null) {
 
438
                                return new SourceRoot(this, res, entry);
 
439
                        }
 
440
                }
 
441
                return null;
 
442
        }
 
443
 
 
444
        /* (non-Javadoc)
 
445
         * @see org.eclipse.cdt.core.model.ICProject#findSourceRoot()
 
446
         */
 
447
        public ISourceRoot findSourceRoot(IResource res) {
 
448
            try {
 
449
                        ISourceRoot[] roots = getAllSourceRoots();
 
450
                        for (ISourceRoot root : roots) {
 
451
                                if (root.isOnSourceEntry(res)) {
 
452
                                        return root;
 
453
                                }
 
454
                        }
 
455
            } catch (CModelException e) {
 
456
            }
 
457
                return null;
 
458
        }
 
459
 
 
460
        /* (non-Javadoc)
 
461
         * @see org.eclipse.cdt.core.model.ICProject#findSourceRoot()
 
462
         */
 
463
        public ISourceRoot findSourceRoot(IPath path) {
 
464
            try {
 
465
                        ISourceRoot[] roots = getAllSourceRoots();
 
466
                        for (ISourceRoot root : roots) {
 
467
                            if (root.getPath().equals(path)) {
 
468
                                        return root;
 
469
                                }
 
470
                        }
 
471
            } catch (CModelException e) {
 
472
            }
 
473
                return null;
 
474
        }
 
475
        
 
476
        /* (non-Javadoc)
 
477
         * @see org.eclipse.cdt.core.model.ICProject#getSourceRoots()
 
478
         */
 
479
        public ISourceRoot[] getSourceRoots() throws CModelException {
 
480
                Object[] children = getChildren();
 
481
                ArrayList<ISourceRoot> result = new ArrayList<ISourceRoot>(children.length);
 
482
                for (Object element : children) {
 
483
                        if (element instanceof ISourceRoot) {
 
484
                                result.add((ISourceRoot) element);
 
485
                        }
 
486
        }
 
487
                return result.toArray(new ISourceRoot[result.size()]);
 
488
        }
 
489
 
 
490
        /**
 
491
         * Get all source roots.
 
492
         * 
 
493
         * @return all source roots
 
494
         * @throws CModelException
 
495
         */
 
496
        public ISourceRoot[] getAllSourceRoots() throws CModelException {
 
497
                CProjectInfo pinfo = (CProjectInfo)CModelManager.getDefault().peekAtInfo(this);
 
498
                ISourceRoot[] roots = null;
 
499
                if (pinfo != null) {
 
500
                        if (pinfo.sourceRoots != null) {
 
501
                                roots = pinfo.sourceRoots;
 
502
                        } else {
 
503
                                List<ISourceRoot> list = computeSourceRoots();
 
504
                                roots = pinfo.sourceRoots = list.toArray(new ISourceRoot[list.size()]);
 
505
                        }
 
506
                } else {
 
507
                        List<ISourceRoot> list = computeSourceRoots();
 
508
                        roots = list.toArray(new ISourceRoot[list.size()]);
 
509
                }
 
510
                return roots;
 
511
        }
 
512
 
 
513
        public IOutputEntry[] getOutputEntries() throws CModelException {
 
514
                CProjectInfo pinfo = (CProjectInfo) CModelManager.getDefault().peekAtInfo(this);
 
515
                IOutputEntry[] outs = null;
 
516
                if (pinfo != null) {
 
517
                        if (pinfo.outputEntries != null) {
 
518
                                outs = pinfo.outputEntries;
 
519
                        } else {
 
520
                                IPathEntry[] entries = getResolvedPathEntries();
 
521
                                outs = pinfo.outputEntries = getOutputEntries(entries);
 
522
                        }
 
523
                } else {
 
524
                        IPathEntry[] entries = getResolvedPathEntries();
 
525
                        outs = getOutputEntries(entries);
 
526
                }
 
527
                return outs;
 
528
        }
 
529
 
 
530
        /**
 
531
         * 
 
532
         */
 
533
        public IOutputEntry[] getOutputEntries(IPathEntry[] entries) throws CModelException {
 
534
                ArrayList<IPathEntry> list = new ArrayList<IPathEntry>(entries.length);
 
535
                for (IPathEntry entrie : entries) {
 
536
                        if (entrie.getEntryKind() == IPathEntry .CDT_OUTPUT) {
 
537
                                list.add(entrie);
 
538
                        }
 
539
                }
 
540
                IOutputEntry[] outputs = new IOutputEntry[list.size()];
 
541
                list.toArray(outputs);
 
542
                return outputs;
 
543
        }
 
544
 
 
545
        /**
 
546
         * 
 
547
         */
 
548
        public boolean isOnOutputEntry(IResource resource) {
 
549
                IPath path = resource.getFullPath();
 
550
                
 
551
                // ensure that folders are only excluded if all of their children are excluded
 
552
                if (resource.getType() == IResource.FOLDER || resource.getType() == IResource.PROJECT) {
 
553
                        path = path.append("*"); //$NON-NLS-1$
 
554
                }
 
555
 
 
556
                try {
 
557
                        IOutputEntry[] entries = getOutputEntries();
 
558
                        for (IOutputEntry entrie : entries) {
 
559
                                boolean on = isOnOutputEntry(entrie, path);
 
560
                                if (on) {
 
561
                                        return on;
 
562
                                }
 
563
                        }
 
564
                } catch (CModelException e) {
 
565
                        //
 
566
                }
 
567
                return false;
 
568
        }
 
569
 
 
570
        private boolean isOnOutputEntry(IOutputEntry entry, IPath path) {
 
571
                if (entry.getPath().isPrefixOf(path) && !CoreModelUtil.isExcluded(path, entry.fullExclusionPatternChars())) {
 
572
                        return true;
 
573
                }
 
574
                return false;
 
575
        }
 
576
 
 
577
        /* (non-Javadoc)
 
578
         * @see org.eclipse.cdt.internal.core.model.Openable#buildStructure(org.eclipse.cdt.internal.core.model.OpenableInfo, org.eclipse.core.runtime.IProgressMonitor, java.util.Map, org.eclipse.core.resources.IResource)
 
579
         */
 
580
        @Override
 
581
        protected boolean buildStructure(OpenableInfo info, IProgressMonitor pm,
 
582
                        Map<ICElement, CElementInfo> newElements, IResource underlyingResource)
 
583
                        throws CModelException {
 
584
                boolean validInfo = false;
 
585
                try {
 
586
                        IResource res = getResource();
 
587
                        if (res != null && res.isAccessible()) {
 
588
                                validInfo = computeChildren(info, res);
 
589
                        } else {
 
590
                                throw newNotPresentException();
 
591
                        }
 
592
                } finally {
 
593
                        if (!validInfo) {
 
594
                                CModelManager.getDefault().removeInfo(this);
 
595
                        }
 
596
                }
 
597
                return validInfo;
 
598
        }
 
599
 
 
600
        protected List<ISourceRoot> computeSourceRoots() throws CModelException {
 
601
                //IPathEntry[] entries = getResolvedPathEntries();
 
602
                ICSourceEntry[] entries = null;
 
603
                ICProjectDescription des = CProjectDescriptionManager.getInstance().getProjectDescription(getProject(), false);
 
604
                if(des != null){
 
605
                        ICConfigurationDescription cfg = des.getDefaultSettingConfiguration();
 
606
                        if(cfg != null)
 
607
                                entries = cfg.getResolvedSourceEntries();
 
608
                }
 
609
                
 
610
                if(entries != null){
 
611
                        ArrayList<ISourceRoot> list = new ArrayList<ISourceRoot>(entries.length);
 
612
                        for (ICSourceEntry sourceEntry : entries) {
 
613
                                ISourceRoot root = getSourceRoot(sourceEntry);
 
614
                                        if (root != null) {
 
615
                                                list.add(root);
 
616
                                        }
 
617
                        }
 
618
                        return list;
 
619
                }
 
620
                return new ArrayList<ISourceRoot>(0);
 
621
        }
 
622
 
 
623
        protected boolean computeChildren(OpenableInfo info, IResource res) throws CModelException {
 
624
                List<ISourceRoot> sourceRoots = computeSourceRoots();
 
625
                List<ICContainer> children = new ArrayList<ICContainer>(sourceRoots.size());
 
626
                children.addAll(sourceRoots);
 
627
                
 
628
                boolean projectIsSourceRoot = false;
 
629
                for (ISourceRoot sourceRoot : sourceRoots)
 
630
                        if (sourceRoot.getResource().equals(getProject())) {
 
631
                                projectIsSourceRoot = true;
 
632
                                break;
 
633
                        }
 
634
                
 
635
                // Now look for output folders
 
636
                try {
 
637
                        IResource[] resources = getProject().members();
 
638
                        for (IResource child : resources) {
 
639
                                if (child.getType() == IResource.FOLDER) {
 
640
                                        boolean found = false;
 
641
                                        for (ISourceRoot sourceRoot : sourceRoots) {
 
642
                                                if (sourceRoot.isOnSourceEntry(child)) {
 
643
                                                        found = true;
 
644
                                                        break;
 
645
                                                }
 
646
                                        }
 
647
                                        
 
648
                                        // Not in source folder, check if it's a container on output entry
 
649
                                        // Also make sure I'm not a source root since my SourceRoot object would
 
650
                                        // have already added this.
 
651
                                        if (!found && isOnOutputEntry(child) && !projectIsSourceRoot)
 
652
                                                children.add(new CContainer(this, child));
 
653
                                }
 
654
                        }
 
655
                } catch (CoreException e) {
 
656
                        // ignore
 
657
                }
 
658
                
 
659
                info.setChildren(children);
 
660
                if (info instanceof CProjectInfo) {
 
661
                        CProjectInfo pinfo = (CProjectInfo)info;
 
662
                        pinfo.sourceRoots= sourceRoots.toArray(new ISourceRoot[sourceRoots.size()]);
 
663
                        pinfo.setNonCResources(null);
 
664
                }
 
665
                return true;
 
666
        }
 
667
        
 
668
        /*
 
669
         * @see ICProject
 
670
         */
 
671
        public boolean isOnSourceRoot(ICElement element) {
 
672
                try {
 
673
                        ISourceRoot[] roots = getSourceRoots();
 
674
                        for (ISourceRoot root : roots) {
 
675
                                if (root.isOnSourceEntry(element)) {
 
676
                                        return true;
 
677
                                }
 
678
                        }
 
679
                } catch (CModelException e) {
 
680
                        // ..
 
681
                }
 
682
                return false;
 
683
        }
 
684
 
 
685
        /*
 
686
         * @see ICProject
 
687
         */
 
688
        public boolean isOnSourceRoot(IResource resource) {
 
689
                try {
 
690
                        ISourceRoot[] roots = getSourceRoots();
 
691
                        for (ISourceRoot root : roots) {
 
692
                                if (root.isOnSourceEntry(resource)) {
 
693
                                        return true;
 
694
                                }
 
695
                        }
 
696
                } catch (CModelException e) {
 
697
                        //
 
698
                }
 
699
                return false;
 
700
        }
 
701
 
 
702
        /* (non-Javadoc)
 
703
         * @see org.eclipse.cdt.core.model.ICElement#exists()
 
704
         */
 
705
        @Override
 
706
        public boolean exists() {
 
707
                if (!isCProject()) {
 
708
                        return false;
 
709
                }
 
710
                return true;
 
711
        }
 
712
 
 
713
        /* (non-Javadoc)
 
714
         * @see org.eclipse.cdt.core.model.ICProject#getNonCResources()
 
715
         */
 
716
        public Object[] getNonCResources() throws CModelException {
 
717
                return ((CProjectInfo) getElementInfo()).getNonCResources(getResource());
 
718
        }
 
719
 
 
720
        /* (non-Javadoc)
 
721
         * @see org.eclipse.cdt.internal.core.model.CElement#closing(java.lang.Object)
 
722
         */
 
723
        @Override
 
724
        protected void closing(Object info) throws CModelException {
 
725
                if (info instanceof CProjectInfo) {
 
726
                        CModelManager.getDefault().removeBinaryRunner(this);
 
727
                        CProjectInfo pinfo = (CProjectInfo)info;
 
728
                        if (pinfo.vBin != null) {
 
729
                                pinfo.vBin.close();
 
730
                        }
 
731
                        if (pinfo.vLib != null) {
 
732
                                pinfo.vLib.close();
 
733
                        }
 
734
                        pinfo.resetCaches();
 
735
                }
 
736
                super.closing(info);
 
737
        }
 
738
 
 
739
        /*
 
740
         * Resets this project's caches
 
741
         */
 
742
        public void resetCaches() {
 
743
                CProjectInfo pinfo = (CProjectInfo) CModelManager.getDefault().peekAtInfo(this);
 
744
                if (pinfo != null){
 
745
                        pinfo.resetCaches();
 
746
                }
 
747
        }
 
748
 
 
749
        @Override
 
750
        public ICElement getHandleFromMemento(String token, MementoTokenizer memento) {
 
751
                switch (token.charAt(0)) {
 
752
                case CEM_SOURCEROOT:
 
753
                        IPath rootPath = Path.EMPTY;
 
754
                        token = null;
 
755
                        while (memento.hasMoreTokens()) {
 
756
                                token = memento.nextToken();
 
757
                                char firstChar = token.charAt(0);
 
758
                                if (firstChar != CEM_SOURCEFOLDER && firstChar != CEM_TRANSLATIONUNIT) {
 
759
                                        rootPath= rootPath.append(token);
 
760
                                        token= null;
 
761
                                } else {
 
762
                                        break;
 
763
                                }
 
764
                        }
 
765
                        if (!rootPath.isAbsolute()) {
 
766
                                rootPath= getProject().getFullPath().append(rootPath);
 
767
                        }
 
768
                        CElement root = (CElement)findSourceRoot(rootPath);
 
769
                        if (root != null) {
 
770
                                if (token != null) {
 
771
                                        return root.getHandleFromMemento(token, memento);
 
772
                                } else {
 
773
                                        return root.getHandleFromMemento(memento);
 
774
                                }
 
775
                        }
 
776
                        break;
 
777
                case CEM_TRANSLATIONUNIT:
 
778
                        if (!memento.hasMoreTokens()) return this;
 
779
                        String tuName = memento.nextToken();
 
780
                        final IPath path= Path.fromPortableString(tuName);
 
781
                        CElement tu= null;
 
782
                        if (!path.isAbsolute()) {
 
783
                                final IProject project= getProject();
 
784
                                if (project != null) {
 
785
                                        IResource resource= project.findMember(path);
 
786
                                        if (resource != null && resource.getType() == IResource.FILE) {
 
787
                                                final IFile file= (IFile)resource;
 
788
                                                tu= (CElement) CModelManager.getDefault().create(file, this);
 
789
                                                if (tu == null) {
 
790
                                                        String contentTypeId= CoreModel.getRegistedContentTypeId(project, file.getName());
 
791
                                                        if (contentTypeId != null) {
 
792
                                                                tu= new TranslationUnit(this, file, contentTypeId);
 
793
                                                        }
 
794
                                                }
 
795
                                        }
 
796
                                }
 
797
                        } else {
 
798
                                tu= (CElement) CoreModel.getDefault().createTranslationUnitFrom(this, path);
 
799
                        }
 
800
                        if (tu != null) {
 
801
                                return tu.getHandleFromMemento(memento);
 
802
                        }
 
803
                        break;
 
804
                }
 
805
                return null;
 
806
        }
 
807
 
 
808
        @Override
 
809
        protected char getHandleMementoDelimiter() {
 
810
                return CEM_CPROJECT;
 
811
        }
 
812
 
 
813
}