KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > plugin > PluginImport


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.plugin;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.io.Serializable JavaDoc;
15 import java.util.Locale JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.osgi.service.resolver.BundleDescription;
19 import org.eclipse.osgi.service.resolver.BundleSpecification;
20 import org.eclipse.osgi.service.resolver.VersionRange;
21 import org.eclipse.osgi.util.ManifestElement;
22 import org.eclipse.pde.core.plugin.IMatchRules;
23 import org.eclipse.pde.core.plugin.IPluginImport;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.core.plugin.IPluginObject;
26 import org.eclipse.pde.core.plugin.ISharedPluginModel;
27 import org.eclipse.pde.internal.core.ICoreConstants;
28 import org.eclipse.pde.internal.core.bundle.BundlePluginBase;
29 import org.eclipse.pde.internal.core.ibundle.IBundle;
30 import org.eclipse.pde.internal.core.ibundle.IBundleModel;
31 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
32 import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
33 import org.eclipse.pde.internal.core.text.bundle.ManifestHeader;
34 import org.eclipse.pde.internal.core.text.bundle.RequireBundleObject;
35 import org.osgi.framework.Constants;
36 import org.w3c.dom.Node JavaDoc;
37
38 public class PluginImport extends IdentifiablePluginObject implements
39         IPluginImport, Serializable JavaDoc {
40
41     private static final long serialVersionUID = 1L;
42     private int match = NONE;
43     private boolean reexported = false;
44     private boolean optional = false;
45     private String JavaDoc version;
46
47     public PluginImport() {
48     }
49     
50     public PluginImport(ISharedPluginModel model, String JavaDoc id) {
51         try {
52             setModel(model);
53             ensureModelEditable();
54             this.fID = id;
55         } catch (CoreException e) {
56         }
57     }
58     
59     public boolean isValid() {
60         return getId()!=null;
61     }
62
63     public int getMatch() {
64         return match;
65     }
66
67     public String JavaDoc getVersion() {
68         return version;
69     }
70
71     public boolean isReexported() {
72         return reexported;
73     }
74
75     public boolean isOptional() {
76         return optional;
77     }
78
79     public void load(BundleDescription description) {
80         this.fID = description.getSymbolicName();
81     }
82     
83     public void load(ManifestElement element, int bundleManifestVersion) {
84         this.fID = element.getValue();
85         if (bundleManifestVersion >= 2) {
86             this.optional = Constants.RESOLUTION_OPTIONAL.equals(element.getDirective(Constants.RESOLUTION_DIRECTIVE));
87             this.reexported = Constants.VISIBILITY_REEXPORT.equals(element.getDirective(Constants.VISIBILITY_DIRECTIVE));
88         }
89         else {
90             this.optional = "true".equals(element.getAttribute(ICoreConstants.OPTIONAL_ATTRIBUTE)); //$NON-NLS-1$
91
this.reexported ="true".equals(element.getAttribute(ICoreConstants.REPROVIDE_ATTRIBUTE)); //$NON-NLS-1$
92
}
93         String JavaDoc bundleVersion = element.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE);
94         if (bundleVersion != null) {
95             try {
96                 VersionRange versionRange = new VersionRange(bundleVersion);
97                 this.version = bundleVersion;
98                 this.match = PluginBase.getMatchRule(versionRange);
99             } catch (IllegalArgumentException JavaDoc e) {
100             }
101         }
102     }
103     
104     public void load(BundleSpecification importModel) {
105         this.fID = importModel.getName();
106         this.reexported = importModel.isExported();
107         this.optional = importModel.isOptional();
108         VersionRange versionRange = importModel.getVersionRange();
109         if (versionRange == null || VersionRange.emptyRange.equals(versionRange)) {
110             this.version = null;
111             match = IMatchRules.NONE;
112         } else {
113             this.version = versionRange.getMinimum() != null ? versionRange.getMinimum().toString() : null;
114             match = PluginBase.getMatchRule(versionRange);
115         }
116     }
117
118     public boolean equals(Object JavaDoc obj) {
119         if (obj == this)
120             return true;
121         if (obj == null)
122             return false;
123         if (obj instanceof IPluginImport) {
124             IPluginImport target = (IPluginImport) obj;
125             // Objects from the same model must be
126
// binary equal
127
if (target.getModel().equals(getModel()))
128                 return false;
129
130             if (target.getId().equals(getId())
131                 && target.isReexported() == isReexported()
132                 && stringEqualWithNull(target.getVersion(),getVersion())
133                 && target.getMatch() == getMatch()
134                 && target.isOptional() == isOptional())
135                 return true;
136         }
137         return false;
138     }
139
140     void load(Node JavaDoc node) {
141         String JavaDoc id = getNodeAttribute(node, "plugin"); //$NON-NLS-1$
142
String JavaDoc export = getNodeAttribute(node, "export"); //$NON-NLS-1$
143
String JavaDoc option = getNodeAttribute(node, "optional"); //$NON-NLS-1$
144
String JavaDoc version = getNodeAttribute(node, "version"); //$NON-NLS-1$
145
String JavaDoc match = getNodeAttribute(node, "match"); //$NON-NLS-1$
146
boolean reexport =
147             export != null && export.toLowerCase(Locale.ENGLISH).equals("true"); //$NON-NLS-1$
148
boolean optional =
149             option != null && option.toLowerCase(Locale.ENGLISH).equals("true"); //$NON-NLS-1$
150
this.match = NONE;
151         if (match != null) {
152             String JavaDoc lmatch = match.toLowerCase(Locale.ENGLISH);
153             if (lmatch.equals("exact")) //$NON-NLS-1$
154
lmatch = RULE_EQUIVALENT;
155             for (int i = 0; i < RULE_NAME_TABLE.length; i++) {
156                 if (lmatch.equals(RULE_NAME_TABLE[i])) {
157                     this.match = i;
158                     break;
159                 }
160             }
161         }
162         this.version = version;
163         this.fID = id;
164         this.reexported = reexport;
165         this.optional = optional;
166     }
167     public void setMatch(int match) throws CoreException {
168         ensureModelEditable();
169         Integer JavaDoc oldValue = new Integer JavaDoc(this.match);
170         this.match = match;
171         firePropertyChanged(P_MATCH, oldValue, new Integer JavaDoc(match));
172     }
173     public void setReexported(boolean value) throws CoreException {
174         ensureModelEditable();
175         Boolean JavaDoc oldValue = new Boolean JavaDoc(reexported);
176         this.reexported = value;
177         firePropertyChanged(P_REEXPORTED, oldValue, new Boolean JavaDoc(value));
178     }
179     public void setOptional(boolean value) throws CoreException {
180         ensureModelEditable();
181         Boolean JavaDoc oldValue = new Boolean JavaDoc(this.optional);
182         this.optional = value;
183         firePropertyChanged(P_OPTIONAL, oldValue, new Boolean JavaDoc(value));
184     }
185     public void setVersion(String JavaDoc version) throws CoreException {
186         ensureModelEditable();
187         String JavaDoc oldValue = this.version;
188         this.version = version;
189         firePropertyChanged(P_VERSION, oldValue, version);
190     }
191
192     public void restoreProperty(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue)
193         throws CoreException {
194         if (name.equals(P_MATCH)) {
195             setMatch(((Integer JavaDoc) newValue).intValue());
196             return;
197         }
198         if (name.equals(P_REEXPORTED)) {
199             setReexported(((Boolean JavaDoc) newValue).booleanValue());
200             return;
201         }
202         if (name.equals(P_OPTIONAL)) {
203             setOptional(((Boolean JavaDoc) newValue).booleanValue());
204             return;
205         }
206         if (name.equals(P_VERSION)) {
207             setVersion(newValue != null ? newValue.toString() : null);
208             return;
209         }
210         super.restoreProperty(name, oldValue, newValue);
211     }
212
213     /* (non-Javadoc)
214      * @see org.eclipse.pde.core.IWritable#write(java.lang.String, java.io.PrintWriter)
215      */

