KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > bundle > BundlePluginBase


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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.pde.internal.core.bundle;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.io.Serializable JavaDoc;
15 import java.util.ArrayList JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.PlatformObject;
19 import org.eclipse.osgi.service.resolver.BundleDescription;
20 import org.eclipse.osgi.service.resolver.BundleSpecification;
21 import org.eclipse.osgi.util.ManifestElement;
22 import org.eclipse.pde.core.IIdentifiable;
23 import org.eclipse.pde.core.IModelChangedEvent;
24 import org.eclipse.pde.core.ModelChangedEvent;
25 import org.eclipse.pde.core.plugin.IExtensions;
26 import org.eclipse.pde.core.plugin.IPluginBase;
27 import org.eclipse.pde.core.plugin.IPluginExtension;
28 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
29 import org.eclipse.pde.core.plugin.IPluginImport;
30 import org.eclipse.pde.core.plugin.IPluginLibrary;
31 import org.eclipse.pde.core.plugin.IPluginModelBase;
32 import org.eclipse.pde.core.plugin.IPluginObject;
33 import org.eclipse.pde.core.plugin.ISharedExtensionsModel;
34 import org.eclipse.pde.core.plugin.ISharedPluginModel;
35 import org.eclipse.pde.internal.core.ICoreConstants;
36 import org.eclipse.pde.internal.core.PDEStateHelper;
37 import org.eclipse.pde.internal.core.TargetPlatformHelper;
38 import org.eclipse.pde.internal.core.ibundle.IBundle;
39 import org.eclipse.pde.internal.core.ibundle.IBundleModel;
40 import org.eclipse.pde.internal.core.ibundle.IBundlePluginBase;
41 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
42 import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
43 import org.eclipse.pde.internal.core.plugin.AbstractExtensions;
44 import org.eclipse.pde.internal.core.plugin.PluginImport;
45 import org.eclipse.pde.internal.core.plugin.PluginLibrary;
46 import org.eclipse.pde.internal.core.text.bundle.BundleClasspathHeader;
47 import org.eclipse.pde.internal.core.text.bundle.BundleNameHeader;
48 import org.eclipse.pde.internal.core.text.bundle.BundleSymbolicNameHeader;
49 import org.eclipse.pde.internal.core.text.bundle.BundleVendorHeader;
50 import org.eclipse.pde.internal.core.text.bundle.BundleVersionHeader;
51 import org.eclipse.pde.internal.core.text.bundle.RequireBundleHeader;
52 import org.osgi.framework.BundleException;
53 import org.osgi.framework.Constants;
54 import org.osgi.framework.Version;
55
56 public class BundlePluginBase extends PlatformObject implements IBundlePluginBase, Serializable JavaDoc {
57
58     private static final long serialVersionUID = 1L;
59     protected IBundlePluginModelBase model;
60     private ArrayList JavaDoc libraries;
61     private ArrayList JavaDoc imports;
62     private String JavaDoc fTarget;
63
64     public void reset() {
65         libraries = null;
66         imports = null;
67     }
68     
69     public String JavaDoc getSchemaVersion() {
70         IExtensions root = getExtensionsRoot();
71         if (root instanceof AbstractExtensions)
72             return ((AbstractExtensions)root).getSchemaVersion();
73         return (root instanceof IPluginBase) ? ((IPluginBase)root).getSchemaVersion() : null;
74     }
75     
76     public void setSchemaVersion(String JavaDoc value) throws CoreException {
77         IExtensions root = getExtensionsRoot();
78         if (root == null)
79             return;
80         if (root instanceof AbstractExtensions)
81             ((AbstractExtensions)root).setSchemaVersion(value);
82         if (root instanceof IPluginBase)
83             ((IPluginBase)root).setSchemaVersion(value);
84     }
85
86     public void modelChanged(IModelChangedEvent event) {
87         if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
88             if (event.getChangeProvider().equals(model.getBundleModel())) {
89                 reset();
90             }
91             getModel().fireModelChanged(event);
92         } else if (!event.getChangeProvider().equals(model.getBundleModel())) {
93             getModel().fireModelChanged(event);
94         }
95     }
96
97     /*
98      * (non-Javadoc)
99      *
100      * @see org.eclipse.pde.internal.core.ibundle.IBundlePluginBase#getBundle()
101      */

