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.options.OptionManager;
013    /**
014    *
015    *
016    * @author Johan Hall
017    */
018    public class ConfigDirChartItem extends ChartItem {
019            private String idName;
020            private String taskName;
021            private String optionFileName;
022            private URL configDirURL;
023            private String configDirName;
024            private ConfigurationDir configDir;
025            private String outCharSet;
026            private String inCharSet;
027            
028            public ConfigDirChartItem() {}
029            
030            public void initialize(FlowChartInstance flowChartinstance, ChartItemSpecification chartItemSpecification) throws MaltChainedException {
031                    super.initialize(flowChartinstance, chartItemSpecification);
032                    
033                    for (String key : chartItemSpecification.getChartItemAttributes().keySet()) {
034                            if (key.equals("id")) {
035                                    idName = chartItemSpecification.getChartItemAttributes().get(key);
036                            }  else if (key.equals("task")) {
037                                    taskName = chartItemSpecification.getChartItemAttributes().get(key);
038                            }
039                    }
040                    if (idName == null) {
041                            idName = getChartElement("configdir").getAttributes().get("id").getDefaultValue();
042                    } else if (taskName == null) {
043                            taskName = getChartElement("configdir").getAttributes().get("task").getDefaultValue();
044                    }
045                    
046                    if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "url") != null && OptionManager.instance().getOptionValue(getOptionContainerIndex(),"config", "url").toString().length() > 0) {
047                            try {                   
048                                    configDirURL = new URL(OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "url").toString());
049                            } catch (MalformedURLException e) {
050                                    throw new ConfigurationException("The URL '"+OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "url").toString()+"' is malformed. ", e);
051                            }
052                    }
053                    if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString().endsWith(".mco")) {
054                            int index = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString().lastIndexOf('.');
055                            configDirName = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString().substring(0,index);
056                    } else {
057                            configDirName = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString();
058                    }
059                    
060                    try {
061                            if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "system", "option_file") != null) {
062                                    optionFileName = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "system", "option_file").toString();
063                            }
064                    } catch (ConfigurationException e) {
065                            throw new ConfigurationException("The option file '"+optionFileName+"' could not be copied. ",e);
066                    }
067                    if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "output", "charset") != null) {
068                            outCharSet = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "output", "charset").toString();
069                    } else {
070                            outCharSet = "UTF-8";
071                    }
072                    
073                    if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "input", "charset") != null) {
074                            inCharSet = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "input", "charset").toString();
075                    } else {
076                            inCharSet = "UTF-8";
077                    }
078                    
079                    configDir = (ConfigurationDir)flowChartinstance.getFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName);
080                    if (configDir == null) {
081                            if (configDirURL != null) {
082                                    configDir = new ConfigurationDir(configDirURL);
083                            } else {
084                                    configDir = new ConfigurationDir(configDirName, idName, getOptionContainerIndex());
085                            }
086                            
087                            flowChartinstance.addFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName, configDir);
088                    }
089                    if (taskName.equals("unpack")) {
090                            configDir.unpackConfigFile();
091                            OptionManager.instance().loadOptions(getOptionContainerIndex(), configDir.getInputStreamReader("savedoptions.sop"));
092                    }
093            }
094            
095            public int preprocess(int signal) throws MaltChainedException {
096                    if (taskName.equals("info")) {
097                            configDir.echoInfoFile();
098                    } else if (taskName.equals("loadsymboltables")) {
099                            flowChartinstance.getSymbolTables().load(configDir.getInputStreamReader("symboltables.sym",inCharSet));
100                    } else if (taskName.equals("createdir")) {
101                            configDir.setCreatedByMaltParserVersion(SystemInfo.getVersion());
102                            configDir.createConfigDirectory();
103                            if (optionFileName != null && optionFileName.length() > 0) {
104                                    configDir.copyToConfig(new File(optionFileName));
105                            }
106                    }
107                    return signal;
108            }
109            
110            
111            public int process(int signal) throws MaltChainedException {
112                    return signal;
113            }
114            
115            public int postprocess(int signal) throws MaltChainedException {
116                    if (taskName.equals("createfile")) {
117                            OptionManager.instance().saveOptions(getOptionContainerIndex(), configDir.getOutputStreamWriter("savedoptions.sop"));
118                            configDir.createConfigFile();
119                    } else if (taskName.equals("deletedir")) {
120                            configDir.terminate();
121                            configDir.deleteConfigDirectory();
122                    } else if (taskName.equals("savesymboltables")) {
123                            flowChartinstance.getSymbolTables().save(configDir.getOutputStreamWriter("symboltables.sym", outCharSet));
124                    }
125                    return signal;
126            }
127            
128            public void terminate() throws MaltChainedException { }
129            
130            public boolean equals(Object obj) {
131                    if (this == obj)
132                            return true;
133                    if (obj == null)
134                            return false;
135                    if (getClass() != obj.getClass())
136                            return false;
137                    return obj.toString().equals(this.toString());
138            }
139            
140            public int hashCode() {
141                    return 217 + (null == toString() ? 0 : toString().hashCode());
142            }
143            
144            public String toString() {
145                    final StringBuilder sb = new StringBuilder();
146                    sb.append("    configdir ");
147                    sb.append("id:");sb.append(idName);
148                    sb.append(' ');
149                    sb.append("task:");sb.append(taskName);
150                    
151                    return sb.toString();
152            }
153    
154    }