1 17 package org.eclipse.emf.ecore.plugin; 18 19 20 import java.lang.reflect.Field ; 21 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IConfigurationElement; 24 import org.eclipse.core.runtime.IExtension; 25 import org.eclipse.core.runtime.IExtensionPoint; 26 import org.eclipse.core.runtime.IExtensionRegistry; 27 import org.eclipse.core.runtime.Platform; 28 29 import org.eclipse.emf.common.util.WrappedException; 30 import org.eclipse.emf.ecore.EPackage; 31 import org.eclipse.emf.ecore.resource.Resource; 32 33 34 public abstract class RegistryReader 35 { 36 protected static final String TAG_DESCRIPTION = "description"; 37 38 protected IExtensionRegistry pluginRegistry; 39 String pluginID; 40 String extensionPointID; 41 42 public RegistryReader(IExtensionRegistry pluginRegistry, String pluginID, String extensionPointID) 43 { 44 super(); 45 this.pluginRegistry = pluginRegistry; 46 this.pluginID = pluginID; 47 this.extensionPointID = extensionPointID; 48 } 49 50 55 protected abstract boolean readElement(IConfigurationElement element); 56 57 60 public void readRegistry() 61 { 62 IExtensionPoint point = pluginRegistry.getExtensionPoint(pluginID, extensionPointID); 63 if (point != null) 64 { 65 IConfigurationElement[] elements = point.getConfigurationElements(); 66 for (int i = 0; i < elements.length; i++) 67 { 68 internalReadElement(elements[i]); 69 } 70 } 71 } 72 73 private void internalReadElement(IConfigurationElement element) 74 { 75 boolean recognized = this.readElement(element); 76 if (recognized) 77 { 78 IConfigurationElement[] children = element.getChildren(); 79 for (int i = 0; i < children.length; ++i) 80 { 81 internalReadElement(children[i]); 82 } 83 } 84 else 85 { 86 logError(element, "Error processing extension: " + element); 87 } 88 } 89 90 94 protected void logError(IConfigurationElement element, String text) 95 { 96 IExtension extension = element.getDeclaringExtension(); 97 System.err.println("Plugin " + extension.getNamespace() + ", extension " + extension.getExtensionPointUniqueIdentifier()); 98 System.err.println(text); 99 } 100 101 104 protected void logMissingAttribute(IConfigurationElement element, String attributeName) 105 { 106 logError(element, "The required attribute '" + attributeName + "' not defined"); 107 } 108 109 public static class PluginClassDescriptor 110 { 111 protected IConfigurationElement element; 112 protected String attributeName; 113 114 public PluginClassDescriptor(IConfigurationElement element, String attributeName) 115 { 116 this.element = element; 117 this.attributeName = attributeName; 118 } 119 120 public Object createInstance() 121 { 122 try 123 { 124 return element.createExecutableExtension(attributeName); 125 } 126 catch (CoreException e) 127 { 128 throw new WrappedException(e); 129 } 130 } 131 } 132 133 static class ResourceFactoryDescriptor extends PluginClassDescriptor implements Resource.Factory.Descriptor 134 { 135 protected Resource.Factory factoryInstance; 136 137 public ResourceFactoryDescriptor(IConfigurationElement e, String attrName) 138 { 139 super(e, attrName); 140 } 141 142 public Resource.Factory createFactory() 143 { 144 if (factoryInstance == null) 145 { 146 factoryInstance = (Resource.Factory)createInstance(); 147 } 148 return factoryInstance; 149 } 150 } 151 152 static class EPackageDescriptor extends PluginClassDescriptor implements EPackage.Descriptor 153 { 154 public EPackageDescriptor(IConfigurationElement element, String attributeName) 155 { 156 super(element, attributeName); 157 } 158 159 public EPackage getEPackage() 160 { 161 try 164 { 165 Class javaClass = Platform.getBundle(element.getDeclaringExtension().getNamespace()).loadClass(element.getAttribute(attributeName)); 166 Field field = javaClass.getField("eINSTANCE"); 167 Object result = field.get(null); 168 return (EPackage)result; 169 } 170 catch (ClassNotFoundException e) 171 { 172 throw new WrappedException(e); 173 } 174 catch (IllegalAccessException e) 175 { 176 throw new WrappedException(e); 177 } 178 catch (NoSuchFieldException e) 179 { 180 throw new WrappedException(e); 181 } 182 } 183 } 184 } 185 | Popular Tags |