KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > tasks > ManifestModifier


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.build.tasks;
12
13 import java.io.*;
14 import java.util.*;
15 import java.util.jar.Attributes JavaDoc;
16 import java.util.jar.Manifest JavaDoc;
17 import java.util.jar.Attributes.Name;
18 import org.apache.tools.ant.BuildException;
19 import org.apache.tools.ant.Task;
20
21 /**
22  * Internal task.
23  * Add, change or remove the keys from a manifest.mf.
24  * @since 3.0
25  */

26 public class ManifestModifier extends Task {
27     private String JavaDoc manifestLocation;
28     private Map newValues = new HashMap();
29     private static String JavaDoc DELIM = "#|"; //$NON-NLS-1$
30
private Manifest JavaDoc manifest = null;
31     private boolean contentChanged = false;
32     
33     /**
34      * Indicate new values to add to the manifest. The format of the parameter is key|value#key|value#...
35      * If a value is specified to null, the key will be removed from the manifest.
36      * @param values
37      */

38     public void setKeyValue(String JavaDoc values) {
39         StringTokenizer tokenizer = new StringTokenizer(values, DELIM, false);
40         while (tokenizer.hasMoreElements()) {
41             String JavaDoc key = tokenizer.nextToken();
42             String JavaDoc value = tokenizer.nextToken();
43             if (value.equals("null")) //$NON-NLS-1$
44
value = null;
45             newValues.put(key, value);
46         }
47     }
48
49     public void execute() {
50         loadManifest();
51
52         applyChanges();
53
54         writeManifest();
55     }
56
57     private void writeManifest() {
58         if (!contentChanged)
59             return;
60         
61         OutputStream os = null;
62         try {
63             os = new BufferedOutputStream(new FileOutputStream(manifestLocation));
64             try {
65                 manifest.write(os);
66             } finally {
67                 os.close();
68             }
69         } catch (IOException e1) {
70             new BuildException("Problem writing the content of the manifest : " + manifestLocation); //$NON-NLS-1$
71
}
72     }
73
74     private void applyChanges() {
75         for (Iterator iter = newValues.entrySet().iterator(); iter.hasNext();) {
76             Map.Entry entry = (Map.Entry) iter.next();
77             String JavaDoc key = (String JavaDoc) entry.getKey();
78             String JavaDoc value = (String JavaDoc) entry.getValue();
79             if (value == null) {
80                 removeAttribute(key);
81             } else {
82                 changeValue(key, value);
83             }
84         }
85         //force a manifest version if none exists
86
if (!manifest.getMainAttributes().containsKey(Name.MANIFEST_VERSION)) {
87             contentChanged = true;
88             manifest.getMainAttributes().put(Name.MANIFEST_VERSION, "1.0"); //$NON-NLS-1$
89
}
90
91     }
92
93     private void loadManifest() {
94         try {
95             InputStream is = new BufferedInputStream(new FileInputStream(manifestLocation));
96             try {
97                 manifest = new Manifest JavaDoc(is);
98             } finally {
99                 is.close();
100             }
101         } catch (IOException e) {
102             new BuildException("Problem reading the content of the manifest : " + manifestLocation); //$NON-NLS-1$
103
}
104     }
105
106     private void changeValue(String JavaDoc key, String JavaDoc value) {
107         if (manifest.getMainAttributes().getValue(key).equals(value))
108             return;
109         contentChanged = true;
110         manifest.getMainAttributes().put(new Attributes.Name JavaDoc(key), value);
111     }
112
113     private void removeAttribute(String JavaDoc key) {
114         contentChanged = true;
115         manifest.getMainAttributes().remove(new Attributes.Name JavaDoc(key));
116     }
117
118     /**
119      *
120      * @param path
121      */

122     public void setManifestLocation(String JavaDoc path) {
123         manifestLocation = path;
124     }
125 }
126
Popular Tags