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: 3c0f99647df2a0ab3554db7fc01b6d4f454f3701 (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
/*******************************************************************************
 * 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.modelhandler;

import java.util.List;
import java.util.Vector;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.wst.sse.core.INodeAdapterFactory;
import org.eclipse.wst.sse.core.internal.SSECorePlugin;
import org.eclipse.wst.sse.core.internal.ltk.modelhandler.IDocumentTypeHandler;


/**
 * 
 * Clients can make use of IExecutableExtension to handle the optional adapter
 * class and key. Typically, many clients use a typical pattern of providing
 * an adapter class and key in their null argument constructor anyway, so
 * they'd only have to use IExecutableExtension if the factory was for more
 * than one.
 */
public class PluginContributedFactoryReader {
	//	protected final String ATTR_ADAPTERKEY = "adapterKeyClass";
	// //$NON-NLS-1$
	//	protected final String ATTR_REGISTERADAPTER = "registerAdapters";
	// //$NON-NLS-1$
	private static PluginContributedFactoryReader reader = null;

	public synchronized static PluginContributedFactoryReader getInstance() {
		if (reader == null) {
			reader = new PluginContributedFactoryReader();
		}
		return reader;
	}

	protected final String ATTR_CLASS = "class"; //$NON-NLS-1$
	protected final String ATTR_CONTENTTYPE = "contentTypeIdentiferId"; //$NON-NLS-1$

	protected final String EXTENSION_POINT_ID = "contentTypeFactoryContribution"; //$NON-NLS-1$
	protected final String TAG_NAME = "factory"; //$NON-NLS-1$

	protected PluginContributedFactoryReader() {
		super();
	}

	public List getFactories(IDocumentTypeHandler handler) {
		return loadRegistry(handler.getId());
	}

	public List getFactories(String type) {
		return loadRegistry(type);
	}

	protected INodeAdapterFactory loadFactoryFromConfigurationElement(IConfigurationElement element, Object requesterType) {
		INodeAdapterFactory factory = null;
		if (element.getName().equals(TAG_NAME)) {
			String contentType = element.getAttribute(ATTR_CONTENTTYPE);
			if (!requesterType.equals(contentType))
				return null;
			String className = element.getAttribute(ATTR_CLASS);
			//			String adapterKeyClass = element.getAttribute(ATTR_ADAPTERKEY);
			//			String registerAdapters =
			// element.getAttribute(ATTR_REGISTERADAPTER);

			// if className is null, then no one defined the extension point
			// for adapter factories
			if (className != null) {
				try {
					factory = (INodeAdapterFactory) element.createExecutableExtension(ATTR_CLASS);
				} catch (CoreException e) {
					// if an error occurs here, its probably that the plugin
					// could not be found/loaded
					org.eclipse.wst.sse.core.internal.Logger.logException("Could not find class: " + className, e); //$NON-NLS-1$
				} catch (Exception e) {
					// if an error occurs here, its probably that the plugin
					// could not be found/loaded -- but in any case we just
					// want
					// to log the error and continue running and best we can.
					org.eclipse.wst.sse.core.internal.Logger.logException("Could not find class: " + className, e); //$NON-NLS-1$
				}
				//				if (plugin != null) {
				//					factory = oldAttributesCode(element, factory, className,
				// plugin);
				//
			}
		}

		return factory;
	}

	protected List loadRegistry(Object contentType) {
		List factoryList = null; // new Vector();
		IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
		IExtensionPoint point = extensionRegistry.getExtensionPoint(SSECorePlugin.ID, EXTENSION_POINT_ID);
		if (point != null) {
			IConfigurationElement[] elements = point.getConfigurationElements();
			if (elements.length > 0) {
				// this is called a lot, so don't create vector unless really
				// needed
				// TODO: could eventually cache in a hashtable, or something,
				// to avoid repeat processing
				factoryList = new Vector();
				for (int i = 0; i < elements.length; i++) {
					INodeAdapterFactory factory = loadFactoryFromConfigurationElement(elements[i], contentType);
					if (factory != null)
						factoryList.add(factory);
				}
			}
		}
		return factoryList;
	}
}

Back to the top