KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > plugin > RegistryReader


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: RegistryReader.java,v 1.4 2005/06/08 06:20:10 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.plugin;
18
19
20 import java.lang.reflect.Field JavaDoc;
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 JavaDoc TAG_DESCRIPTION = "description";
37
38   protected IExtensionRegistry pluginRegistry;
39   String JavaDoc pluginID;
40   String JavaDoc extensionPointID;
41
42   public RegistryReader(IExtensionRegistry pluginRegistry, String JavaDoc pluginID, String JavaDoc extensionPointID)
43   {
44     super();
45     this.pluginRegistry = pluginRegistry;
46     this.pluginID = pluginID;
47     this.extensionPointID = extensionPointID;
48   }
49
50   /**
51    * Implement this method to read element attributes.
52    * If this element has subelements, the reader will recursively cycle through them
53    * and will call this method, so don't do it here.
54    */

55   protected abstract boolean readElement(IConfigurationElement element);
56
57   /**
58    * Reads from the plugin registry and parses it.
59    */

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   /**
91    * Logs the error in the desktop log using the provided
92    * text and the information in the configuration element.
93    */

94   protected void logError(IConfigurationElement element, String JavaDoc 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   /**
102    * Logs a very common registry error when a required attribute is missing.
103    */

104   protected void logMissingAttribute(IConfigurationElement element, String JavaDoc 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 JavaDoc attributeName;
113
114     public PluginClassDescriptor(IConfigurationElement element, String JavaDoc attributeName)
115     {
116       this.element = element;
117       this.attributeName = attributeName;
118     }
119
120     public Object JavaDoc 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 JavaDoc 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 JavaDoc attributeName)
155     {
156       super(element, attributeName);
157     }
158
159     public EPackage getEPackage()
160     {
161       // First try to see if this class has an eInstance
162
//
163
try
164       {
165         Class JavaDoc javaClass = Platform.getBundle(element.getDeclaringExtension().getNamespace()).loadClass(element.getAttribute(attributeName));
166         Field JavaDoc field = javaClass.getField("eINSTANCE");
167         Object JavaDoc result = field.get(null);
168         return (EPackage)result;
169       }
170       catch (ClassNotFoundException JavaDoc e)
171       {
172         throw new WrappedException(e);
173       }
174       catch (IllegalAccessException JavaDoc e)
175       {
176         throw new WrappedException(e);
177       }
178       catch (NoSuchFieldException JavaDoc e)
179       {
180         throw new WrappedException(e);
181       }
182     }
183   }
184 }
185
Popular Tags