001 package org.maltparser.core.flow.system.elem;
002
003 import java.util.LinkedHashMap;
004
005 import org.maltparser.core.exception.MaltChainedException;
006 import org.maltparser.core.flow.FlowException;
007 import org.maltparser.core.flow.item.ChartItem;
008 import org.maltparser.core.flow.system.FlowChartSystem;
009 import org.maltparser.core.plugin.PluginLoader;
010 import org.w3c.dom.Element;
011 import org.w3c.dom.NodeList;
012 /**
013 *
014 *
015 * @author Johan Hall
016 */
017 public class ChartElement {
018 private String item;
019 private Class<? extends ChartItem> chartItemClass;
020 private LinkedHashMap<String,ChartAttribute> attributes;
021
022 public ChartElement() {
023 attributes = new LinkedHashMap<String,ChartAttribute>();
024 }
025
026 public String getItem() {
027 return item;
028 }
029
030 public void setItem(String item) {
031 this.item = item;
032 }
033
034 public void addAttribute(String name, ChartAttribute attribute) {
035 attributes.put(name, attribute);
036 }
037
038 public ChartAttribute getAttribute(String name) {
039 return attributes.get(name);
040 }
041
042 public Class<? extends ChartItem> getChartItemClass() {
043 return chartItemClass;
044 }
045
046
047 public LinkedHashMap<String, ChartAttribute> getAttributes() {
048 return attributes;
049 }
050
051 public void read(Element chartElem, FlowChartSystem flowChartSystem) throws MaltChainedException {
052 setItem(chartElem.getAttribute("item"));
053 String chartItemClassName = chartElem.getAttribute("class");
054 Class<?> clazz = null;
055 try {
056 if (PluginLoader.instance() != null) {
057 clazz = PluginLoader.instance().getClass(chartItemClassName);
058 }
059 if (clazz == null) {
060 clazz = Class.forName(chartItemClassName);
061 }
062 this.chartItemClass = clazz.asSubclass(org.maltparser.core.flow.item.ChartItem.class);
063 } catch (ClassCastException e) {
064 throw new FlowException("The class '"+clazz.getName()+"' is not a subclass of '"+org.maltparser.core.flow.item.ChartItem.class.getName()+"'. ", e);
065 } catch (ClassNotFoundException e) {
066 throw new FlowException("The class "+chartItemClassName+" could not be found. ", e);
067 }
068 NodeList attrElements = chartElem.getElementsByTagName("attribute");
069 for (int i = 0; i < attrElements.getLength(); i++) {
070 ChartAttribute attribute = new ChartAttribute();
071 attribute.read((Element)attrElements.item(i),flowChartSystem);
072 attributes.put(((Element)attrElements.item(i)).getAttribute("name"), attribute);
073 }
074 }
075
076 public String toString() {
077 StringBuilder sb = new StringBuilder();
078 sb.append(" ");
079 sb.append(item);
080 sb.append(' ');
081 sb.append(chartItemClass.getName());
082 sb.append('\n');
083 for (String key : attributes.keySet()) {
084 sb.append(" ");
085 sb.append(attributes.get(key));
086 sb.append('\n');
087 }
088 return sb.toString();
089 }
090 }