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().substring(0,3).equals(SystemInfo.getVersion().substring(0,3))) {
098 SystemLogger.logger().error("The parser model '"+ configDirName+ ".mco' is created by MaltParser "+configDir.getCreatedByMaltParserVersion()+".\n");
099 SystemLogger.logger().error("You have to re-train the parser model to be able to parse with current version of MaltParser.\n");
100 System.exit(1);
101 }
102 OptionManager.instance().loadOptions(getOptionContainerIndex(), configDir.getInputStreamReaderFromConfigFile("savedoptions.sop"));
103 configDir.initDataFormat();
104 } else if (taskName.equals("createdir")) {
105 configDir.setCreatedByMaltParserVersion(SystemInfo.getVersion());
106 configDir.createConfigDirectory();
107 if (optionFileName != null && optionFileName.length() > 0) {
108 configDir.copyToConfig(new File(optionFileName));
109 }
110 configDir.initDataFormat();
111 }
112 }
113
114 public int preprocess(int signal) throws MaltChainedException {
115 if (taskName.equals("unpack")) {
116 SystemLogger.logger().info("Unpacking the parser model '"+ configDirName+ ".mco' ...\n");
117 configDir.unpackConfigFile();
118 } else if (taskName.equals("info")) {
119 configDir.echoInfoFile();
120 } else if (taskName.equals("loadsymboltables")) {
121 configDir.getSymbolTables().load(configDir.getInputStreamReaderFromConfigFileEntry("symboltables.sym",inCharSet));
122 }
123 return signal;
124 }
125
126
127 public int process(int signal) throws MaltChainedException {
128 return signal;
129 }
130
131 public int postprocess(int signal) throws MaltChainedException {
132 if (taskName.equals("createfile")) {
133 OptionManager.instance().saveOptions(getOptionContainerIndex(), configDir.getOutputStreamWriter("savedoptions.sop"));
134 configDir.createConfigFile();
135 } else if (taskName.equals("deletedir")) {
136 configDir.terminate();
137 configDir.deleteConfigDirectory();
138 } else if (taskName.equals("savesymboltables")) {
139 configDir.getSymbolTables().save(configDir.getOutputStreamWriter("symboltables.sym", outCharSet));
140 }
141 return signal;
142 }
143
144 public void terminate() throws MaltChainedException { }
145
146 public boolean equals(Object obj) {
147 if (this == obj)
148 return true;
149 if (obj == null)
150 return false;
151 if (getClass() != obj.getClass())
152 return false;
153 return obj.toString().equals(this.toString());
154 }
155
156 public int hashCode() {
157 return 217 + (null == toString() ? 0 : toString().hashCode());
158 }
159
160 public String toString() {
161 final StringBuilder sb = new StringBuilder();
162 sb.append(" configdir ");
163 sb.append("id:");sb.append(idName);
164 sb.append(' ');
165 sb.append("task:");sb.append(taskName);
166
167 return sb.toString();
168 }
169
170 }