102     public IBundle getBundle() {
103         if (model != null) {
104             IBundleModel bmodel = model.getBundleModel();
105             return bmodel != null ? bmodel.getBundle() : null;
106         }
107         return null;
108     }
109     
110     protected IManifestHeader getManifestHeader(String JavaDoc key) {
111         IBundle bundle = getBundle();
112         return (bundle != null) ? bundle.getManifestHeader(key) : null;
113     }
114
115     public ISharedPluginModel getModel() {
116         return model;
117     }
118
119     void setModel(IBundlePluginModelBase model) {
120         this.model = model;
121     }
122
123     /*
124      * (non-Javadoc)
125      *
126      * @see org.eclipse.pde.internal.core.ibundle.IBundlePluginBase#getExtensionsRoot()
127      */

128     public IExtensions getExtensionsRoot() {
129         if (model != null) {
130             ISharedExtensionsModel emodel = model.getExtensionsModel();
131             return emodel != null ? emodel.getExtensions() : null;
132         }
133         return null;
134     }
135     
136     /*
137      * (non-Javadoc)
138      *
139      * @see org.eclipse.pde.core.plugin.IPluginBase#add(org.eclipse.pde.core.plugin.IPluginLibrary)
140      */

141     public void add(IPluginLibrary library) throws CoreException {
142         if (libraries == null)
143             // if libraries == null, initialize the libraries varible by calling getLibraries()
144
getLibraries();
145         libraries.add(library);
146         Object JavaDoc header = getManifestHeader(Constants.BUNDLE_CLASSPATH);
147         if (header instanceof BundleClasspathHeader) {
148             ((BundleClasspathHeader)header).addLibrary(library.getName());
149         } else {
150             String JavaDoc value = header == null ? null : ((IManifestHeader)header).getValue();
151             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(value == null ? "" : value); //$NON-NLS-1$
152
if (value != null)
153                 buffer.append(",\n "); //$NON-NLS-1$
154
buffer.append(library.getName());
155             getBundle().setHeader(Constants.BUNDLE_CLASSPATH, buffer.toString());
156         }
157         fireStructureChanged(library, true);
158     }
159
160     /*
161      * (non-Javadoc)
162      *
163      * @see org.eclipse.pde.core.plugin.IPluginBase#remove(org.eclipse.pde.core.plugin.IPluginLibrary)
164      */

165     public void remove(IPluginLibrary library) throws CoreException {
166         if (libraries != null) {
167             libraries.remove(library);
168             Object JavaDoc header = getManifestHeader(Constants.BUNDLE_CLASSPATH);
169             if (header instanceof BundleClasspathHeader) {
170                 ((BundleClasspathHeader)header).removeLibrary(library.getName());
171             }
172             fireStructureChanged(library, false);
173         }
174     }
175     
176     /*
177      * (non-Javadoc)
178      *
179      * @see org.eclipse.pde.core.plugin.IPluginBase#add(org.eclipse.pde.core.plugin.IPluginImport)
180      */

