KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > registry > RegistryReader


1 /*******************************************************************************
2  * Copyright (c) 2002, 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.cheatsheets.registry;
12
13 import org.eclipse.core.runtime.*;
14 import org.eclipse.ui.internal.cheatsheets.*;
15
16 /**
17  * Template implementation of a registry reader that creates objects
18  * representing registry contents. Typically, an extension
19  * contains one element, but this reader handles multiple
20  * elements per extension.
21  *
22  * To start reading the extensions from the registry for an
23  * extension point, call the method <code>readRegistry</code>.
24  *
25  * To read children of an IConfigurationElement, call the
26  * method <code>readElementChildren</code> from your implementation
27  * of the method <code>readElement</code>, as it will not be
28  * done by default.
29  */

30 public abstract class RegistryReader {
31     protected static final String JavaDoc TAG_DESCRIPTION = "description"; //$NON-NLS-1$
32

33     /**
34      * The constructor.
35      */

36     /*package*/ RegistryReader() {
37     }
38
39     /**
40      * This method extracts description as a subelement of
41      * the given element.
42      * @return description string if defined, or empty string
43      * if not.
44      */

45     /*package*/ String JavaDoc getDescription(IConfigurationElement config) {
46         IConfigurationElement[] children = config.getChildren(TAG_DESCRIPTION);
47         if (children.length >= 1) {
48             return children[0].getValue();
49         }
50         return ICheatSheetResource.EMPTY_STRING;
51     }
52
53     /**
54      * Logs the error in the workbench log using the provided
55      * text and the information in the configuration element.
56      */

57     private void logError(IConfigurationElement element, String JavaDoc text) {
58         IExtension extension = element.getDeclaringExtension();
59         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
60         buf.append("Plugin " + extension.getContributor().getName() + ", extension " + extension.getExtensionPointUniqueIdentifier()); //$NON-NLS-2$//$NON-NLS-1$
61
buf.append("\n" + text); //$NON-NLS-1$
62

63         IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, buf.toString(), null);
64         CheatSheetPlugin.getPlugin().getLog().log(status);
65     }
66
67     /**
68      * Logs a very common registry error when a required attribute is missing.
69      */

70     /*package*/ void logMissingAttribute(IConfigurationElement element, String JavaDoc attributeName) {
71         logError(element, "Required attribute '" + attributeName + "' not defined"); //$NON-NLS-2$//$NON-NLS-1$
72
}
73
74     /**
75      * Logs a registry error when the configuration element is unknown.
76      */

77     private void logUnknownElement(IConfigurationElement element) {
78         logError(element, "Unknown extension tag found: " + element.getName()); //$NON-NLS-1$
79
}
80
81     /**
82      * Apply a reproducable order to the list of extensions
83      * provided, such that the order will not change as
84      * extensions are added or removed.
85      */

86     private IExtension[] orderExtensions(IExtension[] extensions) {
87         // By default, the order is based on plugin id sorted
88
// in ascending order. The order for a plugin providing
89
// more than one extension for an extension point is
90
// dependent in the order listed in the XML file.
91
Sorter sorter = new Sorter() {
92             public boolean compare(Object JavaDoc extension1, Object JavaDoc extension2) {
93                 String JavaDoc s1 = ((IExtension) extension1).getContributor().getName().toUpperCase();
94                 String JavaDoc s2 = ((IExtension) extension2).getContributor().getName().toUpperCase();
95                 //Return true if elementTwo is 'greater than' elementOne
96
return s2.compareTo(s1) > 0;
97             }
98         };
99
100         Object JavaDoc[] sorted = sorter.sort(extensions);
101         IExtension[] sortedExtension = new IExtension[sorted.length];
102         System.arraycopy(sorted, 0, sortedExtension, 0, sorted.length);
103         return sortedExtension;
104     }
105
106     /**
107      * Implement this method to read element's attributes.
108      * If children should also be read, then implementor
109      * is responsible for calling <code>readElementChildren</code>.
110      * Implementor is also responsible for logging missing
111      * attributes.
112      *
113      * @return true if element was recognized, false if not.
114      */

115     /*package*/ abstract boolean readElement(IConfigurationElement element);
116
117     /**
118      * Read the element's children. This is called by
119      * the subclass' readElement method when it wants
120      * to read the children of the element.
121      */

122     /*package*/ void readElementChildren(IConfigurationElement element) {
123         readElements(element.getChildren());
124     }
125
126     /**
127      * Read each element one at a time by calling the
128      * subclass implementation of <code>readElement</code>.
129      *
130      * Logs an error if the element was not recognized.
131      */

132     private void readElements(IConfigurationElement[] elements) {
133         for (int i = 0; i < elements.length; i++) {
134             if (!readElement(elements[i]))
135                 logUnknownElement(elements[i]);
136         }
137     }
138
139     /**
140      * Read one extension by looping through its
141      * configuration elements.
142      */

143     private void readExtension(IExtension extension) {
144         readElements(extension.getConfigurationElements());
145     }
146
147     /**
148      * Start the registry reading process using the
149      * supplied plugin ID and extension point.
150      */

151     /*package*/ void readRegistry(IExtensionRegistry registry, String JavaDoc pluginId, String JavaDoc extensionPoint) {
152         IExtensionPoint point = registry.getExtensionPoint(pluginId, extensionPoint);
153         if (point != null) {
154             IExtension[] extensions = point.getExtensions();
155             extensions = orderExtensions(extensions);
156             for (int i = 0; i < extensions.length; i++)
157                 readExtension(extensions[i]);
158         }
159     }
160 }
161
Popular Tags