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: 7aaa7395651c34cecafdbac1f6926f3b29dbd267 (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
/*******************************************************************************
 * Copyright (c) 2001, 2007 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.ui.internal;

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

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.wst.sse.core.utils.StringUtils;
import org.eclipse.wst.sse.ui.internal.extension.RegistryReader;
import org.osgi.framework.Bundle;


/**
 * Simple generic ID to class to mapping. Loads a specified class defined in a
 * configuration element with the matching type and target ID. Example
 * plugin.xml section:
 * 
 * <extension
 * point="org.eclipse.wst.sse.ui.editorConfiguration">contentoutlineconfiguration
 * target="org.eclipse.wst.sse.dtd.core.dtdsource"
 * class="org.eclipse.wst.sse.ui.dtd.views.contentoutline.DTDContentOutlineConfiguration"/>
 * </extension>
 * 
 * Used in code by getConfiguration("contentoutlineconfiguration",
 * "org.eclipse.wst.dtd.ui.StructuredTextEditorDTD");
 * 
 */
public class ExtendedConfigurationBuilder extends RegistryReader {
	/**
	 * Extension type to pass into getConfigurations to get content outline
	 * configuration
	 */
	public static final String CONTENTOUTLINECONFIGURATION = "contentOutlineConfiguration"; //$NON-NLS-1$
	/**
	 * Extension type to pass into getConfigurations to get property sheet
	 * configuration
	 */
	public static final String PROPERTYSHEETCONFIGURATION = "propertySheetConfiguration"; //$NON-NLS-1$
	/**
	 * Extension type to pass into getConfigurations to get source viewer
	 * configuration
	 */
	public static final String SOURCEVIEWERCONFIGURATION = "sourceViewerConfiguration"; //$NON-NLS-1$
	/**
	 * Extension type to pass into getConfigurations to get documentation text
	 * hover
	 */
	public static final String DOCUMENTATIONTEXTHOVER = "documentationTextHover"; //$NON-NLS-1$
	/**
	 * Extension type to pass into getConfigurations to get double click
	 * strategy
	 */
	public static final String DOUBLECLICKSTRATEGY = "doubleClickStrategy"; //$NON-NLS-1$

	private static final String ATT_CLASS = "class"; //$NON-NLS-1$
	private static final String ATT_TARGET = "target"; //$NON-NLS-1$
	private static final String ATT_TYPE = "type"; //$NON-NLS-1$
	private static final String CONFIGURATION = "provisionalConfiguration"; //$NON-NLS-1$
	private static Map configurationMap = null;
	private final static boolean debugTime = "true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.wst.sse.ui/extendedconfigurationbuilder/time")); //$NON-NLS-1$  //$NON-NLS-2$
	private static final String DEFINITION = "provisionalDefinition"; //$NON-NLS-1$
	private static final String EP_EXTENDEDCONFIGURATION = "editorConfiguration"; //$NON-NLS-1$
	private static ExtendedConfigurationBuilder instance = null;
	public static final String VALUE = "value"; //$NON-NLS-1$

	/**
	 * Creates an extension. If the extension plugin has not been loaded a
	 * busy cursor will be activated during the duration of the load.
	 * 
	 * @param element
	 *            the config element defining the extension
	 * @param classAttribute
	 *            the name of the attribute carrying the class
	 * @returns the extension object if successful. If an error occurs when
	 *          createing executable extension, the exception is logged, and
	 *          null returned.
	 */
	static Object createExtension(final IConfigurationElement element, final String classAttribute, final String targetID) {
		final Object[] result = new Object[1];
		String pluginId = element.getDeclaringExtension().getNamespace();
		Bundle bundle = Platform.getBundle(pluginId);
		if (bundle.getState() == Bundle.ACTIVE) {
			try {
				result[0] = element.createExecutableExtension(classAttribute);
			}
			catch (Exception e) {
				// catch and log ANY exception while creating the extension
				Logger.logException("error loading class " + classAttribute + " for " + targetID, e); //$NON-NLS-1$ //$NON-NLS-2$
			}
		}
		else {
			BusyIndicator.showWhile(null, new Runnable() {
				public void run() {
					try {
						result[0] = element.createExecutableExtension(classAttribute);
					}
					catch (Exception e) {
						// catch and log ANY exception from extension point
						Logger.logException("error loading class " + classAttribute + " for " + targetID, e); //$NON-NLS-1$ //$NON-NLS-2$
					}
				}
			});
		}
		return result[0];
	}

	public synchronized static ExtendedConfigurationBuilder getInstance() {
		if (instance == null)
			instance = new ExtendedConfigurationBuilder();
		return instance;
	}

	long time0 = 0;

	private ExtendedConfigurationBuilder() {
		super();
	}

	private List createConfigurations(List configurations, String extensionType, String targetID) {
		if (configurations == null)
			return new ArrayList(0);
		List result = new ArrayList(1);
		for (int i = 0; i < configurations.size(); i++) {
			IConfigurationElement element = (IConfigurationElement) configurations.get(i);
			if ((element.getName().equals(extensionType) || (element.getName().equals(CONFIGURATION) && extensionType.equals(element.getAttribute(ATT_TYPE))))) {
				String[] targets = StringUtils.unpack(element.getAttribute(ATT_TARGET));
				for (int j = 0; j < targets.length; j++) {
					if (targetID.equals(targets[j].trim())) {
						Object o = createExtension(element, ATT_CLASS, targetID);
						if (o != null) {
							result.add(o);
						}
					}
				}
			}
		}
		return result;
	}

	private IConfigurationElement[] findConfigurationElements(List configurations, String extensionType, String targetID) {
		if (configurations == null)
			return new IConfigurationElement[0];
		List result = new ArrayList(1);
		for (int i = 0; i < configurations.size(); i++) {
			IConfigurationElement element = (IConfigurationElement) configurations.get(i);
			if ((element.getName().equals(extensionType) || (element.getName().equals(DEFINITION) && extensionType.equals(element.getAttribute(ATT_TYPE))))) {
				String[] targets = StringUtils.unpack(element.getAttribute(ATT_TARGET));
				for (int j = 0; j < targets.length; j++) {
					if (targetID.equals(targets[j].trim())) {
						result.add(element);
					}
				}
			}
		}
		return (IConfigurationElement[]) result.toArray(new IConfigurationElement[0]);
	}

	/**
	 * Returns a configuration for the given extensionType matching the
	 * targetID, if one is available. If more than one configuration is
	 * defined, the first one found is returned.
	 * 
	 * @param extensionType
	 * @param targetID
	 * @return a configuration object, if one was defined
	 */
	public Object getConfiguration(String extensionType, String targetID) {
		if (targetID == null || targetID.length() == 0)
			return null;
		List configurations = getConfigurations(extensionType, targetID);
		if (configurations.isEmpty())
			return null;
		return configurations.get(0);
	}

	/**
	 * Returns all configurations for the given extensionType matching the
	 * targetID, if any are available.
	 * 
	 * @param extensionType
	 * @param targetID
	 * @return a List of configuration objects, which may or may not be empty
	 */
	public List getConfigurations(String extensionType, String targetID) {
		if (targetID == null || targetID.length() == 0)
			return new ArrayList(0);
		if (configurationMap == null) {
			configurationMap = new HashMap(0);
			synchronized (configurationMap) {
				readRegistry(Platform.getExtensionRegistry(), SSEUIPlugin.ID, EP_EXTENDEDCONFIGURATION);
				if (debugTime) {
					System.out.println(getClass().getName() + "#readRegistry():  " + (System.currentTimeMillis() - time0) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
					time0 = System.currentTimeMillis();
				}
			}
		}
		List extensions = (List) configurationMap.get(extensionType);
		List configurations = createConfigurations(extensions, extensionType, targetID);
		if (debugTime) {
			if (!configurations.isEmpty())
				System.out.println(getClass().getName() + "#getConfiguration(" + extensionType + ", " + targetID + "): configurations loaded in " + (System.currentTimeMillis() - time0) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			else
				System.out.println(getClass().getName() + "#getConfiguration(" + extensionType + ", " + targetID + "): ran in " + (System.currentTimeMillis() - time0) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		}
		return configurations;
	}

	/**
	 * Returns all declared definitions for the given extensionType matching
	 * the targetID, if any are available.
	 * 
	 * @param extensionType
	 * @param targetID
	 * @return An array containing the definitions, empty if none were
	 *         declared
	 */
	public String[] getDefinitions(String extensionType, String targetID) {
		if (targetID == null || targetID.length() == 0)
			return new String[0];
		if (debugTime) {
			time0 = System.currentTimeMillis();
		}
		if (configurationMap == null) {
			configurationMap = new HashMap(0);
			synchronized (configurationMap) {
				readRegistry(Platform.getExtensionRegistry(), SSEUIPlugin.ID, EP_EXTENDEDCONFIGURATION);
				if (debugTime) {
					System.out.println(getClass().getName() + "#readRegistry():  " + (System.currentTimeMillis() - time0) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
					time0 = System.currentTimeMillis();
				}
			}
		}
		List definitions = (List) configurationMap.get(extensionType);
		IConfigurationElement[] elements = findConfigurationElements(definitions, extensionType, targetID);
		String[] values = new String[elements.length];
		for (int i = 0; i < values.length; i++) {
			values[i] = elements[i].getAttribute(VALUE);
		}
		if (debugTime) {
			if (values.length > 0)
				System.out.println(getClass().getName() + "#getDefinitions(" + extensionType + ", " + targetID + "): definition loaded in " + (System.currentTimeMillis() - time0) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			else
				System.out.println(getClass().getName() + "#getDefinitions(" + extensionType + ", " + targetID + "): ran in " + (System.currentTimeMillis() - time0) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		}
		return values;
	}

	protected boolean readElement(IConfigurationElement element) {
		String name = element.getName();
		if (name.equals(CONFIGURATION) || name.equals(DEFINITION))
			name = element.getAttribute(ATT_TYPE);
		List configurations = (List) configurationMap.get(name);
		if (configurations == null) {
			configurations = new ArrayList(1);
			configurationMap.put(name, configurations);
		}
		configurations.add(element);
		return true;
	}
}

Back to the top