KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > FileContentManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
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 /**
24  * TODO: implement extension point
25  */

26 public class FileContentManager implements IFileContentManager {
27     
28     private static final String JavaDoc PREF_TEAM_EXTENSION_TYPES= "file_types"; //$NON-NLS-1$
29
private static final String JavaDoc PREF_TEAM_FILENAME_TYPES= "cvs_mode_for_file_without_extensions"; //$NON-NLS-1$
30

31     private static class StringMapping implements IStringMapping {
32         
33         private final String JavaDoc fString;
34         private final int fType;
35         
36         public StringMapping(String JavaDoc string, int type) {
37             fString= string;
38             fType= type;
39         }
40
41         public String JavaDoc 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 JavaDoc 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         /**
65          * If the workspace is an old 2.0 one, read the old file and delete it.
66          *
67          * @param A map where the new mappings should be added.
68          *
69          * @return true if the workspace was a 2.0 one and the old mappings have
70          * been added to the map, false otherwise.
71          *
72          */

73         private boolean loadMappingsFromOldWorkspace(Map map) {
74             // File name of the persisted file type information
75
String JavaDoc STATE_FILE = ".fileTypes"; //$NON-NLS-1$
76
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         /**
98          * Read the saved file type state from the given input stream.
99          *
100          * @param input the input stream to read the saved state from
101          * @throws IOException if an I/O problem occurs
102          */

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                 // Ignore the exception, it will occur if there are no
110
// patterns stored in the state file.
111
return Collections.EMPTY_MAP;
112             }
113             for (int i = 0; i < numberOfMappings; i++) {
114                 final String JavaDoc extension = input.readUTF();
115                 final int type = input.readInt();
116                 result.put(extension, new Integer JavaDoc(type));
117             }
118             return result;
119         }
120     }
121     
122     private final UserStringMappings fUserExtensionMappings, fUserNameMappings;
123     private PluginStringMappings fPluginExtensionMappings;//, fPluginNameMappings;
124
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"); //$NON-NLS-1$
130
}
131     
132     public int getTypeForName(String JavaDoc filename) {
133         final int userType= fUserNameMappings.getType(filename);
134 // final int pluginType= fPluginNameMappings.getType(filename);
135
// return userType != Team.UNKNOWN ? userType : pluginType;
136
return userType;
137     }
138     
139     public int getTypeForExtension(String JavaDoc 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 JavaDoc[] names, int [] types) {
146         fUserNameMappings.addStringMappings(names, types);
147     }
148     
149     public void addExtensionMappings(String JavaDoc[] extensions, int [] types) {
150         fUserExtensionMappings.addStringMappings(extensions, types);
151     }
152     
153     public void setNameMappings(String JavaDoc[] names, int [] types) {
154         fUserNameMappings.setStringMappings(names, types);
155     }
156     
157     public void setExtensionMappings(String JavaDoc[] extensions, int [] types) {
158         fUserExtensionMappings.setStringMappings(extensions, types);
159     }
160     
161     public IStringMapping[] getNameMappings() {
162         return getMappings(fUserNameMappings, null);//fPluginNameMappings);
163
}
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 JavaDoc name= storage.getName();
173         if (name != null && (type= getTypeForName(name)) != Team.UNKNOWN)
174             return type;
175         
176         final String JavaDoc 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         // TODO: There is currently no extension point for this
199
return new IStringMapping[0];//getStringMappings(fPluginNameMappings.referenceMap());
200
}
201
202     public IStringMapping[] getDefaultExtensionMappings() {
203         return getStringMappings(fPluginExtensionMappings.referenceMap());
204     }
205
206     public boolean isKnownExtension(String JavaDoc extension) {
207         return fUserExtensionMappings.referenceMap().containsKey(extension)
208         || fPluginExtensionMappings.referenceMap().containsKey(extension);
209     }
210
211     public boolean isKnownFilename(String JavaDoc filename) {
212         return fUserNameMappings.referenceMap().containsKey(filename);
213 // || fPluginNameMappings.referenceMap().containsKey(filename);
214
}
215     
216     private static String JavaDoc getFileExtension(String JavaDoc 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 ""; //$NON-NLS-1$
224
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 JavaDoc)entry.getKey(), ((Integer JavaDoc)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