1 11 package org.eclipse.ui.internal.navigator.extensions; 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.core.runtime.Platform; 22 import org.eclipse.ui.internal.navigator.NavigatorPlugin; 23 24 43 public abstract class RegistryReader { 44 45 protected static final String TAG_DESCRIPTION = "description"; 47 private boolean isInitialized; 48 private final String extensionPointId; 49 private final String pluginId; 50 private final IExtensionRegistry registry; 51 52 55 protected RegistryReader(String aPluginId, String anExtensionPoint) { 56 this.registry = Platform.getExtensionRegistry(); 57 this.pluginId = aPluginId; 58 this.extensionPointId = anExtensionPoint; 59 } 60 61 66 protected String getDescription(IConfigurationElement config) { 67 IConfigurationElement[] children = config.getChildren(TAG_DESCRIPTION); 68 if (children.length >= 1) { 69 return children[0].getValue(); 70 } 71 return ""; } 73 74 78 protected static void logError(IConfigurationElement element, String text) { 79 IExtension extension = element.getDeclaringExtension(); 80 StringBuffer buf = new StringBuffer (); 81 buf.append("Plugin " + extension.getNamespaceIdentifier() + ", extension " + extension.getExtensionPointUniqueIdentifier()); buf.append("\n" + text); NavigatorPlugin.logError(0, buf.toString(), null); 84 } 85 86 89 protected static void logMissingAttribute(IConfigurationElement element, String attributeName) { 90 logError(element, "Required attribute '" + attributeName + "' not defined"); } 92 93 96 protected static void logMissingElement(IConfigurationElement element, String elementName) { 97 logError(element, "Required sub element '" + elementName + "' not defined"); } 99 100 103 protected static void logUnknownElement(IConfigurationElement element) { 104 logError(element, "Unknown extension tag found: " + element.getName()); } 106 107 111 protected IExtension[] orderExtensions(IExtension[] extensions) { 112 IExtension[] sortedExtension = new IExtension[extensions.length]; 117 System.arraycopy(extensions, 0, sortedExtension, 0, extensions.length); 118 Comparator comparer = new Comparator () { 119 public int compare(Object arg0, Object arg1) { 120 String s1 = ((IExtension) arg0).getNamespaceIdentifier(); 121 String s2 = ((IExtension) arg1).getNamespaceIdentifier(); 122 return s1.compareToIgnoreCase(s2); 123 } 124 }; 125 Collections.sort(Arrays.asList(sortedExtension), comparer); 126 return sortedExtension; 127 } 128 129 136 protected abstract boolean readElement(IConfigurationElement element); 137 138 142 protected void readElementChildren(IConfigurationElement element) { 143 readElements(element.getChildren()); 144 } 145 146 152 protected void readElements(IConfigurationElement[] elements) { 153 for (int i = 0; i < elements.length; i++) { 154 if (!readElement(elements[i])) { 155 logUnknownElement(elements[i]); 156 } 157 } 158 } 159 160 163 protected void readExtension(IExtension extension) { 164 readElements(extension.getConfigurationElements()); 165 } 166 167 170 public void readRegistry() { 171 if (isInitialized) { 172 return; 173 } 174 synchronized (this) { 175 if (!isInitialized) { 176 IExtensionPoint point = registry.getExtensionPoint(pluginId, extensionPointId); 177 if (point == null) { 178 return; 179 } 180 IExtension[] extensions = point.getExtensions(); 181 extensions = orderExtensions(extensions); 182 for (int i = 0; i < extensions.length; i++) { 183 readExtension(extensions[i]); 184 } 185 isInitialized = true; 186 } 187 188 } 189 } 190 } 191 | Popular Tags |