001: /*******************************************************************************
002: * Copyright (c) 2005 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.text.bundle;
011:
012: import org.eclipse.osgi.util.ManifestElement;
013: import org.eclipse.pde.core.plugin.IPluginImport;
014: import org.eclipse.pde.internal.core.ICoreConstants;
015: import org.eclipse.pde.internal.core.bundle.BundlePluginBase;
016: import org.eclipse.pde.internal.core.ibundle.IBundle;
017: import org.osgi.framework.Constants;
018:
019: public class RequireBundleHeader extends CompositeManifestHeader {
020:
021: private static final long serialVersionUID = 1L;
022:
023: public RequireBundleHeader(String name, String value,
024: IBundle bundle, String lineDelimiter) {
025: super (name, value, bundle, lineDelimiter);
026: }
027:
028: public void addBundle(IPluginImport iimport) {
029: addBundle(iimport.getId(), iimport.getVersion(), iimport
030: .isReexported(), iimport.isOptional());
031: }
032:
033: /**
034: * @param iimport
035: * @param index
036: */
037: public void addBundle(IPluginImport iimport, int index) {
038: String id = iimport.getId();
039: String version = iimport.getVersion();
040: boolean exported = iimport.isReexported();
041: boolean optional = iimport.isOptional();
042: // Adapt the plug-in import to a bundle object
043: RequireBundleObject element = new RequireBundleObject(this , id);
044: // Configure the bundle object
045: configureBundle(version, exported, optional, element);
046: // Add the bundle object to the header at the specified index
047: addManifestElement(element, index, true);
048: }
049:
050: public void addBundle(String id) {
051: addBundle(id, null, false, false);
052: }
053:
054: public void addBundle(String id, String version, boolean exported,
055: boolean optional) {
056: RequireBundleObject element = new RequireBundleObject(this , id);
057: configureBundle(version, exported, optional, element);
058: addManifestElement(element);
059: }
060:
061: /**
062: * @param version
063: * @param exported
064: * @param optional
065: * @param element
066: */
067: private void configureBundle(String version, boolean exported,
068: boolean optional, RequireBundleObject element) {
069: int bundleManifestVersion = BundlePluginBase
070: .getBundleManifestVersion(getBundle());
071: if (optional)
072: if (bundleManifestVersion > 1)
073: element.setDirective(Constants.RESOLUTION_DIRECTIVE,
074: Constants.RESOLUTION_OPTIONAL);
075: else
076: element.setAttribute(ICoreConstants.OPTIONAL_ATTRIBUTE,
077: "true"); //$NON-NLS-1$
078:
079: if (exported)
080: if (bundleManifestVersion > 1)
081: element.setDirective(Constants.VISIBILITY_DIRECTIVE,
082: Constants.VISIBILITY_REEXPORT);
083: else
084: element.setAttribute(
085: ICoreConstants.REPROVIDE_ATTRIBUTE, "true"); //$NON-NLS-1$
086:
087: if (version != null && version.trim().length() > 0)
088: element.setAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE,
089: version.trim());
090: }
091:
092: public void removeBundle(String id) {
093: removeManifestElement(id);
094: }
095:
096: public void removeBundle(RequireBundleObject bundle) {
097: removeManifestElement(bundle);
098: }
099:
100: protected PDEManifestElement createElement(ManifestElement element) {
101: return new RequireBundleObject(this , element);
102: }
103:
104: public void updateBundle(int index, IPluginImport iimport) {
105: if (index == -1)
106: return;
107:
108: PDEManifestElement element = getElementAt(index);
109: if (element != null) {
110: element.setValue(iimport.getId());
111:
112: int bundleManifestVersion = BundlePluginBase
113: .getBundleManifestVersion(getBundle());
114: if (iimport.isOptional()) {
115: if (bundleManifestVersion > 1)
116: element.setDirective(
117: Constants.RESOLUTION_DIRECTIVE,
118: Constants.RESOLUTION_OPTIONAL);
119: else
120: element.setAttribute(
121: ICoreConstants.OPTIONAL_ATTRIBUTE, "true"); //$NON-NLS-1$
122: } else {
123: if (bundleManifestVersion > 1)
124: element.setDirective(
125: Constants.RESOLUTION_DIRECTIVE, null);
126: else
127: element.setAttribute(
128: ICoreConstants.OPTIONAL_ATTRIBUTE, null);
129: }
130:
131: if (iimport.isReexported()) {
132: if (bundleManifestVersion > 1)
133: element.setDirective(
134: Constants.VISIBILITY_DIRECTIVE,
135: Constants.VISIBILITY_REEXPORT);
136: else
137: element.setAttribute(
138: ICoreConstants.REPROVIDE_ATTRIBUTE, "true"); //$NON-NLS-1$
139: } else {
140: if (bundleManifestVersion > 1)
141: element.setDirective(
142: Constants.VISIBILITY_DIRECTIVE, null);
143: else
144: element.setAttribute(
145: ICoreConstants.REPROVIDE_ATTRIBUTE, null);
146: }
147: element.setAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE,
148: iimport.getVersion());
149: }
150: update(true);
151: }
152:
153: public RequireBundleObject[] getRequiredBundles() {
154: PDEManifestElement[] elements = getElements();
155: RequireBundleObject[] result = new RequireBundleObject[elements.length];
156: System.arraycopy(elements, 0, result, 0, elements.length);
157: return result;
158: }
159:
160: }
|