KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > converter > PDEPluginConverter


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.converter;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.Dictionary JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.osgi.service.pluginconversion.PluginConversionException;
27 import org.eclipse.osgi.util.ManifestElement;
28 import org.eclipse.pde.internal.core.ICoreConstants;
29 import org.eclipse.pde.internal.core.TargetPlatformHelper;
30 import org.osgi.framework.BundleException;
31
32 public class PDEPluginConverter {
33     
34     public static void convertToOSGIFormat(IProject project, String JavaDoc target, Dictionary JavaDoc dictionary, IProgressMonitor monitor) throws CoreException {
35         convertToOSGIFormat(project, target, dictionary, null, monitor);
36     }
37     
38     public static void convertToOSGIFormat(IProject project, String JavaDoc target, Dictionary JavaDoc dictionary, HashMap JavaDoc newProps, IProgressMonitor monitor) throws CoreException {
39         try {
40             File JavaDoc outputFile = new File JavaDoc(project.getLocation().append(
41                     "META-INF/MANIFEST.MF").toOSString()); //$NON-NLS-1$
42
File JavaDoc inputFile = new File JavaDoc(project.getLocation().toOSString());
43             PluginConverter converter = PluginConverter.getDefault();
44             converter.convertManifest(inputFile, outputFile, false, target, true, dictionary);
45
46             if (newProps != null && newProps.size() > 0)
47                 converter.writeManifest(outputFile, getProperties(outputFile, newProps), false);
48         
49             project.refreshLocal(IResource.DEPTH_INFINITE, null);
50         } catch (PluginConversionException e) {
51         } finally {
52             monitor.done();
53         }
54     }
55     
56     public static void createBundleForFramework(IProject project, HashMap JavaDoc newProps, IProgressMonitor monitor) throws CoreException {
57         try {
58             File JavaDoc outputFile = new File JavaDoc(project.getLocation().append(
59                     "META-INF/MANIFEST.MF").toOSString()); //$NON-NLS-1$
60
File JavaDoc inputFile = new File JavaDoc(project.getLocation().toOSString());
61             PluginConverter converter = PluginConverter.getDefault();
62             double version = TargetPlatformHelper.getTargetVersion();
63             String JavaDoc versionString = version <= 3.1 ? ICoreConstants.TARGET31 : TargetPlatformHelper.getTargetVersionString();
64             converter.convertManifest(inputFile, outputFile, false, versionString, true, null);
65             
66             Map JavaDoc prop = getProperties(outputFile, newProps);
67             prop.remove(ICoreConstants.ECLIPSE_AUTOSTART);
68             prop.remove(ICoreConstants.ECLIPSE_LAZYSTART);
69             converter.writeManifest(outputFile, prop, false);
70             project.refreshLocal(IResource.DEPTH_INFINITE, null);
71         } catch (PluginConversionException e) {
72         } catch (CoreException e) {
73         } finally {
74             monitor.done();
75         }
76     }
77     
78     private static Map JavaDoc getProperties(File JavaDoc file, HashMap JavaDoc newProps) {
79         try {
80             Map JavaDoc prop = ManifestElement.parseBundleManifest(new FileInputStream JavaDoc(file), null);
81             if (newProps != null && newProps.size() > 0) {
82                 Iterator JavaDoc iter = newProps.keySet().iterator();
83                 while (iter.hasNext()) {
84                     String JavaDoc key = iter.next().toString();
85                     prop.put(key, newProps.get(key));
86                 }
87             }
88             return prop;
89         } catch (FileNotFoundException JavaDoc e) {
90         } catch (BundleException e) {
91         } catch (IOException JavaDoc e) {
92         }
93         return new HashMap JavaDoc();
94     }
95     
96 }
97
Popular Tags