// 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.classpath;

import com.symantec.itools.io.Directory;
import com.symantec.itools.io.FileSystem;
import com.symantec.itools.lang.Debug;
import com.symantec.itools.util.StringTokenizer;
import java.io.*;
import java.net.URL;
import java.util.Hashtable;
import java.util.Vector;

// Referenced classes of package com.symantec.itools.lang.classpath:
//            ClasspathEntry, ClasspathEntryTypesIdentifier, ClasspathElement

public class Classpath
{

    public static Classpath getSystemClasspath()
    {
        return systemCp;
    }

    public static Classpath create(String s, int i)
    {
        return new Classpath(s, i);
    }

    public static Classpath create(String s, int i, ClasspathEntryTypesIdentifier classpathentrytypesidentifier)
    {
        return new Classpath(s, i, classpathentrytypesidentifier);
    }

    protected Classpath()
    {
        shortHandToEntryMap = new Hashtable();
        nameToValidEntryMap = new Hashtable();
        nameToActualEntryMap = new Hashtable();
        StringBuffer stringbuffer = new StringBuffer();
        String s = System.getProperty("sun.boot.class.path");
        if(s != null && !s.equals(""))
        {
            stringbuffer.append(s);
            stringbuffer.append(File.pathSeparatorChar);
        }
        s = System.getProperty("java.ext.dirs");
        if(s != null && !s.equals(""))
            try
            {
                String as[] = (new Directory(s)).list(false);
                for(int i = 0; i < as.length; i++)
                {
                    stringbuffer.append(as[i]);
                    stringbuffer.append(File.pathSeparatorChar);
                }

            }
            catch(IOException _ex) { }
        s = System.getProperty("java.class.path");
        if(s != null && !s.equals(""))
        {
            stringbuffer.append(s);
            stringbuffer.append(File.pathSeparatorChar);
        }
        createStyle = 0;
        setClasspath(stringbuffer.toString(), createStyle);
        setEntryTypes(ClasspathEntryTypesIdentifier.DEFAULT);
    }

    protected Classpath(String s, int i)
    {
        shortHandToEntryMap = new Hashtable();
        nameToValidEntryMap = new Hashtable();
        nameToActualEntryMap = new Hashtable();
        createStyle = i;
        setClasspath(s, createStyle);
    }

    protected Classpath(String s, int i, ClasspathEntryTypesIdentifier classpathentrytypesidentifier)
    {
        shortHandToEntryMap = new Hashtable();
        nameToValidEntryMap = new Hashtable();
        nameToActualEntryMap = new Hashtable();
        createStyle = i;
        setClasspath(s, i);
        setEntryTypes(classpathentrytypesidentifier);
    }

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

        for(int k = 0; k < actualClasspath.length; k++)
        {
            String s1 = actualClasspath[k];
            File file = new File(s1);
            ClasspathEntry classpathentry = ClasspathEntry.create(s1, i);
            if(!vector.contains(s1) && file.exists())
                vector.addElement(classpathentry);
            String s2;
            if(file.isFile())
                s2 = "ZIP";
            else
                s2 = "FILE";
            String s3 = s2 + k;
            shortHandToEntryMap.put(s3, classpathentry);
            nameToActualEntryMap.put(s1, classpathentry);
        }

