Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3197bdc6e3ab89c0911491b455909b11f847defc (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
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.team.internal.core;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.team.core.Team;

/**
 *
 */
public class PluginStringMappings {

    private final String fExtensionID;
    private final String fAttributeName;

    private SortedMap<String, Integer> fMappings;

    public PluginStringMappings(String extensionID, String stringAttributeName) {
        fExtensionID= extensionID;
        fAttributeName= stringAttributeName;
    }

    /**
     * Load all the extension patterns contributed by plugins.
     * @return a map with the patterns
     */
    private SortedMap<String, Integer> loadPluginPatterns() {

        final SortedMap<String, Integer> result= new TreeMap<>();

        final TeamPlugin plugin = TeamPlugin.getPlugin();
        if (plugin == null)
            return result;

        final IExtensionPoint extension = Platform.getExtensionRegistry().getExtensionPoint(TeamPlugin.ID, fExtensionID);//TeamPlugin.FILE_TYPES_EXTENSION);
        if (extension == null)
            return result;

        final IExtension[] extensions =  extension.getExtensions();

        for (int i = 0; i < extensions.length; i++) {
            IConfigurationElement[] configElements = extensions[i].getConfigurationElements();

            for (int j = 0; j < configElements.length; j++) {

                final String ext = configElements[j].getAttribute(fAttributeName);//"extension");
                final String type = configElements[j].getAttribute("type"); //$NON-NLS-1$
                if (ext == null || type == null)
                    continue;

                if (type.equals("text")) { //$NON-NLS-1$
                    result.put(ext, Integer.valueOf(Team.TEXT));
                } else if (type.equals("binary")) { //$NON-NLS-1$
                    result.put(ext, Integer.valueOf(Team.BINARY));
                }
            }
        }
        return result;
    }

    public Map<String, Integer> referenceMap() {
        if (fMappings == null) {
            fMappings= loadPluginPatterns();
        }
        return fMappings;
    }

    public int getType(String filename) {
        final Map<String, Integer> mappings= referenceMap();
        return mappings.containsKey(filename) ? mappings.get(filename).intValue() : Team.UNKNOWN;
    }
}

Back to the top