181     public void add(IPluginImport iimport) throws CoreException {
182         if (iimport == null)
183             return;
184         if (imports == null)
185             // if imports == null, intitialize the imports list by calling getImports()
186
getImports();
187         addImport(iimport);
188         fireStructureChanged(iimport, true);
189     }
190     
191     public void add(IPluginImport[] iimports) throws CoreException {
192         if (iimports != null && iimports.length > 0) {
193             if (imports == null)
194                 // if imports == null, intitialize the imports list by calling getImports()
195
getImports();
196             for (int i = 0; i < iimports.length; i++) {
197                 if (iimports[i] != null)
198                     addImport(iimports[i]);
199             }
200             fireStructureChanged(iimports, true);
201         }
202     }
203     
204     private void addImport(IPluginImport iimport) {
205         imports.add(iimport);
206         Object JavaDoc header = getManifestHeader(Constants.REQUIRE_BUNDLE);
207         if (header instanceof RequireBundleHeader) {
208             ((RequireBundleHeader)header).addBundle(iimport);
209         } else {
210             String JavaDoc value = header == null ? null : ((IManifestHeader)header).getValue();
211             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(value == null ? "" : value); //$NON-NLS-1$
212
if (value != null)
213                 buffer.append(",\n "); //$NON-NLS-1$
214
buffer.append(iimport.getId());
215             int bundleManifestVersion = getBundleManifestVersion(getBundle());
216             if (iimport.isOptional())
217                 if (bundleManifestVersion > 1)
218                     buffer.append(";" + Constants.RESOLUTION_DIRECTIVE + ":=" + Constants.RESOLUTION_OPTIONAL); //$NON-NLS-1$ //$NON-NLS-2$
219
else
220                     buffer.append(";" + ICoreConstants.OPTIONAL_ATTRIBUTE + "=true"); //$NON-NLS-1$ //$NON-NLS-2$
221
if (iimport.isReexported())
222                 if (bundleManifestVersion > 1)
223                     buffer.append(";" + Constants.VISIBILITY_DIRECTIVE + ":=" + Constants.VISIBILITY_REEXPORT); //$NON-NLS-1$ //$NON-NLS-2$
224
else
225                     buffer.append(";" + ICoreConstants.REPROVIDE_ATTRIBUTE + "=true"); //$NON-NLS-1$ //$NON-NLS-2$
226
String JavaDoc version = iimport.getVersion();
227             if (version != null && version.trim().length() > 0)
228                 buffer.append(";" + Constants.BUNDLE_VERSION_ATTRIBUTE + "=\"" + version.trim() + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
229
getBundle().setHeader(Constants.REQUIRE_BUNDLE, buffer.toString());
230         }
231     }
232     
233     /*
234      * (non-Javadoc)
235      *
236      * @see org.eclipse.pde.core.plugin.IPluginBase#remove(org.eclipse.pde.core.plugin.IPluginImport)
237      */

238     public void remove(IPluginImport pluginImport) throws CoreException {
239         if (imports != null) {
240             imports.remove(pluginImport);
241             Object JavaDoc header = getManifestHeader(Constants.REQUIRE_BUNDLE);
242             if (header instanceof RequireBundleHeader) {
243                 ((RequireBundleHeader)header).removeBundle(pluginImport.getId());
244             }
245             fireStructureChanged(pluginImport, false);
246         }
247     }
248     
249     public void remove(IPluginImport[] pluginImports) throws CoreException {
250         if (imports != null) {
251             for (int i = 0; i < pluginImports.length; i++) {
252                 imports.remove(pluginImports[i]);
253                 Object JavaDoc header = getManifestHeader(Constants.REQUIRE_BUNDLE);
254                 if (header instanceof RequireBundleHeader) {
255                     ((RequireBundleHeader)header).removeBundle(pluginImports[i].getId());
256                 }
257             }
258             fireStructureChanged(pluginImports, false);
259         }
260     }
261
262     /*
263      * (non-Javadoc)
264      *
265      * @see org.eclipse.pde.core.plugin.IPluginBase#getLibraries()
266      */

267     public IPluginLibrary[] getLibraries() {
268         IBundle bundle = getBundle();
269         if (bundle == null)
270             return new IPluginLibrary[0];
271         if (libraries == null) {
272             libraries = new ArrayList JavaDoc();
273             String JavaDoc value = bundle.getHeader(Constants.BUNDLE_CLASSPATH);
274             if (value != null) {
275                 try {
276                     ManifestElement[] elements = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, value);
277                     for (int i = 0; i < elements.length; i++) {
278                         PluginLibrary library = new PluginLibrary();
279                         library.setModel(getModel());
280                         library.setInTheModel(true);
281                         library.setParent(this);
282                         library.load(elements[i].getValue());
283                         libraries.add(library);
284                     }
285                 } catch (BundleException e) {
286                 }
287             }
288         }
289         return (IPluginLibrary[]) libraries.toArray(new IPluginLibrary[libraries.size()]);
290     }
291     
292     /*
293      * (non-Javadoc)
294      *
295      * @see org.eclipse.pde.core.plugin.IPluginBase#getImports()
296      */

