Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 69b13b7742799e79d54dc9d2c030817ce9b00487 (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
/*******************************************************************************
 * Copyright (c) 2001, 2004 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.sse.core.internal.tasks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.wst.sse.core.internal.Logger;
import org.eclipse.wst.sse.core.internal.SSECorePlugin;
import org.eclipse.wst.sse.core.internal.provisional.tasks.IFileTaskScanner;
import org.eclipse.wst.sse.core.internal.util.StringUtils;

public class FileTaskScannerRegistryReader {
	private class ScannerInfo {
		String fId;
		IFileTaskScanner fScanner;

		ScannerInfo(String id, IFileTaskScanner scanner) {
			super();
			fId = id;
			fScanner = scanner;
		}

		public boolean equals(Object obj) {
			return obj instanceof ScannerInfo && fId.equals(((ScannerInfo) obj).fId);
		}

		public String getId() {
			return fId;
		}

		public IFileTaskScanner getScanner() {
			return fScanner;
		}
	}

	private static FileTaskScannerRegistryReader _instance = null;

	public static FileTaskScannerRegistryReader getInstance() {
		if (_instance == null) {
			_instance = new FileTaskScannerRegistryReader();
		}
		return _instance;
	}

	private String ATT_CLASS = "class"; //$NON-NLS-1$

	private String ATT_CONTENT_TYPES = "contentTypeIds"; //$NON-NLS-1$

	private String ATT_ID = "id"; //$NON-NLS-1$

	private IConfigurationElement[] fScannerElements;

	// a mapping from content types to ScannerInfo instances
	private Map fScannerInfos = null;

	private String NAME_SCANNER = "scanner"; //$NON-NLS-1$

	private String SCANNER_EXTENSION_POINT_ID = SSECorePlugin.ID + ".taskscanner"; //$NON-NLS-1$

	private FileTaskScannerRegistryReader() {
		super();
	}

	IFileTaskScanner[] getFileTaskScanners(IContentType[] contentTypes) {
		if (fScannerElements == null) {
			readRegistry();
		}

		List scannerInfos = new ArrayList(1);

		for (int i = 0; i < contentTypes.length; i++) {
			ScannerInfo[] scannerInfosForContentType = (ScannerInfo[]) fScannerInfos.get(contentTypes[i].getId());
			if (scannerInfosForContentType == null) {
				scannerInfosForContentType = loadScanners(contentTypes[i]);
			}
			// only add non-duplicate scanners
			for (int j = 0; j < scannerInfosForContentType.length; j++) {
				if (!scannerInfos.contains(scannerInfosForContentType[j])) {
					scannerInfos.add(scannerInfosForContentType[j]);
				}
			}
		}
		IFileTaskScanner[] scanners = new IFileTaskScanner[scannerInfos.size()];
		for (int i = 0; i < scanners.length; i++) {
			scanners[i] = ((ScannerInfo) scannerInfos.get(i)).getScanner();
		}
		return scanners;
	}

	public String[] getSupportedContentTypeIds() {
		if (fScannerElements == null) {
			readRegistry();
		}

		// find the relevant extensions
		List types = new ArrayList(0);
		IConfigurationElement[] scannerElements = fScannerElements;
		for (int j = 0; j < scannerElements.length; j++) {
			if (!scannerElements[j].getName().equals(NAME_SCANNER))
				continue;
			String[] contentTypeIds = StringUtils.unpack(scannerElements[j].getAttribute(ATT_CONTENT_TYPES));
			for (int i = 0; i < contentTypeIds.length; i++) {
				if (!types.contains(contentTypeIds[i])) {
					types.add(contentTypeIds[i]);
				}
			}
		}

		return (String[]) types.toArray(new String[types.size()]);
	}

	private ScannerInfo[] loadScanners(IContentType contentType) {
		List elements = new ArrayList(0);
		ScannerInfo[] scannerInfos = null;
		IConfigurationElement[] delegateElements = fScannerElements;
		if (contentType != null) {
			IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
			for (int j = 0; j < delegateElements.length; j++) {
				if (!delegateElements[j].getName().equals(NAME_SCANNER))
					continue;
				String[] supportedContentTypeIds = StringUtils.unpack(delegateElements[j].getAttribute(ATT_CONTENT_TYPES));
				IContentType[] supportedContentTypes = new IContentType[supportedContentTypeIds.length];
				for (int k = 0; k < supportedContentTypeIds.length; k++) {
					supportedContentTypes[k] = contentTypeManager.getContentType(supportedContentTypeIds[k].trim());
				}
				for (int k = 0; k < supportedContentTypeIds.length; k++) {
					// allow subtypes to be returned as well
					if (supportedContentTypes[k] != null && contentType.isKindOf(supportedContentTypes[k])) {
						elements.add(delegateElements[j]);
					}
				}
			}
			// instantiate and save the scanners
			List scannerInfoList = new ArrayList(elements.size());
			for (int i = 0; i < elements.size(); i++) {
				try {
					IFileTaskScanner scanner = (IFileTaskScanner) ((IConfigurationElement) elements.get(i)).createExecutableExtension(ATT_CLASS);
					if (scanner != null) {
						scannerInfoList.add(new ScannerInfo(((IConfigurationElement) elements.get(i)).getAttribute(ATT_ID), scanner));
					}
				}
				catch (CoreException e) {
					Logger.logException("Non-fatal exception creating task scanner for " + contentType.getId(), e); //$NON-NLS-1$
				}
			}
			scannerInfos = (ScannerInfo[]) scannerInfoList.toArray(new ScannerInfo[scannerInfoList.size()]);
			fScannerInfos.put(contentType.getId(), scannerInfos);
			if (Logger.DEBUG_TASKSREGISTRY) {
				System.out.println("Created " + scannerInfos.length + " task scanner for " + contentType.getId()); //$NON-NLS-1$ //$NON-NLS-2$
			}
		}
		return scannerInfos;
	}

	private void readRegistry() {
		fScannerInfos = new HashMap();
		// Just remember the elements, so plugins don't have to be activated,
		// unless extension attributes match those of interest
		IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(SCANNER_EXTENSION_POINT_ID);
		if (point != null) {
			fScannerElements = point.getConfigurationElements();
		}
	}
}

Back to the top