tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Conor MacNeill <co...@m64.com>
Subject PATCH: Eliminate duplicated code in Jar.java and Zip.java
Date Thu, 09 Dec 1999 13:12:36 GMT
Hi,

This patch eliminates the duplicated code in Jar.java and Zip.java by 
making Jar.java extend Zip.java. A desirable result of this is 
the ability to specify an items value of "*" for Jar tasks indicating 
inclusion into the jar file of all files/dirs in the basedir directory.

Cheers
Conor


Index: Jar.java
===================================================================
RCS file: /home/cvspublic/jakarta-tools/ant/src/main/org/apache/tools/ant/taskdefs/Jar.java,v
retrieving revision 1.5
diff -r1.5 Jar.java
60,62d59
< import java.util.Enumeration;
< import java.util.StringTokenizer;
< import java.util.Vector;
71c68
< public class Jar extends Task {
---
> public class Jar extends Zip {
73,75d69
<     private File jarFile;
<     private File baseDir;
<     private Vector items = new Vector();
77d70
<     private Vector ignoreList = new Vector();
80c73
< 	jarFile = project.resolveFile(jarFilename);
---
> 	setZipfile(jarFilename);
83,116d75
<     public void setBasedir(String baseDirname) {
< 	baseDir = project.resolveFile(baseDirname);
<     }
< 
<     public void setItems(String itemString) {
< 	StringTokenizer tok = new StringTokenizer(itemString, ",", false);
< 	while (tok.hasMoreTokens()) {
< 	    items.addElement(tok.nextToken().trim());
< 	}
<     }
<     /**
<         List of filenames and directory names to not 
<         include in the final .jar file. They should be either 
<         , or " " (space) separated.
<         <p>
<         For example:
<         <p>
<         ignore="package.html, foo.class"
<         <p>
<         The ignored files will be logged.
<         
<         @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
<     */
<     public void setIgnore(String ignoreString) {
<         ignoreString = ignoreString;
<         if (ignoreString != null && ignoreString.length() > 0) {
<             StringTokenizer tok =
<             new StringTokenizer(ignoreString, ", ", false);
<             while (tok.hasMoreTokens()) {
<                 ignoreList.addElement ( tok.nextToken().trim() );
<             }
<         }
<     }
<     
121,122c80,83
<     public void execute() throws BuildException {
< 	project.log("Building jar: " + jarFile.getAbsolutePath());
---
>     protected void initZipOutputStream(ZipOutputStream zOut)
> 	throws IOException, BuildException
>     {
> 	zOut.setMethod(ZipOutputStream.DEFLATED);
124,169c85,97
< 	try {
< 	    FileOutputStream out = new FileOutputStream(jarFile);
< 	    ZipOutputStream zOut = new ZipOutputStream(out);
< 	    zOut.setMethod(ZipOutputStream.DEFLATED);
< 	    
< 	    // add manifest first
< 	    if (manifest != null) {
< 		ZipEntry ze = new ZipEntry("META-INF/");
< 		zOut.putNextEntry(ze);
< 		jarFile(manifest, zOut, "META-INF/MANIFEST.MF");
< 	    } else {
< 		ZipEntry ze = new ZipEntry("META-INF/");
< 		zOut.putNextEntry(ze);
< 		String s = "/org/apache/tools/ant/defaultManifest.mf";
< 		InputStream in = this.getClass().getResourceAsStream(s);
<         if ( in == null )
<             throw new BuildException ( "Could not find: " + s );
< 		jarFile(in, zOut, "META-INF/MANIFEST.MF");
< 	    }
< 	    
< 	    // add items
< 	    
< 	    Enumeration e = items.elements();
< 	    while (e.hasMoreElements()) {
<             String s = (String)e.nextElement();
<             // check to make sure item is not in ignore list
<             // shouldn't be ignored here, but just want to make sure
<             if (! ignoreList.contains(s)) {
<                 File f = new File(baseDir, s);
<                 if (f.isDirectory()) {
<                     jarDir(f, zOut, s + "/");
<                 } else {
<                     jarFile(f, zOut, s);
<                 }
<             } else {
<                 project.log("Ignored: " + s);
<             }
< 	    }
< 
< 	    // close up
< 	    
< 	    zOut.close();
< 	    out.close();
< 	} catch (IOException ioe) {
< 	    String msg = "Problem creating jar " + ioe.getMessage();
< 	    throw new BuildException(msg);
---
> 	// add manifest first
> 	if (manifest != null) {
> 	    ZipEntry ze = new ZipEntry("META-INF/");
> 	    zOut.putNextEntry(ze);
> 	    zipFile(manifest, zOut, "META-INF/MANIFEST.MF");
> 	} else {
> 	    ZipEntry ze = new ZipEntry("META-INF/");
> 	    zOut.putNextEntry(ze);
> 	    String s = "/org/apache/tools/ant/defaultManifest.mf";
> 	    InputStream in = this.getClass().getResourceAsStream(s);
>             if ( in == null )
> 		throw new BuildException ( "Could not find: " + s );
> 	    zipFile(in, zOut, "META-INF/MANIFEST.MF");
173,174c101,102
<     private void jarDir(File dir, ZipOutputStream zOut, String vPath)
< 	throws IOException
---
>     protected void zipDir(File dir, ZipOutputStream zOut, String vPath)
>         throws IOException
182,219c110
< 	
< 	String[] list = dir.list();
< 	for (int i = 0; i < list.length; i++) {
< 	    String f = list[i];
<         // check to make sure item is not in ignore list
<         if (! ignoreList.contains(f)) {
<             File file = new File(dir, f);
<             if (file.isDirectory()) {
<                 jarDir(file, zOut, vPath + f + "/");
<             } else {
<                 jarFile(file, zOut, vPath + f);
<             }
<         } else {
<             project.log("Ignored: " + f);
<         }
< 	}
<     }
< 
<     private void jarFile(InputStream in, ZipOutputStream zOut, String vPath)
< 	throws IOException
<     {
< 	ZipEntry ze = new ZipEntry(vPath);
< 	zOut.putNextEntry(ze);
< 	
< 	byte[] buffer = new byte[8 * 1024];
< 	int count = 0;
< 	do {
< 	    zOut.write(buffer, 0, count);
< 	    count = in.read(buffer, 0, buffer.length);
< 	} while (count != -1);
<     }
<     
<     private void jarFile(File file, ZipOutputStream zOut, String vPath)
<         throws IOException
<     {
< 	FileInputStream fIn = new FileInputStream(file);
< 	jarFile(fIn, zOut, vPath);
< 	fIn.close();
---
> 	super.zipDir(dir, zOut, vPath);
Index: Zip.java
===================================================================
RCS file: /home/cvspublic/jakarta-tools/ant/src/main/org/apache/tools/ant/taskdefs/Zip.java,v
retrieving revision 1.3
diff -r1.3 Zip.java
147c147
<             zOut.setMethod(ZipOutputStream.DEFLATED);
---
> 	    initZipOutputStream(zOut);
182c182,188
<     private void zipDir(File dir, ZipOutputStream zOut, String vPath)
---
>     protected void initZipOutputStream(ZipOutputStream zOut)
> 	throws IOException, BuildException
>     {
> 	zOut.setMethod(ZipOutputStream.DEFLATED);
>     }
> 
>     protected void zipDir(File dir, ZipOutputStream zOut, String vPath)
202c208
<     private void zipFile(InputStream in, ZipOutputStream zOut, String vPath)
---
>     protected void zipFile(InputStream in, ZipOutputStream zOut, String vPath)
216c222
<     private void zipFile(File file, ZipOutputStream zOut, String vPath)
---
>     protected void zipFile(File file, ZipOutputStream zOut, String vPath)

--
Conor MacNeill
conor@m64.com
M64 Pty Limited

Mime
View raw message