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