        int l = vector.size();
        validClasspath = new String[l];
        validEntries = new ClasspathEntry[validClasspath.length];
        vector.copyInto(validEntries);
        for(int i1 = 0; i1 < validEntries.length; i1++)
        {
            validClasspath[i1] = validEntries[i1].getName();
            nameToValidEntryMap.put(validClasspath[i1], validEntries[i1]);
        }

    }

    public void setEntryTypes(ClasspathEntryTypesIdentifier classpathentrytypesidentifier)
    {
        if(validEntries != null)
        {
            Vector vector = new Vector();
            Vector vector1 = new Vector();
            Vector vector2 = new Vector();
            for(int i = 0; i < validEntries.length; i++)
                if(validEntries[i].setType(classpathentrytypesidentifier) == 1)
                    vector.addElement(validEntries[i].getName());
                else
                if(validEntries[i].setType(classpathentrytypesidentifier) == 2)
                    vector1.addElement(validEntries[i].getName());
                else
                    vector2.addElement(validEntries[i].getName());

            bootEntries = new String[vector.size()];
            vector.copyInto(bootEntries);
            extEntries = new String[vector1.size()];
            vector1.copyInto(extEntries);
            appEntries = new String[vector2.size()];
            vector2.copyInto(appEntries);
        }
    }

    public String[] getBootEntries()
    {
        return bootEntries;
    }

    public String[] getExtensionEntries()
    {
        return extEntries;
    }

    public String[] getApplicationEntries()
    {
        return appEntries;
    }

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

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

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

    public ClasspathEntry getEntry(String s)
    {
        ClasspathEntry classpathentry = (ClasspathEntry)shortHandToEntryMap.get(s);
        return classpathentry;
    }

    public boolean contains(String s, boolean flag)
    {
        boolean flag1 = false;
        if(flag)
            s = FileSystem.getCanonicalPath(s, false);
        if(nameToActualEntryMap.get(s) != null)
            flag1 = true;
        return flag1;
    }

    public boolean isValid(String s)
    {
        boolean flag = false;
        s = FileSystem.getCanonicalPath(s, true);
        if(nameToValidEntryMap.get(s) != null)
            flag = true;
        return flag;
    }

    public ClasspathEntry find(String s)
    {
        return find(s, false, false);
    }

    public ClasspathEntry find(String s, boolean flag, boolean flag1)
    {
        for(int i = 0; i < validEntries.length; i++)
            if(validEntries[i].find(s) && (!flag || validEntries[i].getType() != 1) && (!flag1 || validEntries[i].getType() != 2))
                return validEntries[i];

        return null;
    }

    public InputStream findFile(String s)
    {
        return findFile(s, false, false);
    }

    public ClasspathElement findElement(String s)
    {
        return findElement(s, false, false);
    }

    public ClasspathElement findElement(String s, boolean flag, boolean flag1)
    {
        for(int i = 0; i < validEntries.length; i++)
            if(validEntries[i].find(s) && (!flag || validEntries[i].getType() != 1) && (!flag1 || validEntries[i].getType() != 2))
            {
                Debug.logInformation("Classpath.findFile: file " + s + " is in " + validEntries[i]);
                return validEntries[i].findElement(s);
            }

        Debug.logInformation("Classpath.findFile: file " + s + " not found.");
        return null;
    }

    public InputStream findFile(String s, boolean flag, boolean flag1)
    {
        for(int i = 0; i < validEntries.length; i++)
            if(validEntries[i].find(s) && (!flag || validEntries[i].getType() != 1) && (!flag1 || validEntries[i].getType() != 2))
            {
                Debug.logInformation("Classpath.findFile: file " + s + " is in " + validEntries[i]);
                return validEntries[i].getInputStream(s);
            }

        Debug.logInformation("Classpath.findFile: file " + s + " not found.");
        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);

            Classpath classpath = new Classpath(stringbuffer.toString(), createStyle);
            return classpath;
        } else
        {
            return this;
        }
    }

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

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

    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 < validClasspath.length; i++)
        {
            boolean flag = false;
            for(int j = 0; j < actualClasspath.length; j++)
            {
                if(!actualClasspath[i].equals(validClasspath[j]))
                    continue;
                flag = true;
                break;
            }

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

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

    public static void main(String args[])
    {
        Classpath classpath = getSystemClasspath();
        System.out.println("boot entries");
        String args1[] = classpath.getBootEntries();
        for(int i = 0; i < args1.length; i++)
            System.out.println("   " + args1[i]);

        System.out.println();
        System.out.println("ext entries");
        args1 = classpath.getExtensionEntries();
        for(int j = 0; j < args1.length; j++)
            System.out.println("   " + args1[j]);

        System.out.println();
        System.out.println("app entries");
        args1 = classpath.getApplicationEntries();
        for(int k = 0; k < args1.length; k++)
            System.out.println("   " + args1[k]);

    }

    protected static final Classpath systemCp;
    public static final int ALLFLYWEIGHT = 0;
    public static final int NEWNOTFLYWEIGHT = 1;
    public static final int NOFLYWEIGHT = 2;
    protected String actualClasspath[];
    protected String validClasspath[];
    protected ClasspathEntry validEntries[];
    protected Hashtable shortHandToEntryMap;
    protected Hashtable nameToValidEntryMap;
    protected Hashtable nameToActualEntryMap;
    protected String bootEntries[];
    protected String extEntries[];
    protected String appEntries[];
    protected int createStyle;

    static 
    {
        Classpath classpath;
        try
        {
            classpath = new Classpath();
        }
        catch(Throwable throwable)
        {
            throwable.printStackTrace();
            classpath = new Classpath();
        }
        systemCp = classpath;
    }
}
