KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > registry > IDERegistryReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.ide.registry;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.Hashtable JavaDoc;
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 /**
25  * Template implementation of a registry reader that creates objects
26  * representing registry contents. Typically, an extension
27  * contains one element, but this reader handles multiple
28  * elements per extension.
29  *
30  * To start reading the extensions from the registry for an
31  * extension point, call the method <code>readRegistry</code>.
32  *
33  * To read children of an IConfigurationElement, call the
34  * method <code>readElementChildren</code> from your implementation
35  * of the method <code>readElement</code>, as it will not be
36  * done by default.
37  */

38 public abstract class IDERegistryReader {
39     protected static final String JavaDoc TAG_DESCRIPTION = "description"; //$NON-NLS-1$
40

41     protected static Hashtable JavaDoc extensionPoints = new Hashtable JavaDoc();
42
43     private static final Comparator JavaDoc comparer = new Comparator JavaDoc() {
44         public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
45             IExtension i1 = (IExtension) arg0;
46             String JavaDoc s1 = i1.getNamespace();
47             IExtension i2 = (IExtension) arg1;
48             String JavaDoc s2 = i2.getNamespace();
49             return s1.compareToIgnoreCase(s2);
50         }
51     };
52
53     /**
54      * The constructor.
55      */

56     protected IDERegistryReader() {
57     }
58
59     /**
60      * This method extracts description as a subelement of
61      * the given element.
62      * @return description string if defined, or empty string
63      * if not.
64      */

65     protected String JavaDoc getDescription(IConfigurationElement config) {
66         IConfigurationElement[] children = config.getChildren(TAG_DESCRIPTION);
67         if (children.length >= 1) {
68             return children[0].getValue();
69         }
70         return "";//$NON-NLS-1$
71
}
72
73     /**
74      * Logs the error in the workbench log using the provided
75      * text and the information in the configuration element.
76      */

77     protected void logError(IConfigurationElement element, String JavaDoc text) {
78         IExtension extension = element.getDeclaringExtension();
79         String JavaDoc pluginId = extension.getNamespace();
80         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
81         buf.append("Plugin " + pluginId + ", extension " //$NON-NLS-2$//$NON-NLS-1$
82
+ extension.getExtensionPointUniqueIdentifier());
83         buf.append("\n" + text);//$NON-NLS-1$
84
IDEWorkbenchPlugin.log(buf.toString());
85     }
86
87     /**
88      * Logs a very common registry error when a required attribute is missing.
89      */

90     protected void logMissingAttribute(IConfigurationElement element,
91             String JavaDoc attributeName) {
92         logError(element,
93                 "Required attribute '" + attributeName + "' not defined");//$NON-NLS-2$//$NON-NLS-1$
94
}
95
96     /**
97      * Logs a very common registry error when a required child is missing.
98      */

99     protected void logMissingElement(IConfigurationElement element,
100             String JavaDoc elementName) {
101         logError(element,
102                 "Required sub element '" + elementName + "' not defined");//$NON-NLS-2$//$NON-NLS-1$
103
}
104
105     /**
106      * Logs a registry error when the configuration element is unknown.
107      */

108     protected void logUnknownElement(IConfigurationElement element) {
109         logError(element, "Unknown extension tag found: " + element.getName());//$NON-NLS-1$
110
}
111
112     /**
113      * Apply a reproducable order to the list of extensions
114      * provided, such that the order will not change as
115      * extensions are added or removed.
116      */

117     protected IExtension[] orderExtensions(IExtension[] extensions) {
118         // By default, the order is based on plugin id sorted
119
// in ascending order. The order for a plugin providing
120
// more than one extension for an extension point is
121
// dependent in the order listed in the XML file.
122
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     /**
129      * Implement this method to read element's attributes.
130      * If children should also be read, then implementor
131      * is responsible for calling <code>readElementChildren</code>.
132      * Implementor is also responsible for logging missing
133      * attributes.
134      *
135      * @return true if element was recognized, false if not.
136      */

137     protected abstract boolean readElement(IConfigurationElement element);
138
139     /**
140      * Read the element's children. This is called by
141      * the subclass' readElement method when it wants
142      * to read the children of the element.
143      */

144     protected void readElementChildren(IConfigurationElement element) {
145         readElements(element.getChildren());
146     }
147
148     /**
149      * Read each element one at a time by calling the
150      * subclass implementation of <code>readElement</code>.
151      *
152      * Logs an error if the element was not recognized.
153      */

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     /**
163      * Read one extension by looping through its
164      * configuration elements.
165      */

166     protected void readExtension(IExtension extension) {
167         readElements(extension.getConfigurationElements());
168     }
169
170     /**
171      * Start the registry reading process using the
172      * supplied plugin ID and extension point.
173      */

174     protected void readRegistry(IExtensionRegistry registry, String JavaDoc pluginId,
175             String JavaDoc extensionPoint) {
176         String JavaDoc pointId = pluginId + "-" + extensionPoint; //$NON-NLS-1$
177
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