Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4b1bd582b35dd46d0161930da95bdb0613ba4278 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.core.internal.filebuffers;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.content.IContentDescription;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;

import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.IAnnotationModelFactory;
import org.eclipse.core.filebuffers.IDocumentFactory;
import org.eclipse.core.filebuffers.IDocumentSetupParticipant;

import org.eclipse.jface.text.Assert;


/**
 * This registry manages sharable document factories. Document factories are specified 
 * in <code>plugin.xml</code> per file name extension.
 */
public class ExtensionsRegistry {
	
	/**
	 * Adapts IContentType with the ability to check
	 * equality. This allows to use them in a collection.
	 */
	private static class ContentTypeAdapter {
		
		/** The adapted content type. */
		private IContentType fContentType;

		/**
		 * Creates a new content type adapter for the
		 * given content type.
		 * 
		 * @param contentType the content type to be adapted
		 */
		public ContentTypeAdapter(IContentType  contentType) {
			Assert.isNotNull(contentType);
			fContentType= contentType;
		}
		
		/**
		 * Return the adapted content type.
		 * 
		 * @return the content type
		 */
		public IContentType getContentType() {
			return fContentType;
		}

		/**
		 * Return the Id of the adapted content type.
		 * 
		 * @return the Id
		 */
		public String getId() {
			return fContentType.getId();
		}

		/*
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		public boolean equals(Object obj) {
			return obj instanceof ContentTypeAdapter && fContentType.getId().equals(((ContentTypeAdapter)obj).getId());
		}
		
		/*
		 * @see java.lang.Object#hashCode()
		 */
		public int hashCode() {
			return fContentType.getId().hashCode();
		}
	}
	
	private final static String WILDCARD= "*";  //$NON-NLS-1$
	
	/** The mapping between file attributes and configuration elements describing document factories. */
	private Map fFactoryDescriptors= new HashMap();
	/** The mapping between configuration elements for document factories and instantiated document factories. */
	private Map fFactories= new HashMap();
	/** The mapping between file attributes and configuration elements describing document setup participants. */
	private Map fSetupParticipantDescriptors= new HashMap();
	/** The mapping between configuration elements for setup participants and instantiated setup participants. */
	private Map fSetupParticipants= new HashMap();
	/** The mapping between file attributes and configuration elements describing annotation model factories. */
	private Map fAnnotationModelFactoryDescriptors= new HashMap();
	/** The mapping between configuration elements for annotation model factories */
	private Map fAnnotationModelFactories= new HashMap();
	/** The content type manager. */
	private IContentTypeManager fContentTypeManager= Platform.getContentTypeManager();
	
	
	/**
	 * Creates a new document factory registry and initializes it with the information
	 * found in the plug-in registry.
	 */
	public ExtensionsRegistry() {
		initialize("documentCreation", "contentTypeId", true,  fFactoryDescriptors); //$NON-NLS-1$ //$NON-NLS-2$
		initialize("documentCreation", "fileNames", false, fFactoryDescriptors); //$NON-NLS-1$ //$NON-NLS-2$
		initialize("documentCreation", "extensions",  false, fFactoryDescriptors); //$NON-NLS-1$ //$NON-NLS-2$
		initialize("documentSetup", "contentTypeId", true, fSetupParticipantDescriptors); //$NON-NLS-1$ //$NON-NLS-2$
		initialize("documentSetup", "fileNames", false, fSetupParticipantDescriptors); //$NON-NLS-1$ //$NON-NLS-2$
		initialize("documentSetup", "extensions", false, fSetupParticipantDescriptors); //$NON-NLS-1$ //$NON-NLS-2$
		initialize("annotationModelCreation", "contentTypeId", true, fAnnotationModelFactoryDescriptors); //$NON-NLS-1$ //$NON-NLS-2$
		initialize("annotationModelCreation", "fileNames", false, fAnnotationModelFactoryDescriptors); //$NON-NLS-1$ //$NON-NLS-2$
		initialize("annotationModelCreation", "extensions", false, fAnnotationModelFactoryDescriptors); //$NON-NLS-1$ //$NON-NLS-2$
		
	}
	
	/**
	 * Reads the comma-separated value from the given configuration element for the given attribute name and remembers
	 * the configuration element in the given map under the individual tokens of the attribute value.
	 * 
	 * @param attributeName the name of the attribute
	 * @param element the configuration element
	 * @param map the map which remembers the configuration element 
	 */
	private void read(String attributeName, IConfigurationElement element, Map map) {
		String value= element.getAttribute(attributeName);
		if (value != null) {
			StringTokenizer tokenizer= new StringTokenizer(value, ","); //$NON-NLS-1$
			while (tokenizer.hasMoreTokens()) {
				String token= tokenizer.nextToken().trim();
				
				Set s= (Set) map.get(token);
				if (s == null) {
					s= new HashSet();
					map.put(token, s);
				}
				s.add(element);
			}
		}
	}
	