216     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
217         // This is a round-about way to do this; but, leveraging existing
218
// functionality is key. The fact we have to do this suggests a model
219
// limitation.
220
// Emulating the behaviour of the text edit operations.
221
// RequireBundleObjects are created from PluginImport objects and have
222
// access to the MANIFEST.MF write mechanism
223

224         // Get the model
225
IPluginModelBase modelBase = getPluginModel();
226         // Ensure the model is a bundle model
227
if ((modelBase instanceof IBundlePluginModelBase) == false) {
228             writer.print(indent);
229             writer.print("<import plugin=\"" + getId() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
230
if (isReexported())
231                 writer.print(" export=\"true\""); //$NON-NLS-1$
232
if (isOptional())
233                 writer.print(" optional=\"true\""); //$NON-NLS-1$
234
if (version != null && version.length() > 0)
235                 writer.print(" version=\"" + version + "\""); //$NON-NLS-1$ //$NON-NLS-2$
236
if (match != NONE && match != COMPATIBLE) {
237                 String JavaDoc matchValue = RULE_NAME_TABLE[match];
238                 writer.print(" match=\"" + matchValue + "\""); //$NON-NLS-1$ //$NON-NLS-2$
239
}
240             writer.println("/>"); //$NON-NLS-1$
241
return;
242         }
243         IBundleModel bundleModel = ((IBundlePluginModelBase)modelBase).getBundleModel();
244         // Ensure the bundle manifest is present
245
if (bundleModel == null) {
246             return;
247         }
248         // Get the bundle
249
IBundle bundle = bundleModel.getBundle();
250         // Get the require bundle manifest header
251
IManifestHeader manifestHeader = bundle.getManifestHeader(Constants.REQUIRE_BUNDLE);
252         // Ensure the header was found (it has to be there since the calling
253
// of this method is a result of a copy operation)
254
if ((manifestHeader instanceof ManifestHeader) == false) {
255             return;
256         }
257         ManifestHeader header = (ManifestHeader)manifestHeader;
258         // Create the new temporary require bundle object (used only for
259
// writing)
260
RequireBundleObject element = new RequireBundleObject(header, fID);
261         // Get the manifest version for backwards compatibility
262
int bundleManifestVersion = BundlePluginBase.getBundleManifestVersion(bundle);
263         // Configure its properties using the values of this object
264
// Field: Optional
265
if (optional) {
266             if (bundleManifestVersion > 1) {
267                 element.setDirective(Constants.RESOLUTION_DIRECTIVE, Constants.RESOLUTION_OPTIONAL);
268             } else {
269                 element.setAttribute(ICoreConstants.OPTIONAL_ATTRIBUTE, "true"); //$NON-NLS-1$
270
}
271         }
272         // Field: Re-exported
273
if (reexported) {
274             if (bundleManifestVersion > 1) {
275                 element.setDirective(Constants.VISIBILITY_DIRECTIVE, Constants.VISIBILITY_REEXPORT);
276             } else {
277                 element.setAttribute(ICoreConstants.REPROVIDE_ATTRIBUTE, "true"); //$NON-NLS-1$
278
}
279         }
280         // Field: Version
281
if ((version != null) &&
282                 (version.trim().length() > 0)) {
283             element.setAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE, version.trim());
284         }
285         // Write the textual representation
286
writer.print(element.write());
287     }
288     
289     /* (non-Javadoc)
290      * @see org.eclipse.pde.internal.core.plugin.IdentifiablePluginObject#reconnect(org.eclipse.pde.core.plugin.ISharedPluginModel, org.eclipse.pde.core.plugin.IPluginObject)
291      */

292     public void reconnect(ISharedPluginModel model, IPluginObject parent) {
293         super.reconnect(model, parent);
294         // No transient fields
295
}
296     
297     /* (non-Javadoc)
298      * @see org.eclipse.pde.internal.core.plugin.PluginObject#writeDelimeter(java.io.PrintWriter)
299      */

300     public void writeDelimeter(PrintWriter JavaDoc writer) {
301         writer.println(',');
302         writer.print(' ');
303     }
304     
305 }
306
Popular Tags