Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4c7dd374f2f3436750f30935b1c54e0bbd3f860c (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
/*******************************************************************************
 * 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.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.team.core.IFileContentManager;
import org.eclipse.team.core.IStringMapping;
import org.eclipse.team.core.Team;

/**
 * TODO: implement extension point
 */
public class FileContentManager implements IFileContentManager {

	private static final String PREF_TEAM_EXTENSION_TYPES= "file_types"; //$NON-NLS-1$
	private static final String PREF_TEAM_FILENAME_TYPES= "cvs_mode_for_file_without_extensions"; //$NON-NLS-1$

	private static class StringMapping implements IStringMapping {

		private final String fString;
		private final int fType;

		public StringMapping(String string, int type) {
			fString= string;
			fType= type;
		}

		@Override
		public String getString() {
			return fString;
		}

		@Override
		public int getType() {
			return fType;
		}
	}

	private static class UserExtensionMappings extends UserStringMappings {

		public UserExtensionMappings(String key) {
			super(key);
		}

		@Override
		protected Map<String, Integer> loadMappingsFromPreferences() {
			final Map<String, Integer> result= super.loadMappingsFromPreferences();
			if (loadMappingsFromOldWorkspace(result)) {
				TeamPlugin.getPlugin().savePluginPreferences();
			}
			return result;
		}

		/**
		 * If the workspace is an old 2.0 one, read the old file and delete it.
		 *
		 * @param A map where the new mappings should be added.
		 *
		 * @return true if the workspace was a 2.0 one and the old mappings have
		 * been added to the map, false otherwise.
		 *
		 */
		private boolean loadMappingsFromOldWorkspace(Map<String, Integer> map) {
			// File name of the persisted file type information
			String STATE_FILE = ".fileTypes"; //$NON-NLS-1$
			IPath pluginStateLocation = TeamPlugin.getPlugin().getStateLocation().append(STATE_FILE);
			File f = pluginStateLocation.toFile();

			if (!f.exists())
				return false;

			try {
				DataInputStream input = new DataInputStream(new FileInputStream(f));
				try {
					map.putAll(readOldFormatExtensionMappings(input));
				} finally {
					input.close();
					f.delete();
				}
			} catch (IOException ex) {
				TeamPlugin.log(IStatus.ERROR, ex.getMessage(), ex);
				return false;
			}
			return true;
		}

		/**
		 * Read the saved file type state from the given input stream.
		 *
		 * @param input the input stream to read the saved state from
		 * @throws IOException if an I/O problem occurs
		 */
		private Map<String, Integer> readOldFormatExtensionMappings(DataInputStream input) throws IOException {
			final Map<String, Integer> result= new TreeMap<>();
			int numberOfMappings = 0;
			try {
				numberOfMappings = input.readInt();
			} catch (EOFException e) {
				// Ignore the exception, it will occur if there are no
				// patterns stored in the state file.
				return Collections.emptyMap();
			}
			for (int i = 0; i < numberOfMappings; i++) {
				final String extension = input.readUTF();
				final int type = input.readInt();
				result.put(extension, Integer.valueOf(type));
			}
			return result;
		}
	}

	private final UserStringMappings fUserExtensionMappings, fUserNameMappings;
	private PluginStringMappings fPluginExtensionMappings;//, fPluginNameMappings;
	private IContentType textContentType;

	public FileContentManager() {
		fUserExtensionMappings= new UserExtensionMappings(PREF_TEAM_EXTENSION_TYPES);
		fUserNameMappings= new UserStringMappings(PREF_TEAM_FILENAME_TYPES);
		fPluginExtensionMappings= new PluginStringMappings(TeamPlugin.FILE_TYPES_EXTENSION, "extension"); //$NON-NLS-1$
	}

	@Override
	public int getTypeForName(String filename) {
		final int userType= fUserNameMappings.getType(filename);
//        final int pluginType= fPluginNameMappings.getType(filename);
//        return userType != Team.UNKNOWN ? userType : pluginType;
		return userType;
	}

	@Override
	public int getTypeForExtension(String extension) {
		final int userType= fUserExtensionMappings.getType(extension);
		final int pluginType= fPluginExtensionMappings.getType(extension);
		return userType != Team.UNKNOWN ? userType : pluginType;
	}

	@Override
	public void addNameMappings(String[] names, int [] types) {
		fUserNameMappings.addStringMappings(names, types);
	}

	@Override
	public void addExtensionMappings(String[] extensions, int [] types) {
		fUserExtensionMappings.addStringMappings(extensions, types);
	}

	@Override
	public void setNameMappings(String[] names, int [] types) {
		fUserNameMappings.setStringMappings(names, types);
	}

	@Override
	public void setExtensionMappings(String[] extensions, int [] types) {
		fUserExtensionMappings.setStringMappings(extensions, types);
	}

	@Override
	public IStringMapping[] getNameMappings() {
		return getMappings(fUserNameMappings, null);//fPluginNameMappings);
	}

	@Override
	public IStringMapping[] getExtensionMappings() {
		return getMappings(fUserExtensionMappings, fPluginExtensionMappings);
	}

	@Override
	public int getType(IStorage storage) {
		int type;

		final String name= storage.getName();
		if (name != null && (type= getTypeForName(name)) != Team.UNKNOWN)
			return type;

		final String extension= getFileExtension(name);
		if (extension != null && (type= getTypeForExtension(extension)) != Team.UNKNOWN)
			return type;

		IContentType contentType = Platform.getContentTypeManager().findContentTypeFor(name);
		if (contentType != null) {
			IContentType textType = getTextContentType();
			if (contentType.isKindOf(textType)) {
				return Team.TEXT;
			}
		}

		return Team.UNKNOWN;
	}

	private IContentType getTextContentType() {
		if (textContentType == null)
			textContentType = Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT);
		return textContentType;
	}

	@Override
	public IStringMapping[] getDefaultNameMappings() {
		// TODO: There is currently no extension point for this
		return new IStringMapping[0];//getStringMappings(fPluginNameMappings.referenceMap());
	}

	@Override
	public IStringMapping[] getDefaultExtensionMappings() {
		return getStringMappings(fPluginExtensionMappings.referenceMap());
	}

	@Override
	public boolean isKnownExtension(String extension) {
		return fUserExtensionMappings.referenceMap().containsKey(extension)
		|| fPluginExtensionMappings.referenceMap().containsKey(extension);
	}

	@Override
	public boolean isKnownFilename(String filename) {
		return fUserNameMappings.referenceMap().containsKey(filename);
//        || fPluginNameMappings.referenceMap().containsKey(filename);
	}

	private static String getFileExtension(String name) {
		if (name == null)
			return null;
		int index = name.lastIndexOf('.');
		if (index == -1)
			return null;
		if (index == (name.length() - 1))
			return ""; //$NON-NLS-1$
		return name.substring(index + 1);
	}

	private static IStringMapping [] getStringMappings(Map map) {
		final IStringMapping [] result= new IStringMapping [map.size()];
		int index= 0;
		for (final Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
			final Map.Entry entry= (Map.Entry)iter.next();
			result[index++]= new StringMapping((String)entry.getKey(), ((Integer)entry.getValue()).intValue());
		}
		return result;
	}

	private IStringMapping [] getMappings(UserStringMappings userMappings, PluginStringMappings pluginMappings) {
		final Map<String, Integer> mappings= new HashMap<>();
		if (pluginMappings != null)
			mappings.putAll(pluginMappings.referenceMap());
		mappings.putAll(userMappings.referenceMap());
		return getStringMappings(mappings);
	}
}

Back to the top