	/**
	 * Reads the value from the given configuration element for the given attribute name and remembers
	 * the configuration element in the given map under the individual content type of the attribute value.
	 * 
	 * @param attributeName the name of the attribute
	 * @param element the configuration element
	 * @param map the map which remembers the configuration element 
	 */
	private void readContentType(String attributeName, IConfigurationElement element, Map map) {
		String value= element.getAttribute(attributeName);
		if (value != null) {
			IContentType contentType= fContentTypeManager.getContentType(value);
			if (contentType == null) {
				log(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, 0, FileBuffersMessages.getFormattedString("ExtensionsRegistry.error.contentTypeDoesNotExist", new Object[] { value }), null)); //$NON-NLS-1$
				return;
			}
			ContentTypeAdapter adapter= new ContentTypeAdapter(contentType);
			Set s= (Set) map.get(adapter);
			if (s == null) {
				s= new HashSet();
				map.put(adapter, s);
			}
			s.add(element);
		}
	}
	
	/**
	 * Adds an entry to the log of this plug-in for the given status
	 * @param status the status to log
	 */
	private void log(IStatus status) {
		ILog log= FileBuffersPlugin.getDefault().getLog();
		log.log(status);
	}
	
	/**
	 * Initializes this registry. It retrieves all implementers of the given
	 * extension point and remembers those implementers based on the
	 * file name extensions in the given map.
	 * 
	 * @param extensionPointName the name of the extension point
	 * @param childElementName the name of the child elements
	 * @param isContentTypeId the child element is a content type id
	 * @param descriptors the map to be filled 
	 */
	private void initialize(String extensionPointName, String childElementName, boolean isContentTypeId, Map descriptors) {
		
		IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(FileBuffersPlugin.PLUGIN_ID, extensionPointName);
		if (extensionPoint == null) {
			log(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, 0, FileBuffersMessages.getFormattedString("ExtensionsRegistry.error.extensionPointNotFound", new Object[] { extensionPointName}), null)); //$NON-NLS-1$
			return;
		}
		
		IConfigurationElement[] elements= extensionPoint.getConfigurationElements();
		for (int i= 0; i < elements.length; i++) {
			if (isContentTypeId)
				readContentType(childElementName, elements[i], descriptors);
			else
				read(childElementName, elements[i], descriptors);
		}
	}
	
	/**
	 * Returns the executable extension for the given configuration element.
	 * If there is no instantiated extension remembered for this
	 * element, a new extension is created and put into the cache if it is of the requested type.
	 * 
	 * @param entry the configuration element
	 * @param extensions the map of instantiated extensions
	 * @param extensionType the requested result type
	 * @return the executable extension for the given configuration element.
	 */
	private Object getExtension(IConfigurationElement entry, Map extensions, Class extensionType) {
		Object extension= extensions.get(entry);
		if (extension != null)
			return extension;
			
		try {
			extension= entry.createExecutableExtension("class"); //$NON-NLS-1$
		} catch (CoreException x) {
			log(x.getStatus());
		}
		
		if (extensionType.isInstance(extension)) {
			extensions.put(entry, extension);
			return extension;
		}
		
		return null;
	}
	
	/**
	 * Returns the first enumerated element of the given set.
	 * 
	 * @param set the set from which to choose
	 * @return the selected configuration element
	 */
	private IConfigurationElement selectConfigurationElement(Set set) {
		if (set != null && !set.isEmpty()) {
			Iterator e= set.iterator();
			return (IConfigurationElement) e.next();
		}
		return null;
	}

	/**
	 * Returns a sharable document factory for the given file name or file extension.
	 *
	 * @param nameOrExtension the name or extension to be used for lookup
	 * @return the sharable document factory or <code>null</code>
	 */
	private IDocumentFactory getDocumentFactory(String nameOrExtension) {
		Set set= (Set) fFactoryDescriptors.get(nameOrExtension);
		if (set != null) {
			IConfigurationElement entry= selectConfigurationElement(set);
			return (IDocumentFactory) getExtension(entry, fFactories, IDocumentFactory.class);
		}
		return null;
	}
	
	/**
	 * Returns a sharable document factory for the given content types.
	 *
	 * @param contentTypes the content types used to find the factory
	 * @return the sharable document factory or <code>null</code>
	 */
	private IDocumentFactory getDocumentFactory(IContentType[] contentTypes) {
		Set set= null;
		int i= 0;
		while (i < contentTypes.length && set == null) {
			set= (Set) fFactoryDescriptors.get(new ContentTypeAdapter(contentTypes[i++]));
		}

		if (set != null) {
			IConfigurationElement entry= selectConfigurationElement(set);
			return (IDocumentFactory) getExtension(entry, fFactories, IDocumentFactory.class);
		}
		return null;
	}
	
	/**
	 * Returns the set of setup participants for the given file name or extension.
	 * 
	 * @param nameOrExtension the name or extension to be used for lookup
	 * @return the sharable set of document setup participants
	 */
	private List getDocumentSetupParticipants(String nameOrExtension) {
		Set set= (Set) fSetupParticipantDescriptors.get(nameOrExtension);
		if (set == null)
			return null;
		
		List participants= new ArrayList();
		Iterator e= set.iterator();
		while (e.hasNext()) {
			IConfigurationElement entry= (IConfigurationElement) e.next();
			Object participant= getExtension(entry, fSetupParticipants, IDocumentSetupParticipant.class);
			if (participant != null)
				participants.add(participant);
		}
		
		return participants;
	}
	
	/**
	 * Returns the set of setup participants for the given content types.
	 * 
	 * @param contentTypes the contentTypes to be used for lookup
	 * @return the sharable set of document setup participants
	 */
	private List getDocumentSetupParticipants(IContentType[] contentTypes) {
		Set resultSet= new HashSet();
		int i= 0;
		while (i < contentTypes.length) {
			Set set= (Set) fFactoryDescriptors.get(new ContentTypeAdapter(contentTypes[i++]));
			if (set != null)
				resultSet.addAll(set);
		}
		
		List participants= new ArrayList();
		Iterator e= resultSet.iterator();
		while (e.hasNext()) {
			IConfigurationElement entry= (IConfigurationElement) e.next();
			Object participant= getExtension(entry, fSetupParticipants, IDocumentSetupParticipant.class);
			if (participant != null)
				participants.add(participant);
		}
		
		return participants;
	}
	
	/**
	 * Returns a sharable annotation model factory for the given content types.
	 *
	 * @param contentTypes the content types used to find the factory
	 * @return the sharable annotation model factory or <code>null</code>
	 */
	private IAnnotationModelFactory getAnnotationModelFactory(IContentType[] contentTypes) {
		Set set= null;
		int i= 0;
		while (i < contentTypes.length && set == null) {
			set= (Set) fAnnotationModelFactoryDescriptors.get(new ContentTypeAdapter(contentTypes[i++]));
		}

		if (set != null) {
			IConfigurationElement entry= selectConfigurationElement(set);
			return (IAnnotationModelFactory) getExtension(entry, fAnnotationModelFactories, IAnnotationModelFactory.class);
		}
		return null;
	}
	
	/**
	 * Returns a sharable annotation model factory for the given file name or file extension.
	 *
	 * @param extension the name or extension to be used for lookup
	 * @return the sharable document factory or <code>null</code>
	 */
	private IAnnotationModelFactory getAnnotationModelFactory(String extension) {
		Set set= (Set) fAnnotationModelFactoryDescriptors.get(extension);
		if (set != null) {
			IConfigurationElement entry= selectConfigurationElement(set);
			return (IAnnotationModelFactory) getExtension(entry, fAnnotationModelFactories, IAnnotationModelFactory.class);
		}
		return null;
	}
	
	private IContentType[] findContentTypes(IPath path) {
		IFile file= FileBuffers.getWorkspaceFileAtLocation(path);
		if (file == null)
			return new IContentType[0];
		
		IContentDescription contentDescription;
		try {
			contentDescription= file.getContentDescription();
		} catch (CoreException ex) {
			contentDescription= null;
		}
		
		if (contentDescription != null) {
			IContentType contentType= contentDescription.getContentType();
			if (contentType != null)
				return new IContentType[] {contentType};
		}
		
		return fContentTypeManager.findContentTypesFor(path.lastSegment());
	}
	
	/**
	 * Returns the sharable document factory for the given location.
	 *
	 * @param location the location for which to looked up the factory
	 * @return the sharable document factory
	 */
	public IDocumentFactory getDocumentFactory(IPath location) {
		IDocumentFactory factory= getDocumentFactory(findContentTypes(location));
		if (factory == null)
			factory= getDocumentFactory(location.lastSegment());
		if (factory == null)
			factory= getDocumentFactory(location.getFileExtension());
		if (factory == null)
			factory= getDocumentFactory(WILDCARD);
		return factory;
	}
	
	/**
	 * Returns the sharable set of document setup participants for the given location.
	 * 
	 * @param location the location for which to look up the setup participants
	 * @return the sharable set of document setup participants
	 */
	public IDocumentSetupParticipant[] getDocumentSetupParticipants(IPath location) {
		List participants= new ArrayList();
		
		List p= getDocumentSetupParticipants(findContentTypes(location));
		if (p != null)
			participants.addAll(p);
		
		p= getDocumentSetupParticipants(location.lastSegment());
		if (p != null)
			participants.addAll(p);
		
		p= getDocumentSetupParticipants(location.getFileExtension());
		if (p != null)
			participants.addAll(p);
			
		p= getDocumentSetupParticipants(WILDCARD);
		if (p != null)
			participants.addAll(p);
			
		IDocumentSetupParticipant[] result= new IDocumentSetupParticipant[participants.size()];
		participants.toArray(result);
		return result;
	}
	
	/**
	 * Returns the sharable annotation model factory for the given location.
	 * 
	 * @param location the location for which to look up the factory
	 * @return the sharable annotation model factory
	 */
	public IAnnotationModelFactory getAnnotationModelFactory(IPath location) {
		IAnnotationModelFactory factory= getAnnotationModelFactory(findContentTypes(location));
		if (factory == null)
			factory= getAnnotationModelFactory(location.lastSegment());
		if (factory == null)
			factory= getAnnotationModelFactory(location.getFileExtension());
		if (factory == null)
			factory= getAnnotationModelFactory(WILDCARD);
		return factory;
	}
}

Back to the top