1 11 12 package org.eclipse.team.internal.core; 13 14 import java.io.*; 15 import java.util.*; 16 17 import org.eclipse.core.resources.IStorage; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.core.runtime.content.IContentType; 20 import org.eclipse.core.runtime.content.IContentTypeManager; 21 import org.eclipse.team.core.*; 22 23 26 public class FileContentManager implements IFileContentManager { 27 28 private static final String PREF_TEAM_EXTENSION_TYPES= "file_types"; private static final String PREF_TEAM_FILENAME_TYPES= "cvs_mode_for_file_without_extensions"; 31 private static class StringMapping implements IStringMapping { 32 33 private final String fString; 34 private final int fType; 35 36 public StringMapping(String string, int type) { 37 fString= string; 38 fType= type; 39 } 40 41 public String getString() { 42 return fString; 43 } 44 45 public int getType() { 46 return fType; 47 } 48 } 49 50 private static class UserExtensionMappings extends UserStringMappings { 51 52 public UserExtensionMappings(String key) { 53 super(key); 54 } 55 56 protected Map loadMappingsFromPreferences() { 57 final Map result= super.loadMappingsFromPreferences(); 58 if (loadMappingsFromOldWorkspace(result)) { 59 TeamPlugin.getPlugin().savePluginPreferences(); 60 } 61 return result; 62 } 63 64 73 private boolean loadMappingsFromOldWorkspace(Map map) { 74 String STATE_FILE = ".fileTypes"; IPath pluginStateLocation = TeamPlugin.getPlugin().getStateLocation().append(STATE_FILE); 77 File f = pluginStateLocation.toFile(); 78 79 if (!f.exists()) 80 return false; 81 82 try { 83 DataInputStream input = new DataInputStream(new FileInputStream(f)); 84 try { 85 map.putAll(readOldFormatExtensionMappings(input)); 86 } finally { 87 input.close(); 88 f.delete(); 89 } 90 } catch (IOException ex) { 91 TeamPlugin.log(IStatus.ERROR, ex.getMessage(), ex); 92 return false; 93 } 94 return true; 95 } 96 97 103 private Map readOldFormatExtensionMappings(DataInputStream input) throws IOException { 104 final Map result= new TreeMap(); 105 int numberOfMappings = 0; 106 try { 107 numberOfMappings = input.readInt(); 108 } catch (EOFException e) { 109 return Collections.EMPTY_MAP; 112 } 113 for (int i = 0; i < numberOfMappings; i++) { 114 final String extension = input.readUTF(); 115 final int type = input.readInt(); 116 result.put(extension, new Integer (type)); 117 } 118 return result; 119 } 120 } 121 122 private final UserStringMappings fUserExtensionMappings, fUserNameMappings; 123 private PluginStringMappings fPluginExtensionMappings; private IContentType textContentType; 125 126 public FileContentManager() { 127 fUserExtensionMappings= new UserExtensionMappings(PREF_TEAM_EXTENSION_TYPES); 128 fUserNameMappings= new UserStringMappings(PREF_TEAM_FILENAME_TYPES); 129 fPluginExtensionMappings= new PluginStringMappings(TeamPlugin.FILE_TYPES_EXTENSION, "extension"); } 131 132 public int getTypeForName(String filename) { 133 final int userType= fUserNameMappings.getType(filename); 134 return userType; 137 } 138 139 public int getTypeForExtension(String extension) { 140 final int userType= fUserExtensionMappings.getType(extension); 141 final int pluginType= fPluginExtensionMappings.getType(extension); 142 return userType != Team.UNKNOWN ? userType : pluginType; 143 } 144 145 public void addNameMappings(String [] names, int [] types) { 146 fUserNameMappings.addStringMappings(names, types); 147 } 148 149 public void addExtensionMappings(String [] extensions, int [] types) { 150 fUserExtensionMappings.addStringMappings(extensions, types); 151 } 152 153 public void setNameMappings(String [] names, int [] types) { 154 fUserNameMappings.setStringMappings(names, types); 155 } 156 157 public void setExtensionMappings(String [] extensions, int [] types) { 158 fUserExtensionMappings.setStringMappings(extensions, types); 159 } 160 161 public IStringMapping[] getNameMappings() { 162 return getMappings(fUserNameMappings, null); } 164 165 public IStringMapping[] getExtensionMappings() { 166 return getMappings(fUserExtensionMappings, fPluginExtensionMappings); 167 } 168 169 public int getType(IStorage storage) { 170 int type; 171 172 final String name= storage.getName(); 173 if (name != null && (type= getTypeForName(name)) != Team.UNKNOWN) 174 return type; 175 176 final String extension= getFileExtension(name); 177 if (extension != null && (type= getTypeForExtension(extension)) != Team.UNKNOWN) 178 return type; 179 180 IContentType contentType = Platform.getContentTypeManager().findContentTypeFor(name); 181 if (contentType != null) { 182 IContentType textType = getTextContentType(); 183 if (contentType.isKindOf(textType)) { 184 return Team.TEXT; 185 } 186 } 187 188 return Team.UNKNOWN; 189 } 190 191 private IContentType getTextContentType() { 192 if (textContentType == null) 193 textContentType = Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT); 194 return textContentType; 195 } 196 197 public IStringMapping[] getDefaultNameMappings() { 198 return new IStringMapping[0]; } 201 202 public IStringMapping[] getDefaultExtensionMappings() { 203 return getStringMappings(fPluginExtensionMappings.referenceMap()); 204 } 205 206 public boolean isKnownExtension(String extension) { 207 return fUserExtensionMappings.referenceMap().containsKey(extension) 208 || fPluginExtensionMappings.referenceMap().containsKey(extension); 209 } 210 211 public boolean isKnownFilename(String filename) { 212 return fUserNameMappings.referenceMap().containsKey(filename); 213 } 215 216 private static String getFileExtension(String name) { 217 if (name == null) 218 return null; 219 int index = name.lastIndexOf('.'); 220 if (index == -1) 221 return null; 222 if (index == (name.length() - 1)) 223 return ""; return name.substring(index + 1); 225 } 226 227 private static IStringMapping [] getStringMappings(Map map) { 228 final IStringMapping [] result= new IStringMapping [map.size()]; 229 int index= 0; 230 for (final Iterator iter = map.entrySet().iterator(); iter.hasNext();) { 231 final Map.Entry entry= (Map.Entry)iter.next(); 232 result[index++]= new StringMapping((String )entry.getKey(), ((Integer )entry.getValue()).intValue()); 233 } 234 return result; 235 } 236 237 private IStringMapping [] getMappings(UserStringMappings userMappings, PluginStringMappings pluginMappings) { 238 final Map mappings= new HashMap(); 239 if (pluginMappings != null) 240 mappings.putAll(pluginMappings.referenceMap()); 241 mappings.putAll(userMappings.referenceMap()); 242 return getStringMappings(mappings); 243 } 244 } 245 | Popular Tags |