001    package org.maltparser.core.flow.system;
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.FlowException;
016    import org.maltparser.core.flow.system.elem.ChartElement;
017    import org.maltparser.core.helper.URLFinder;
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 FlowChartSystem {
029            private HashMap<String,ChartElement> chartElements;
030            
031            public FlowChartSystem() {
032                    chartElements = new HashMap<String,ChartElement>();
033            }
034            
035            public void load(String urlstring) throws MaltChainedException {
036                    final URLFinder f = new URLFinder();
037                    load(f.findURL(urlstring));
038            }
039            
040            public void load(PluginLoader plugins) throws MaltChainedException {
041                     for (Plugin plugin : plugins) {
042                            URL url = null;
043                            try {
044                                    url = new URL("jar:"+plugin.getUrl() + "!/appdata/plugin.xml");
045                            } catch (MalformedURLException e) {
046                                    throw new FeatureException("Malformed URL: 'jar:"+plugin.getUrl() + "!plugin.xml'", e);
047                            }
048                            try { 
049                                    InputStream is = url.openStream();
050                                    is.close();
051                            } catch (IOException e) {
052                                    continue;
053                            }
054    
055                            load(url);
056                    }
057            }
058            
059            public void load(URL specModelURL) throws MaltChainedException {
060            try {
061                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
062                DocumentBuilder db = dbf.newDocumentBuilder();
063                Element root = null;
064    
065                root = db.parse(specModelURL.openStream()).getDocumentElement();
066    
067                if (root == null) {
068                    throw new FlowException("The flow chart system file '"+specModelURL.getFile()+"' cannot be found. ");
069                }
070                
071                readChartElements(root);
072            } catch (IOException e) {
073                    throw new FlowException("The flow chart system file '"+specModelURL.getFile()+"' cannot be found. ", e);
074            } catch (ParserConfigurationException e) {
075                    throw new FlowException("Problem parsing the file "+specModelURL.getFile()+". ", e);
076            } catch (SAXException e) {
077                    throw new FlowException("Problem parsing the file "+specModelURL.getFile()+". ", e);
078            }
079            }
080            
081            public void readChartElements(Element root) throws MaltChainedException {
082                    NodeList chartElem = root.getElementsByTagName("chartelement");
083                    for (int i = 0; i < chartElem.getLength(); i++) {
084                            ChartElement chartElement = new ChartElement();
085                            chartElement.read((Element)chartElem.item(i), this);
086                            chartElements.put(((Element)chartElem.item(i)).getAttribute("item"),chartElement);
087                    }
088            }
089            
090            public ChartElement getChartElement(String name) {
091                    return chartElements.get(name);
092            }
093            
094            
095            public String toString() {
096                    StringBuilder sb = new StringBuilder();
097                    sb.append("CHART ELEMENTS:\n");
098                    for (String key : chartElements.keySet()) {
099                            sb.append(chartElements.get(key));
100                            sb.append('\n');
101                    }
102                    return sb.toString();
103            }
104    }