package ca.janeg.cb;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


/**
 *  Constructs a new CBClassGroup object by extracting
 *  class names from a .jar or .zip archive file.
 *  Extracted class names are stored for retreival by package or
 *  alphabetically by name.
 *
 *@author     Jane Griscti jane@janeg.ca
 *@created    January 5, 2002
 */
class CBClassGroup {
    private ArrayList entries       = new ArrayList();
    private String[] sortedByPkg;
    private String[] sortedByClass;
    private String groupName;


    CBClassGroup( final ZipFile zip ) throws IOException {
        groupName = zip.getName();

        Enumeration allEntries  = zip.entries();

        ZipEntry zipEntry       = null;
        String name;

        while( allEntries.hasMoreElements() ) {
            zipEntry = (ZipEntry)allEntries.nextElement();
            name = zipEntry.getName();

            // only want full paths, not partials
            if( name.endsWith( ".java" ) || name.endsWith( ".class" ) ) {
                    // drop the .java or .class ending
                    StringTokenizer stok  = new StringTokenizer( name, "." );
                    String token          = stok.nextToken();
                    entries.add( token );
            }
        }

        Collections.sort( (ArrayList)entries );
        sortedByPkg = (String[])entries.toArray( new String[0] );

        Collections.sort( (ArrayList)entries, CBNameComparator.getInstance() );
        sortedByClass = (String[])entries.toArray( new String[0] );
        entries = null;

    }


    /**
     *  Gets the class name entries sorted by package.
     *
     *@return    An array of class names sorted by package.
     */
    String[] getByPackageName() {
        return sortedByPkg;
    }


    /**
     *  Gets the class name entries sorted by class.
     *
     *@return    An array of class names sorted by the class simple name.
     */
    String[] getByClassName() {
        return sortedByClass;
    }


    /**
     *  Gets the name of the group of entries.
     *
     *@return    The fullpath name of the file containing this group of entries.
     */
    String getGroupName() {
        return groupName;
    }

}