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

it.vige.cities.Generator Maven / Gradle / Ivy

The newest version!
package it.vige.cities;

import static java.util.Locale.getDefault;
import static org.apache.commons.cli.Option.builder;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.MissingOptionException;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.slf4j.Logger;

import it.vige.cities.result.Nodes;
import it.vige.cities.templates.GeoNames;
import it.vige.cities.templates.en.Britannica;
import it.vige.cities.templates.it.ComuniItaliani;
import it.vige.cities.templates.it.ExtraGeoNames;
import it.vige.cities.templates.it.Tuttitalia;

/**
 * Generator of names
 * @author lucastancapiano
 */
public class Generator extends Template {

	/**
	 * Single case sensitive
	 */
	public final static String SINGLE_CASE_SENSITIVE = "s";

	/**
	 * Single country
	 */
	public final static String SINGLE_COUNTRY = "c";

	/**
	 * Singlw provider
	 */
	public final static String SINGLE_PROVIDER = "p";

	/**
	 * Single duplicated names
	 */
	public final static String SINGLE_DUPLICATED_NAMES = "d";

	/**
	 * Single user
	 */
	public final static String SINGLE_USER = "u";

	/**
	 * Multi case sensitive
	 */
	public final static String MULTI_CASE_SENSITIVE = "case_sensitive";

	/**
	 * Multi country
	 */
	public final static String MULTI_COUNTRY = "country";

	/**
	 * Multi provider
	 */
	public final static String MULTI_PROVIDER = "provider";

	/**
	 * Multi duplicated names
	 */
	public final static String MULTI_DUPLICATED_NAMES = "duplicated";

	/**
	 * Multi user
	 */
	public final static String MULTI_USER = "user";

	private static Logger logger = getLogger(Generator.class);

	private boolean caseSensitive;
	private boolean duplicatedNames;
	private String provider;
	private String username;
	private boolean overwrite;

	/**
	 * Generator
	 * @param configuration the configuration
	 * @param overwrite     true if the result is to overwrite
	 */
	public Generator(Configuration configuration, boolean overwrite) {
		this.caseSensitive = configuration.isCaseSensitive();
		this.duplicatedNames = configuration.isDuplicatedNames();
		this.provider = configuration.getProvider();
		this.country = configuration.getCountry();
		this.username = configuration.getUsername();
		this.overwrite = overwrite;
	}

	/**
	 * Configure options
	 * @param args the parameters
	 * @return the command line
	 * @throws ParseException
	 */
	private static CommandLine configureOptions(String[] args) throws ParseException {
		Options options = new Options();
		options.addOption(builder(SINGLE_CASE_SENSITIVE).longOpt(MULTI_CASE_SENSITIVE).type(Boolean.class)
				.desc(MULTI_CASE_SENSITIVE).build());
		options.addOption(builder(SINGLE_COUNTRY).longOpt(MULTI_COUNTRY).type(String.class).hasArg().numberOfArgs(1)
				.desc(MULTI_COUNTRY).build());
		options.addOption(builder(SINGLE_PROVIDER).longOpt(MULTI_PROVIDER).type(String.class).hasArg().numberOfArgs(1)
				.desc(MULTI_PROVIDER).build());
		options.addOption(builder(SINGLE_DUPLICATED_NAMES).longOpt(MULTI_DUPLICATED_NAMES).type(Boolean.class)
				.desc(MULTI_DUPLICATED_NAMES).build());
		options.addOption(builder(SINGLE_USER).longOpt(MULTI_USER).type(String.class).hasArg().numberOfArgs(1)
				.desc(MULTI_USER).build());

		CommandLineParser parser = new DefaultParser();
		CommandLine cmd = parser.parse(options, args);
		return cmd;
	}

	/**
	 * Templates
	 * @return the list of templates
	 */
	private List