it.vige.cities.Generator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cities-generator Show documentation
Show all versions of cities-generator Show documentation
Generate a file with the cities of your country
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 getTemplates() {
List templates = new ArrayList();
if (country.equals(Countries.it.name())) {
if (provider == null || provider.equals(it.vige.cities.templates.it.Providers.COMUNI_ITALIANI.name())) {
templates.add(new ComuniItaliani(caseSensitive, duplicatedNames));
templates.add(new Tuttitalia(caseSensitive, duplicatedNames));
templates.add(new ExtraGeoNames(Countries.it.name(), caseSensitive, duplicatedNames, username));
templates.add(new GeoNames(Countries.it.name(), caseSensitive, duplicatedNames, username));
} else if (provider.equals(it.vige.cities.templates.it.Providers.TUTTITALIA.name())) {
templates.add(new Tuttitalia(caseSensitive, duplicatedNames));
templates.add(new ComuniItaliani(caseSensitive, duplicatedNames));
templates.add(new ExtraGeoNames(Countries.it.name(), caseSensitive, duplicatedNames, username));
templates.add(new GeoNames(Countries.it.name(), caseSensitive, duplicatedNames, username));
} else if (provider.equals(it.vige.cities.templates.it.Providers.COMUNI_ITALIANI.name())) {
templates.add(new Tuttitalia(caseSensitive, duplicatedNames));
templates.add(new ComuniItaliani(caseSensitive, duplicatedNames));
templates.add(new ExtraGeoNames(Countries.it.name(), caseSensitive, duplicatedNames, username));
templates.add(new GeoNames(Countries.it.name(), caseSensitive, duplicatedNames, username));
} else if (provider.equals(it.vige.cities.templates.it.Providers.EXTRA_GEONAMES.name())) {
templates.add(new ExtraGeoNames(Countries.it.name(), caseSensitive, duplicatedNames, username));
templates.add(new Tuttitalia(caseSensitive, duplicatedNames));
templates.add(new ComuniItaliani(caseSensitive, duplicatedNames));
templates.add(new GeoNames(Countries.it.name(), caseSensitive, duplicatedNames, username));
}
} else if (country.equals(Countries.uk.name())) {
if (provider == null || provider.equals(it.vige.cities.templates.en.Providers.GEONAMES.name())) {
templates.add(new GeoNames(Countries.uk.name(), caseSensitive, duplicatedNames, username));
templates.add(new Britannica(caseSensitive, duplicatedNames));
} else if (provider.equals(it.vige.cities.templates.en.Providers.BRITANNICA.name())) {
templates.add(new Britannica(caseSensitive, duplicatedNames));
templates.add(new GeoNames(Countries.uk.name(), caseSensitive, duplicatedNames, username));
}
} else
templates.add(new GeoNames(country, caseSensitive, duplicatedNames, username));
return templates;
}
/**
* Overwrite
* @param templates the list of templates
* @return the generated nodes
*/
private Nodes overwrite(List templates) {
Nodes result = null;
Iterator iterator = templates.iterator();
while (iterator.hasNext() && result == null) {
try {
result = iterator.next().generate();
} catch (Exception ex) {
result = null;
}
}
return result;
}
/**
* Generate
*/
@Override
public Nodes generate() {
logger.info(
"Start object generation for country: " + country + ", provider: " + provider + ", caseSensitive: "
+ caseSensitive + ", duplicatedNames: " + duplicatedNames + ", username: " + username,
", overwrite: " + overwrite);
Nodes result = null;
List templates = getTemplates();
if (overwrite)
result = overwrite(templates);
else if (exists())
try {
result = readFile();
} catch (Exception e) {
result = null;
}
else
result = overwrite(templates);
logger.info("End object generation");
return result;
}
/**
* Generate file
*/
@Override
public Result generateFile() {
logger.info("Start file generation for country: " + country + ", provider: " + provider + ", caseSensitive: "
+ caseSensitive + ", duplicatedNames: " + duplicatedNames);
Result result = null;
List templates = getTemplates();
result = templates.get(0).generateFile();
if (result == Result.KO)
templates.get(1).generateFile();
return result;
}
/**
* Country
* @return the country
*/
public String getCountry() {
return country;
}
/**
* Case sensitive
* @return the case sensitive parameter
*/
public boolean isCaseSensitive() {
return caseSensitive;
}
/**
* Duplicated names
* @return the duplicated names parameters
*/
public boolean isDuplicatedNames() {
return duplicatedNames;
}
/**
* Provider
* @return the provider
*/
public String getProvider() {
return provider;
}
/**
* Overwrite
* @return the overwrite parameter
*/
public boolean isOverwrite() {
return overwrite;
}
/**
* Overwrite
* @param overwrite the overwrite parameter
*/
public void setOverwrite(boolean overwrite) {
this.overwrite = overwrite;
}
/**
* Main
* @param args the parameters to start the generator
* @throws Exception if there is a problem
*/
public static void main(String[] args) throws Exception {
CommandLine cmd = null;
try {
cmd = configureOptions(args);
String country = null;
Object fromCountry = cmd.getParsedOptionValue(SINGLE_COUNTRY);
if (fromCountry == null)
country = getDefault().getCountry().toLowerCase();
else
country = fromCountry + "";
String provider = cmd.hasOption(SINGLE_PROVIDER) ? cmd.getParsedOptionValue(SINGLE_PROVIDER) + "" : null;
boolean caseSensitive = cmd.hasOption(SINGLE_CASE_SENSITIVE);
boolean duplicatedNames = cmd.hasOption(SINGLE_DUPLICATED_NAMES);
Configuration configuration = new Configuration();
configuration.setCaseSensitive(caseSensitive);
configuration.setCountry(country);
configuration.setDuplicatedNames(duplicatedNames);
configuration.setProvider(provider);
Generator generator = new Generator(configuration, true);
generator.generateFile();
} catch (MissingOptionException ex) {
logger.error(ex.getMessage());
}
}
}