Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2008, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ import java.util.*; import java.util.Map.Entry; import java.util.jar.*; import org.eclipse.osgi.service.resolver.*; import org.osgi.framework.*; public class Main { private static String ELEMENT_SEPARATOR = "; "; private static final Object EQUALS_QUOTE = "=\""; private static void addImports(Attributes attrigutes, BundleDescription compositeDesc, ExportPackageDescription[] matchingExports) { ExportPackageDescription[] exports = compositeDesc.getExportPackages(); List systemExports = getSystemExports(matchingExports); if (exports.length == 0 && systemExports.size() == 0) return; StringBuffer importStatement = new StringBuffer(); Collection importedNames = new ArrayList(exports.length); int i = 0; for (; i < exports.length; i++) { if (i != 0) importStatement.append(','); importedNames.add(exports[i].getName()); getImportFrom(exports[i], importStatement); } for (Iterator iSystemExports = systemExports.iterator(); iSystemExports.hasNext();) { ExportPackageDescription systemExport = (ExportPackageDescription) iSystemExports.next(); if (!importedNames.contains(systemExport.getName())) { if (i != 0) importStatement.append(','); i++; importStatement.append(systemExport.getName()).append(ELEMENT_SEPARATOR) .append(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE).append('=') .append(Constants.SYSTEM_BUNDLE_SYMBOLICNAME); } } attrigutes.putValue(Constants.IMPORT_PACKAGE, importStatement.toString()); } private static List getSystemExports(ExportPackageDescription[] matchingExports) { ArrayList list = null; for (int i = 0; i < matchingExports.length; i++) { if (matchingExports[i].getExporter().getBundleId() != 0) continue; if (list == null) list = new ArrayList(); list.add(matchingExports[i]); } return list == null ? Collections.EMPTY_LIST : list; } private static void getImportFrom(ExportPackageDescription export, StringBuffer importStatement) { importStatement.append(export.getName()).append(ELEMENT_SEPARATOR); Version version = export.getVersion(); importStatement.append(Constants.VERSION_ATTRIBUTE).append(EQUALS_QUOTE).append('[').append(version) .append(',').append(new Version(version.getMajor(), version.getMinor(), version.getMicro() + 1)) .append(')').append('\"'); addMap(importStatement, export.getAttributes(), "="); //$NON-NLS-1$ } private static void addMap(StringBuffer manifest, Map values, String assignment) { if (values == null) return; // nothing to add for (Iterator iEntries = values.entrySet().iterator(); iEntries.hasNext();) { manifest.append(ELEMENT_SEPARATOR); Map.Entry entry = (Entry) iEntries.next(); manifest.append(entry.getKey()).append(assignment).append('\"'); Object value = entry.getValue(); if (value instanceof String[]) { String[] strings = (String[]) value; for (int i = 0; i < strings.length; i++) { if (i != 0) manifest.append(','); manifest.append(strings[i]); } } else { manifest.append(value); } manifest.append('\"'); } } }