// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) deadcode 
// Source File Name:   Classpath.java

package com.symantec.itools.lang;

import com.symantec.itools.io.FileSystem;
import com.symantec.itools.util.StringTokenizer;
import java.io.File;
import java.net.URL;
import java.util.Hashtable;
import java.util.Vector;

// Referenced classes of package com.symantec.itools.lang:
//            ClasspathEntry, JavaName, NotJavaNameException

public class Classpath
{

    public Classpath()
    {
        this(System.getProperty("java.class.path"));
    }

    public Classpath(String s)
    {
        filesTables = new Hashtable();
        setClasspath(s);
    }

    public void setClasspath(String s)
    {
        String as[] = (new StringTokenizer(s, File.pathSeparator)).getTokens();
        actualClasspath = new String[as.length];
        for(int i = 0; i < as.length; i++)
            actualClasspath[i] = FileSystem.getCanonicalPath(as[i], true);

        as = FileSystem.getValidPath(s, true);
        Vector vector = new Vector();
        for(int j = 0; j < as.length; j++)
            if(!vector.contains(as[j]))
                vector.addElement(as[j]);

        validClasspath = new String[vector.size()];
        vector.copyInto(validClasspath);
        validEntries = new ClasspathEntry[validClasspath.length];
        for(int k = 0; k < validEntries.length; k++)
            validEntries[k] = new ClasspathEntry(validClasspath[k]);

        actualEntriesTable = new Hashtable();
        for(int l = 0; l < actualClasspath.length; l++)
        {
            String s1;
            if((new File(actualClasspath[l])).isFile())
                s1 = "ZIP";
            else
                s1 = "FILE";
            actualEntriesTable.put(s1 + l, new ClasspathEntry(actualClasspath[l]));
        }

    }

    public String[] getActualClasspath()
    {
        return actualClasspath;
    }

    public String[] getValidClasspath()
    {
        return validClasspath;
    }

    public ClasspathEntry getEntry(URL url)
    {
        if(url == null)
            return null;
        String s = url.getFile();
        if(!s.startsWith("/ZIP") && !s.startsWith("/FILE"))
            return null;
        else
            return getEntry(s.substring(1, s.indexOf('/', 1)));
    }

    public boolean contains(String s)
    {
        s = FileSystem.getCanonicalPath(s, true);
        for(int i = 0; i < actualClasspath.length; i++)
            if(FileSystem.compareFilenames(actualClasspath[i], s))
                return true;

        return false;
    }

    public boolean isValid(String s)
    {
        s = FileSystem.getCanonicalPath(s, true);
        for(int i = 0; i < validClasspath.length; i++)
            if(FileSystem.compareFilenames(validClasspath[i], s))
                return true;

        return false;
    }

    public ClasspathEntry getEntry(String s)
    {
        return (ClasspathEntry)actualEntriesTable.get(s);
    }

    public JavaName convertFileNameToJavaName(File file)
        throws NotJavaNameException
    {
        return convertFileNameToJavaName(FileSystem.getCanonicalPath(file, true));
    }

    public JavaName convertFileNameToJavaName(String s)
        throws NotJavaNameException
    {
        for(int i = 0; i < validEntries.length; i++)
            if(validEntries[i].isDirectory())
            {
                String s1 = validEntries[i].getName();
                if(FileSystem.isInPath(s, s1))
                    return new JavaName(s.substring(s1.length()).replace(File.separatorChar, '/'));
            }

        return new JavaName(s.substring(s.lastIndexOf(File.separatorChar) + 1));
    }

    public void load()
    {
        for(int i = 0; i < validEntries.length; i++)
            if(filesTables.get(validEntries[i].getName()) == null)
                filesTables.put(validEntries[i].getName(), validEntries[i].load(this));

    }

    public ClasspathEntry find(String s)
    {
        for(int i = 0; i < validEntries.length; i++)
            if(validEntries[i].find(s))
                return validEntries[i];

        return null;
    }

    public String getActualClasspathAsString()
    {
        StringBuffer stringbuffer = new StringBuffer();
        int i;
        for(i = 0; i < actualClasspath.length - 1; i++)
            stringbuffer.append(actualClasspath[i]).append(File.pathSeparatorChar);

        stringbuffer.append(actualClasspath[i]);
        return stringbuffer.toString();
    }

    public String getValidClasspathAsString()
    {
        StringBuffer stringbuffer = new StringBuffer();
        int i;
        for(i = 0; i < validClasspath.length - 1; i++)
            stringbuffer.append(validClasspath[i]).append(File.pathSeparatorChar);

        stringbuffer.append(validClasspath[i]);
        return stringbuffer.toString();
    }

    public Classpath removeEntry(String s)
    {
        s = FileSystem.getCanonicalPath(s, true);
        int i;
        for(i = 0; i < validClasspath.length; i++)
            if(FileSystem.compareFilenames(s, validClasspath[i]))
                break;

        if(i <= validClasspath.length)
        {
            StringBuffer stringbuffer = new StringBuffer();
            for(int j = 0; j < validClasspath.length; j++)
                if(j != i)
                    stringbuffer.append(validClasspath[j]).append(File.pathSeparatorChar);

            return new Classpath(stringbuffer.toString());
        } else
        {
            return this;
        }
    }

    public Classpath prependEntry(String s)
    {
        return new Classpath(s + File.pathSeparatorChar + getValidClasspathAsString());
    }

    public Classpath appendEntry(String s)
    {
        return new Classpath(getValidClasspathAsString() + File.pathSeparatorChar + s);
    }

    public String[] getValidEntries()
    {
        String as[] = new String[validClasspath.length];
        for(int i = 0; i < as.length; i++)
            as[i] = validClasspath[i];

        return as;
    }

    public String[] getInvalidEntries()
    {
        Vector vector = new Vector();
        for(int i = 0; i < actualClasspath.length; i++)
        {
            boolean flag = false;
            for(int j = 0; j < validClasspath.length; j++)
            {
                if(!validClasspath[j].equals(actualClasspath[i]))
                    continue;
                flag = true;
                break;
            }

            if(!flag)
                vector.addElement(actualClasspath[i]);
        }

        String as[] = new String[vector.size()];
        vector.copyInto(as);
        return as;
    }

    public void cleanup()
    {
        for(int i = 0; i < validEntries.length; i++)
            validEntries[i].cleanup();

    }

    protected void finalize()
        throws Throwable
    {
        super.finalize();
        cleanup();
    }

    protected Hashtable filesTables;
    protected String actualClasspath[];
    protected String validClasspath[];
    protected ClasspathEntry validEntries[];
    protected Hashtable actualEntriesTable;
}
