From 201b39cc14f0da7d5264edc02a45fe91f2cbb5e2 Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Fri, 12 Oct 2001 08:11:32 +0000 Subject: [PATCH] Added task to create a manifest easily from build file. Submitted by: thomas.kerle@pd.admin.ch git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269777 13f79535-47bb-0310-9956-ffa450edef68 --- .../ant/taskdefs/optional/ManifestFile.java | 319 ++++++++++++++++++ 1 file changed, 319 insertions(+) create mode 100644 src/main/org/apache/tools/ant/taskdefs/optional/ManifestFile.java diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ManifestFile.java b/src/main/org/apache/tools/ant/taskdefs/optional/ManifestFile.java new file mode 100644 index 000000000..ca4d752f1 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ManifestFile.java @@ -0,0 +1,319 @@ +package org.apache.tools.ant.taskdefs.optional; + +import java.io.*; +import java.util.*; +import org.apache.tools.ant.*; +import org.apache.tools.ant.types.*; + +/** + * Task for creating a manifest file for a jar archiv. + * use: + *
+ *   
+ *   
+ *     
+ *         
+ *     
+ *   
+ * 
+ * + *@version 1.0 2001-10-11 + *@author Thomas Kerle + */ +public class ManifestFile extends Task { + + private static final String newLine = System.getProperty("line.separator"); + private static final String keyValueSeparator = ":"; + private static final String UPDATE_ = "update"; + private static final String REPLACEALL_ = "replaceAll"; + + private File manifestFile; + private Vector entries; + private EntryContainer container; + private String currentMethod; + + public ManifestFile() { + entries = new Vector(); + container = new EntryContainer(); + } + + /** + * execute task + * @exception BuildException : Failure in building + */ + public void execute() throws BuildException { + checkParameters(); + if (isUpdate(currentMethod)) + readFile(); + + executeOperation(); + writeFile(); + } + + /** + * adding entries to a container + * @exception BuildException + */ + private void executeOperation() throws BuildException { + Enumeration enum = entries.elements(); + + while (enum.hasMoreElements()) { + Entry entry = (Entry) enum.nextElement(); + entry.addTo (container); + } + } + + /** + * creating entries by Ant + * + * + */ + public Entry createEntry() { + Entry entry = new Entry(); + entries.addElement(entry); + return entry; + } + + + private boolean isUpdate (String method) { + return method.equals(UPDATE_.toUpperCase()); + } + + private boolean isReplaceAll (String method) { + return method.equals(REPLACEALL_.toUpperCase()); + } + + /** + * Setter for the method attribute (update/replaceAll) + * @param method Method to set task + */ + public void setMethod (String method) { + currentMethod = method.toUpperCase(); + } + + /** + * Setter for the file attribute + * @param filename for the manifest + */ + public void setFile(File f) { + manifestFile = f; + } + + + private StringBuffer buildBuffer () { + StringBuffer buffer = new StringBuffer (); + + ListIterator iterator = container.elements(); + + while (iterator.hasNext()) { + Entry entry = (Entry) iterator.next(); + + String key = (String) entry.getKey(); + String value = (String) entry.getValue(); + String entry_string = key + keyValueSeparator + value; + + buffer.append (entry_string + this.newLine); + } + + return buffer; + } + + + private void writeFile() throws BuildException { + try { + manifestFile.delete(); + log ("Replacing or creating new manifest file " + manifestFile.getAbsolutePath()); + if (manifestFile.createNewFile()) { + FileOutputStream fos = new FileOutputStream(manifestFile); + + StringBuffer buffer = buildBuffer(); + + int size = buffer.length(); + + for (int i=0; i