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: b5d49d412d56bbeec3ac9d79002fa53d8581cbc2 (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) 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.ui.internal.provisional.registry;

import java.util.HashMap;

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.ui.internal.Logger;


/**
 * This class just converts what's in the plugins registry into a form more
 * easily useable by others, the ContentTypeRegistry.
 */
class AdapterFactoryRegistryReader {
	protected final static String ATT_CLASS = "class"; //$NON-NLS-1$

	protected final static String ATT_ID = "id"; //$NON-NLS-1$

	private static boolean DEBUG = false;
	protected final static String EXTENSION_POINT_ID = "adapterFactoryDescription"; //$NON-NLS-1$
	//
	protected final static String PLUGIN_ID = "org.eclipse.wst.sse.ui"; //$NON-NLS-1$
	protected final static String TAG_CONTENT_TYPE = "contentType"; //$NON-NLS-1$

	protected final static String TAG_NAME = "adapterFactoryDescription"; //$NON-NLS-1$

	public final static String UNKNOWN_CONTENT_TYPE = "unknown"; //$NON-NLS-1$

	/**
	 * adds configuration element to contentTypeId map [contentTypeId ->
	 * element2providerMap] | V [element -> provider]
	 * 
	 * NOTE: this doesn't create the provider yet, that must be done on demand
	 * and stored in the appropriate element2provider
	 * 
	 * @param map
	 * @param contentTypeId
	 * @param element
	 */
	private static void addElementForContentType(HashMap map, String contentTypeId, IConfigurationElement element) {

		Object o = map.get(contentTypeId);
		if (o == null) {
			HashMap element2provider = new HashMap();
			// don't create the executable extension yet
			element2provider.put(element, null);
			map.put(contentTypeId, element2provider);

			if (DEBUG)
				System.out.println("added " + element.getAttribute(ATT_CLASS) + ", but didn't create exec extension"); //$NON-NLS-1$ //$NON-NLS-2$
		} else {
			// add element to unknown list (not executable ext yet...)
			HashMap element2provider = (HashMap) o;
			element2provider.put(element, null);

			if (DEBUG)
				System.out.println("added " + element.getAttribute(ATT_CLASS) + " to unknown list, but didn't create exec extension"); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	/**
	 * the map passed in: [contentTypeId -> element2providerMap] | V [element ->
	 * provider]
	 * 
	 * @param element
	 * @param map
	 * @return
	 */
	protected static AdapterFactoryProvider readElement(IConfigurationElement element, HashMap map) {

		AdapterFactoryProvider adapterFactoryProvider = null;
		if (element.getName().equals(TAG_NAME)) {
			try {
				IConfigurationElement[] children = element.getChildren();
				boolean specifiedContentType = false;
				if (children != null && children.length > 0) {
					// content types are specified
					for (int i = 0; i < children.length; i++) {
						if (children[i].getName().equals(TAG_CONTENT_TYPE)) {
							// it's possible to have non-contentType childrent
							specifiedContentType = true;
							String contentType = children[i].getAttribute(ATT_ID);
							addElementForContentType(map, contentType, element);
						}
					}
				}
				if (!specifiedContentType) {
					// no content type association
					addElementForContentType(map, UNKNOWN_CONTENT_TYPE, element);
				}
			} catch (Exception e) {
				// if the provider throws any exception, just log and continue
				Logger.logException(e);
			}
		}
		return adapterFactoryProvider;
	}

	/**
	 * We simply require an 'add' method, of what ever it is we are to read
	 * into
	 */
	static void readRegistry(HashMap map) {
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint point = registry.getExtensionPoint(PLUGIN_ID, EXTENSION_POINT_ID);
		if (point != null) {
			IConfigurationElement[] elements = point.getConfigurationElements();
			for (int i = 0; i < elements.length; i++) {
				readElement(elements[i], map);
			}
		}
	}

	protected IConfigurationElement configElement = null;

	//    protected final static String ADAPTER_CLASS = "adapterClass";
	// //$NON-NLS-1$
	//    protected final static String DOC_TYPE_ID = "docTypeId"; //$NON-NLS-1$
	//    protected final static String MIME_TYPE_LIST = "mimeTypeList";
	// //$NON-NLS-1$
	//
	/**
	 * ContentTypeRegistryReader constructor comment.
	 */
	AdapterFactoryRegistryReader() {
		super();
	}
}

Back to the top