    001    package org.maltparser.core.flow;
    002
    003    import java.io.IOException;
    004    import java.io.InputStream;
    005    import java.net.MalformedURLException;
    006    import java.net.URL;
    007    import java.util.HashMap;
    008
    009    import javax.xml.parsers.DocumentBuilder;
    010    import javax.xml.parsers.DocumentBuilderFactory;
    011    import javax.xml.parsers.ParserConfigurationException;
    012
    013    import org.maltparser.core.exception.MaltChainedException;
    014    import org.maltparser.core.feature.FeatureException;
    015    import org.maltparser.core.flow.spec.ChartSpecification;
    016    import org.maltparser.core.flow.system.FlowChartSystem;
    017    import org.maltparser.core.helper.Util;
    018    import org.maltparser.core.plugin.Plugin;
    019    import org.maltparser.core.plugin.PluginLoader;
    020    import org.w3c.dom.Element;
    021    import org.w3c.dom.NodeList;
    022    import org.xml.sax.SAXException;
    023    /**
    024    *
    025    *
    026    * @author Johan Hall
    027    */
    028    public class FlowChartManager {
    029            private static FlowChartManager uniqueInstance = new FlowChartManager();
    030            private final FlowChartSystem flowChartSystem;
    031            private final HashMap<String,ChartSpecification> chartSpecifications;
    032
    033            public FlowChartManager() {
    034                    flowChartSystem = new FlowChartSystem();
    035                    chartSpecifications = new HashMap<String,ChartSpecification>();
    036            }
    037
    038            /**
    039            * Returns a reference to the single instance.
    040            */
    041            public static FlowChartManager instance() {
    042                    return uniqueInstance;
    043            }
    044
    045            public void load(String urlstring) throws MaltChainedException {
    046                    load(Util.findURL(urlstring));
    047            }
    048
    049            public void load(PluginLoader plugins) throws MaltChainedException {
    050                     for (Plugin plugin : plugins) {
    051                            URL url = null;
    052                            try {
    053                                    url = new URL("jar:"+plugin.getUrl() + "!/appdata/plugin.xml");
    054                            } catch (MalformedURLException e) {
    055                                    throw new FeatureException("Malformed URL: 'jar:"+plugin.getUrl() + "!plugin.xml'", e);
    056                            }
    057                            try {
    058                                    InputStream is = url.openStream();
    059                                    is.close();
    060                            } catch (IOException e) {
    061                                    continue;
    062                            }
    063
    064                            load(url);
    065                    }
    066            }
    067
    068            public void load(URL url) throws MaltChainedException {
    069            try {
    070                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    071                DocumentBuilder db = dbf.newDocumentBuilder();
    072                Element root = null;
    073
    074                root = db.parse(url.openStream()).getDocumentElement();
    075                if (root == null) {
    076                    throw new FlowException("The flow chart specification file '"+url.getFile()+"' cannot be found. ");
    077                }
    078                readFlowCharts(root);
    079            } catch (IOException e) {
    080                    throw new FlowException("The flow chart specification file '"+url.getFile()+"' cannot be found. ", e);
    081            } catch (ParserConfigurationException e) {
    082                    throw new FlowException("Problem parsing the flow chart file "+url.getFile()+". ", e);
    083            } catch (SAXException e) {
    084                    throw new FlowException("Problem parsing the flow chart file "+url.getFile()+". ", e);
    085            }
    086            }
    087
    088            private void readFlowCharts(Element flowcharts) throws MaltChainedException {
    089                    NodeList flowChartList = flowcharts.getElementsByTagName("flowchart");
    090                    for (int i = 0; i < flowChartList.getLength(); i++) {
    091                            String flowChartName = ((Element)flowChartList.item(i)).getAttribute("name");
    092                            if (!chartSpecifications.containsKey(flowChartName)) {
    093                                    ChartSpecification chart = new ChartSpecification();
    094                                    chartSpecifications.put(flowChartName, chart);
    095                                    chart.read((Element)flowChartList.item(i), this);
    096                            } else {
    097                                    throw new FlowException("Problem parsing the flow chart file. The flow chart with the name "+flowChartName+" already exists. ");
    098                            }
    099                    }
    100            }
    101
    102            public FlowChartInstance initialize(int optionContainerIndex, String flowChartName) throws MaltChainedException {
    103                    return new FlowChartInstance(optionContainerIndex, chartSpecifications.get(flowChartName), this);
    104            }
    105
    106            public FlowChartSystem getFlowChartSystem() {
    107                    return flowChartSystem;
    108            }
    109
    110            public String toString() {
    111                    final StringBuilder sb = new StringBuilder();
    112                    sb.append("FLOW CHART SYSTEM\n");
    113                    sb.append(flowChartSystem);
    114                    sb.append('\n');
    115                    sb.append("FLOW CHARTS:\n");
    116                    for (String key : chartSpecifications.keySet()) {
    117                            sb.append(chartSpecifications.get(key));
    118                            sb.append('\n');
    119                    }
    120                    return sb.toString();
    121            }
    122
    123    }