package org.apache.maven.vdoclet;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * ====================================================================
 */

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.tags.velocity.JellyContextAdapter;
import org.apache.velocity.context.Context;

import com.thoughtworks.qdox.model.JavaSource;

import vdoclet.Generator;
import vdoclet.docinfo.DocInfo;
import vdoclet.docinfo.QDoxBuilder;

/**
 * Command-line entry-point to vDoclet
 */
public class VDocletBean
{
    /** output directory */
    File srcDir;
    
    /** output directory */
    File destDir;

    /** output directory */
    String outputFile;

    /** control template */
    String template;

    /** encoding */
    String encoding;

    /** verbose flag */
    boolean verbose = false;
    
    /** docinfo */
    DocInfo docInfo;

    /** The jelly context */
    JellyContext jellyContext;

    /**
     * Alias for srcPath
     */
    public void setSrcDir(File srcDir)
    {
        this.srcDir = srcDir;
    }

    /**
     * destDir attribute
     */
    public void setDestDir(File destDir)
    {
        this.destDir = destDir;
    }

    /** 
     * Set the Jelly context.
     * 
     * @param jellyContext the Jelly context
     */
    public void setJellyContext(JellyContext jellyContext)
    {
        this.jellyContext = jellyContext;
    }

    /**
     * destDir attribute
     */
    public void setOutputFile(String outputFile)
    {
        this.outputFile = outputFile;
    }

    /**
     * template attribute
     */
    public void setTemplate(String template)
    {
        this.template = template;
    }
    
    /**
     * encoding attribute
     */
    public void setEncoding(String encoding)
    {
        this.encoding = encoding;
    }

    /**
     * Description of the Method
     */
    public void execute()
        throws Exception
    {
        parseSources();
        generate();
    }

    /**
     * Description of the Method
     */
    void log(String msg)
    {
        if (verbose)
        {
            System.err.println(msg);
        }
    }

    /**
     * Parse the source-files
     */
    void parseSources()
        throws IOException
    {
        com.thoughtworks.qdox.JavaDocBuilder builder =
            new com.thoughtworks.qdox.JavaDocBuilder();
        
        builder.addSourceTree(srcDir);
        
        JavaSource[] javaSources = builder.getSources();
        log(javaSources.length + " source-files");
        docInfo = QDoxBuilder.build(javaSources);
        docInfo.setSourcePath(srcDir.getAbsolutePath());
    }

    /**
     * Kick-start generation
     */
    void generate()
        throws Exception
    {
        if (!destDir.exists())
        {
            destDir.mkdirs();
        }
        
        Generator generator = new Generator(destDir);
        generator.setAttribute("docInfo", docInfo);

        if (jellyContext != null)
        {
            generator.setAttribute("jellyContext", getJellyContextAdapter());
        }

        if (encoding != null)
        {
            generator.setAttribute("encoding", encoding);
        }
        generator.eval(template, new FileWriter(new File(destDir, outputFile)));
    }

    private Context getJellyContextAdapter()
    {
        JellyContextAdapter adapter = new JellyContextAdapter(jellyContext);

        adapter.setReadOnly(true);

        return adapter;
    }
}

