You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by jo...@hyperreal.org on 1999/11/25 03:27:14 UTC

cvs commit: jakarta-tools/ant/src/main/org/apache/tools/ant/taskdefs Zip.java defaults.properties KeySubst.java

jons        99/11/24 18:27:10

  Modified:    ant/src/main/org/apache/tools/ant/taskdefs
                        defaults.properties KeySubst.java
  Added:       ant/src/main/org/apache/tools/ant/taskdefs Zip.java
  Log:
  added zip class
  
  fixed keysubst to actually work...thanks to brett for the help
  with the replace method cause i can't code for shit. ;-)
  
  Revision  Changes    Path
  1.7       +2 -1      jakarta-tools/ant/src/main/org/apache/tools/ant/taskdefs/defaults.properties
  
  Index: defaults.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-tools/ant/src/main/org/apache/tools/ant/taskdefs/defaults.properties,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- defaults.properties	1999/11/24 20:03:58	1.6
  +++ defaults.properties	1999/11/25 02:26:59	1.7
  @@ -11,4 +11,5 @@
   expand=org.apache.tools.ant.taskdefs.Expand
   echo=org.apache.tools.ant.taskdefs.Echo
   javadoc2=org.apache.tools.ant.taskdefs.Javadoc2
  -keysubst=org.apache.tools.ant.taskdefs.KeySubst
  \ No newline at end of file
  +keysubst=org.apache.tools.ant.taskdefs.KeySubst
  +zip=org.apache.tools.ant.taskdefs.Zip
  \ No newline at end of file
  
  
  
  1.2       +34 -48    jakarta-tools/ant/src/main/org/apache/tools/ant/taskdefs/KeySubst.java
  
  Index: KeySubst.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tools/ant/src/main/org/apache/tools/ant/taskdefs/KeySubst.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- KeySubst.java	1999/11/24 20:03:59	1.1
  +++ KeySubst.java	1999/11/25 02:27:00	1.2
  @@ -67,10 +67,9 @@
    * @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
    */
   public class KeySubst extends Task {
  -    private File source; // required
  -    private File dest; // required
  +    private File source = null;
  +    private File dest = null;
       private String sep = "*";
  -    private static String delim = "@";
       private Hashtable replacements = new Hashtable();
       
       /**
  @@ -86,6 +85,7 @@
           BufferedWriter bw = null;
           try {
               br = new BufferedReader(new FileReader(source));
  +            dest.delete();
               bw = new BufferedWriter(new FileWriter(dest));
   
               String line = null;
  @@ -93,9 +93,13 @@
               int length;
               line = br.readLine();
               while (line != null) {
  -                newline = replace ( line, replacements );
  -                bw.write ( newline );
  -                bw.newLine();
  +                if ( line.length() == 0 ) {
  +                    bw.newLine();
  +                } else {
  +                    newline = KeySubst.replace ( line, replacements );
  +                    bw.write ( newline );
  +                    bw.newLine();
  +                }
                   line = br.readLine();
               }
               bw.flush();
  @@ -127,14 +131,6 @@
           this.sep = sep;
       }
       /**
  -        Sets the deliminator characters that go around the 
  -        keywords. Ie: @foo@. The "@" in this case is the 
  -        deliminator. It is also the default.
  -    */
  -    public void setDelim(String delim) {
  -        this.delim = delim;
  -    }
  -    /**
           Format string is like this:
           <p>
           name=value*name2=value
  @@ -155,62 +151,52 @@
                   
                   String name = itok.nextToken();
                   String value = itok.nextToken();
  -//                System.out.println ( "KeySubst Name: " + name );
  -//                System.out.println ( "KeySubst Value: " + value );
  +//                project.log ( "Name: " + name );
  +//                project.log ( "Value: " + value );
                   replacements.put ( name, value );
               }
           }
       }
           
  -/*
  +
       public static void main(String[] args)
       {
           try{
           Hashtable hash = new Hashtable();
  -        hash.put ( "w", "yoaaaau" );
  -        hash.put ( "a", "ffff" );
  -        System.out.println ( KeySubst.replace ( "f @w@ - @a@ are", hash ) );
  +        hash.put ( "VERSION", "1.0.3" );
  +        hash.put ( "b", "ffff" );
  +        System.out.println ( KeySubst.replace ( "$f ${VERSION} f ${b} jj $", hash ) );
           }catch ( Exception e)
           {
               e.printStackTrace();
           }
       }
  -*/
  +
       /**
           Does replacement on text using the hashtable of keys.
  -        This could probably be done a lot better, but for now 
  -        it does the job and this isn't a time criticial application.
           
           @returns the string with the replacements in it.
       */
  -    public static String replace ( String text, Hashtable keys )
  +    public static String replace ( String origString, Hashtable keys )
           throws BuildException
       {
  -        StringBuffer sb=new StringBuffer();
  -        int i=0;
  -        int prev=0;
  -        int pos=0;
  -        while( (pos=text.indexOf( delim, prev )) >= 0 ) {
  -            if(pos>0)
  -                sb.append( text.substring( prev, pos ) );
  -
  -            pos++;
  -            int endName=text.indexOf( delim, pos );
  -
  -            String n=text.substring( pos, endName );
  -            if ( keys.containsKey( n ) )
  -            {
  -                sb.append ( (String) keys.get(n) );
  -                sb.append ( text.charAt( pos + n.length() + 1 ) );
  -                prev=pos + n.length() + 2;
  -            }
  -            else
  -            {
  -                sb.append ( delim + n + delim);
  -                prev=endName + 1;
  +        StringBuffer finalString=new StringBuffer();
  +        int index=0;
  +        int i = 0;
  +        String key = null;
  +        while ((index = origString.indexOf("${", i)) > -1) {
  +            key = origString.substring(index + 2, origString.indexOf("}", index+3));
  +            finalString.append (origString.substring(i, index));
  +            if ( keys.containsKey ( key ) ) {
  +                finalString.append (keys.get(key));
  +            } else {
  +                finalString.append ( "${" );
  +                finalString.append ( key );
  +                finalString.append ( "}" );
               }
  +            i = index + 3 + key.length();
           }
  -        if( prev < text.length() ) sb.append( text.substring( prev ) );
  -        return sb.toString();
  +        finalString.append (origString.substring(i));
  +        return finalString.toString();
       }
   }
  
  
  
  1.1                  jakarta-tools/ant/src/main/org/apache/tools/ant/taskdefs/Zip.java
  
  Index: Zip.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.tools.ant.taskdefs;
  
  import org.apache.tools.ant.*;
  
  import java.io.*;
  import java.util.Enumeration;
  import java.util.StringTokenizer;
  import java.util.Vector;
  import java.util.zip.*;
  
  /**
   * Same as the Jar task, but creates .zip files without the MANIFEST 
   * stuff that .jar files have.
   *
   * @author duncan@x180.com
   * @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
   */
  
  public class Zip extends Task {
  
      private File zipFile;
      private File baseDir;
      private Vector items = new Vector();
      private File manifest;    
      private Vector ignoreList = new Vector();
      
      public void setZipfile(String zipFilename) {
      zipFile = project.resolveFile(zipFilename);
      }
  
      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() );
              }
          }
      }
      
      public void setManifest(String manifestFilename) {
      manifest = project.resolveFile(manifestFilename);
      }
  
      public void execute() throws BuildException {
          project.log("Building zip: " + zipFile.getAbsolutePath());
      
          try {
              ZipOutputStream zOut = new ZipOutputStream(new FileOutputStream(zipFile));
              zOut.setMethod(ZipOutputStream.DEFLATED);
              
              // 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()) {
                          zipDir(f, zOut, s + "/");
                      } else {
                          zipFile(f, zOut, s);
                      }
                  } else {
                      project.log("Ignored: " + s);
                  }
              }
      
              // close up            
              zOut.close();
          } catch (IOException ioe) {
              String msg = "Problem creating zip " + ioe.getMessage();
              throw new BuildException(msg);
          }
      }
  
      private void zipDir(File dir, ZipOutputStream zOut, String vPath)
          throws IOException
      {
          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()) {
                      zipDir(file, zOut, vPath + f + "/");
                  } else {
                      zipFile(file, zOut, vPath + f);
                  }
              } else {
                  project.log("Ignored: " + f);
              }
          }
      }
  
      private void zipFile(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 zipFile(File file, ZipOutputStream zOut, String vPath)
          throws IOException
      {
          FileInputStream fIn = new FileInputStream(file);
          zipFile(fIn, zOut, vPath);
          fIn.close();
      }
  }