All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.java_cl_parser.UsageException Maven / Gradle / Ivy

package com.googlecode.java_cl_parser;

import org.apache.commons.io.IOUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UsageException extends Exception {
	private final List availableOptions;
	private Object cliEntry;

	public UsageException(Object entry, List options)
	{
		cliEntry = entry;
		availableOptions = options;
	}

	public UsageException(Object entry, List options, String message)
	{
		super(message);

		cliEntry = entry;
		availableOptions = options;
	}

	public UsageException(Object entry, List options, String message, Throwable cause)
	{
		super(message, cause);

		cliEntry = entry;
		availableOptions = options;
	}

	public UsageException(Object entry, List options, Throwable cause)
	{
		super(cause);

		cliEntry = entry;
		availableOptions = options;
	}

	public List getAvailableOptions()
	{
		return availableOptions;
	}

	public String getFormattedUsageStatement()
	{
		CLIEntry entry = cliEntry.getClass().getAnnotation(CLIEntry.class);

		Map map = new HashMap();

		String value = entry.nickName();
		if (value.equals("\0"))
		{
			value = cliEntry.getClass().getSimpleName();
		}
		map.put("nickName", value);

		map.put("description", StringUtil.wrapTextToList(entry.description(), 80));
		map.put("version", entry.version());

		value = entry.contact();
		if (!value.equals("\0"))
		{
			map.put("contact", value);
		}

		value = entry.versionControl();
		if (!value.equals("\0"))
		{
			map.put("scm", value);
		}

		value = entry.documentationUrl();
		if (!value.equals("\0"))
		{
			map.put("documentationUrl", value);
		}

		List optLines = new ArrayList();

		for (CommonsCLILauncher.Data option : availableOptions)
		{
			OptionLine oline = new OptionLine();

			oline.setShortName("-" + option.clioption.name());
			oline.setLongName("--" + option.clioption.longName());
			oline.setDataType("");

			String defaultValue = option.clioption.defaultValue();

			if (defaultValue.equals("\0"))
			{
				defaultValue = "";
			}

			String type = "";

			if (option.type != null)
			{
				switch (option.type)
				{

					case bool:
					case Bool:
						type = "boolean";
						break;
					case integer:
					case Integer:
						type = "integer";
						break;
					case string:
					case String:
					case Object:
						type = "string";
						break;
				}
			}

			oline.setDataType(type);

			oline.setDefaultValue(defaultValue);
			oline.setDescriptionLines(StringUtil.wrapTextToList(option.clioption.description(), 60));

			optLines.add(oline);
		}

		map.put("options", optLines);

		try
		{
			String template = IOUtils.toString(getClass().getResource("/usage_template.vm"));

			VelocityContext context = new VelocityContext(map);

			VelocityEngine velocityEngine = new VelocityEngine();

			velocityEngine.init();

			StringWriter w = new StringWriter();

			velocityEngine.evaluate(context, w, "log", template);

			return w.toString();
		}
		catch (IOException e)
		{
			throw new RuntimeException(e);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy