KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.CoreException;
14 import org.eclipse.osgi.service.resolver.VersionRange;
15 import org.eclipse.osgi.util.ManifestElement;
16 import org.eclipse.pde.internal.core.ICoreConstants;
17 import org.eclipse.pde.internal.core.ibundle.IBundle;
18 import org.eclipse.pde.internal.core.ibundle.IBundleFragment;
19 import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
20 import org.eclipse.pde.internal.core.plugin.PluginBase;
21 import org.eclipse.pde.internal.core.text.bundle.FragmentHostHeader;
22 import org.osgi.framework.BundleException;
23 import org.osgi.framework.Constants;
24
25 public class BundleFragment extends BundlePluginBase implements IBundleFragment {
26
27     private static final long serialVersionUID = 1L;
28
29     public String JavaDoc getPluginId() {
30         return getValue(Constants.FRAGMENT_HOST, true);
31     }
32
33     /* (non-Javadoc)
34      * @see org.eclipse.pde.core.plugin.IFragment#getPluginVersion()
35      */

36     public String JavaDoc getPluginVersion() {
37         String JavaDoc version = getAttribute(Constants.FRAGMENT_HOST, Constants.BUNDLE_VERSION_ATTRIBUTE);
38         try {
39             VersionRange versionRange = new VersionRange(version);
40             if (versionRange != null) {
41                 return versionRange.getMinimum() != null ? versionRange.getMinimum().toString() : version;
42             }
43         } catch (NumberFormatException JavaDoc e) {
44         }
45         return version;
46     }
47
48     /* (non-Javadoc)
49      * @see org.eclipse.pde.core.plugin.IFragment#getRule()
50      */

51     public int getRule() {
52         String JavaDoc version = getAttribute(Constants.FRAGMENT_HOST, Constants.BUNDLE_VERSION_ATTRIBUTE);
53         VersionRange versionRange = new VersionRange(version);
54         return PluginBase.getMatchRule(versionRange);
55     }
56
57     /* (non-Javadoc)
58      * @see org.eclipse.pde.core.plugin.IFragment#setPluginId(java.lang.String)
59      */

60     public void setPluginId(String JavaDoc id) throws CoreException {
61         IBundle bundle = getBundle();
62         if (bundle != null) {
63             String JavaDoc oldValue = getPluginId();
64             IManifestHeader header = getManifestHeader(Constants.FRAGMENT_HOST);
65             if (header instanceof FragmentHostHeader) {
66                 ((FragmentHostHeader)header).setHostId(id);
67             } else {
68                 bundle.setHeader(Constants.FRAGMENT_HOST, writeFragmentHost(id, getPluginVersion()));
69             }
70             model.fireModelObjectChanged(this, P_PLUGIN_ID, oldValue, id);
71         }
72     }
73
74     /* (non-Javadoc)
75      * @see org.eclipse.pde.core.plugin.IFragment#setPluginVersion(java.lang.String)
76      */

77     public void setPluginVersion(String JavaDoc version) throws CoreException {
78         IBundle bundle = getBundle();
79         if (bundle != null) {
80             String JavaDoc oldValue = getPluginVersion();
81             IManifestHeader header = getManifestHeader(Constants.FRAGMENT_HOST);
82             if (header instanceof FragmentHostHeader) {
83                 ((FragmentHostHeader)header).setHostRange(version);
84             } else {
85                 bundle.setHeader(Constants.FRAGMENT_HOST, writeFragmentHost(getPluginId(), version));
86             }
87             model.fireModelObjectChanged(this, P_PLUGIN_VERSION, oldValue, version);
88         }
89     }
90
91     /* (non-Javadoc)
92      * @see org.eclipse.pde.core.plugin.IFragment#setRule(int)
93      */

94     public void setRule(int rule) throws CoreException {
95     }
96     
97     private String JavaDoc writeFragmentHost(String JavaDoc id, String JavaDoc version) {
98         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
99         if (id != null)
100             buffer.append(id);
101         
102         if (version != null && version.trim().length() > 0) {
103             buffer.append(";" + Constants.BUNDLE_VERSION_ATTRIBUTE + "=\"" + version.trim() + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
104
}
105         return buffer.toString();
106     }
107     
108     private String JavaDoc getAttribute(String JavaDoc key, String JavaDoc attribute) {
109         IBundle bundle = getBundle();
110         if (bundle == null)
111             return null;
112         String JavaDoc value = bundle.getHeader(key);
113         if (value == null)
114             return null;
115         try {
116             ManifestElement[] elements = ManifestElement.parseHeader(key, value);
117             if (elements.length > 0)
118                 return elements[0].getAttribute(attribute);
119         } catch (BundleException e) {
120         }
121         return null;
122     }
123     
124     public boolean isPatch() {
125         return "true".equals(getValue(ICoreConstants.PATCH_FRAGMENT, false)); //$NON-NLS-1$
126
}
127
128 }
129
Popular Tags