297     public IPluginImport[] getImports() {
298         if (imports == null) {
299             imports = new ArrayList JavaDoc();
300             BundleDescription description = model.getBundleDescription();
301             if (description != null) {
302                 BundleSpecification[] required = description.getRequiredBundles();
303                 for (int i = 0; i < required.length; i++) {
304                     PluginImport importElement = new PluginImport();
305                     importElement.setModel(getModel());
306                     importElement.setInTheModel(true);
307                     importElement.setParent(this);
308                     imports.add(importElement);
309                     importElement.load(required[i]);
310                 }
311                 BundleDescription[] imported = PDEStateHelper.getImportedBundles(description);
312                 for (int i = 0; i < imported.length; i++) {
313                     PluginImport importElement = new PluginImport();
314                     importElement.setModel(getModel());
315                     importElement.setInTheModel(true);
316                     importElement.setParent(this);
317                     imports.add(importElement);
318                     importElement.load(imported[i]);
319                 }
320             } else {
321                 IBundle bundle = getBundle();
322                 if (bundle != null) {
323                     try {
324                         String JavaDoc value = bundle.getHeader(Constants.REQUIRE_BUNDLE);
325                         int bundleManifestVersion = getBundleManifestVersion(bundle);
326                         if (value != null) {
327                             ManifestElement[] elements = ManifestElement.parseHeader(Constants.REQUIRE_BUNDLE, value);
328                             for (int i = 0; i < elements.length; i++) {
329                                 PluginImport importElement = new PluginImport();
330                                 importElement.setModel(getModel());
331                                 importElement.setInTheModel(true);
332                                 importElement.setParent(this);
333                                 imports.add(importElement);
334                                 importElement.load(elements[i], bundleManifestVersion);
335                             }
336                         }
337                     } catch (BundleException e) {
338                     }
339                 }
340             }
341         }
342         return (IPluginImport[])imports.toArray(new IPluginImport[imports.size()]);
343     }
344
345     /*
346      * (non-Javadoc)
347      *
348      * @see org.eclipse.pde.core.plugin.IPluginBase#getProviderName()
349      */

350     public String JavaDoc getProviderName() {
351         IBundle bundle = getBundle();
352         return bundle == null ? null : bundle.getHeader(Constants.BUNDLE_VENDOR);
353     }
354     
355     /*
356      * (non-Javadoc)
357      *
358      * @see org.eclipse.pde.core.plugin.IPluginBase#setProviderName(java.lang.String)
359      */

360     public void setProviderName(String JavaDoc providerName) throws CoreException {
361         IBundle bundle = getBundle();
362         if (bundle != null) {
363             String JavaDoc old = getProviderName();
364             IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_VENDOR);
365             if (header instanceof BundleVendorHeader)
366                 ((BundleVendorHeader)header).setVendor(providerName);
367             else
368                 bundle.setHeader(Constants.BUNDLE_VENDOR, providerName);
369             model.fireModelObjectChanged(this, IPluginBase.P_PROVIDER, old, providerName);
370         }
371     }
372
373     /*
374      * (non-Javadoc)
375      *
376      * @see org.eclipse.pde.core.plugin.IPluginBase#getVersion()
377      */

378     public String JavaDoc getVersion() {
379         BundleDescription desc = model.getBundleDescription();
380         if (desc != null) {
381             Version version = desc.getVersion();
382             return (version != null) ? version.toString() : null;
383         }
384         return getValue(Constants.BUNDLE_VERSION, false);
385     }
386     
387     /*
388      * (non-Javadoc)
389      *
390      * @see org.eclipse.pde.core.plugin.IPluginBase#setVersion(java.lang.String)
391      */

392     public void setVersion(String JavaDoc version) throws CoreException {
393         IBundle bundle = getBundle();
394         if (bundle != null) {
395             String JavaDoc old = getVersion();
396             IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_VERSION);
397             if (header instanceof BundleVersionHeader)
398                 ((BundleVersionHeader)header).setVersionRange(version);
399             else
400                 bundle.setHeader(Constants.BUNDLE_VERSION, version);
401             model.fireModelObjectChanged(this, IPluginBase.P_VERSION, old, version);
402         }
403     }
404
405     /*
406      * (non-Javadoc)
407      *
408      * @see org.eclipse.pde.core.plugin.IPluginBase#swap(org.eclipse.pde.core.plugin.IPluginLibrary,
409      * org.eclipse.pde.core.plugin.IPluginLibrary)
410      */

