1 11 package org.eclipse.ui.internal.registry; 12 13 import java.util.Arrays ; 14 import java.util.Collections ; 15 import java.util.Comparator ; 16 17 import org.eclipse.core.runtime.IConfigurationElement; 18 import org.eclipse.core.runtime.IExtension; 19 import org.eclipse.core.runtime.IExtensionPoint; 20 import org.eclipse.core.runtime.IExtensionRegistry; 21 import org.eclipse.ui.internal.WorkbenchPlugin; 22 23 37 public abstract class RegistryReader { 38 39 44 protected RegistryReader() { 45 } 46 47 51 protected static void logError(IConfigurationElement element, String text) { 52 IExtension extension = element.getDeclaringExtension(); 53 StringBuffer buf = new StringBuffer (); 54 buf 55 .append("Plugin " + extension.getNamespace() + ", extension " + extension.getExtensionPointUniqueIdentifier()); buf.append("\n" + text); WorkbenchPlugin.log(buf.toString()); 58 } 59 60 63 protected static void logMissingAttribute(IConfigurationElement element, 64 String attributeName) { 65 logError(element, 66 "Required attribute '" + attributeName + "' not defined"); } 68 69 72 protected static void logMissingElement(IConfigurationElement element, 73 String elementName) { 74 logError(element, 75 "Required sub element '" + elementName + "' not defined"); } 77 78 81 protected static void logUnknownElement(IConfigurationElement element) { 82 logError(element, "Unknown extension tag found: " + element.getName()); } 84 85 92 public static IExtension[] orderExtensions(IExtension[] extensions) { 93 IExtension[] sortedExtension = new IExtension[extensions.length]; 98 System.arraycopy(extensions, 0, sortedExtension, 0, extensions.length); 99 Comparator comparer = new Comparator () { 100 public int compare(Object arg0, Object arg1) { 101 String s1 = ((IExtension) arg0).getNamespace(); 102 String s2 = ((IExtension) arg1).getNamespace(); 103 return s1.compareToIgnoreCase(s2); 104 } 105 }; 106 Collections.sort(Arrays.asList(sortedExtension), comparer); 107 return sortedExtension; 108 } 109 110 119 protected abstract boolean readElement(IConfigurationElement element); 120 121 126 protected void readElementChildren(IConfigurationElement element) { 127 readElements(element.getChildren()); 128 } 129 130 136 protected void readElements(IConfigurationElement[] elements) { 137 for (int i = 0; i < elements.length; i++) { 138 if (!readElement(elements[i])) { 139 logUnknownElement(elements[i]); 140 } 141 } 142 } 143 144 148 protected void readExtension(IExtension extension) { 149 readElements(extension.getConfigurationElements()); 150 } 151 152 160 public void readRegistry(IExtensionRegistry registry, String pluginId, 161 String extensionPoint) { 162 IExtensionPoint point = registry.getExtensionPoint(pluginId, 163 extensionPoint); 164 if (point == null) { 165 return; 166 } 167 IExtension[] extensions = point.getExtensions(); 168 extensions = orderExtensions(extensions); 169 for (int i = 0; i < extensions.length; i++) { 170 readExtension(extensions[i]); 171 } 172 } 173 174 181 public static String getDescription(IConfigurationElement configElement) { 182 IConfigurationElement[] children = configElement.getChildren(IWorkbenchRegistryConstants.TAG_DESCRIPTION); 183 if (children.length >= 1) { 184 return children[0].getValue(); 185 } 186 return ""; } 188 189 201 public static String getClassValue(IConfigurationElement configElement, String classAttributeName) { 202 String className = configElement.getAttribute(classAttributeName); 203 if (className != null) { 204 return className; 205 } 206 IConfigurationElement [] candidateChildren = configElement.getChildren(classAttributeName); 207 if (candidateChildren.length == 0) { 208 return null; 209 } 210 211 return candidateChildren[0].getAttribute(IWorkbenchRegistryConstants.ATT_CLASS); 212 } 213 } 214 | Popular Tags |