KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > text > bundle > RequireBundleHeader


1 /*******************************************************************************
2  * Copyright (c) 2005 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.text.bundle;
12
13 import org.eclipse.osgi.util.ManifestElement;
14 import org.eclipse.pde.core.plugin.IPluginImport;
15 import org.eclipse.pde.internal.core.ICoreConstants;
16 import org.eclipse.pde.internal.core.bundle.BundlePluginBase;
17 import org.eclipse.pde.internal.core.ibundle.IBundle;
18 import org.osgi.framework.Constants;
19
20 public class RequireBundleHeader extends CompositeManifestHeader {
21
22     private static final long serialVersionUID = 1L;
23
24     public RequireBundleHeader(String JavaDoc name, String JavaDoc value, IBundle bundle, String JavaDoc lineDelimiter) {
25         super(name, value, bundle, lineDelimiter);
26     }
27     
28     public void addBundle(IPluginImport iimport) {
29         addBundle(iimport.getId(), iimport.getVersion(), iimport.isReexported(), iimport.isOptional());
30     }
31     
32     public void addBundle(String JavaDoc id) {
33         addBundle(id, null, false, false);
34     }
35     
36     public void addBundle(String JavaDoc id, String JavaDoc version, boolean exported, boolean optional) {
37         RequireBundleObject element = new RequireBundleObject(this, id);
38
39         int bundleManifestVersion = BundlePluginBase.getBundleManifestVersion(getBundle());
40         if (optional)
41             if (bundleManifestVersion > 1)
42                 element.setDirective(Constants.RESOLUTION_DIRECTIVE, Constants.RESOLUTION_OPTIONAL);
43             else
44                 element.setAttribute(ICoreConstants.OPTIONAL_ATTRIBUTE, "true"); //$NON-NLS-1$
45

46         if (exported)
47             if (bundleManifestVersion > 1)
48                 element.setDirective(Constants.VISIBILITY_DIRECTIVE, Constants.VISIBILITY_REEXPORT);
49             else
50                 element.setAttribute(ICoreConstants.REPROVIDE_ATTRIBUTE, "true"); //$NON-NLS-1$
51

52         if (version != null && version.trim().length() > 0)
53             element.setAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE, version.trim());
54
55         addManifestElement(element);
56     }
57     
58     public void removeBundle(String JavaDoc id) {
59         removeManifestElement(id);
60     }
61     
62     public void removeBundle(RequireBundleObject bundle) {
63         removeManifestElement(bundle);
64     }
65     
66     protected PDEManifestElement createElement(ManifestElement element) {
67         return new RequireBundleObject(this, element);
68     }
69     
70     public void updateBundle(int index, IPluginImport iimport) {
71         if (index == -1)
72             return;
73         
74         PDEManifestElement element = getElementAt(index);
75         if (element != null) {
76             element.setValue(iimport.getId());
77
78             int bundleManifestVersion = BundlePluginBase.getBundleManifestVersion(getBundle());
79             if (iimport.isOptional()) {
80                 if (bundleManifestVersion > 1)
81                     element.setDirective(Constants.RESOLUTION_DIRECTIVE, Constants.RESOLUTION_OPTIONAL);
82                 else
83                     element.setAttribute(ICoreConstants.OPTIONAL_ATTRIBUTE, "true"); //$NON-NLS-1$
84
} else {
85                 if (bundleManifestVersion > 1)
86                     element.setDirective(Constants.RESOLUTION_DIRECTIVE, null);
87                 else
88                     element.setAttribute(ICoreConstants.OPTIONAL_ATTRIBUTE, null);
89             }
90             
91             if (iimport.isReexported()) {
92                 if (bundleManifestVersion > 1)
93                     element.setDirective(Constants.VISIBILITY_DIRECTIVE, Constants.VISIBILITY_REEXPORT);
94                 else
95                     element.setAttribute(ICoreConstants.REPROVIDE_ATTRIBUTE, "true"); //$NON-NLS-1$
96
} else {
97                 if (bundleManifestVersion > 1)
98                     element.setDirective(Constants.VISIBILITY_DIRECTIVE, null);
99                 else
100                     element.setAttribute(ICoreConstants.REPROVIDE_ATTRIBUTE, null);
101             }
102             element.setAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE, iimport.getVersion());
103         }
104         update(true);
105     }
106     
107     public RequireBundleObject[] getRequiredBundles() {
108         PDEManifestElement[] elements = getElements();
109         RequireBundleObject[] result = new RequireBundleObject[elements.length];
110         System.arraycopy(elements, 0, result, 0, elements.length);
111         return result;
112     }
113     
114 }
115
Popular Tags