org.patrodyne.etl.transformio.Main Maven / Gradle / Ivy
Show all versions of patrodyne-etl-TransformIO Show documentation
// PatroDyne: Patron Supported Dynamic Executables, http://patrodyne.org
// Released under LGPL license. See terms at http://www.gnu.org.
package org.patrodyne.etl.transformio;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Properties;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import org.patrodyne.etl.transformio.cli.CommandLI;
import org.patrodyne.etl.transformio.gui.GraphicalUI;
import org.patrodyne.etl.transformio.tui.TextualUI;
/**
*
* Launch the Main application to transform an input source into an output
* target.
*
*
*
* By design, the Main class limits imports to the Java Class Library before
* transferring control to one of the user interface classes: GraphicalUI,
* TextualUI or CommandLI.
*
*
*
* More specifically, this class does not import the logging library
* org.slf4j because the choice of a specific logger is a runtime
* option. At runtime, the logging package is loaded using the Java Extension
* Mechanism for optional packages. In the event that the optional packages are
* not where they should be, as specified by -Djava.ext.dirs, any class
* that imports one will also fail. It is the job of the Main class to catch
* this sort of failure or any other exceptions and inform the user.
*
*
*
* In summary, this class starts the application and other TransformIO
* classes import optional packages using the standard extension mechanism. The
* extension path is specified using a system property, java.ext.dirs.
*
*
* @author Rick O'Sullivan
*/
public class Main
{
/** Represents the command line options as a dictionary of properties. */
protected static Properties options = new Properties();
/**
* Launch the application using command line options to choose the
* user interface mode and provide an optional batch configuration.
* Throwables are trapped for error handling.
*
* @param args Command line arguments.
*/
public static void main(String[] args)
{
try
{
// Parse CLI arguments into a set of property options.
for (int i=0; i < args.length; ++i)
{
String[] nvp = args[i].split("=", 2);
if ( nvp.length == 2 )
options.put(nvp[0].toLowerCase(), nvp[1]);
else if ( nvp.length == 1 )
{
// A single value option is either 'help'
// or presumably a batch name.
if ( nvp[0].toLowerCase().equalsIgnoreCase("help"))
options.put(nvp[0].toLowerCase(), "");
else
options.put("batch", nvp[0]);
}
else
throw new IllegalArgumentException("invalid option at position "+(i+1)+": "+args[i]);
}
// Validate options, start or complain.
if ( validateOptions(options) )
{
// CommandLI.showOptions(options);
switch ( getMode() )
{
case GUI: GraphicalUI.start(options); break;
case TUI: TextualUI.start(options); break;
case CLI: CommandLI.start(options); break;
}
}
else
CommandLI.showUsage();
}
catch (Throwable t)
{
errorHandler(t);
}
}
/**
* Validate command line options.
*
* Conditions that return false:
*
*
* - no options
* - help option
* - option key is not help, mode or batch.
* - mode option when value is not GUI, TUI or CLI.
* - batch option when value is empty.
* - mode is CLI and batch is not present.
*
*
* @param options A dictionary of properties.
*
* @return True, when the properties are valid; otherwise, false.
*/
protected static boolean validateOptions(Properties options)
{
String mode=null, batch=null;
if ( options.isEmpty() || options.containsKey("help") )
return false;
Iterator