1 11 package org.eclipse.ui.internal.ide.registry; 12 13 import java.util.Arrays ; 14 import java.util.Collections ; 15 import java.util.Comparator ; 16 import java.util.Hashtable ; 17 18 import org.eclipse.core.runtime.IConfigurationElement; 19 import org.eclipse.core.runtime.IExtension; 20 import org.eclipse.core.runtime.IExtensionPoint; 21 import org.eclipse.core.runtime.IExtensionRegistry; 22 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 23 24 38 public abstract class IDERegistryReader { 39 protected static final String TAG_DESCRIPTION = "description"; 41 protected static Hashtable extensionPoints = new Hashtable (); 42 43 private static final Comparator comparer = new Comparator () { 44 public int compare(Object arg0, Object arg1) { 45 IExtension i1 = (IExtension) arg0; 46 String s1 = i1.getNamespace(); 47 IExtension i2 = (IExtension) arg1; 48 String s2 = i2.getNamespace(); 49 return s1.compareToIgnoreCase(s2); 50 } 51 }; 52 53 56 protected IDERegistryReader() { 57 } 58 59 65 protected String getDescription(IConfigurationElement config) { 66 IConfigurationElement[] children = config.getChildren(TAG_DESCRIPTION); 67 if (children.length >= 1) { 68 return children[0].getValue(); 69 } 70 return ""; } 72 73 77 protected void logError(IConfigurationElement element, String text) { 78 IExtension extension = element.getDeclaringExtension(); 79 String pluginId = extension.getNamespace(); 80 StringBuffer buf = new StringBuffer (); 81 buf.append("Plugin " + pluginId + ", extension " + extension.getExtensionPointUniqueIdentifier()); 83 buf.append("\n" + text); IDEWorkbenchPlugin.log(buf.toString()); 85 } 86 87 90 protected void logMissingAttribute(IConfigurationElement element, 91 String attributeName) { 92 logError(element, 93 "Required attribute '" + attributeName + "' not defined"); } 95 96 99 protected void logMissingElement(IConfigurationElement element, 100 String elementName) { 101 logError(element, 102 "Required sub element '" + elementName + "' not defined"); } 104 105 108 protected void logUnknownElement(IConfigurationElement element) { 109 logError(element, "Unknown extension tag found: " + element.getName()); } 111 112 117 protected IExtension[] orderExtensions(IExtension[] extensions) { 118 IExtension[] sortedExtension = new IExtension[extensions.length]; 123 System.arraycopy(extensions, 0, sortedExtension, 0, extensions.length); 124 Collections.sort(Arrays.asList(sortedExtension), comparer); 125 return sortedExtension; 126 } 127 128 137 protected abstract boolean readElement(IConfigurationElement element); 138 139 144 protected void readElementChildren(IConfigurationElement element) { 145 readElements(element.getChildren()); 146 } 147 148 154 protected void readElements(IConfigurationElement[] elements) { 155 for (int i = 0; i < elements.length; i++) { 156 if (!readElement(elements[i])) { 157 logUnknownElement(elements[i]); 158 } 159 } 160 } 161 162 166 protected void readExtension(IExtension extension) { 167 readElements(extension.getConfigurationElements()); 168 } 169 170 174 protected void readRegistry(IExtensionRegistry registry, String pluginId, 175 String extensionPoint) { 176 String pointId = pluginId + "-" + extensionPoint; IExtension[] extensions = (IExtension[]) extensionPoints.get(pointId); 178 if (extensions == null) { 179 IExtensionPoint point = registry.getExtensionPoint(pluginId, 180 extensionPoint); 181 if (point == null) { 182 return; 183 } 184 extensions = point.getExtensions(); 185 extensions = orderExtensions(extensions); 186 extensionPoints.put(pointId, extensions); 187 } 188 for (int i = 0; i < extensions.length; i++) { 189 readExtension(extensions[i]); 190 } 191 } 192 } 193 | Popular Tags |