001: /*******************************************************************************
002: * Copyright (c) 2003, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.bundle;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.osgi.service.resolver.VersionRange;
014: import org.eclipse.osgi.util.ManifestElement;
015: import org.eclipse.pde.internal.core.ICoreConstants;
016: import org.eclipse.pde.internal.core.ibundle.IBundle;
017: import org.eclipse.pde.internal.core.ibundle.IBundleFragment;
018: import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
019: import org.eclipse.pde.internal.core.plugin.PluginBase;
020: import org.eclipse.pde.internal.core.text.bundle.FragmentHostHeader;
021: import org.osgi.framework.BundleException;
022: import org.osgi.framework.Constants;
023:
024: public class BundleFragment extends BundlePluginBase implements
025: IBundleFragment {
026:
027: private static final long serialVersionUID = 1L;
028:
029: public String getPluginId() {
030: return getValue(Constants.FRAGMENT_HOST, true);
031: }
032:
033: /* (non-Javadoc)
034: * @see org.eclipse.pde.core.plugin.IFragment#getPluginVersion()
035: */
036: public String getPluginVersion() {
037: String version = getAttribute(Constants.FRAGMENT_HOST,
038: Constants.BUNDLE_VERSION_ATTRIBUTE);
039: try {
040: VersionRange versionRange = new VersionRange(version);
041: if (versionRange != null) {
042: return versionRange.getMinimum() != null ? versionRange
043: .getMinimum().toString() : version;
044: }
045: } catch (NumberFormatException e) {
046: }
047: return version;
048: }
049:
050: /* (non-Javadoc)
051: * @see org.eclipse.pde.core.plugin.IFragment#getRule()
052: */
053: public int getRule() {
054: String version = getAttribute(Constants.FRAGMENT_HOST,
055: Constants.BUNDLE_VERSION_ATTRIBUTE);
056: VersionRange versionRange = new VersionRange(version);
057: return PluginBase.getMatchRule(versionRange);
058: }
059:
060: /* (non-Javadoc)
061: * @see org.eclipse.pde.core.plugin.IFragment#setPluginId(java.lang.String)
062: */
063: public void setPluginId(String id) throws CoreException {
064: IBundle bundle = getBundle();
065: if (bundle != null) {
066: String oldValue = getPluginId();
067: IManifestHeader header = getManifestHeader(Constants.FRAGMENT_HOST);
068: if (header instanceof FragmentHostHeader) {
069: ((FragmentHostHeader) header).setHostId(id);
070: } else {
071: bundle.setHeader(Constants.FRAGMENT_HOST,
072: writeFragmentHost(id, getPluginVersion()));
073: }
074: model.fireModelObjectChanged(this , P_PLUGIN_ID, oldValue,
075: id);
076: }
077: }
078:
079: /* (non-Javadoc)
080: * @see org.eclipse.pde.core.plugin.IFragment#setPluginVersion(java.lang.String)
081: */
082: public void setPluginVersion(String version) throws CoreException {
083: IBundle bundle = getBundle();
084: if (bundle != null) {
085: String oldValue = getPluginVersion();
086: IManifestHeader header = getManifestHeader(Constants.FRAGMENT_HOST);
087: if (header instanceof FragmentHostHeader) {
088: ((FragmentHostHeader) header).setHostRange(version);
089: } else {
090: bundle.setHeader(Constants.FRAGMENT_HOST,
091: writeFragmentHost(getPluginId(), version));
092: }
093: model.fireModelObjectChanged(this , P_PLUGIN_VERSION,
094: oldValue, version);
095: }
096: }
097:
098: /* (non-Javadoc)
099: * @see org.eclipse.pde.core.plugin.IFragment#setRule(int)
100: */
101: public void setRule(int rule) throws CoreException {
102: }
103:
104: private String writeFragmentHost(String id, String version) {
105: StringBuffer buffer = new StringBuffer();
106: if (id != null)
107: buffer.append(id);
108:
109: if (version != null && version.trim().length() > 0) {
110: buffer
111: .append(";" + Constants.BUNDLE_VERSION_ATTRIBUTE + "=\"" + version.trim() + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
112: }
113: return buffer.toString();
114: }
115:
116: private String getAttribute(String key, String attribute) {
117: IBundle bundle = getBundle();
118: if (bundle == null)
119: return null;
120: String value = bundle.getHeader(key);
121: if (value == null)
122: return null;
123: try {
124: ManifestElement[] elements = ManifestElement.parseHeader(
125: key, value);
126: if (elements.length > 0)
127: return elements[0].getAttribute(attribute);
128: } catch (BundleException e) {
129: }
130: return null;
131: }
132:
133: public boolean isPatch() {
134: return "true".equals(getValue(ICoreConstants.PATCH_FRAGMENT, false)); //$NON-NLS-1$
135: }
136:
137: }
|