411     public void swap(IPluginLibrary l1, IPluginLibrary l2) throws CoreException {
412         if (libraries != null) {
413             int index1 = libraries.indexOf(l1);
414             int index2 = libraries.indexOf(l2);
415             libraries.set(index1, l2);
416             libraries.set(index2, l1);
417             Object JavaDoc header = getManifestHeader(Constants.BUNDLE_CLASSPATH);
418             if (header instanceof BundleClasspathHeader) {
419                 ((BundleClasspathHeader)header).swap(index1, index2);
420             }
421             model.fireModelObjectChanged(this, P_IMPORT_ORDER, l1, l2);
422         }
423     }
424     
425     protected void fireStructureChanged(Object JavaDoc object, boolean added) {
426         int type = (added)?IModelChangedEvent.INSERT:IModelChangedEvent.REMOVE;
427         model.fireModelChanged(new ModelChangedEvent(model, type, new Object JavaDoc[]{object}, null ));
428     }
429     
430     protected void fireStructureChanged(Object JavaDoc[] objects, boolean added) {
431         int type = (added)?IModelChangedEvent.INSERT:IModelChangedEvent.REMOVE;
432         model.fireModelChanged(new ModelChangedEvent(model, type, objects, null ));
433     }
434
435     /*
436      * (non-Javadoc)
437      *
438      * @see org.eclipse.pde.core.plugin.IExtensions#add(org.eclipse.pde.core.plugin.IPluginExtension)
439      */

440     public void add(IPluginExtension extension) throws CoreException {
441         IExtensions extensions = getExtensionsRoot();
442         if (extensions == null)
443             return;
444         extensions.add(extension);
445         
446         // reset singleton
447
if (getExtensions().length == 1 && getExtensionPoints().length == 0)
448             updateSingleton(true);
449     }
450
451     /*
452      * (non-Javadoc)
453      *
454      * @see org.eclipse.pde.core.plugin.IExtensions#add(org.eclipse.pde.core.plugin.IPluginExtensionPoint)
455      */

456     public void add(IPluginExtensionPoint point) throws CoreException {
457         IExtensions extensions = getExtensionsRoot();
458         if (extensions == null)
459             return;
460         extensions.add(point);
461         
462         //reset singleton
463
if (getExtensions().length == 0 && getExtensionPoints().length == 1)
464             updateSingleton(true);
465     }
466     
467     public String JavaDoc getResourceString(String JavaDoc key) {
468         return model.getResourceString(key);
469     }
470
471     /*
472      * (non-Javadoc)
473      *
474      * @see org.eclipse.pde.core.plugin.IExtensions#getExtensionPoints()
475      */

476     public IPluginExtensionPoint[] getExtensionPoints() {
477         IExtensions extensions = getExtensionsRoot();
478         if (extensions == null)
479             return new IPluginExtensionPoint[0];
480         return extensions.getExtensionPoints();
481     }
482
483     /*
484      * (non-Javadoc)
485      *
486      * @see org.eclipse.pde.core.plugin.IExtensions#getExtensions()
487      */

488     public IPluginExtension[] getExtensions() {
489         IExtensions extensions = getExtensionsRoot();
490         if (extensions == null)
491             return new IPluginExtension[0];
492         return extensions.getExtensions();
493     }
494     
495     public int getIndexOf(IPluginExtension e) {
496         IExtensions extensions = getExtensionsRoot();
497         if (extensions == null)
498             return -1;
499         return extensions.getIndexOf(e);
500     }
501
502     /*
503      * (non-Javadoc)
504      *
505      * @see org.eclipse.pde.core.plugin.IExtensions#remove(org.eclipse.pde.core.plugin.IPluginExtension)
506      */

507     public void remove(IPluginExtension extension) throws CoreException {
508         IExtensions extensions = getExtensionsRoot();
509         if (extensions != null) {
510             extensions.remove(extension);
511             // reset singleton directive
512
if (getExtensions().length == 0 && getExtensionPoints().length == 0)
513                 updateSingleton(false);
514         }
515     }
516
517     /*
518      * (non-Javadoc)
519      *
520      * @see org.eclipse.pde.core.plugin.IExtensions#remove(org.eclipse.pde.core.plugin.IPluginExtensionPoint)
521      */

