blob: 3a5671ddc47098d34ffe5b1e48ee0aaa22528cdb [file] [log] [blame]
cbridghab4a2da02004-11-24 13:10:41 +00001/*******************************************************************************
2 * Copyright (c) 2003, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11/*
12 * Created on Apr 27, 2004
13 *
14 * To change the template for this generated file go to Window - Preferences -
15 * Java - Code Generation - Code and Comments
16 */
17package org.eclipse.wst.common.internal.emfworkbench.integration;
18
19import java.util.ArrayList;
20import java.util.Hashtable;
21import java.util.Iterator;
22import java.util.List;
23
24import org.eclipse.core.runtime.CoreException;
25import org.eclipse.core.runtime.IConfigurationElement;
26import org.eclipse.core.runtime.IExtension;
27import org.eclipse.core.runtime.IExtensionPoint;
cbridghab4a2da02004-11-24 13:10:41 +000028import org.eclipse.core.runtime.Platform;
29import org.eclipse.emf.common.util.EList;
30import org.eclipse.emf.ecore.EClass;
31import org.eclipse.emf.ecore.EClassifier;
32import org.eclipse.emf.ecore.EPackage;
33import org.eclipse.emf.ecore.EStructuralFeature;
cbridgha0f16a052005-03-18 21:39:33 +000034import org.eclipse.jem.util.logger.proxy.Logger;
cbridghab4a2da02004-11-24 13:10:41 +000035
36/**
37 * @author jsholl
38 *
39 */
40public class ModifierHelperRegistry {
41 private static final String PLUGIN_ID = "org.eclipse.wst.common.internal.emfworkbench.integration"; //$NON-NLS-1$
42 private static final String EXTENSION_POINT = "ModifierHelperFactory"; //$NON-NLS-1$
43 private static final String FACTORY_CLASS = "class"; //$NON-NLS-1$
44 private static final String PACKAGE = "package"; //$NON-NLS-1$
45 private static final String PACKAGE_URI = "uri"; //$NON-NLS-1$
46 private static final String NAME = "name"; //$NON-NLS-1$
47 private static final String TYPE = "type"; //$NON-NLS-1$
48 private static final String FEATURE = "feature"; //$NON-NLS-1$
49 private static final String FEATURE_ACTION = "action"; //$NON-NLS-1$
50 private static final String FEATURE_ACTION_SET = "set"; //$NON-NLS-1$
51 private static final String FEATURE_ACTION_UNSET = "unset"; //$NON-NLS-1$
52 private static final String FEATURE_ACTION_BOTH = "both"; //default //$NON-NLS-1$
53 private static ModifierHelperRegistry INSTANCE = null;
54 // Hashtable mapping features to a list of FactoryHolders
55 private Hashtable featureHash = new Hashtable();
56 private Hashtable factoryHash = new Hashtable();
57
58 private class FactoryHolder {
59 private int actionType;
60 private IConfigurationElement element;
61
62 public FactoryHolder(IConfigurationElement element, int actionType) {
63 this.element = element;
64 this.actionType = actionType;
65 }
66
67 public ModifierHelperFactory getFactory(int actionTypeArg) {
68 if (this.actionType == actionTypeArg || this.actionType == ModifierHelper.ACTION_BOTH) {
69 String hashKey = getFactoryHash(element);
70 ModifierHelperFactory factory = (ModifierHelperFactory) factoryHash.get(hashKey);
71 if (null == factory) {
72 try {
73 factory = (ModifierHelperFactory) element.createExecutableExtension(FACTORY_CLASS);
74 factoryHash.put(hashKey, factory);
75 } catch (CoreException e) {
76 Logger.getLogger().logError(e);
77 }
78 }
79 return factory;
80 }
81 return null;
82 }
83
84 public boolean equals(Object obj) {
85 if (super.equals(obj)) {
86 return true;
87 }
88 FactoryHolder holder = (FactoryHolder) obj;
89 return getFactoryHash(element).equals(getFactoryHash(holder.element));
90 }
91 }
92
93 private ModifierHelperRegistry() {
94 readExtensions();
95 }
96
97 private void readExtensions() {
vbhadrir10e15b92005-08-23 19:20:16 +000098 IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(PLUGIN_ID, EXTENSION_POINT);
cbridghab4a2da02004-11-24 13:10:41 +000099 if (point == null)
100 return;
101 IConfigurationElement[] elements = point.getConfigurationElements();
102 for (int i = 0; i < elements.length; i++) {
103 readFactory(elements[i]);
104 }
105 }
106
107 private void readFactory(IConfigurationElement element) {
108 String factoryClassName = element.getAttribute(FACTORY_CLASS);
109 if (null == factoryClassName) {
110 logError(element, "No " + FACTORY_CLASS + " defined."); //$NON-NLS-1$ //$NON-NLS-2$
111 }
112 IConfigurationElement[] packages = element.getChildren(PACKAGE);
113 if (packages.length == 0) {
114 logError(element, "No " + PACKAGE + " defined."); //$NON-NLS-1$ //$NON-NLS-2$
115 }
116 for (int j = 0; j < packages.length; j++) {
117 readPackage(element, packages[j]);
118 }
119 }
120
121 private void readPackage(IConfigurationElement factoryElement, IConfigurationElement element) {
122 String packageURI = element.getAttribute(PACKAGE_URI);
123 if (null == packageURI) {
124 logError(element, "No " + PACKAGE_URI + " defined."); //$NON-NLS-1$ //$NON-NLS-2$
125 return;
126 }
127 EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(packageURI);
128 if (null == ePackage) {
129 logError(element, PACKAGE + " " + packageURI + " can not be found."); //$NON-NLS-1$ //$NON-NLS-2$
130 return;
131 }
132 IConfigurationElement[] types = element.getChildren(TYPE);
133 if (types.length == 0) {
134 logError(element, "No " + TYPE + " defined."); //$NON-NLS-1$ //$NON-NLS-2$
135 }
136 for (int i = 0; i < types.length; i++) {
137 readType(factoryElement, ePackage, types[i]);
138 }
139 }
140
141 private void readType(IConfigurationElement factoryElement, EPackage ePackage, IConfigurationElement element) {
142 String typeName = element.getAttribute(NAME);
143 if (null == typeName) {
144 logError(element, "No " + NAME + " defined."); //$NON-NLS-1$ //$NON-NLS-2$
145 return;
146 }
147 EClassifier eClassifier = ePackage.getEClassifier(typeName);
148 if (null == eClassifier) {
149 logError(element, TYPE + " " + typeName + " can not be found in " + PACKAGE + " " + ePackage.getName()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
150 return;
151 }
152 EClass eClass = (EClass) eClassifier;
153 IConfigurationElement[] features = element.getChildren(FEATURE);
154 if (features.length == 0) {
155 logError(element, "No " + FEATURE + " defined."); //$NON-NLS-1$ //$NON-NLS-2$
156 return;
157 }
158 for (int i = 0; i < features.length; i++) {
159 readFeature(factoryElement, eClass, features[i]);
160 }
161 }
162
163 private void readFeature(IConfigurationElement factoryElement, EClass eClass, IConfigurationElement element) {
164 String featureName = element.getAttribute(NAME);
165 if (null == featureName) {
166 logError(element, "No " + NAME + " defined."); //$NON-NLS-1$ //$NON-NLS-2$
167 return;
168 }
169 String action = element.getAttribute(FEATURE_ACTION);
170 if (null == action) {
171 action = FEATURE_ACTION_BOTH;
172 }
173 int actionType = -1;
174 if (action.equalsIgnoreCase(FEATURE_ACTION_BOTH)) {
175 actionType = ModifierHelper.ACTION_BOTH;
176 } else if (action.equalsIgnoreCase(FEATURE_ACTION_SET)) {
177 actionType = ModifierHelper.ACTION_SET;
178 } else if (action.equalsIgnoreCase(FEATURE_ACTION_UNSET)) {
179 actionType = ModifierHelper.ACTION_UNSET;
180 }
181 if (actionType == -1) {
182 logError(element, "Invalid " + FEATURE_ACTION + "=" + action + " defined. Valid values are: " + FEATURE_ACTION_BOTH + ", " + FEATURE_ACTION_SET + ", or " + FEATURE_ACTION_UNSET); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
183 return;
184 }
185 EStructuralFeature feature = null;
186 EList allFeatures = eClass.getEAllStructuralFeatures();
187 EStructuralFeature tempFeature = null;
188 Iterator iterator = allFeatures.iterator();
189 while (null == feature && iterator.hasNext()) {
190 tempFeature = (EStructuralFeature) iterator.next();
191 if (tempFeature.getName().equals(featureName)) {
192 feature = tempFeature;
193 }
194 }
195 if (feature == null) {
196 logError(element, FEATURE + " " + featureName + " can not be found in " + TYPE + " " + eClass.getName()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
197 return;
198 }
199 List factoryHolderList = (List) featureHash.get(feature);
200 if (null == factoryHolderList) {
201 factoryHolderList = new ArrayList();
202 featureHash.put(feature, factoryHolderList);
203 }
204 FactoryHolder factoryHolder = new FactoryHolder(factoryElement, actionType);
205 if (factoryHolderList.contains(factoryHolder)) {
206 logError(element, "Duplicate" + FEATURE + ":" + featureName + " defined for " + FACTORY_CLASS + ":" + factoryElement.getAttribute(FACTORY_CLASS)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
207 return;
208 }
209 factoryHolderList.add(factoryHolder);
210 }
211
212 private String getFactoryHash(IConfigurationElement factoryElement) {
vbhadrir10e15b92005-08-23 19:20:16 +0000213 return factoryElement.getDeclaringExtension().getNamespace() + factoryElement.getAttribute(FACTORY_CLASS);
cbridghab4a2da02004-11-24 13:10:41 +0000214 }
215
216 public static void logError(IConfigurationElement element, String text) {
217 IExtension extension = element.getDeclaringExtension();
cbridghab4a2da02004-11-24 13:10:41 +0000218 StringBuffer buf = new StringBuffer();
vbhadrir10e15b92005-08-23 19:20:16 +0000219 buf.append("Plugin " + extension.getNamespace() + ", extension " + extension.getExtensionPointUniqueIdentifier()); //$NON-NLS-1$ //$NON-NLS-2$
cbridghab4a2da02004-11-24 13:10:41 +0000220 buf.append("\n" + text); //$NON-NLS-1$
221 Logger.getLogger().logError(buf.toString());
222 }
223
224 public static ModifierHelperRegistry getInstance() {
225 if (null == INSTANCE) {
226 INSTANCE = new ModifierHelperRegistry();
227 }
228 return INSTANCE;
229 }
230
231 /**
232 * returns a list of ModifierHelpers
233 *
234 * @param baseHelper
235 * @param actionFlag
236 * @return
237 */
238 public List getHelpers(ModifierHelper baseHelper) {
239 int actionFlag = baseHelper.shouldUnsetValue() ? ModifierHelper.ACTION_UNSET : ModifierHelper.ACTION_SET;
240 EStructuralFeature feature = baseHelper.getFeature();
241 List factoryList = getFactories(feature, actionFlag);
242 if (null == factoryList) {
243 return null;
244 }
245 ArrayList helpers = new ArrayList();
246 Iterator it = factoryList.iterator();
247 ModifierHelperFactory factory = null;
248 while (it.hasNext()) {
249 factory = (ModifierHelperFactory) it.next();
250 Object helper = factory.getHelper(baseHelper, actionFlag);
251 if (null != helper) {
252 helpers.add(helper);
253 }
254 }
255 return helpers;
256 }
257
258 private List getFactories(EStructuralFeature feature, int actionFlag) {
259 List factoryHolderList = (List) featureHash.get(feature);
260 if (null == factoryHolderList) {
261 return null;
262 }
263 List factoryList = new ArrayList();
264 ModifierHelperFactory factory = null;
265 for (int i = 0; i < factoryHolderList.size(); i++) {
266 factory = ((FactoryHolder) factoryHolderList.get(i)).getFactory(actionFlag);
267 if (null != factory) {
268 factoryList.add(factory);
269 }
270 }
271 return factoryList;
272 }
273}