package jas2.loader;
import java.util.*;
import java.util.zip.*;
import java.io.*;

/**
 * A ClassPath object represents a Java classpath. Elements in the classpath
 * can be JAR files or Directories.
 */

public class ClassPath
{
	/**
	 *  Creates empty ClassPath
	 */ 
	public ClassPath()
	{
	}
	/**
	 * Builds a ClassPath from a String
	 */
	public ClassPath(String path)
	{
		StringTokenizer tk = new StringTokenizer(path,File.pathSeparator);
		while (tk.hasMoreElements())
		{
			try
			{
				add(tk.nextToken());
			}
			catch (ZipException x) {}
			catch (IOException x) {};
		}
	}
	public void add(String s) throws ZipException, IOException
	{
		File f = new File(s);
		if (f.isDirectory()) v.addElement(f);
		else
		{
			ZipFile z = new ZipFile(f);
			v.addElement(z);
		}
	}
	public void add(File f)
	{
		v.addElement(f);
	}
	public void add(ZipFile z)
	{
		v.addElement(z);
	}
	public Enumeration elements()
	{
		return v.elements();
	}
	public void setContents(Vector newVector)
	{
		v = newVector;
	}
	public void clear()
	{
		Enumeration e = v.elements();
		v = new Vector();
		close(e);
	}
	private void close(Enumeration e)
	{
		e = v.elements();
		while (e.hasMoreElements())
		{
			Object o = e.nextElement();
			// Hopefully this is safe now.
			try
			{
				if (o instanceof ZipFile) ((ZipFile) o).close();
			}
			catch (IOException x) {}
		}
	}
	public void finalize()
	{
		close(v.elements());
	}
	public String getClassPath()
	{
		String sep = File.pathSeparator;
		StringBuffer result = new StringBuffer();
		Enumeration e = elements();
		while (e.hasMoreElements())
		{
			Object o = e.nextElement();
			result.append(o instanceof ZipFile ? ((ZipFile) o).getName():o.toString());
			if (e.hasMoreElements()) result.append(sep);
		}
		return result.toString();
	}
	public String toString()
	{
		return getClassPath();
	}
	private Vector v = new Vector();
}

 

Ads help maintain this website.