KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > model > loader > ModelLoaderUtil


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.intro.impl.model.loader;
12
13 import org.eclipse.core.runtime.IConfigurationElement;
14 import org.eclipse.core.runtime.Platform;
15 import org.eclipse.ui.internal.intro.impl.model.AbstractBaseIntroElement;
16 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroElement;
17 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroIdElement;
18 import org.eclipse.ui.internal.intro.impl.model.util.BundleUtil;
19 import org.eclipse.ui.internal.intro.impl.util.Log;
20 import org.osgi.framework.Bundle;
21 import org.w3c.dom.Element JavaDoc;
22
23
24 /**
25  * Utilities class for help with loading the intro model from the Platform
26  * runtime model and from the DOM of content files.
27  */

28 public class ModelLoaderUtil {
29
30     /**
31      * Utility method to validate an elements name.
32      *
33      * @param element
34      * @param validName
35      * @return
36      */

37     public static boolean isValidElementName(IConfigurationElement element,
38             String JavaDoc validName) {
39
40         if (element.getName().equals(validName))
41             return true;
42         // bad element name.
43
return false;
44     }
45
46     /**
47      * Utility method to verify that there is only a single configElement in the
48      * passed array of elements. If the array is empty, null is returned. If
49      * there is more than one element in the array, the first one is picked, but
50      * this fact is logged. Attribute passed is used for logging.
51      *
52      * @param configElements
53      * @return the first configElement in the array, or null if the array is
54      * empty.
55      */

56     public static IConfigurationElement validateSingleContribution(
57             IConfigurationElement[] configElements, String JavaDoc logAttribute) {
58
59         int arraySize = configElements.length;
60         if (arraySize == 0)
61             // No one contributed to extension. return null.
62
return null;
63
64         // we should only have one, so use first one.
65
IConfigurationElement configElement = configElements[0];
66         if (Log.logInfo) {
67             String JavaDoc msg = "Loading " + getLogString(configElement, logAttribute); //$NON-NLS-1$
68
Log.info(msg);
69         }
70
71         if (arraySize != 1) {
72             // we have more than one, warn in the log.
73
for (int i = 1; i < arraySize; i++)
74                 // log each extra extension.
75
Log.warning(getLogString(configElements[i], logAttribute)
76                         + " ignored due to multiple contributions"); //$NON-NLS-1$
77
}
78         return configElement;
79     }
80
81     /**
82      * Utility method to return a string to display in .log. If logAttribute is
83      * not null, its value is also printed.
84      */

85     public static String JavaDoc getLogString(IConfigurationElement element,
86             String JavaDoc logAttribute) {
87         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(element.getName());
88         buffer.append(" element"); //$NON-NLS-1$
89
if (logAttribute != null) {
90             buffer.append(" with "); //$NON-NLS-1$
91
buffer.append(logAttribute);
92             buffer.append("=\""); //$NON-NLS-1$
93
buffer.append(element.getAttribute(logAttribute));
94         }
95         buffer.append("\" in extension: "); //$NON-NLS-1$
96
buffer.append(element.getDeclaringExtension()
97             .getExtensionPointUniqueIdentifier());
98         buffer.append(" in Bundle: "); //$NON-NLS-1$
99
buffer.append(element.getContributor().getName());
100
101
102         return buffer.toString();
103     }
104
105
106
107
108     /**
109      * Utility method to verify that there is only a single Element in the
110      * passed array of elements. If the list is empty, null is returned. If
111      * there is more than one element in the array, the first one is picked, but
112      * this fact is logged. Attribute and bundle passed is used for logging.
113      *
114      * @param Elements
115      *
116      * @return the first Element in the array, or null if the array is empty.
117      */

118     public static Element validateSingleContribution(Bundle bundle,
119             Element[] elements, String JavaDoc logAttribute) {
120
121         int arraySize = elements.length;
122         if (arraySize == 0)
123             // element list in empty. return null.
124
return null;
125
126         // we should only have one, so use first one.
127
Element element = elements[0];
128         if (Log.logInfo) {
129             String JavaDoc msg = "Loading " //$NON-NLS-1$
130
+ getLogString(bundle, element, logAttribute);
131             Log.info(msg);
132         }
133
134         if (arraySize != 1) {
135             // we have more than one, warn in the log.
136
for (int i = 1; i < arraySize; i++) {
137                 if (Log.logWarning)
138                     // log each extra extension.
139
Log.warning(getLogString(bundle, element, logAttribute)
140                             + " ignored due to multiple contributions"); //$NON-NLS-1$
141
}
142         }
143         return element;
144     }
145
146     /**
147      * Utility method to return a string to display in .log. If logAttribute is
148      * not null, its value is also printed.
149      */

150     public static String JavaDoc getLogString(Bundle bundle, Element element,
151             String JavaDoc logAttribute) {
152         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(element.getNodeName());
153         buffer.append(" element"); //$NON-NLS-1$
154
if (logAttribute != null) {
155             buffer.append(" with "); //$NON-NLS-1$
156
buffer.append(logAttribute);
157             buffer.append("=\""); //$NON-NLS-1$
158
buffer.append(element.getAttribute(logAttribute));
159         }
160         buffer.append("\" from xml file in Bundle:"); //$NON-NLS-1$
161
buffer.append(bundle.getSymbolicName());
162
163
164         return buffer.toString();
165     }
166
167
168
169     /**
170      * Util class for creating class instances from plugins.
171      *
172      * @param pluginId
173      * @param className
174      * @return
175      */

176     public static Object JavaDoc createClassInstance(String JavaDoc pluginId, String JavaDoc className) {
177         // quick exits.
178
if (pluginId == null || className == null)
179             return null;
180         Bundle bundle = Platform.getBundle(pluginId);
181         if (!BundleUtil.bundleHasValidState(bundle))
182             return null;
183
184         Class JavaDoc aClass;
185         Object JavaDoc aObject;
186         try {
187             aClass = bundle.loadClass(className);
188             aObject = aClass.newInstance();
189             return aObject;
190         } catch (Exception JavaDoc e) {
191             Log.error("Intro Could not instantiate: " + className + " in " //$NON-NLS-1$ //$NON-NLS-2$
192
+ pluginId, e);
193             return null;
194         }
195     }
196
197
198
199     /**
200      * Creates a key for the given element. Returns null if any id is null along
201      * the path.
202      *
203      * @param element
204      * @return
205      */

206     public static StringBuffer JavaDoc createPathToElementKey(
207             AbstractIntroIdElement element, boolean full) {
208         if (element.getId() == null)
209             return null;
210         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(element.getId());
211         AbstractBaseIntroElement parent = (AbstractBaseIntroElement) element
212             .getParent();
213         while (parent != null
214                 && !parent.isOfType(AbstractIntroElement.MODEL_ROOT)) {
215             if (parent.getId() == null)
216                 return null;
217             buffer.insert(0, parent.getId() + "."); //$NON-NLS-1$
218
parent = (AbstractBaseIntroElement) parent.getParent();
219         }
220         return buffer;
221     }
222 }
223
Popular Tags