import org.apache.log4j.Logger;
public class Main {
+ private static final int ACTION_DEFAULT = 0;
+ private static final int ACTION_LICENSE = 1;
+ private static final int ACTION_DESCRIPTION = 2;
+ private static final int ACTION_HELP = 3;
+ private static final int ACTION_CAPABILITIES = 4;
+ private static final int ACTION_CONFIGURE = 5;
private File configFile;
private Config config;
private PrintStream outputWriter;
logger.info("under certain conditions; `tv_grab_nl_java --license' for details.");
}
- public void run() throws FactoryConfigurationError, Exception {
+ public void fetchData() throws FactoryConfigurationError, Exception {
if (!config.quiet) {
showHeader();
logger.info("Fetching programme data for " + this.days
System.out.println(copyright);
}
- public void processOptions(String[] args) throws FileNotFoundException {
+ public Options createOptions() {
Options options = new Options();
options.addOption(
OptionBuilder
OptionBuilder.withLongOpt("license")
.withDescription("Show license information")
.create());
- // .addOption(OptionBuilder.withLongOpt("preferredmethod").withDescription("Show preferred method").create();
+ // .addOption(OptionBuilder.withLongOpt("preferredmethod").withDescription("Show preferred method").create();
+ return options;
+ }
+
+ public int processOptions(Options options, String[] args) throws FileNotFoundException {
+ int action = ACTION_DEFAULT;
CommandLine line = null;
try {
}
if (line.hasOption("license")) {
- showLicense();
- System.exit(0);
+ action = ACTION_LICENSE;
+ return action; // do not read config file
}
if (line.hasOption("config-file")) {
configFile = new File(line.getOptionValue("config-file"));
Logger.getRootLogger().setLevel(Level.ERROR);
}
if (line.hasOption("description")) {
- showHeader();
- System.out.println();
- System.out
- .println("tv_grab_nl_java is a parser for Dutch TV listings using the tvgids.nl JSON interface");
- System.exit(0);
+ action = ACTION_DESCRIPTION;
}
if (line.hasOption("help")) {
- // automatically generate the help statement
- HelpFormatter formatter = new HelpFormatter();
- formatter.printHelp("tv_grab_nl_java", options);
- System.exit(0);
+ action = ACTION_HELP;
}
if (line.hasOption("output")) {
this.outputWriter = new PrintStream(new FileOutputStream(
this.offset = Integer.parseInt(line.getOptionValue("offset"));
}
if (line.hasOption("capabilities")) {
+ action = ACTION_CAPABILITIES;
+ }
+ if (line.hasOption("configure")) {
+ action = ACTION_CONFIGURE;
+ }
+ return action;
+ }
+
+ public static File defaultConfigFile() {
+ return FileUtils.getFile(FileUtils.getUserDirectory(), ".xmltv",
+ "tv_grab_nl_java.conf");
+ }
+
+ public void run(String[] args) throws FactoryConfigurationError, Exception {
+ Options options = createOptions();
+ int action = processOptions(options, args);
+ switch(action) {
+ case ACTION_DEFAULT:
+ fetchData();
+ break;
+ case ACTION_LICENSE:
+ showLicense();
+ break;
+ case ACTION_DESCRIPTION:
+ showHeader();
+ System.out.println();
+ System.out.println("tv_grab_nl_java is a parser for Dutch TV listings using the tvgids.nl JSON interface");
+ break;
+ case ACTION_HELP:
+ // automatically generate the help statement
+ HelpFormatter formatter = new HelpFormatter();
+ formatter.printHelp("tv_grab_nl_java", options);
+ break;
+ case ACTION_CAPABILITIES:
System.out.println("baseline");
System.out.println("manualconfig");
System.out.println("cache");
// System.out.println("preferredmethod");
- System.exit(0);
- }
- if (line.hasOption("configure")) {
+ break;
+ case ACTION_CONFIGURE:
try {
configure();
} catch (IOException e) {
logger.warn("Exception during configure");
logger.debug("Stack trace: ", e);
}
- System.exit(0);
- }
- }
-
- public static File defaultConfigFile() {
- return FileUtils.getFile(FileUtils.getUserDirectory(), ".xmltv",
- "tv_grab_nl_java.conf");
+ break;
+ default:
+ logger.error("Unknown main action value "+action);
+ }
}
public static void main(String[] args) {
Main main = new Main();
try {
- main.processOptions(args);
- main.run();
+ main.run(args);
} catch (Exception e) {
logger.error("Error in tv_grab_nl_java application", e);
}