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
9
* IBM Corporation - Initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.internal.core.language;
13
import java.util.Iterator;
14
import java.util.LinkedList;
15
import java.util.List;
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;
27
* Resolves the effective language for various resources such as
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;
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.
42
* This method will always return at least one mapping.
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
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>();
56
if (project != null) {
57
ProjectLanguageConfiguration mappings = manager.getLanguageConfiguration(project);
59
if (mappings != null) {
60
// File-level mappings
61
if (filePath != null) {
63
String id = mappings.getLanguageForFile(configuration, filePath);
65
inheritedLanguages.add(new LanguageMapping(manager.getLanguage(id), FILE_MAPPING));
67
return createLanguageMappingArray(inheritedLanguages);
71
// Check for a file mapping that's global across all configurations in
73
if (configuration != null) {
74
id = mappings.getLanguageForFile(null, filePath);
76
inheritedLanguages.add(new LanguageMapping(manager.getLanguage(id), FILE_MAPPING));
78
return createLanguageMappingArray(inheritedLanguages);
85
// Project-level mappings
86
String id = mappings.getLanguageForContentType(configuration, contentTypeId);
88
inheritedLanguages.add(new LanguageMapping(manager.getLanguage(id), PROJECT_MAPPING));
90
return createLanguageMappingArray(inheritedLanguages);
94
// Check for a content type mapping that's global across all configurations in
96
if (configuration != null) {
97
id = mappings.getLanguageForContentType(null, contentTypeId);
99
inheritedLanguages.add(new LanguageMapping(manager.getLanguage(id), PROJECT_MAPPING));
101
return createLanguageMappingArray(inheritedLanguages);
108
// Workspace mappings
109
WorkspaceLanguageConfiguration workspaceMappings = manager.getWorkspaceLanguageConfiguration();
110
String id = workspaceMappings.getLanguageForContentType(contentTypeId);
112
inheritedLanguages.add(new LanguageMapping(manager.getLanguage(id), WORKSPACE_MAPPING));
114
return createLanguageMappingArray(inheritedLanguages);
119
IContentType contentType = Platform.getContentTypeManager().getContentType(contentTypeId);
120
inheritedLanguages.add(new LanguageMapping(manager.getLanguage(contentType), DEFAULT_MAPPING));
121
return createLanguageMappingArray(inheritedLanguages);
124
private static LanguageMapping[] createLanguageMappingArray(List<LanguageMapping> inheritedLanguages) {
125
LanguageMapping[] results = new LanguageMapping[inheritedLanguages.size()];
126
Iterator<LanguageMapping> mappings = inheritedLanguages.iterator();
128
while (mappings.hasNext()) {
129
LanguageMapping mapping = mappings.next();
130
results[i] = mapping;