// 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:   DocletInvoker.java

package com.sun.tools.javadoc;

import com.sun.javadoc.RootDoc;
import com.sun.tools.javac.v8.util.List;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.*;
import java.net.*;
import java.util.StringTokenizer;

// Referenced classes of package com.sun.tools.javadoc:
//            Messager

public class DocletInvoker
{
    private static class DocletInvokeException extends Exception
    {

        private DocletInvokeException()
        {
        }

    }


    public boolean start(RootDoc rootdoc)
    {
        String s = "start";
        Class aclass[] = new Class[1];
        Object aobj[] = new Object[1];
        try
        {
            aclass[0] = Class.forName("com.sun.javadoc.RootDoc");
        }
        catch(ClassNotFoundException classnotfoundexception)
        {
            return false;
        }
        aobj[0] = rootdoc;
        Object obj;
        try
        {
            obj = invoke(s, null, aclass, aobj);
        }
        catch(DocletInvokeException docletinvokeexception)
        {
            return false;
        }
        if(obj instanceof Boolean)
        {
            return ((Boolean)obj).booleanValue();
        } else
        {
            messager.error(null, "main.must_return_boolean", docletClassName, s);
            return false;
        }
    }

    public boolean validOptions(List list)
    {
        String as[][] = (String[][])list.toArray(new String[list.length()][]);
        String s = "validOptions";
        Messager messager1 = messager;
        Class aclass[] = new Class[2];
        Object aobj[] = new Object[2];
        aclass[0] = as.getClass();
        try
        {
            aclass[1] = Class.forName("com.sun.javadoc.DocErrorReporter");
        }
        catch(ClassNotFoundException classnotfoundexception)
        {
            return false;
        }
        aobj[0] = as;
        aobj[1] = messager1;
        Object obj;
        try
        {
            obj = invoke(s, Boolean.TRUE, aclass, aobj);
        }
        catch(DocletInvokeException docletinvokeexception)
        {
            return false;
        }
        if(obj instanceof Boolean)
        {
            return ((Boolean)obj).booleanValue();
        } else
        {
            messager.error(null, "main.must_return_boolean", docletClassName, s);
            return false;
        }
    }

    public int optionLength(String s)
    {
        String s1 = "optionLength";
        Class aclass[] = new Class[1];
        Object aobj[] = new Object[1];
        aclass[0] = s.getClass();
        aobj[0] = s;
        Object obj;
        try
        {
            obj = invoke(s1, new Integer(0), aclass, aobj);
        }
        catch(DocletInvokeException docletinvokeexception)
        {
            return -1;
        }
        if(obj instanceof Integer)
        {
            return ((Integer)obj).intValue();
        } else
        {
            messager.error(null, "main.must_return_int", docletClassName, s1);
            return -1;
        }
    }

    static URL fileToURL(File file)
    {
        String s;
        try
        {
            s = file.getCanonicalPath();
        }
        catch(IOException ioexception)
        {
            s = file.getAbsolutePath();
        }
        s = s.replace(File.separatorChar, '/');
        if(!s.startsWith("/"))
            s = "/" + s;
        if(!file.isFile())
            s = s + "/";
        return new URL("file", "", s);
        MalformedURLException malformedurlexception;
        malformedurlexception;
        throw new IllegalArgumentException("file");
    }

    static URL[] pathToURLs(String s)
    {
        StringTokenizer stringtokenizer = new StringTokenizer(s, File.pathSeparator);
        URL aurl[] = new URL[stringtokenizer.countTokens()];
        int i = 0;
        do
        {
            if(!stringtokenizer.hasMoreTokens())
                break;
            URL url = fileToURL(new File(stringtokenizer.nextToken()));
            if(url != null)
                aurl[i++] = url;
        } while(true);
        if(aurl.length != i)
        {
            URL aurl1[] = new URL[i];
            System.arraycopy(aurl, 0, aurl1, 0, i);
            aurl = aurl1;
        }
        return aurl;
    }

    public DocletInvoker(Messager messager1, String s, String s1)
    {
        messager = messager1;
        docletClassName = s;
        String s2 = null;
        s2 = appendPath(System.getProperty("env.class.path"), s2);
        s2 = appendPath(System.getProperty("java.class.path"), s2);
        s2 = appendPath(s1, s2);
        URL aurl[] = pathToURLs(s2);
        appClassLoader = new URLClassLoader(aurl);
        Class class1 = null;
        try
        {
            class1 = appClassLoader.loadClass(s);
        }
        catch(ClassNotFoundException classnotfoundexception)
        {
            messager1.error(null, "main.doclet_class_not_found", s);
            messager1.exit();
        }
        docletClass = class1;
    }

    private String appendPath(String s, String s1)
    {
        if(s == null || s.length() == 0)
            return s1 != null ? s1 : ".";
        if(s1 == null || s1.length() == 0)
            return s;
        else
            return s + File.pathSeparator + s1;
    }

    private Object invoke(String s, Object obj, Class aclass[], Object aobj[])
        throws DocletInvokeException
    {
        Method method;
        try
        {
            method = docletClass.getMethod(s, aclass);
        }
        catch(NoSuchMethodException nosuchmethodexception)
        {
            if(obj == null)
            {
                messager.error(null, "main.doclet_method_not_found", docletClassName, s);
                throw new DocletInvokeException();
            } else
            {
                return obj;
            }
        }
        catch(SecurityException securityexception)
        {
            messager.error(null, "main.doclet_method_not_accessible", docletClassName, s);
            throw new DocletInvokeException();
        }
        if(!Modifier.isStatic(method.getModifiers()))
        {
            messager.error(null, "main.doclet_method_must_be_static", docletClassName, s);
            throw new DocletInvokeException();
        }
        Thread.currentThread().setContextClassLoader(appClassLoader);
        return method.invoke(null, aobj);
        Object obj1;
        obj1;
        messager.error(null, "main.internal_error_exception_thrown", docletClassName, s, ((IllegalArgumentException) (obj1)).toString());
        throw new DocletInvokeException();
        obj1;
        messager.error(null, "main.doclet_method_not_accessible", docletClassName, s);
        throw new DocletInvokeException();
        obj1;
        messager.error(null, "main.internal_error_exception_thrown", docletClassName, s, ((NullPointerException) (obj1)).toString());
        throw new DocletInvokeException();
        obj1;
        Throwable throwable = ((InvocationTargetException) (obj1)).getTargetException();
        if(throwable instanceof OutOfMemoryError)
        {
            messager.error(null, "main.out.of.memory");
        } else
        {
            messager.error(null, "main.exception_thrown", docletClassName, s, ((InvocationTargetException) (obj1)).toString());
            ((InvocationTargetException) (obj1)).getTargetException().printStackTrace();
        }
        throw new DocletInvokeException();
    }

    private final Class docletClass;
    private final String docletClassName;
    private final ClassLoader appClassLoader;
    private final Messager messager;
}
