KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > registry > ConfigurationElement


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.core.internal.registry;
12
13 import java.util.Hashtable JavaDoc;
14 import org.eclipse.core.runtime.*;
15 import org.eclipse.core.runtime.spi.RegistryContributor;
16 import org.eclipse.osgi.util.NLS;
17
18 /**
19  * An object which represents the user-defined contents of an extension
20  * in a plug-in manifest.
21  */

22 public class ConfigurationElement extends RegistryObject {
23     static final ConfigurationElement[] EMPTY_ARRAY = new ConfigurationElement[0];
24
25     //The id of the parent element. It can be a configuration element or an extension
26
int parentId;
27     byte parentType; //This value is only interesting when running from cache.
28

29     //Store the properties and the value of the configuration element.
30
//The format is the following:
31
// [p1, v1, p2, v2, configurationElementValue]
32
//If the array size is even, there is no "configurationElementValue (ie getValue returns null)".
33
//The properties and their values are alternated (v1 is the value of p1).
34
private String JavaDoc[] propertiesAndValue;
35
36     //The name of the configuration element
37
private String JavaDoc name;
38
39     //ID of the actual contributor of this element
40
//This value can be null when the element is loaded from disk and the owner has been uninstalled.
41
//This happens when the configuration is obtained from a delta containing removed extension.
42
private String JavaDoc contributorId;
43
44     protected ConfigurationElement(ExtensionRegistry registry, boolean persist) {
45         super(registry, persist);
46     }
47
48     protected ConfigurationElement(int self, String JavaDoc contributorId, String JavaDoc name, String JavaDoc[] propertiesAndValue, int[] children, int extraDataOffset, int parent, byte parentType, ExtensionRegistry registry, boolean persist) {
49         super(registry, persist);
50
51         setObjectId(self);
52         this.contributorId = contributorId;
53         this.name = name;
54         this.propertiesAndValue = propertiesAndValue;
55         setRawChildren(children);
56         setExtraDataOffset(extraDataOffset);
57         parentId = parent;
58         this.parentType = parentType;
59     }
60
61     void throwException(String JavaDoc message, Throwable JavaDoc exception) throws CoreException {
62         throw new CoreException(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, IRegistryConstants.PLUGIN_ERROR, message, exception));
63     }
64
65     protected String JavaDoc getValue() {
66         return getValueAsIs();
67     }
68
69     String JavaDoc getValueAsIs() {
70         if (propertiesAndValue.length != 0 && propertiesAndValue.length % 2 == 1)
71             return propertiesAndValue[propertiesAndValue.length - 1];
72         return null;
73     }
74
75     public String JavaDoc getAttribute(String JavaDoc attrName) {
76         return getAttributeAsIs(attrName);
77     }
78
79     String JavaDoc getAttributeAsIs(String JavaDoc attrName) {
80         if (propertiesAndValue.length <= 1)
81             return null;
82         int size = propertiesAndValue.length - (propertiesAndValue.length % 2);
83         for (int i = 0; i < size; i += 2) {
84             if (propertiesAndValue[i].equals(attrName))
85                 return propertiesAndValue[i + 1];
86         }
87         return null;
88     }
89
90     protected String JavaDoc[] getAttributeNames() {
91         if (propertiesAndValue.length <= 1)
92             return RegistryObjectManager.EMPTY_STRING_ARRAY;
93
94         int size = propertiesAndValue.length / 2;
95         String JavaDoc[] result = new String JavaDoc[size];
96         for (int i = 0; i < size; i++) {
97             result[i] = propertiesAndValue[i * 2];
98         }
99         return result;
100     }
101
102     void setProperties(String JavaDoc[] value) {
103         propertiesAndValue = value;
104     }
105
106     protected String JavaDoc[] getPropertiesAndValue() {
107         return propertiesAndValue;
108     }
109
110     void setValue(String JavaDoc value) {
111         if (propertiesAndValue.length == 0) {
112             propertiesAndValue = new String JavaDoc[] {value};
113             return;
114         }
115         if (propertiesAndValue.length % 2 == 1) {
116             propertiesAndValue[propertiesAndValue.length - 1] = value;
117             return;
118         }
119         String JavaDoc[] newPropertiesAndValue = new String JavaDoc[propertiesAndValue.length + 1];
120         System.arraycopy(propertiesAndValue, 0, newPropertiesAndValue, 0, propertiesAndValue.length);
121         newPropertiesAndValue[propertiesAndValue.length] = value;
122         propertiesAndValue = newPropertiesAndValue;
123     }
124
125     void setContributorId(String JavaDoc id) {
126         this.contributorId = id;
127     }
128
129     protected String JavaDoc getContributorId() {
130         return contributorId;
131     }
132
133     public ConfigurationElement[] getChildren(String JavaDoc childrenName) {
134         if (getRawChildren().length == 0)
135             return ConfigurationElement.EMPTY_ARRAY;
136
137         ConfigurationElement[] result = new ConfigurationElement[1]; //Most of the time there is only one match
138
int idx = 0;
139         RegistryObjectManager objectManager = registry.getObjectManager();
140         for (int i = 0; i < children.length; i++) {
141             ConfigurationElement toTest = (ConfigurationElement) objectManager.getObject(children[i], noExtraData() ? RegistryObjectManager.CONFIGURATION_ELEMENT : RegistryObjectManager.THIRDLEVEL_CONFIGURATION_ELEMENT);
142             if (toTest.name.equals(childrenName)) {
143                 if (idx != 0) {
144                     ConfigurationElement[] copy = new ConfigurationElement[result.length + 1];
145                     System.arraycopy(result, 0, copy, 0, result.length);
146                     result = copy;
147                 }
148                 result[idx++] = toTest;
149             }
150         }
151         if (idx == 0)
152             result = ConfigurationElement.EMPTY_ARRAY;
153         return result;
154     }
155
156     void setParentId(int objectId) {
157         parentId = objectId;
158     }
159
160     protected String JavaDoc getName() {
161         return name;
162     }
163
164     void setName(String JavaDoc name) {
165         this.name = name;
166     }
167
168     void setParentType(byte type) {
169         parentType = type;
170     }
171
172     public IContributor getContributor() {
173         return registry.getObjectManager().getContributor(contributorId);
174     }
175
176     protected Object JavaDoc createExecutableExtension(String JavaDoc attributeName) throws CoreException {
177         String JavaDoc prop = null;
178         String JavaDoc executable;
179         String JavaDoc contributorName = null;
180         String JavaDoc className = null;
181         Object JavaDoc initData = null;
182         int i;
183
184         if (attributeName != null)
185             prop = getAttribute(attributeName);
186         else {
187             // property not specified, try as element value
188
prop = getValue();
189             if (prop != null) {
190                 prop = prop.trim();
191                 if (prop.equals("")) //$NON-NLS-1$
192
prop = null;
193             }
194         }
195
196         if (prop == null) {
197             // property not defined, try as a child element
198
ConfigurationElement[] exec;
199             ConfigurationElement[] parms;
200             ConfigurationElement element;
201             Hashtable JavaDoc initParms;
202             String JavaDoc pname;
203
204             exec = getChildren(attributeName);
205             if (exec.length != 0) {
206                 element = exec[0]; // assumes single definition
207
contributorName = element.getAttribute("plugin"); //$NON-NLS-1$
208
className = element.getAttribute("class"); //$NON-NLS-1$
209
parms = element.getChildren("parameter"); //$NON-NLS-1$
210
if (parms.length != 0) {
211                     initParms = new Hashtable JavaDoc(parms.length + 1);
212                     for (i = 0; i < parms.length; i++) {
213                         pname = parms[i].getAttribute("name"); //$NON-NLS-1$
214
if (pname != null)
215                             initParms.put(pname, parms[i].getAttribute("value")); //$NON-NLS-1$
216
}
217                     if (!initParms.isEmpty())
218                         initData = initParms;
219                 }
220             } else {
221                 // specified name is not a simple attribute nor child element
222
throwException(NLS.bind(RegistryMessages.exExt_extDefNotFound, attributeName), null);
223             }
224         } else {
225             // simple property or element value, parse it into its components
226
i = prop.indexOf(':');
227             if (i != -1) {
228                 executable = prop.substring(0, i).trim();
229                 initData = prop.substring(i + 1).trim();
230             } else
231                 executable = prop;
232
233             i = executable.indexOf('/');
234             if (i != -1) {
235                 contributorName = executable.substring(0, i).trim();
236                 className = executable.substring(i + 1).trim();
237             } else
238                 className = executable;
239         }
240
241         // create a new instance
242
RegistryContributor defaultContributor = registry.getObjectManager().getContributor(contributorId);
243         Object JavaDoc result = registry.createExecutableExtension(defaultContributor, className, contributorName);
244
245         // Check if we have extension adapter and initialize;
246
// Make the call even if the initialization string is null
247
try {
248             // We need to take into account both "old" and "new" style executable extensions
249
ConfigurationElementHandle confElementHandle = new ConfigurationElementHandle(registry.getObjectManager(), getObjectId());
250             if (result instanceof IExecutableExtension)
251                 ((IExecutableExtension) result).setInitializationData(confElementHandle, attributeName, initData);
252         } catch (CoreException ce) {
253             // user code threw exception
254
throw ce;
255         } catch (Exception JavaDoc te) {
256             // user code caused exception
257
throwException(NLS.bind(RegistryMessages.plugin_initObjectError, getContributor().getName(), className), te);
258         }
259
260         // Deal with executable extension factories.
261
if (result instanceof IExecutableExtensionFactory)
262             result = ((IExecutableExtensionFactory) result).create();
263
264         return result;
265     }
266
267 }
268
Popular Tags