1 11 package org.eclipse.pde.internal.build.tasks; 12 13 import java.io.*; 14 import java.util.*; 15 import java.util.jar.Attributes ; 16 import java.util.jar.Manifest ; 17 import java.util.jar.Attributes.Name; 18 import org.apache.tools.ant.BuildException; 19 import org.apache.tools.ant.Task; 20 21 26 public class ManifestModifier extends Task { 27 private String manifestLocation; 28 private Map newValues = new HashMap(); 29 private static String DELIM = "#|"; private Manifest manifest = null; 31 private boolean contentChanged = false; 32 33 38 public void setKeyValue(String values) { 39 StringTokenizer tokenizer = new StringTokenizer(values, DELIM, false); 40 while (tokenizer.hasMoreElements()) { 41 String key = tokenizer.nextToken(); 42 String value = tokenizer.nextToken(); 43 if (value.equals("null")) 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); } 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 key = (String ) entry.getKey(); 78 String value = (String ) entry.getValue(); 79 if (value == null) { 80 removeAttribute(key); 81 } else { 82 changeValue(key, value); 83 } 84 } 85 if (!manifest.getMainAttributes().containsKey(Name.MANIFEST_VERSION)) { 87 contentChanged = true; 88 manifest.getMainAttributes().put(Name.MANIFEST_VERSION, "1.0"); } 90 91 } 92 93 private void loadManifest() { 94 try { 95 InputStream is = new BufferedInputStream(new FileInputStream(manifestLocation)); 96 try { 97 manifest = new Manifest (is); 98 } finally { 99 is.close(); 100 } 101 } catch (IOException e) { 102 new BuildException("Problem reading the content of the manifest : " + manifestLocation); } 104 } 105 106 private void changeValue(String key, String value) { 107 if (manifest.getMainAttributes().getValue(key).equals(value)) 108 return; 109 contentChanged = true; 110 manifest.getMainAttributes().put(new Attributes.Name (key), value); 111 } 112 113 private void removeAttribute(String key) { 114 contentChanged = true; 115 manifest.getMainAttributes().remove(new Attributes.Name (key)); 116 } 117 118 122 public void setManifestLocation(String path) { 123 manifestLocation = path; 124 } 125 } 126 | Popular Tags |