    001    package org.maltparser;
    002
    003    import java.io.File;
    004    import java.util.Date;
    005
    006    import org.maltparser.core.exception.MaltChainedException;
    007    import org.maltparser.core.helper.SystemInfo;
    008    import org.maltparser.core.helper.SystemLogger;
    009    import org.maltparser.core.options.OptionManager;
    010    import org.maltparser.core.plugin.PluginLoader;
    011
    012    /**
    013     * MaltConsoleEngine controls the MaltParser system using the console version.
    014     *
    015     * @author Johan Hall
    016     * @since 1.0
    017    **/
    018    public class MaltConsoleEngine {
    019            public static final int OPTION_CONTAINER = 0;
    020
    021            /**
    022             * Creates a MaltConsoleEngine object
    023             *
    024             */
    025            public MaltConsoleEngine() {
    026                    try {
    027                            /* Option and Plug-in management */
    028                            OptionManager.instance().loadOptionDescriptionFile();
    029                            if (SystemInfo.getMaltJarPath() != null) {
    030                                    PluginLoader.instance().loadPlugins(new File(SystemInfo.getMaltJarPath().getParent()+"/plugin"));
    031                            }
    032                            OptionManager.instance().generateMaps();
    033                    } catch (MaltChainedException e) {
    034                            if (SystemLogger.logger().isDebugEnabled()) {
    035                                    SystemLogger.logger().debug("",e);
    036                            } else {
    037                                    SystemLogger.logger().error(e.getMessageChain());
    038                            }
    039                            System.exit(1);
    040                    }
    041            }
    042
    043            /**
    044             * Starts the console engine.
    045             *
    046             * @param args command-line arguments
    047             */
    048            public void startEngine(String[] args) {
    049                    try {
    050                            final OptionManager om = OptionManager.instance();
    051                            final boolean hasArg = om.parseCommandLine(args,OPTION_CONTAINER);
    052                            /* Update the verbosity level according to the verbosity option */
    053                            String verbosity = null;
    054                            if (hasArg) {
    055                                    verbosity = (String)OptionManager.instance().getOptionValue(OPTION_CONTAINER,"system", "verbosity");
    056                            } else {
    057                                    verbosity = (String)OptionManager.instance().getOptionDefaultValue("system", "verbosity");
    058                            }
    059                            if (verbosity != null) {
    060                                    SystemLogger.instance().setSystemVerbosityLevel(verbosity.toUpperCase());
    061                            }
    062                            /* Help or reading the option file */
    063                            if (!hasArg || om.getNumberOfOptionValues(OPTION_CONTAINER) == 0) {
    064                                    SystemLogger.logger().info(SystemInfo.header());
    065                                    SystemLogger.logger().info(SystemInfo.shortHelp());
    066                                    return;
    067                            } else if (om.getOptionValue(OPTION_CONTAINER,"system", "help") != null) {
    068                                    SystemLogger.logger().info(SystemInfo.header());
    069                                    SystemLogger.logger().info(om.getOptionDescriptions());
    070                                    return;
    071                            } else {
    072                                    if (om.getOptionValue(OPTION_CONTAINER,"system", "option_file") != null && om.getOptionValue(0,"system", "option_file").toString().length() > 0) {
    073                                            om.parseOptionInstanceXMLfile((String)om.getOptionValue(OPTION_CONTAINER,"system", "option_file"));
    074                                    }
    075                            }
    076                            maltParser();
    077                    } catch (MaltChainedException e) {
    078                            if (SystemLogger.logger().isDebugEnabled()) {
    079                                    SystemLogger.logger().debug("",e);
    080                            } else {
    081                                    SystemLogger.logger().error(e.getMessageChain());
    082                            }
    083                            System.exit(1);
    084                    }
    085            }
    086
    087            /**
    088             * Creates and executes a MaltParser configuration
    089             *
    090             * @throws MaltChainedException
    091             */
    092            private void maltParser() throws MaltChainedException {
    093                    if (SystemLogger.logger() != null && SystemLogger.logger().isInfoEnabled()) {
    094                            SystemLogger.logger().info(SystemInfo.header() +"\n");
    095                            SystemLogger.logger().info("Started: " + new Date(System.currentTimeMillis()) +"\n");
    096                    }
    097                    Engine engine = new Engine();
    098                    engine.initialize(OPTION_CONTAINER);
    099                    engine.process(OPTION_CONTAINER);
    100                    engine.terminate(OPTION_CONTAINER);
    101                    if (SystemLogger.logger().isInfoEnabled()) {
    102                            SystemLogger.logger().info("Finished: " + new Date(System.currentTimeMillis())+"\n");
    103                    }
    104            }
    105    }