1 11 package org.eclipse.team.internal.ui.registry; 12 13 import java.util.Hashtable ; 14 15 import org.eclipse.core.runtime.*; 16 import org.eclipse.osgi.util.NLS; 17 import org.eclipse.swt.custom.BusyIndicator; 18 import org.eclipse.team.internal.ui.TeamUIMessages; 19 import org.eclipse.team.internal.ui.TeamUIPlugin; 20 import org.eclipse.team.internal.ui.Utils.Sorter; 21 import org.osgi.framework.Bundle; 22 23 public abstract class RegistryReader { 24 protected static final String TAG_DESCRIPTION = "description"; protected static Hashtable extensionPoints = new Hashtable (); 26 27 37 public static Object createExtension(final IConfigurationElement element, 38 final String classAttribute) throws CoreException { 39 try { 40 if (isActivated(element.getDeclaringExtension() 43 .getContributor().getName())) { 44 return element.createExecutableExtension(classAttribute); 45 } 46 final Object [] ret = new Object [1]; 47 final CoreException[] exc = new CoreException[1]; 48 BusyIndicator.showWhile(null, new Runnable () { 49 public void run() { 50 try { 51 ret[0] = element 52 .createExecutableExtension(classAttribute); 53 } catch (CoreException e) { 54 exc[0] = e; 55 } 56 } 57 }); 58 if (exc[0] != null) { 59 throw exc[0]; 60 } 61 return ret[0]; 62 63 } catch (CoreException core) { 64 throw core; 65 } catch (Exception e) { 66 throw new CoreException(new Status(IStatus.ERROR, TeamUIPlugin.ID, 67 IStatus.ERROR, NLS.bind(TeamUIMessages.RegistryReader_0, element.getNamespaceIdentifier(), element.getName()),e)); 68 } 69 } 70 71 private static boolean isActivated(String bundleId) { 72 return isActivated(Platform.getBundle(bundleId)); 73 } 74 75 private static boolean isActivated(Bundle bundle) { 76 return bundle != null && (bundle.getState() & (Bundle.ACTIVE | Bundle.STOPPING)) != 0; 77 } 78 79 82 protected RegistryReader() { 83 } 84 89 protected String getDescription(IConfigurationElement config) { 90 IConfigurationElement[] children = config.getChildren(TAG_DESCRIPTION); 91 if (children.length >= 1) { 92 return children[0].getValue(); 93 } 94 return ""; } 96 100 protected void logError(IConfigurationElement element, String text) { 101 IExtension extension = element.getDeclaringExtension(); 102 StringBuffer buf = new StringBuffer (); 103 buf.append("Plugin " + extension.getNamespaceIdentifier() + ", extension " + extension.getExtensionPointUniqueIdentifier()); buf.append("\n" + text); TeamUIPlugin.log(IStatus.ERROR, buf.toString(), null); 106 } 107 110 protected void logMissingAttribute(IConfigurationElement element, String attributeName) { 111 logError(element, "Required attribute '" + attributeName + "' not defined"); } 113 114 117 protected void logMissingElement(IConfigurationElement element, String elementName) { 118 logError(element, "Required sub element '" + elementName + "' not defined"); } 120 121 124 protected void logUnknownElement(IConfigurationElement element) { 125 logError(element, "Unknown extension tag found: " + element.getName()); } 127 131 protected IExtension[] orderExtensions(IExtension[] extensions) { 132 Sorter sorter = new Sorter() { 137 public boolean compare(Object extension1, Object extension2) { 138 String s1 = ((IExtension) extension1).getNamespaceIdentifier(); 139 String s2 = ((IExtension) extension2).getNamespaceIdentifier(); 140 return s2.compareToIgnoreCase(s1) > 0; 142 } 143 }; 144 145 Object [] sorted = sorter.sort(extensions); 146 IExtension[] sortedExtension = new IExtension[sorted.length]; 147 System.arraycopy(sorted, 0, sortedExtension, 0, sorted.length); 148 return sortedExtension; 149 } 150 157 protected abstract boolean readElement(IConfigurationElement element); 158 162 protected void readElementChildren(IConfigurationElement element) { 163 readElements(element.getChildren()); 164 } 165 171 protected void readElements(IConfigurationElement[] elements) { 172 for (int i = 0; i < elements.length; i++) { 173 if (!readElement(elements[i])) 174 logUnknownElement(elements[i]); 175 } 176 } 177 180 protected void readExtension(IExtension extension) { 181 readElements(extension.getConfigurationElements()); 182 } 183 190 public void readRegistry(IExtensionRegistry registry, String pluginId, String extensionPoint) { 191 String pointId = pluginId + "-" + extensionPoint; IExtension[] extensions = (IExtension[]) extensionPoints.get(pointId); 193 if (extensions == null) { 194 IExtensionPoint point = registry.getExtensionPoint(pluginId, extensionPoint); 195 if (point == null) 196 return; 197 extensions = point.getExtensions(); 198 extensionPoints.put(pointId, extensions); 199 } 200 for (int i = 0; i < extensions.length; i++) 201 readExtension(extensions[i]); 202 } 203 } 204 | Popular Tags |