~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/language/LanguageMappingResolver.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) 2007, 2008 IBM Corporation 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
 *   IBM Corporation - Initial API and implementation
 
10
 *******************************************************************************/
 
11
package org.eclipse.cdt.internal.core.language;
 
12
 
 
13
import java.util.Iterator;
 
14
import java.util.LinkedList;
 
15
import java.util.List;
 
16
 
 
17
import org.eclipse.cdt.core.language.ProjectLanguageConfiguration;
 
18
import org.eclipse.cdt.core.language.WorkspaceLanguageConfiguration;
 
19
import org.eclipse.cdt.core.model.LanguageManager;
 
20
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
 
21
import org.eclipse.core.resources.IProject;
 
22
import org.eclipse.core.runtime.CoreException;
 
23
import org.eclipse.core.runtime.Platform;
 
24
import org.eclipse.core.runtime.content.IContentType;
 
25
 
 
26
/**
 
27
 * Resolves the effective language for various resources such as
 
28
 * files and projects.
 
29
 */
 
30
public class LanguageMappingResolver {
 
31
        public static final int DEFAULT_MAPPING = 0;
 
32
        public static final int WORKSPACE_MAPPING = 1;
 
33
        public static final int PROJECT_MAPPING = 2;
 
34
        public static final int FILE_MAPPING = 3;
 
35
        
 
36
        /**
 
37
         * Returns the effective language for the file specified by the given path.
 
38
         * If <code>fetchAll</code> is <code>true</code> all inherited language
 
39
         * mappings will be returned in order of precedence.  Otherwise, only the
 
40
         * effective language will be returned.
 
41
         * 
 
42
         * This method will always return at least one mapping.
 
43
         * 
 
44
         * @param project the project that contains the given file
 
45
         * @param filePath the path to the file
 
46
         * @param contentTypeId the content type of the file (optional)
 
47
         * @param fetchAll if <code>true</code>, returns all inherited language mappings.
 
48
         *                 Otherwise, returns only the effective language.
 
49
         * @return the effective language for the file specified by the given path.
 
50
         * @throws CoreException
 
51
         */
 
52
        public static LanguageMapping[] computeLanguage(IProject project, String filePath, ICConfigurationDescription configuration, String contentTypeId, boolean fetchAll) throws CoreException {
 
53
                LanguageManager manager = LanguageManager.getInstance();
 
54
                List<LanguageMapping> inheritedLanguages = new LinkedList<LanguageMapping>();
 
55
                
 
56
                if (project != null) {
 
57
                        ProjectLanguageConfiguration mappings = manager.getLanguageConfiguration(project);
 
58
                        
 
59
                        if (mappings != null) {
 
60
                                // File-level mappings
 
61
                                if (filePath != null) {
 
62
                                        
 
63
                                        String id = mappings.getLanguageForFile(configuration, filePath);
 
64
                                        if (id != null) {
 
65
                                                inheritedLanguages.add(new LanguageMapping(manager.getLanguage(id), FILE_MAPPING));
 
66
                                                if (!fetchAll) {
 
67
                                                        return createLanguageMappingArray(inheritedLanguages);
 
68
                                                }
 
69
                                        }
 
70
                                        
 
71
                                        // Check for a file mapping that's global across all configurations in
 
72
                                        // the project.
 
73
                                        if (configuration != null) {
 
74
                                                id = mappings.getLanguageForFile(null, filePath);
 
75
                                                if (id != null) {
 
76
                                                        inheritedLanguages.add(new LanguageMapping(manager.getLanguage(id), FILE_MAPPING));
 
77
                                                        if (!fetchAll) {
 
78
                                                                return createLanguageMappingArray(inheritedLanguages);
 
79
                                                        }
 
80
                                                }
 
81
                                                
 
82
                                        }
 
83
                                }
 
84
                        
 
85
                                // Project-level mappings
 
86
                                String id = mappings.getLanguageForContentType(configuration, contentTypeId);
 
87
                                if (id != null) {
 
88
                                        inheritedLanguages.add(new LanguageMapping(manager.getLanguage(id), PROJECT_MAPPING));
 
89
                                        if (!fetchAll) {
 
90
                                                return createLanguageMappingArray(inheritedLanguages);
 
91
                                        }
 
92
                                }
 
93
                                
 
94
                                // Check for a content type mapping that's global across all configurations in
 
95
                                // the project.
 
96
                                if (configuration != null) {
 
97
                                        id = mappings.getLanguageForContentType(null, contentTypeId);
 
98
                                        if (id != null) {
 
99
                                                inheritedLanguages.add(new LanguageMapping(manager.getLanguage(id), PROJECT_MAPPING));
 
100
                                                if (!fetchAll) {
 
101
                                                        return createLanguageMappingArray(inheritedLanguages);
 
102
                                                }
 
103
                                        }
 
104
                                }
 
105
                        }
 
106
                }
 
107
                
 
108
                // Workspace mappings
 
109
                WorkspaceLanguageConfiguration workspaceMappings = manager.getWorkspaceLanguageConfiguration();
 
110
                String id = workspaceMappings.getLanguageForContentType(contentTypeId);
 
111
                if (id != null) {
 
112
                        inheritedLanguages.add(new LanguageMapping(manager.getLanguage(id), WORKSPACE_MAPPING));
 
113
                        if (!fetchAll) {
 
114
                                return createLanguageMappingArray(inheritedLanguages);
 
115
                        }
 
116
                }
 
117
                
 
118
                // Platform mappings
 
119
                IContentType contentType = Platform.getContentTypeManager().getContentType(contentTypeId);
 
120
                inheritedLanguages.add(new LanguageMapping(manager.getLanguage(contentType), DEFAULT_MAPPING));
 
121
                return createLanguageMappingArray(inheritedLanguages);
 
122
        }
 
123
 
 
124
        private static LanguageMapping[] createLanguageMappingArray(List<LanguageMapping> inheritedLanguages) {
 
125
                LanguageMapping[] results = new LanguageMapping[inheritedLanguages.size()];
 
126
                Iterator<LanguageMapping> mappings = inheritedLanguages.iterator();
 
127
                int i = 0;
 
128
                while (mappings.hasNext()) {
 
129
                        LanguageMapping mapping = mappings.next();
 
130
                        results[i] = mapping;
 
131
                        i++;
 
132
                }
 
133
                return results;
 
134
        }
 
135
}