KickJava   Java API By Example, From Geeks To Geeks.

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


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.pde.internal.core.plugin;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Locale JavaDoc;
15
16 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
17 import javax.xml.parsers.ParserConfigurationException JavaDoc;
18 import javax.xml.parsers.SAXParser JavaDoc;
19 import javax.xml.parsers.SAXParserFactory JavaDoc;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.osgi.service.resolver.BundleDescription;
23 import org.eclipse.osgi.service.resolver.BundleSpecification;
24 import org.eclipse.osgi.service.resolver.VersionRange;
25 import org.eclipse.pde.core.IModelChangedEvent;
26 import org.eclipse.pde.core.plugin.IMatchRules;
27 import org.eclipse.pde.core.plugin.IPluginBase;
28 import org.eclipse.pde.core.plugin.IPluginImport;
29 import org.eclipse.pde.core.plugin.IPluginLibrary;
30 import org.eclipse.pde.internal.core.PDECoreMessages;
31 import org.eclipse.pde.internal.core.PDEState;
32 import org.eclipse.pde.internal.core.PDEStateHelper;
33 import org.osgi.framework.Version;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38 public abstract class PluginBase extends AbstractExtensions implements IPluginBase {
39     private static final Version maxVersion = new Version(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
40
41     private ArrayList JavaDoc fLibraries = new ArrayList JavaDoc();
42     private ArrayList JavaDoc fImports = new ArrayList JavaDoc();
43     private String JavaDoc fProviderName;
44     private String JavaDoc fId;
45     private String JavaDoc fVersion;
46     private boolean fHasBundleStructure;
47
48     public void add(IPluginLibrary library) throws CoreException {
49         ensureModelEditable();
50         fLibraries.add(library);
51         ((PluginLibrary) library).setInTheModel(true);
52         ((PluginLibrary) library).setParent(this);
53         fireStructureChanged(library, IModelChangedEvent.INSERT);
54     }
55     public void add(IPluginImport iimport) throws CoreException {
56         ensureModelEditable();
57         ((PluginImport) iimport).setInTheModel(true);
58         ((PluginImport) iimport).setParent(this);
59         fImports.add(iimport);
60         fireStructureChanged(iimport, IModelChangedEvent.INSERT);
61     }
62     public void add(IPluginImport[] iimports) throws CoreException {
63         ensureModelEditable();
64         for (int i = 0; i < iimports.length; i++) {
65             ((PluginImport) iimports[i]).setInTheModel(true);
66             ((PluginImport) iimports[i]).setParent(this);
67             fImports.add(iimports[i]);
68         }
69         fireStructureChanged(iimports, IModelChangedEvent.INSERT);
70     }
71     public IPluginLibrary[] getLibraries() {
72         return (IPluginLibrary[])fLibraries.toArray(new IPluginLibrary[fLibraries.size()]);
73     }
74     public IPluginImport[] getImports() {
75         return (IPluginImport[])fImports.toArray(new IPluginImport[fImports.size()]);
76     }
77     public IPluginBase getPluginBase() {
78         return this;
79     }
80     public String JavaDoc getProviderName() {
81         return fProviderName;
82     }
83     public String JavaDoc getVersion() {
84         return fVersion;
85     }
86     public String JavaDoc getId() {
87         return fId;
88     }
89
90     void load(BundleDescription bundleDesc, PDEState state) {
91         fId = bundleDesc.getSymbolicName();
92         fVersion = bundleDesc.getVersion().toString();
93         fName = state.getPluginName(bundleDesc.getBundleId());
94         fProviderName = state.getProviderName(bundleDesc.getBundleId());
95         fSchemaVersion = state.getSchemaVersion(bundleDesc.getBundleId());
96         fHasBundleStructure = state.hasBundleStructure(bundleDesc.getBundleId());
97         loadRuntime(bundleDesc, state);
98         loadImports(bundleDesc);
99         loadExtensionPoints(state.getExtensionPoints(bundleDesc.getBundleId()));
100         loadExtensions(state.getExtensions(bundleDesc.getBundleId()));
101     }
102     
103     void loadExtensions(Node JavaDoc[] list) {
104         fExtensions = new ArrayList JavaDoc();
105         for (int i = 0; i < list.length; i++) {
106             PluginExtension extension = new PluginExtension();
107             extension.setInTheModel(true);
108             extension.setModel(getModel());
109             extension.setParent(this);
110             extension.load(list[i]);
111             fExtensions.add(extension);
112         }
113     }
114     
115     void loadExtensionPoints(Node JavaDoc[] list) {
116         fExtensionPoints = new ArrayList JavaDoc(list.length);
117         for (int i = 0; i < list.length; i++) {
118             PluginExtensionPoint extPoint = new PluginExtensionPoint();
119             extPoint.setInTheModel(true);
120             extPoint.setModel(getModel());
121             extPoint.setParent(this);
122             extPoint.load(list[i]);
123             fExtensionPoints.add(extPoint);
124         }
125     }
126     
127     public void restoreProperty(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue)
128         throws CoreException {
129         if (name.equals(P_ID)) {
130             setId(newValue != null ? newValue.toString() : null);
131             return;
132         }
133         if (name.equals(P_VERSION)) {
134             setVersion(newValue != null ? newValue.toString() : null);
135             return;
136         }
137         if (name.equals(P_PROVIDER)) {
138             setProviderName(newValue != null ? newValue.toString() : null);
139             return;
140         }
141         if (name.equals(P_LIBRARY_ORDER)) {
142             swap((IPluginLibrary) oldValue, (IPluginLibrary) newValue);
143             return;
144         }
145         super.restoreProperty(name, oldValue, newValue);
146     }
147
148     void load(Node JavaDoc node, String JavaDoc schemaVersion) {
149         if (node == null)
150             return;
151         fSchemaVersion = schemaVersion;
152         fId = getNodeAttribute(node, "id"); //$NON-NLS-1$
153
fName = getNodeAttribute(node, "name"); //$NON-NLS-1$
154
fProviderName = getNodeAttribute(node, "provider-name"); //$NON-NLS-1$
155
fVersion = getNodeAttribute(node, "version"); //$NON-NLS-1$
156

157         NodeList JavaDoc children = node.getChildNodes();
158         for (int i = 0; i < children.getLength(); i++) {
159             Node JavaDoc child = children.item(i);
160             if (child.getNodeType() == Node.ELEMENT_NODE) {
161                 processChild(child);
162             }
163         }
164     }
165
166     void loadRuntime(BundleDescription description, PDEState state) {
167         String JavaDoc[] libraryNames = state.getLibraryNames(description.getBundleId());
168         for (int i = 0; i < libraryNames.length; i++) {
169             PluginLibrary library = new PluginLibrary();
170             library.setModel(getModel());
171             library.setInTheModel(true);
172             library.setParent(this);
173             library.load(libraryNames[i]);
174             fLibraries.add(library);
175         }
176     }
177
178     void loadRuntime(Node JavaDoc node) {
179         NodeList JavaDoc children = node.getChildNodes();
180         for (int i = 0; i < children.getLength(); i++) {
181             Node JavaDoc child = children.item(i);
182             if (child.getNodeType() == Node.ELEMENT_NODE
183                 && child.getNodeName().toLowerCase(Locale.ENGLISH).equals("library")) { //$NON-NLS-1$
184
PluginLibrary library = new PluginLibrary();
185                 library.setModel(getModel());
186                 library.setInTheModel(true);
187                 library.setParent(this);
188                 fLibraries.add(library);
189                 library.load(child);
190             }
191         }
192     }
193
194     void loadImports(BundleDescription description) {
195         BundleSpecification[] required = description.getRequiredBundles();
196         for (int i = 0; i < required.length; i++) {
197             PluginImport importElement = new PluginImport();
198             importElement.setModel(getModel());
199             importElement.setInTheModel(true);
200             importElement.setParent(this);
201             fImports.add(importElement);
202             importElement.load(required[i]);
203         }
204         BundleDescription[] imported = PDEStateHelper.getImportedBundles(description);
205         for (int i = 0; i < imported.length; i++) {
206             PluginImport importElement = new PluginImport();
207             importElement.setModel(getModel());
208             importElement.setInTheModel(true);
209             importElement.setParent(this);
210             fImports.add(importElement);
211             importElement.load(imported[i]);
212         }
213     }
214     
215     void loadImports(Node JavaDoc node) {
216         NodeList JavaDoc children = node.getChildNodes();
217         for (int i = 0; i < children.getLength(); i++) {
218             Node JavaDoc child = children.item(i);
219             if (child.getNodeType() == Node.ELEMENT_NODE
220                 && child.getNodeName().toLowerCase(Locale.ENGLISH).equals("import")) { //$NON-NLS-1$
221
PluginImport importElement = new PluginImport();
222                 importElement.setModel(getModel());
223                 importElement.setInTheModel(true);
224                 importElement.setParent(this);
225                 fImports.add(importElement);
226                 importElement.load(child);
227             }
228         }
229     }
230     protected void processChild(Node JavaDoc child) {
231         String JavaDoc name = child.getNodeName().toLowerCase(Locale.ENGLISH);
232         if (name.equals("runtime")) { //$NON-NLS-1$
233
loadRuntime(child);
234         } else if (name.equals("requires")) { //$NON-NLS-1$
235
loadImports(child);
236         } else {
237             super.processChild(child);
238         }
239     }
240
241     public void remove(IPluginLibrary library) throws CoreException {
242         ensureModelEditable();
243         fLibraries.remove(library);
244         ((PluginLibrary) library).setInTheModel(false);
245         fireStructureChanged(library, IModelChangedEvent.REMOVE);
246     }
247     public void remove(IPluginImport iimport) throws CoreException {
248         ensureModelEditable();
249         fImports.remove(iimport);
250         ((PluginImport) iimport).setInTheModel(false);
251         fireStructureChanged(iimport, IModelChangedEvent.REMOVE);
252     }
253     public void remove(IPluginImport[] iimports) throws CoreException {
254         ensureModelEditable();
255         for (int i = 0; i < iimports.length; i++){
256             fImports.remove(iimports[i]);
257             ((PluginImport) iimports[i]).setInTheModel(false);
258         }
259         fireStructureChanged(iimports, IModelChangedEvent.REMOVE);
260     }
261     public void reset() {
262         fLibraries = new ArrayList JavaDoc();
263         fImports = new ArrayList JavaDoc();
264         fProviderName = null;
265         fSchemaVersion = null;
266         fVersion = ""; //$NON-NLS-1$
267
fName = ""; //$NON-NLS-1$
268
fId = ""; //$NON-NLS-1$
269
if (getModel() != null && getModel().getUnderlyingResource() != null) {
270             fId = getModel().getUnderlyingResource().getProject().getName();
271             fName = fId;
272             fVersion = "0.0.0"; //$NON-NLS-1$
273
}
274         super.reset();
275     }
276     
277     public void setProviderName(String JavaDoc providerName) throws CoreException {
278         ensureModelEditable();
279         String JavaDoc oldValue = fProviderName;
280         fProviderName = providerName;
281         firePropertyChanged(P_PROVIDER, oldValue, fProviderName);
282     }
283     
284     public void setVersion(String JavaDoc newVersion) throws CoreException {
285         ensureModelEditable();
286         String JavaDoc oldValue = fVersion;
287         fVersion = newVersion;
288         firePropertyChanged(P_VERSION, oldValue, fVersion);
289     }
290     
291     public void setId(String JavaDoc newId) throws CoreException {
292         ensureModelEditable();
293         String JavaDoc oldValue = fId;
294         fId = newId;
295         firePropertyChanged(P_ID, oldValue, fId);
296     }
297
298     public void internalSetVersion(String JavaDoc newVersion) {
299         fVersion = newVersion;
300     }
301
302     public void swap(IPluginLibrary l1, IPluginLibrary l2)
303         throws CoreException {
304         ensureModelEditable();
305         int index1 = fLibraries.indexOf(l1);
306         int index2 = fLibraries.indexOf(l2);
307         if (index1 == -1 || index2 == -1)
308             throwCoreException(PDECoreMessages.PluginBase_librariesNotFoundException);
309         fLibraries.set(index2, l1);
310         fLibraries.set(index1, l2);
311         firePropertyChanged(this, P_LIBRARY_ORDER, l1, l2);
312     }
313     
314     /* (non-Javadoc)
315      * @see org.eclipse.pde.core.plugin.IPluginBase#swap(org.eclipse.pde.core.plugin.IPluginImport, org.eclipse.pde.core.plugin.IPluginImport)
316      */

317     public void swap(IPluginImport import1, IPluginImport import2)
318             throws CoreException {
319         ensureModelEditable();
320         int index1 = fImports.indexOf(import1);
321         int index2 = fImports.indexOf(import2);
322         if (index1 == -1 || index2 == -1)
323             throwCoreException(PDECoreMessages.PluginBase_importsNotFoundException);
324         fImports.set(index2, import1);
325         fImports.set(index1, import2);
326         firePropertyChanged(this, P_IMPORT_ORDER, import1, import2);
327     }
328
329     public boolean isValid() {
330         return hasRequiredAttributes();
331     }
332     protected boolean hasRequiredAttributes(){
333         if (fName==null) return false;
334         if (fId==null) return false;
335         if (fVersion==null) return false;
336
337         // validate libraries
338
for (int i = 0; i < fLibraries.size(); i++) {
339             IPluginLibrary library = (IPluginLibrary)fLibraries.get(i);
340             if (!library.isValid()) return false;
341         }
342         // validate imports
343
for (int i = 0; i < fImports.size(); i++) {
344             IPluginImport iimport = (IPluginImport)fImports.get(i);
345             if (!iimport.isValid()) return false;
346         }
347         return super.hasRequiredAttributes();
348     }
349     
350     protected SAXParser JavaDoc getSaxParser() throws ParserConfigurationException JavaDoc, SAXException JavaDoc, FactoryConfigurationError JavaDoc {
351         return SAXParserFactory.newInstance().newSAXParser();
352     }
353
354     public static int getMatchRule(VersionRange versionRange) {
355         if (versionRange == null || versionRange.getMinimum() == null)
356             return IMatchRules.NONE;
357
358         Version minimum = versionRange.getMinimum();
359         Version maximum = versionRange.getMaximum() == null ? maxVersion : versionRange.getMaximum();
360
361         if (maximum.compareTo(maxVersion) >= 0)
362             return IMatchRules.GREATER_OR_EQUAL;
363         else if (minimum.equals(maximum))
364             return IMatchRules.PERFECT;
365         else if (!versionRange.isIncluded(minimum) || versionRange.isIncluded(maximum))
366             return IMatchRules.NONE; // no real match rule for this
367
else if (minimum.getMajor() == maximum.getMajor() - 1)
368             return IMatchRules.COMPATIBLE;
369         else if (minimum.getMajor() != maximum.getMajor())
370             return IMatchRules.NONE; // no real match rule for this
371
else if (minimum.getMinor() == maximum.getMinor() - 1)
372             return IMatchRules.EQUIVALENT;
373         else if (minimum.getMinor() != maximum.getMinor())
374             return IMatchRules.NONE; // no real match rule for this
375
else if (minimum.getMicro() == maximum.getMicro() - 1)
376             return IMatchRules.PERFECT; // this is as close as we got
377

378         return IMatchRules.NONE; // no real match rule for this
379
}
380     
381     public boolean hasBundleStructure() {
382         return fHasBundleStructure;
383     }
384     
385 }
386
Popular Tags