522     public void remove(IPluginExtensionPoint extensionPoint)
523         throws CoreException {
524         IExtensions extensions = getExtensionsRoot();
525         if (extensions != null) {
526             extensions.remove(extensionPoint);
527             // reset singleton directive
528
if (getExtensions().length == 0 && getExtensionPoints().length == 0)
529                 updateSingleton(false);
530         }
531     }
532
533     protected void updateSingleton(boolean singleton) {
534         IManifestHeader header = getManifestHeader(Constants.BUNDLE_SYMBOLICNAME);
535         if (header instanceof BundleSymbolicNameHeader)
536             ((BundleSymbolicNameHeader)header).setSingleton(singleton);
537         else {
538             if (singleton) {
539                 String JavaDoc version = getBundle().getHeader(Constants.BUNDLE_MANIFESTVERSION);
540                 if (version == null)
541                     version = "1"; //$NON-NLS-1$
542
String JavaDoc value = header.getValue();
543                 String JavaDoc singletonValue = null;
544                 if (version != null && Integer.parseInt(version) >= 2)
545                     singletonValue = Constants.SINGLETON_DIRECTIVE + ":=true"; //$NON-NLS-1$
546
else
547                     singletonValue = Constants.SINGLETON_DIRECTIVE + "=true"; //$NON-NLS-1$
548
if (value.indexOf(singletonValue) != -1)
549                     return;
550                 getBundle().setHeader(Constants.BUNDLE_SYMBOLICNAME, value + "; " + singletonValue); //$NON-NLS-1$
551
}
552             // No current need to remove singleton directive outside of text model.
553
}
554     }
555
556     /*
557      * (non-Javadoc)
558      *
559      * @see org.eclipse.pde.core.plugin.IExtensions#swap(org.eclipse.pde.core.plugin.IPluginExtension,
560      * org.eclipse.pde.core.plugin.IPluginExtension)
561      */

562     public void swap(IPluginExtension e1, IPluginExtension e2)
563         throws CoreException {
564         IExtensions extensions = getExtensionsRoot();
565         if (extensions != null) {
566             extensions.swap(e1, e2);
567         }
568     }
569     
570     /* (non-Javadoc)
571      * @see org.eclipse.pde.core.plugin.IPluginBase#swap(org.eclipse.pde.core.plugin.IPluginImport, org.eclipse.pde.core.plugin.IPluginImport)
572      */

573     public void swap(IPluginImport import1, IPluginImport import2)
574             throws CoreException {
575         if (imports != null) {
576             int index1 = imports.indexOf(import1);
577             int index2 = imports.indexOf(import2);
578             imports.set(index1, import2);
579             imports.set(index2, import1);
580             Object JavaDoc header = getManifestHeader(Constants.REQUIRE_BUNDLE);
581             if (header instanceof RequireBundleHeader) {
582                 ((RequireBundleHeader)header).swap(index1, index2);
583             }
584             model.fireModelObjectChanged(this, P_IMPORT_ORDER, import1, import2);
585         }
586     }
587
588     /*
589      * (non-Javadoc)
590      *
591      * @see org.eclipse.pde.core.IIdentifiable#getId()
592      */

593     public String JavaDoc getId() {
594         return getValue(Constants.BUNDLE_SYMBOLICNAME, true);
595     }
596     
597     // The key should be a manifest header key, and parse should be true if it needs to be parsed by ManifestElement.parseHeader()
598
protected String JavaDoc getValue(String JavaDoc key, boolean parse) {
599         IBundle bundle = getBundle();
600         if (bundle == null)
601             return null;
602         String JavaDoc value = bundle.getHeader(key);
603         if (value == null || !parse)
604             return value;
605         try {
606             ManifestElement[] elements = ManifestElement.parseHeader(key, value);
607             if (elements.length > 0)
608                 return elements[0].getValue();
609         } catch (BundleException e) {
610         }
611         return null;
612     }
613         
614     /*
615      * (non-Javadoc)
616      *
617      * @see org.eclipse.pde.core.IIdentifiable#setId(java.lang.String)
618      */

