001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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 - Initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.build.tasks;
011:
012: import java.io.*;
013: import java.util.*;
014: import java.util.jar.Attributes;
015: import java.util.jar.Manifest;
016: import java.util.jar.Attributes.Name;
017: import org.apache.tools.ant.BuildException;
018: import org.apache.tools.ant.Task;
019:
020: /**
021: * Internal task.
022: * Add, change or remove the keys from a manifest.mf.
023: * @since 3.0
024: */
025: public class ManifestModifier extends Task {
026: private String manifestLocation;
027: private Map newValues = new HashMap();
028: private static String DELIM = "#|"; //$NON-NLS-1$
029: private Manifest manifest = null;
030: private boolean contentChanged = false;
031:
032: /**
033: * Indicate new values to add to the manifest. The format of the parameter is key|value#key|value#...
034: * If a value is specified to null, the key will be removed from the manifest.
035: * @param values
036: */
037: public void setKeyValue(String values) {
038: StringTokenizer tokenizer = new StringTokenizer(values, DELIM,
039: false);
040: while (tokenizer.hasMoreElements()) {
041: String key = tokenizer.nextToken();
042: String value = tokenizer.nextToken();
043: if (value.equals("null")) //$NON-NLS-1$
044: value = null;
045: newValues.put(key, value);
046: }
047: }
048:
049: public void execute() {
050: loadManifest();
051:
052: applyChanges();
053:
054: writeManifest();
055: }
056:
057: private void writeManifest() {
058: if (!contentChanged)
059: return;
060:
061: OutputStream os = null;
062: try {
063: os = new BufferedOutputStream(new FileOutputStream(
064: manifestLocation));
065: try {
066: manifest.write(os);
067: } finally {
068: os.close();
069: }
070: } catch (IOException e1) {
071: new BuildException(
072: "Problem writing the content of the manifest : " + manifestLocation); //$NON-NLS-1$
073: }
074: }
075:
076: private void applyChanges() {
077: for (Iterator iter = newValues.entrySet().iterator(); iter
078: .hasNext();) {
079: Map.Entry entry = (Map.Entry) iter.next();
080: String key = (String) entry.getKey();
081: String value = (String) entry.getValue();
082: if (value == null) {
083: removeAttribute(key);
084: } else {
085: changeValue(key, value);
086: }
087: }
088: //force a manifest version if none exists
089: if (!manifest.getMainAttributes().containsKey(
090: Name.MANIFEST_VERSION)) {
091: contentChanged = true;
092: manifest.getMainAttributes().put(Name.MANIFEST_VERSION,
093: "1.0"); //$NON-NLS-1$
094: }
095:
096: }
097:
098: private void loadManifest() {
099: try {
100: InputStream is = new BufferedInputStream(
101: new FileInputStream(manifestLocation));
102: try {
103: manifest = new Manifest(is);
104: } finally {
105: is.close();
106: }
107: } catch (IOException e) {
108: new BuildException(
109: "Problem reading the content of the manifest : " + manifestLocation); //$NON-NLS-1$
110: }
111: }
112:
113: private void changeValue(String key, String value) {
114: if (manifest.getMainAttributes().getValue(key).equals(value))
115: return;
116: contentChanged = true;
117: manifest.getMainAttributes().put(new Attributes.Name(key),
118: value);
119: }
120:
121: private void removeAttribute(String key) {
122: contentChanged = true;
123: manifest.getMainAttributes().remove(new Attributes.Name(key));
124: }
125:
126: /**
127: *
128: * @param path
129: */
130: public void setManifestLocation(String path) {
131: manifestLocation = path;
132: }
133: }
|