KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > configurator > FeatureEntry


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.update.internal.configurator;
12
13 import java.net.*;
14 import java.util.ArrayList JavaDoc;
15 import java.util.MissingResourceException JavaDoc;
16 import java.util.ResourceBundle JavaDoc;
17
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.update.configurator.*;
20 import org.eclipse.update.internal.configurator.branding.*;
21 import org.osgi.framework.*;
22 import org.w3c.dom.*;
23
24
25 /**
26  *
27  * Feature information
28  */

29 public class FeatureEntry
30         implements
31             IPlatformConfiguration.IFeatureEntry,
32             IConfigurationConstants,
33             IBundleGroup,
34             IBundleGroupConstants,
35             IProductConstants {
36     private String JavaDoc id;
37     private String JavaDoc version;
38     private String JavaDoc pluginVersion;
39     private String JavaDoc application;
40     private URL[] root;
41     private boolean primary;
42     private String JavaDoc pluginIdentifier;
43     private String JavaDoc url;
44     private String JavaDoc description;
45     private String JavaDoc licenseURL;
46     private ArrayList JavaDoc plugins;
47     private AboutInfo branding;
48     private SiteEntry site;
49     private ResourceBundle JavaDoc resourceBundle;
50     private boolean fullyParsed;
51
52     public FeatureEntry(String JavaDoc id, String JavaDoc version, String JavaDoc pluginIdentifier, String JavaDoc pluginVersion, boolean primary, String JavaDoc application, URL[] root) {
53         if (id == null)
54             throw new IllegalArgumentException JavaDoc();
55         this.id = id;
56         this.version = version;
57         this.pluginVersion = pluginVersion;
58         this.pluginIdentifier = pluginIdentifier;
59         this.primary = primary;
60         this.application = application;
61         this.root = (root == null ? new URL[0] : root);
62     }
63
64     public FeatureEntry( String JavaDoc id, String JavaDoc version, String JavaDoc pluginVersion, boolean primary, String JavaDoc application, URL[] root) {
65         this(id, version, id, pluginVersion, primary, application, root);
66     }
67
68     public void setSite(SiteEntry site) {
69         this.site = site;
70     }
71     
72     public SiteEntry getSite() {
73         return this.site;
74     }
75     
76     public void addPlugin(PluginEntry plugin) {
77         if (plugins == null)
78             plugins = new ArrayList JavaDoc();
79         plugins.add(plugin);
80     }
81     
82     public PluginEntry[] getPluginEntries() {
83         if (plugins == null)
84             fullParse();
85         return (PluginEntry[])plugins.toArray(new PluginEntry[plugins.size()]);
86     }
87     
88     /**
89      * Sets the url string (relative to the site url)
90      * @param url
91      */

92     public void setURL(String JavaDoc url) {
93         this.url = url;
94     }
95     
96     /**
97      * @return the feature url (relative to the site): features/org.eclipse.platform/
98      */

99     public String JavaDoc getURL() {
100 // if (url == null)
101
// url = FEATURES + "/" + id + "_" + version + "/";
102
return url;
103     }
104     
105     /*
106      * @see IFeatureEntry#getFeatureIdentifier()
107      */

108     public String JavaDoc getFeatureIdentifier() {
109         return id;
110     }
111
112     /*
113      * @see IFeatureEntry#getFeatureVersion()
114      */

115     public String JavaDoc getFeatureVersion() {
116         return version;
117     }
118
119     /*
120      * @see IFeatureEntry#getFeaturePluginVersion()
121      */

122     public String JavaDoc getFeaturePluginVersion() {
123         return pluginVersion != null && pluginVersion.length() > 0 ? pluginVersion : null;
124     }
125
126     /*
127      * @see IFeatureEntry#getFeaturePluginIdentifier()
128      */

129     public String JavaDoc getFeaturePluginIdentifier() {
130         // if no plugin is specified, use the feature id
131
return pluginIdentifier != null && pluginIdentifier.length() > 0 ? pluginIdentifier : id;
132     }
133     
134     /*
135      * @see IFeatureEntry#getFeatureApplication()
136      */

137     public String JavaDoc getFeatureApplication() {
138         return application;
139     }
140
141     /*
142      * @see IFeatureEntry#getFeatureRootURLs()
143      */

144     public URL[] getFeatureRootURLs() {
145         return root;
146     }
147
148     /*
149      * @see IFeatureEntry#canBePrimary()
150      */

151     public boolean canBePrimary() {
152         return primary;
153     }
154
155     public Element toXML(Document doc) {
156         URL installURL = Utils.getInstallURL();
157         
158         Element featureElement = doc.createElement(CFG_FEATURE_ENTRY);
159         // write out feature entry settings
160
if (id != null)
161             featureElement.setAttribute(CFG_FEATURE_ENTRY_ID, id);
162         if (primary)
163             featureElement.setAttribute(CFG_FEATURE_ENTRY_PRIMARY, "true"); //$NON-NLS-1$
164
if (version != null)
165             featureElement.setAttribute(CFG_FEATURE_ENTRY_VERSION, version);
166         if (pluginVersion != null && !pluginVersion.equals(version) && pluginVersion.length() > 0)
167             featureElement.setAttribute(CFG_FEATURE_ENTRY_PLUGIN_VERSION, pluginVersion);
168         if (pluginIdentifier != null && !pluginIdentifier.equals(id) && pluginIdentifier.length() > 0)
169             featureElement.setAttribute(CFG_FEATURE_ENTRY_PLUGIN_IDENTIFIER, pluginIdentifier);
170         if (application != null)
171             featureElement.setAttribute(CFG_FEATURE_ENTRY_APPLICATION, application);
172         if (url != null)
173             // make externalized URL install relative
174
featureElement.setAttribute(CFG_URL, Utils.makeRelative(installURL, url));
175         
176         URL[] roots = getFeatureRootURLs();
177         for (int i=0; i<roots.length; i++) {
178             // make externalized URL install relative
179
String JavaDoc root = Utils.makeRelative(installURL, roots[i]).toExternalForm();
180             if (root.trim().length() > 0){
181                 Element rootElement = doc.createElement(CFG_FEATURE_ENTRY_ROOT);
182                 rootElement.appendChild(doc.createTextNode(root));
183                 featureElement.appendChild(rootElement);
184             }
185         }
186         
187         return featureElement;
188     }
189     
190     public void setDescription(String JavaDoc description) {
191         this.description = description;
192     }
193     
194     /* (non-Javadoc)
195      * @see org.eclipse.core.runtime.IBundleGroup#getBundles()
196      */

197     public Bundle JavaDoc[] getBundles() {
198         if (plugins == null)
199             fullParse();
200         
201         ArrayList JavaDoc bundles = new ArrayList JavaDoc(plugins.size());
202         for (int i=0; i<plugins.size(); i++) {
203             PluginEntry plugin = (PluginEntry)plugins.get(i);
204             // get the highest version for the plugin
205
Bundle JavaDoc bundle = Utils.getBundle(plugin.getPluginIdentifier());
206             if (bundle != null)
207                 bundles.add(bundle);
208         }
209         return (Bundle JavaDoc[])bundles.toArray(new Bundle JavaDoc[bundles.size()]);
210     }
211     /* (non-Javadoc)
212      * @see org.eclipse.core.runtime.IBundleGroup#getDescription()
213      */

214     public String JavaDoc getDescription() {
215         if (description == null)
216             fullParse();
217         return description;
218     }
219     /* (non-Javadoc)
220      * @see org.eclipse.core.runtime.IBundleGroup#getIdentifier()
221      */

222     public String JavaDoc getIdentifier() {
223         return id;
224     }
225     /* (non-Javadoc)
226      * @see org.eclipse.core.runtime.IBundleGroup#getName()
227      */

228     public String JavaDoc getName() {
229         if (branding == null)
230             branding = AboutInfo.readFeatureInfo(id, version, getFeaturePluginIdentifier());
231         return branding.getProductName();
232     }
233     /* (non-Javadoc)
234      * @see org.eclipse.core.runtime.IBundleGroup#getProperty(java.lang.String)
235      */

236     public String JavaDoc getProperty(String JavaDoc key) {
237         if (key == null)
238             return null;
239         
240         if (branding == null)
241             branding = AboutInfo.readFeatureInfo(id, version, getFeaturePluginIdentifier());
242         
243         // IBundleGroupConstants
244
if (key.equals(FEATURE_IMAGE))
245             return branding.getFeatureImageURL() == null ? null : branding.getFeatureImageURL().toExternalForm();
246         else if (key.equals(TIPS_AND_TRICKS_HREF))
247             return branding.getTipsAndTricksHref();
248         else if (key.equals(IBundleGroupConstants.WELCOME_PAGE)) // same value is used by product and bundle group
249
return branding.getWelcomePageURL() == null ? null : branding.getWelcomePageURL().toExternalForm();
250         else if (key.equals(WELCOME_PERSPECTIVE))
251             return branding.getWelcomePerspectiveId();
252         // IProductConstants
253
else if (key.equals(APP_NAME))
254             return branding.getAppName();
255         else if (key.equals(ABOUT_TEXT))
256             return branding.getAboutText();
257         else if (key.equals(ABOUT_IMAGE))
258             return branding.getAboutImageURL() == null ? null : branding.getAboutImageURL().toExternalForm();
259         else if (key.equals(WINDOW_IMAGE))
260             return branding.getWindowImageURL()== null ? null : branding.getWindowImageURL().toExternalForm();
261         else if (key.equals(WINDOW_IMAGES)) {
262             URL[] urls = branding.getWindowImagesURLs();
263             if (urls == null)
264                 return null;
265             StringBuffer JavaDoc windowImagesURLs = new StringBuffer JavaDoc();
266             for (int i=0; i<urls.length; i++){
267                 windowImagesURLs.append(urls[i].toExternalForm());
268                 if (i != urls.length-1)
269                     windowImagesURLs.append(',');
270             }
271             return windowImagesURLs.toString();
272         } else if (key.equals(LICENSE_HREF))
273             return getLicenseURL();
274         
275         return null;
276     }
277     /* (non-Javadoc)
278      * @see org.eclipse.core.runtime.IBundleGroup#getProviderName()
279      */

280     public String JavaDoc getProviderName() {
281         if (branding == null)
282             branding = AboutInfo.readFeatureInfo(id, version, getFeaturePluginIdentifier());
283         return branding.getProviderName();
284     }
285     /* (non-Javadoc)
286      * @see org.eclipse.core.runtime.IBundleGroup#getVersion()
287      */

288     public String JavaDoc getVersion() {
289         return version;
290     }
291     /* (non-Javadoc)
292      * @see org.eclipse.core.runtime.IProduct#getApplication()
293      */

294     public String JavaDoc getApplication() {
295         return application;
296     }
297     /* (non-Javadoc)
298      * @see org.eclipse.core.runtime.IProduct#getId()
299      */

300     public String JavaDoc getId() {
301         return id;
302     }
303     
304     public ResourceBundle JavaDoc getResourceBundle(){
305         if (resourceBundle != null)
306             return resourceBundle;
307         
308         // Determine the properties file location
309
if (site == null)
310             return null;
311         
312         ResourceBundle JavaDoc bundle = null;
313         try {
314             URL propertiesURL = new URL(site.getResolvedURL(), getURL());
315             ClassLoader JavaDoc l = new URLClassLoader(new URL[] { propertiesURL }, null);
316             bundle = ResourceBundle.getBundle(IConfigurationConstants.CFG_FEATURE_ENTRY, Utils.getDefaultLocale(), l);
317         } catch (MissingResourceException JavaDoc e) {
318             Utils.log(e.getLocalizedMessage());
319         } catch (MalformedURLException e) {
320             Utils.log(e.getLocalizedMessage());
321         }
322         return bundle;
323     }
324     
325     public void setLicenseURL(String JavaDoc licenseURL) {
326         this.licenseURL = licenseURL;
327     }
328     
329     public String JavaDoc getLicenseURL() {
330         if (licenseURL == null)
331             fullParse();
332         if (licenseURL == null)
333             return null;
334         
335         String JavaDoc resolvedURL = Utils.getResourceString(getResourceBundle(), licenseURL);
336         if (resolvedURL.startsWith("http://")) //$NON-NLS-1$
337
return resolvedURL;
338         try {
339             return new URL(getSite().getResolvedURL(), getURL() + resolvedURL).toExternalForm();
340         } catch (MalformedURLException e) {
341             return resolvedURL;
342         }
343     }
344     
345     private void fullParse() {
346         if (fullyParsed)
347             return;
348         fullyParsed = true;
349         if (plugins == null)
350             plugins = new ArrayList JavaDoc();
351         FullFeatureParser parser = new FullFeatureParser(this);
352         parser.parse();
353     }
354     
355     public Bundle JavaDoc getDefiningBundle() {
356         return Utils.getBundle(getFeaturePluginIdentifier());
357     }
358     
359     public boolean hasBranding() {
360         String JavaDoc bundleId = getFeaturePluginIdentifier();
361         return bundleId != null && Utils.getBundle(bundleId) != null;
362     }
363 }
364
Popular Tags