    001    package org.maltparser.core.config;
    002
    003    import java.io.File;
    004    import java.net.MalformedURLException;
    005    import java.net.URL;
    006
    007    import org.maltparser.core.exception.MaltChainedException;
    008    import org.maltparser.core.flow.FlowChartInstance;
    009    import org.maltparser.core.flow.item.ChartItem;
    010    import org.maltparser.core.flow.spec.ChartItemSpecification;
    011    import org.maltparser.core.helper.SystemInfo;
    012    import org.maltparser.core.helper.SystemLogger;
    013    import org.maltparser.core.options.OptionManager;
    014    /**
    015    *
    016    *
    017    * @author Johan Hall
    018    */
    019    public class ConfigDirChartItem extends ChartItem {
    020            private String idName;
    021            private String taskName;
    022            private String optionFileName;
    023            private URL configDirURL;
    024            private String configDirName;
    025            private ConfigurationDir configDir;
    026            private String outCharSet;
    027            private String inCharSet;
    028
    029            public ConfigDirChartItem() {}
    030
    031            public void initialize(FlowChartInstance flowChartinstance, ChartItemSpecification chartItemSpecification) throws MaltChainedException {
    032                    super.initialize(flowChartinstance, chartItemSpecification);
    033
    034                    for (String key : chartItemSpecification.getChartItemAttributes().keySet()) {
    035                            if (key.equals("id")) {
    036                                    idName = chartItemSpecification.getChartItemAttributes().get(key);
    037                            }  else if (key.equals("task")) {
    038                                    taskName = chartItemSpecification.getChartItemAttributes().get(key);
    039                            }
    040                    }
    041                    if (idName == null) {
    042                            idName = getChartElement("configdir").getAttributes().get("id").getDefaultValue();
    043                    } else if (taskName == null) {
    044                            taskName = getChartElement("configdir").getAttributes().get("task").getDefaultValue();
    045                    }
    046
    047                    if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "url") != null && OptionManager.instance().getOptionValue(getOptionContainerIndex(),"config", "url").toString().length() > 0) {
    048                            try {
    049                                    configDirURL = new URL(OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "url").toString());
    050                            } catch (MalformedURLException e) {
    051                                    throw new ConfigurationException("The URL '"+OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "url").toString()+"' is malformed. ", e);
    052                            }
    053                    }
    054                    if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString().endsWith(".mco")) {
    055                            int index = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString().lastIndexOf('.');
    056                            configDirName = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString().substring(0,index);
    057                    } else {
    058                            configDirName = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString();
    059                    }
    060
    061                    try {
    062                            if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "system", "option_file") != null) {
    063                                    optionFileName = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "system", "option_file").toString();
    064                            }
    065                    } catch (ConfigurationException e) {
    066                            throw new ConfigurationException("The option file '"+optionFileName+"' could not be copied. ",e);
    067                    }
    068                    if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "output", "charset") != null) {
    069                            outCharSet = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "output", "charset").toString();
    070                    } else {
    071                            outCharSet = "UTF-8";
    072                    }
    073
    074                    if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "input", "charset") != null) {
    075                            inCharSet = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "input", "charset").toString();
    076                    } else {
    077                            inCharSet = "UTF-8";
    078                    }
    079
    080                    configDir = (ConfigurationDir)flowChartinstance.getFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName);
    081                    if (configDir == null) {
    082                            if (configDirURL != null) {
    083                                    configDir = new ConfigurationDir(configDirURL);
    084                            } else {
    085                                    configDir = new ConfigurationDir(configDirName, idName, getOptionContainerIndex());
    086                            }
    087
    088                            flowChartinstance.addFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName, configDir);
    089                    }
    090                    if (taskName.equals("versioning")) {
    091                            configDir.versioning();
    092                    } else if (taskName.equals("loadsavedoptions")) {
    093                            configDir.initCreatedByMaltParserVersionFromInfoFile();
    094                            if (configDir.getCreatedByMaltParserVersion() == null) {
    095                                    SystemLogger.logger().warn("Couln't determine which version of MaltParser that created the parser model: " + configDirName+ ".mco\n MaltParser will terminate\n");
    096                                    System.exit(1);
    097                            } else if (!configDir.getCreatedByMaltParserVersion().startsWith(SystemInfo.getVersion())) {
    098                                    if (configDir.getCreatedByMaltParserVersion().startsWith("1.3")) {
    099                                            SystemLogger.logger().warn("The parser model '"+ configDirName+ ".mco' is created by MaltParser "+configDir.getCreatedByMaltParserVersion()+".\n");
    100                                            SystemLogger.logger().warn("You can convert the parser model to a MaltParser "+SystemInfo.getVersion()+" parser model:\n");
    101                                            SystemLogger.logger().warn("   java -jar malt.jar -c "+configDirName+" -m versioning\n");
    102                                            SystemLogger.logger().warn("MaltParser will terminate.\n");
    103                                    } else {
    104                                            SystemLogger.logger().warn("The parser model '"+ configDirName+ ".mco' is created by MaltParser "+configDir.getCreatedByMaltParserVersion()+", which cannot be used by MaltParser "+SystemInfo.getVersion()+".\n");
    105                                            SystemLogger.logger().warn("Please retrain the parser model with MaltParser "+SystemInfo.getVersion()+" or download MaltParser "+configDir.getCreatedByMaltParserVersion()+" from http://maltparser.org/download.html\n");
    106                                            SystemLogger.logger().warn("MaltParser will terminate\n");
    107                                    }
    108                                    System.exit(1);
    109                            }
    110                            OptionManager.instance().loadOptions(getOptionContainerIndex(), configDir.getInputStreamReaderFromConfigFile("savedoptions.sop"));
    111                            configDir.initDataFormat();
    112                    } else if (taskName.equals("createdir")) {
    113                            configDir.setCreatedByMaltParserVersion(SystemInfo.getVersion());
    114                            configDir.createConfigDirectory();
    115                            if (optionFileName != null && optionFileName.length() > 0) {
    116                                    configDir.copyToConfig(new File(optionFileName));
    117                            }
    118                            configDir.initDataFormat();
    119                    }
    120            }
    121
    122            public int preprocess(int signal) throws MaltChainedException {
    123                    if (taskName.equals("unpack")) {
    124                            SystemLogger.logger().info("Unpacking the parser model '"+ configDirName+ ".mco' ...\n");
    125                            configDir.unpackConfigFile();
    126                    } else if (taskName.equals("info")) {
    127                            configDir.echoInfoFile();
    128                    } else if (taskName.equals("loadsymboltables")) {
    129                            configDir.getSymbolTables().load(configDir.getInputStreamReaderFromConfigFileEntry("symboltables.sym",inCharSet));
    130                    }
    131                    return signal;
    132            }
    133
    134
    135            public int process(int signal) throws MaltChainedException {
    136                    return signal;
    137            }
    138
    139            public int postprocess(int signal) throws MaltChainedException {
    140                    if (taskName.equals("createfile")) {
    141                            OptionManager.instance().saveOptions(getOptionContainerIndex(), configDir.getOutputStreamWriter("savedoptions.sop"));
    142                            configDir.createConfigFile();
    143                    } else if (taskName.equals("deletedir")) {
    144                            configDir.terminate();
    145                            configDir.deleteConfigDirectory();
    146                    } else if (taskName.equals("savesymboltables")) {
    147                            configDir.getSymbolTables().save(configDir.getOutputStreamWriter("symboltables.sym", outCharSet));
    148                    }
    149                    return signal;
    150            }
    151
    152            public void terminate() throws MaltChainedException { }
    153
    154            public boolean equals(Object obj) {
    155                    if (this == obj)
    156                            return true;
    157                    if (obj == null)
    158                            return false;
    159                    if (getClass() != obj.getClass())
    160                            return false;
    161                    return obj.toString().equals(this.toString());
    162            }
    163
    164            public int hashCode() {
    165                    return 217 + (null == toString() ? 0 : toString().hashCode());
    166            }
    167
    168            public String toString() {
    169                    final StringBuilder sb = new StringBuilder();
    170                    sb.append("    configdir ");
    171                    sb.append("id:");sb.append(idName);
    172                    sb.append(' ');
    173                    sb.append("task:");sb.append(taskName);
    174
    175                    return sb.toString();
    176            }
    177
    178    }