Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4ba1333068378132f50a0f122df4df84d0a533af (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
/*******************************************************************************
 * Copyright (c) 2010, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.bidi.internal;

import java.util.*;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.bidi.StructuredTextTypeHandlerFactory;
import org.eclipse.equinox.bidi.custom.StructuredTextTypeHandler;
import org.eclipse.equinox.bidi.internal.consumable.*;

/**
 * Provides services related to registered structured text handlers.
 */
public class StructuredTextTypesCollector implements IRegistryEventListener {

	private static final String EXT_POINT = "org.eclipse.equinox.bidi.bidiTypes"; //$NON-NLS-1$

	private static final String CE_NAME = "typeDescription"; //$NON-NLS-1$
	private static final String ATTR_TYPE = "type"; //$NON-NLS-1$
	private static final String ATTR_HANDLER = "class"; //$NON-NLS-1$

	private Map<String, StructuredTextTypeHandler> types;
	private Map<String, IConfigurationElement> factories;

	static private StructuredTextTypesCollector instance = new StructuredTextTypesCollector();

	private StructuredTextTypesCollector() {
		IExtensionRegistry registry = RegistryFactory.getRegistry();
		if (registry != null)
			registry.addListener(this, EXT_POINT);
	}

	/**
	 * @return a static instance.
	 */
	static public StructuredTextTypesCollector getInstance() {
		return instance;
	}

	/**
	 * @return a list of all the registered structured text handler types.
	 */
	public String[] getTypes() {
		if (types == null)
			read();
		int size = types.size();
		String[] result = new String[size];
		types.keySet().toArray(result);
		return result;
	}

	/**
	 * @param type the identifier for a structured text handler.
	 * 
	 * @return the structured text handler instance.
	 */
	public StructuredTextTypeHandler getHandler(String type) {
		if (types == null)
			read();
		Object handler = types.get(type);
		if (handler instanceof StructuredTextTypeHandler)
			return (StructuredTextTypeHandler) handler;
		return null;
	}

	private void read() {
		if (types == null)
			types = new HashMap<>();
		else
			types.clear();

		if (factories == null)
			factories = new HashMap<String, IConfigurationElement>();
		else
			factories.clear();

		IExtensionRegistry registry = RegistryFactory.getRegistry();
		if (registry == null) {
			types.putAll(getDefaultTypeHandlers());
			return;
		}

		IExtensionPoint extPoint = registry.getExtensionPoint(EXT_POINT);
		IExtension[] extensions = extPoint.getExtensions();

		for (int i = 0; i < extensions.length; i++) {
			IConfigurationElement[] confElements = extensions[i].getConfigurationElements();
			for (int j = 0; j < confElements.length; j++) {
				if (!CE_NAME.equals(confElements[j].getName()))
					StructuredTextActivator.logError("BiDi types: unexpected element name " + confElements[j].getName(), new IllegalArgumentException()); //$NON-NLS-1$
				String type = confElements[j].getAttribute(ATTR_TYPE);
				Object handler;
				try {
					handler = confElements[j].createExecutableExtension(ATTR_HANDLER);
				} catch (CoreException e) {
					StructuredTextActivator.logError("BiDi types: unable to create handler for " + type, e); //$NON-NLS-1$
					continue;
				}
				if (handler instanceof StructuredTextTypeHandler) {
					types.put(type, (StructuredTextTypeHandler) handler);
					factories.put(type, confElements[j]);
				}
			}
		}
	}

	public void added(IExtension[] extensions) {
		types = null;
		factories = null;
	}

	public void removed(IExtension[] extensions) {
		types = null;
		factories = null;
	}

	public void added(IExtensionPoint[] extensionPoints) {
		types = null;
		factories = null;
	}

	public void removed(IExtensionPoint[] extensionPoints) {
		types = null;
		factories = null;
	}

	/**
	 * Returns the default structured text type handlers. These handlers are
	 * also supported without OSGi running.
	 * 
	 * @return a map from structured text type handler identifier (key type: {@link String})
	 *         to structured text type handler (value type: {@link StructuredTextTypeHandler}).
	 */
	public static Map<String, StructuredTextTypeHandler> getDefaultTypeHandlers() {
		Map<String, StructuredTextTypeHandler> types = new LinkedHashMap<String, StructuredTextTypeHandler>();

		types.put(StructuredTextTypeHandlerFactory.COMMA_DELIMITED, new StructuredTextComma());
		types.put(StructuredTextTypeHandlerFactory.EMAIL, new StructuredTextEmail());
		types.put(StructuredTextTypeHandlerFactory.FILE, new StructuredTextFile());
		types.put(StructuredTextTypeHandlerFactory.JAVA, new StructuredTextJava());
		types.put(StructuredTextTypeHandlerFactory.REGEX, new StructuredTextRegex());
		types.put(StructuredTextTypeHandlerFactory.SQL, new StructuredTextSql());
		types.put(StructuredTextTypeHandlerFactory.UNDERSCORE, new StructuredTextUnderscore());
		types.put(StructuredTextTypeHandlerFactory.URL, new StructuredTextURL());
		types.put(StructuredTextTypeHandlerFactory.XPATH, new StructuredTextXPath());

		return Collections.unmodifiableMap(types);
	}
}

Back to the top