JarBuilder.java :  » XML-UI » XUI » net » xoetrope » builder » editor » ant » Java Open Source

Java Open Source » XML UI » XUI 
XUI » net » xoetrope » builder » editor » ant » JarBuilder.java
/*
 * Created on 01-Dec-2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package net.xoetrope.builder.editor.ant;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

import net.xoetrope.builder.editor.ant.taskdefs.Jar;

/**
 * Class to generate a jar file for distribution. Includes class files, screens
 * and resources. Uses Ant to build so the Ant jar file needs to be included in
 * the classpath. The version of Ant used for initial development was 1.5.
 */
public class JarBuilder {

  public JarBuilder()
  {
  }

  /**
   * Build the jar for the project including classes, screens and resources
   * @param fileName The name of the jar file to be built
   * @param rootDir the root directory of the project
   */
  public void buildJar( String fileName, String rootDir )
  {
    createBuildDir( rootDir );
    Jar jar = new Jar();
    try {
      JarOutputStream jos = new JarOutputStream( new FileOutputStream( rootDir + "\\build\\" + fileName ) );
      jos.setLevel( 0 );
//      jar.initZipOutputStream( jos );
      zipDir( jos, jar, rootDir + "\\classes", "" );
      zipDir( jos, jar, rootDir + "\\pages", "" );
      zipDir( jos, jar, rootDir + "\\resources", "" );
      jos.flush();
      jos.close();
    }
    catch ( Exception ex ) {
      ex.printStackTrace();
    }
  }

  /**
   * recursive function which builds up the directory based on the new directory
   * name found
   * @param zos ZippedOutputStream for the jar file
   * @param jar the Ant Jar object used for writing out the file streams
   * @param rootDir the root directory for the files (the project root)
   * @param dir the offset dir being build up (as in the case of packages)
   */
  public void zipDir( JarOutputStream jos, Jar jar, String rootDir, String dir )
  {
    try {
      File folder = new File( rootDir + File.separatorChar );
      File[] files = folder.listFiles();
      for ( int i=0; i<files.length; i++ ){
        if ( files[i].isDirectory() ){
          zipDir( jos, jar, files[ i ].getAbsolutePath(), dir + files[ i ].getName() + "\\" );
        } else{
          putJarEntry( jos, files[i], dir + files[ i ].getName() );
        }
      }
    }
    catch ( Exception ex ) {
      ex.printStackTrace();
    }
  }

  private void putJarEntry( JarOutputStream jos, File f, String dir )
  {
    try {
      JarEntry je = new JarEntry( dir );
      jos.putNextEntry( je );
      FileInputStream bais = new FileInputStream( f );

      byte b[] = new byte[ 1024 ];
      byte fileBytes[];
      int len = bais.read( b );
      while ( len > -1 ) {
        fileBytes = new byte[ len ];
        System.arraycopy( b, 0, fileBytes, 0, len );
        jos.write( fileBytes, 0, len );
        b = new byte[ 1024 ];
        len = bais.read( b );
      }
      jos.closeEntry();
    }
    catch ( IOException ex ) {
      ex.printStackTrace();
    }
  }

  /**
   * Create the build directory
   * @param rootDir The project root directory
   */
  private void createBuildDir( String rootDir )
  {
    File file = new File( rootDir + "\\build" );
    try {
      file.mkdir();
    }
    catch ( Exception ex ) {
      ex.printStackTrace();
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.