1 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 getPluginId() { 30 return getValue(Constants.FRAGMENT_HOST, true); 31 } 32 33 36 public String getPluginVersion() { 37 String 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 e) { 44 } 45 return version; 46 } 47 48 51 public int getRule() { 52 String version = getAttribute(Constants.FRAGMENT_HOST, Constants.BUNDLE_VERSION_ATTRIBUTE); 53 VersionRange versionRange = new VersionRange(version); 54 return PluginBase.getMatchRule(versionRange); 55 } 56 57 60 public void setPluginId(String id) throws CoreException { 61 IBundle bundle = getBundle(); 62 if (bundle != null) { 63 String 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 77 public void setPluginVersion(String version) throws CoreException { 78 IBundle bundle = getBundle(); 79 if (bundle != null) { 80 String 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 94 public void setRule(int rule) throws CoreException { 95 } 96 97 private String writeFragmentHost(String id, String version) { 98 StringBuffer buffer = new StringBuffer (); 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() + "\""); } 105 return buffer.toString(); 106 } 107 108 private String getAttribute(String key, String attribute) { 109 IBundle bundle = getBundle(); 110 if (bundle == null) 111 return null; 112 String 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)); } 127 128 } 129 | Popular Tags |