package org.pmdreports;

import org.pmdreports.manager.ConfigManager;
import org.pmdreports.manager.PMDManager;
import org.pmdreports.manager.ReportManager;
import org.pmdreports.util.PMDReportsException;

/**
 * Driver class for PMDReports. Run PMD and then generate the reports.
 * 
 * @author Ben Northrop, Sep 27, 2007
 */
public final class PMDReports {

	/**
	 * Private constructor since should not be instantiated.
	 */
	private PMDReports() {
		// do nothing
	}

	/**
	 * Run PMD and generate reports.
	 */
	public static void main(String[] args) {
		final String configDir = stripParameter("-configDir", args);
		final String outputDir = stripParameter("-outputDir", args);

		ConfigManager.newInstance(configDir, outputDir);

		PMDManager pmdManager = new PMDManager();
		pmdManager.runPMD();

		ReportManager reportManager = new ReportManager();
		reportManager.generateReports();
	}

	/**
	 * Retrieve a given parameter, given the option. For example, if passing in
	 * "-outputdir" this will get the next param that goes with it.
	 */
	private static String stripParameter(String option, String[] args) {
		if (args == null || args.length == 0) {
			return "";
		}
		
		for (int cnt = 0; cnt < args.length; cnt++) {
			if (args[cnt].equalsIgnoreCase(option)) {
				if (args.length < cnt + 2) {
					throw new PMDReportsException("No value after " + option);
				}
				return args[cnt + 1];
			}

		}
		return "";
	}
}
