package de.gne.ant.javahelp;

import java.util.List;
import java.util.LinkedList;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import java.lang.NoClassDefFoundError;

/**
 * Ant-Task for JavaHelp jhindexer
 * Copyright (C) 2001 Bernd Dutkowski, Michael Simon
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *	<taskdef name="jhindexer" classname="de.gne.ant.javahelp.JHIndexer"/>
 *	<jhindexer config="..." databasedir="..." locale="..." logfile="..." nostopwords="true|false" verbose="true|false" dir="..."/>
 *
 * @author	Bernd Dutkowski	<bernd.dutkowski@g-ne.de>
 * @author	Michael Simon	<michael.simon@g-ne.de>
 * @created	2001/11/10
 * @version	0.1
 * @see		org.apache.tools.ant.Task
 */
public class JHIndexer extends Task
{
	private String config;
	private String databasedir;
	private String locale;
	private String logfile;
	private boolean nostopwords;
	private boolean verbose;
	private String dir;

	public JHIndexer()
	{
		config = null;
		databasedir = null;
		locale = null;
		logfile = null;
		nostopwords = false;
		verbose = false;
		dir = null;
	}

	/**
	 * @param config Config File Name
	 * @throws org.apache.tools.ant.BuildException
	 */
	public void setConfig(String config) throws BuildException
	{
		this.config = config;
	}

	/**
	 * @param databasedir name of the database output folder
	 * @throws org.apache.tools.ant.BuildException
	 */
	public void setDatabaseDir(String databasedir) throws BuildException
	{
		this.databasedir = databasedir;
	}

	/**
	 * @param locale The name of the locale as described in java.util.Locale
	 * @throws org.apache.tools.ant.BuildException
	 * @see java.util.Locale
	 */
	public void setLocale(String locale) throws BuildException
	{
		this.locale = locale;
	}

	/**
	 * @param logfile Captures jhindexer messages in a specified file
	 * @throws org.apache.tools.ant.BuildException
	 */
	public void setLogFile(String logfile) throws BuildException
	{
		this.logfile = logfile;
	}

	/**
	 * @param nostopwords
	 * @throws org.apache.tools.ant.BuildException
	 */
	public void setNoStopWords(boolean nostopwords) throws BuildException
	{
		this.nostopwords = nostopwords;
	}
	
	/**
	 * @param verbose
	 * @throws org.apache.tools.ant.BuildException
	 */
	public void setVerbose(boolean verbose) throws BuildException
	{
		this.verbose = verbose;
	}

	/**
	 * @param dir a folder, the folder is searched recursively for JavaHelp system content files
	 * @throws org.apache.tools.ant.BuildException
	 */
	public void setDir(String dir) throws BuildException
	{
		this.dir = dir;
	}
	
	/**
	 * @throws org.apache.tools.ant.BuildException
	 */
	public void execute() throws BuildException
	{
		List parameterList;
		String [] parameterArray;
		int i;
		int size;
		if((dir == null) || (dir.length() == 0))
		{
			throw new BuildException("missing directory");
		}
		parameterList = new LinkedList();
		if(config != null)
		{
			parameterList.add("-c");
			parameterList.add(config);
		}
		if(databasedir != null)
		{
			parameterList.add("-db");
			parameterList.add(databasedir);
		}
		if(locale != null)
		{
			parameterList.add("-locale");
			parameterList.add(locale);
		}
		if(logfile != null)
		{
			parameterList.add("-logfile");
			parameterList.add(logfile);
		}
		if(nostopwords)
		{
			parameterList.add("-nostopwords");
		}
		if(verbose)
		{
			parameterList.add("-verbose");
		}
		parameterList.add(dir);
		size = parameterList.size();
		parameterArray = new String[size];
		for(i = 0; i < size; i++)
		{
			parameterArray[i] = (String) parameterList.get(i);
		}
		parameterList = null;
		try
		{
			com.sun.java.help.search.Indexer.main(parameterArray);
		}
		catch(NoClassDefFoundError ncdfe)
		{
			throw new BuildException("you need jhall.jar in your ant-lib directory");
		}
		catch(Exception e)
		{
			throw new BuildException(e.toString());
		}
    }
}

