package org.apache.maven.jxr;

/* ====================================================================
 *   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 org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

/**
 * Creates an html-based, cross referenced  version of Java source code
 * for a project.
 *
 * @author <a href="mailto:lucas@collab.net">Josh Lucas</a>
 * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
 * @author <a href="mailto:brian@brainslug.com">Brian Leonard</a>
 * @version $Id$
 */
public class JxrBean
{
    /*
     * See the doc comments for the corresponding getter/setter methods
     */
    private List sourceDirs;

    private String destDir;

    private Locale locale;

    private String inputEncoding;

    private String outputEncoding;

    private String javadocDir;

    private String windowTitle;

    private String docTitle;

    private String bottom;

    private String templateDir;

    private final Log log = LogFactory.getLog( JxrBean.class );

    /**
     * Default constructor
     */
    public JxrBean()
    {
        sourceDirs = new LinkedList();
    }

    /**
     * Starts the cross-referencing and indexing.
     */
    public void xref()
        throws JxrException
    {
        org.apache.maven.jxr.log.Log logger = new org.apache.maven.jxr.log.Log()
        {
            public void info( String message )
            {
                log.info( message );
            }

            public void debug( String message )
            {
                log.info( message );
            }

            public void warn( String message )
            {
                log.info( message );
            }

            public void error( String message )
            {
                log.info( message );
            }
        };

        JXR jxr = new JXR();
        jxr.setDest( destDir );
        jxr.setLocale( locale );
        jxr.setInputEncoding( inputEncoding );
        jxr.setOutputEncoding( outputEncoding );
        jxr.setJavadocLinkDir( javadocDir );
        jxr.setRevision( "HEAD" );
        jxr.setLog( logger );

        try
        {
            jxr.xref( sourceDirs, getTemplateDir(), getWindowTitle(), getDocTitle(), getBottom() );
        }
        catch ( IOException e )
        {
            throw new JxrException( "Error processing files", e );
        }
    }

    /**
     * Sets a single source directory to be cross-referenced.
     *
     * @param sourceDir The source directory to be cross-referenced.
     */
    public void setSourceDir( String sourceDir )
    {
        if ( !sourceDirs.isEmpty() )
        {
            sourceDirs.clear();
        }
        addSourceDir( sourceDir );
    }

    /**
     * Adds a directory to the list of those to be cross-referenced.
     *
     * @param sourceDir The source directory to be cross-referenced.
     */
    public void addSourceDir( String sourceDir )
    {
        sourceDirs.add( sourceDir );
    }


    /**
     * DestDir is the directory in which jxr will write its output
     *
     * @param destDir the destination directory for jxr output
     */
    public void setDestDir( String destDir )
    {
        this.destDir = destDir;
    }

    /**
     * see setDestDir(String)
     */
    public String getDestDir()
    {
        return destDir;
    }

    public Locale getLocale()
    {
        return locale;
    }

    public void setLocale( Locale locale )
    {
        this.locale = locale;
    }

    /**
     * InputEncoding is the encoding of source files.
     *
     * @param inputEncoding encoding of source files
     */
    public void setInputEncoding( String inputEncoding )
    {
        this.inputEncoding = inputEncoding;
    }

    /**
     * see setInputEncoding(String)
     */
    public String getInputEncoding()
    {
        return inputEncoding;
    }


    /**
     * OutputEncoding is the encoding of output files.
     *
     * @param outputEncoding encoding of output files
     */
    public void setOutputEncoding( String outputEncoding )
    {
        this.outputEncoding = outputEncoding;
    }

    /**
     * see setOutputEncoding(String)
     */
    public String getOutputEncoding()
    {
        return outputEncoding;
    }


    /**
     * JavadocDir is used to cross-reference the source code with
     * the appropriate javadoc pages.
     *
     * If <code>null</code>, no javadoc link will be added.
     *
     * @param javadocDir The root directory containing javadocs
     */
    public void setJavadocDir( String javadocDir )
    {
        this.javadocDir = javadocDir;
    }

    /**
     * see setJavadocDir(String)
     */
    public String getJavadocDir()
    {
        return javadocDir;
    }

    /**
     * see DirectoryIndexer
     *
     * @param windowTitle used by DirectoryIndexer
     * @see DirectoryIndexer#setWindowTitle(String) setWindowTitle(String)
     */
    public void setWindowTitle( String windowTitle )
    {
        this.windowTitle = windowTitle;
    }

    /**
     * see DirectoryIndexer
     *
     * @see DirectoryIndexer#setWindowTitle(String) setWindowTitle(String)
     */
    public String getWindowTitle()
    {
        return windowTitle;
    }

    /**
     * see DirectoryIndexer
     *
     * @param docTitle used by DirectoryIndexer
     * @see DirectoryIndexer#setDocTitle(String) setDocTitle(String)
     */
    public void setDocTitle( String docTitle )
    {
        this.docTitle = docTitle;
    }

    /**
     * see DirectoryIndexer
     *
     * @see DirectoryIndexer#setDocTitle(String) setDocTitle(String)
     */
    public String getDocTitle()
    {
        return docTitle;
    }

    /**
     * see DirectoryIndexer
     *
     * @param bottom used by DirectoryIndexer
     * @see DirectoryIndexer#setBottom(String) setBottom(String)
     */
    public void setBottom( String bottom )
    {
        this.bottom = bottom;
    }

    /**
     * see DirectoryIndexer
     *
     * @see DirectoryIndexer#setBottom(String) setBottom(String)
     */
    public String getBottom()
    {
        return bottom;
    }

    /**
     * TemplateDir is where the navigation templates are located
     *
     * @param templateDir the template directory
     */
    public void setTemplateDir( String templateDir )
    {
        this.templateDir = templateDir;
    }

    /**
     * see setTemplateDir(String)
     */
    public String getTemplateDir()
    {
        return templateDir;
    }

}

