Skip to main content
summaryrefslogtreecommitdiffstats
blob: af4e33cca14ba0870c2e5cf6da65e303ada73ad5 (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
/*******************************************************************************
 * Copyright (c) 2007, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.core.internal.filebuffers;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.content.IContentDescription;
import org.eclipse.core.runtime.content.IContentType;

import org.eclipse.core.resources.IFile;

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


/**
 * This is a special {@link ExtensionsRegistry} that is
 * optimized for <code>IFile<code>s..
 *
 * @since 3.3
 */
public class ResourceExtensionRegistry extends ExtensionsRegistry {

	/**
	 * Returns the set of content types for the given location.
	 *
	 * @param location the location for which to look up the content types
	 * @param locationKind the kind of the given location
	 * @return the set of content types for the location
	 */
	@Override
	protected IContentType[] findContentTypes(IPath location, LocationKind locationKind) {
		if (locationKind != LocationKind.LOCATION) {
			IFile file= FileBuffers.getWorkspaceFileAtLocation(location);
			if (file != null)
				return findContentTypes(file);
		}
		return fContentTypeManager.findContentTypesFor(location.lastSegment());
	}

	/**
	 * Returns the sharable document factory for the given file.
	 *
	 * @param file the file for which to looked up the factory
	 * @return the sharable document factory
	 * @deprecated As of 3.5
	 */
	@Deprecated
	org.eclipse.core.filebuffers.IDocumentFactory getDocumentFactory(IFile file) {
		org.eclipse.core.filebuffers.IDocumentFactory factory= getDocumentFactory(findContentTypes(file));
		if (factory == null) {
			factory= getDocumentFactory(file.getFullPath().lastSegment());
		}
		if (factory == null)
			factory= getDocumentFactory(file.getFileExtension());
		if (factory == null)
			factory= getDocumentFactory(WILDCARD);
		return factory;
	}

	/**
	 * Returns the sharable annotation model factory for the given file.
	 *
	 * @param file the file for which to look up the factory
	 * @return the sharable annotation model factory
	 */
	IAnnotationModelFactory getAnnotationModelFactory(IFile file) {
		IAnnotationModelFactory factory= getAnnotationModelFactory(findContentTypes(file));
		if (factory == null)
			factory= getAnnotationModelFactory(file.getFullPath().lastSegment());
		if (factory == null)
			factory= getAnnotationModelFactory(file.getFileExtension());
		if (factory == null)
			factory= getAnnotationModelFactory(WILDCARD);
		return factory;
	}

	/**
	 * Returns the set of content types for the given location.
	 *
	 * @param file the file for which to look up the content types
	 * @return the set of content types for the location
	 */
	private IContentType[] findContentTypes(IFile file) {
		try {
			IContentDescription contentDescription= file.getContentDescription();
			if (contentDescription != null) {
				IContentType contentType= contentDescription.getContentType();
				if (contentType != null)
					return new IContentType[] {contentType};
			}
		} catch (CoreException x) {
			// go for the default
		}
		return fContentTypeManager.findContentTypesFor(file.getFullPath().lastSegment());
	}

	/**
	 * Returns the sharable set of document setup participants for the given file.
	 *
	 * @param file the file for which to look up the setup participants
	 * @return the sharable set of document setup participants
	 */
	IDocumentSetupParticipant[] getDocumentSetupParticipants(IFile file) {
		Set<IDocumentSetupParticipant> participants= new HashSet<>();

		List<IDocumentSetupParticipant> p= getDocumentSetupParticipants(findContentTypes(file));
		if (p != null)
			participants.addAll(p);

		p= getDocumentSetupParticipants(file.getFullPath().lastSegment());
		if (p != null)
			participants.addAll(p);

		p= getDocumentSetupParticipants(file.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;
	}

}

Back to the top