619     public void setId(String JavaDoc id) throws CoreException {
620         IBundle bundle = getBundle();
621         if (bundle != null) {
622             String JavaDoc old = getId();
623             IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_SYMBOLICNAME);
624             if (header instanceof BundleSymbolicNameHeader)
625                 ((BundleSymbolicNameHeader)header).setId(id);
626             else
627                 bundle.setHeader(Constants.BUNDLE_SYMBOLICNAME, id);
628             model.fireModelObjectChanged(this, IIdentifiable.P_ID, old, id);
629         }
630     }
631
632     /*
633      * (non-Javadoc)
634      *
635      * @see org.eclipse.pde.core.plugin.IPluginObject#getPluginModel()
636      */

637     public IPluginModelBase getPluginModel() {
638         return model;
639     }
640
641     /*
642      * (non-Javadoc)
643      *
644      * @see org.eclipse.pde.core.plugin.IPluginObject#getName()
645      */

646     public String JavaDoc getName() {
647         return getValue(Constants.BUNDLE_NAME, false);
648     }
649
650     /*
651      * (non-Javadoc)
652      *
653      * @see org.eclipse.pde.core.plugin.IPluginObject#setName(java.lang.String)
654      */

655     public void setName(String JavaDoc name) throws CoreException {
656         IBundle bundle = getBundle();
657         if (bundle != null) {
658             String JavaDoc old = getName();
659             IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_NAME);
660             if (header instanceof BundleNameHeader)
661                 ((BundleNameHeader)header).setBundleName(name);
662             else
663                 bundle.setHeader(Constants.BUNDLE_NAME, name);
664             model.fireModelObjectChanged(this, IPluginObject.P_NAME, old, name);
665         }
666     }
667     
668     /*
669      * (non-Javadoc)
670      *
671      * @see org.eclipse.pde.core.plugin.IPluginObject#isInTheModel()
672      */

673     public boolean isInTheModel() {
674         return model != null;
675     }
676
677     /*
678      * (non-Javadoc)
679      *
680      * @see org.eclipse.pde.core.plugin.IPluginObject#getTranslatedName()
681      */

682     public String JavaDoc getTranslatedName() {
683         return getResourceString(getName());
684     }
685
686     /*
687      * (non-Javadoc)
688      *
689      * @see org.eclipse.pde.core.plugin.IPluginObject#getParent()
690      */

691     public IPluginObject getParent() {
692         return null;
693     }
694
695     /*
696      * (non-Javadoc)
697      *
698      * @see org.eclipse.pde.core.plugin.IPluginObject#getPluginBase()
699      */

700     public IPluginBase getPluginBase() {
701         return this;
702     }
703
704     /*
705      * (non-Javadoc)
706      *
707      * @see org.eclipse.pde.core.plugin.IPluginObject#isValid()
708      */

709     public boolean isValid() {
710         IExtensions extensions = getExtensionsRoot();
711         return getBundle() != null
712             && getBundle().getHeader(Constants.BUNDLE_SYMBOLICNAME) != null
713             && (extensions == null || extensions.isValid());
714     }
715
716     /*
717      * (non-Javadoc)
718      *
719      * @see org.eclipse.pde.core.IWritable#write(java.lang.String,
720      * java.io.PrintWriter)
721      */

722     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
723     }
724
725     /* (non-Javadoc)
726      * @see org.eclipse.pde.core.plugin.IPluginObject#setInTheModel(boolean)
727      */

728     public void setInTheModel(boolean inModel) {
729     }
730
731     static public int getBundleManifestVersion(IBundle bundle) {
732         String JavaDoc version = bundle.getHeader(Constants.BUNDLE_MANIFESTVERSION);
733         if (version == null)
734             return 1; // default to 1
735
try {
736             return Integer.parseInt(version);
737         } catch (NumberFormatException JavaDoc e) {
738             return 1; // default to 1
739
}
740     }
741
742     public void updateImport(IPluginImport iimport) {
743         Object JavaDoc header = getManifestHeader(Constants.REQUIRE_BUNDLE);
744         if (header instanceof RequireBundleHeader && imports != null)
745             ((RequireBundleHeader)header).updateBundle(imports.indexOf(iimport), iimport);
746     }
747
748     public String JavaDoc getTargetVersion() {
749         return fTarget != null ? fTarget : TargetPlatformHelper.getTargetVersionString();
750     }
751
752     public void setTargetVersion(String JavaDoc target) {
753         fTarget = target;
754     }
755
756 }
757
Popular Tags