001package org.maltparser.core.flow.spec; 002 003import java.util.LinkedHashSet; 004 005import org.maltparser.core.exception.MaltChainedException; 006import org.maltparser.core.flow.FlowChartManager; 007import org.maltparser.core.flow.FlowException; 008import org.w3c.dom.Element; 009import org.w3c.dom.NodeList; 010/** 011* 012* 013* @author Johan Hall 014*/ 015public class ChartSpecification { 016 private String name; 017 private LinkedHashSet<ChartItemSpecification> preProcessChartItemSpecifications; 018 private LinkedHashSet<ChartItemSpecification> processChartItemSpecifications; 019 private LinkedHashSet<ChartItemSpecification> postProcessChartItemSpecifications; 020 021 public ChartSpecification() { 022 preProcessChartItemSpecifications = new LinkedHashSet<ChartItemSpecification>(7); 023 processChartItemSpecifications = new LinkedHashSet<ChartItemSpecification>(7); 024 postProcessChartItemSpecifications = new LinkedHashSet<ChartItemSpecification>(7); 025 } 026 027 public String getName() { 028 return name; 029 } 030 public void setName(String name) { 031 this.name = name; 032 } 033 034 public LinkedHashSet<ChartItemSpecification> getPreProcessChartItemSpecifications() { 035 return preProcessChartItemSpecifications; 036 } 037 038 public void addPreProcessChartItemSpecifications(ChartItemSpecification chartItemSpecification) { 039 preProcessChartItemSpecifications.add(chartItemSpecification); 040 } 041 042 public void removePreProcessChartItemSpecifications(ChartItemSpecification chartItemSpecification) { 043 preProcessChartItemSpecifications.remove(chartItemSpecification); 044 } 045 046 public LinkedHashSet<ChartItemSpecification> getProcessChartItemSpecifications() { 047 return processChartItemSpecifications; 048 } 049 050 public void addProcessChartItemSpecifications(ChartItemSpecification chartItemSpecification) { 051 processChartItemSpecifications.add(chartItemSpecification); 052 } 053 054 public void removeProcessChartItemSpecifications(ChartItemSpecification chartItemSpecification) { 055 processChartItemSpecifications.remove(chartItemSpecification); 056 } 057 058 public LinkedHashSet<ChartItemSpecification> getPostProcessChartItemSpecifications() { 059 return postProcessChartItemSpecifications; 060 } 061 062 public void addPostProcessChartItemSpecifications(ChartItemSpecification chartItemSpecification) { 063 postProcessChartItemSpecifications.add(chartItemSpecification); 064 } 065 066 public void removePostProcessChartItemSpecifications(ChartItemSpecification chartItemSpecification) { 067 postProcessChartItemSpecifications.remove(chartItemSpecification); 068 } 069 070 public void read(Element chartElem, FlowChartManager flowCharts) throws MaltChainedException { 071 setName(chartElem.getAttribute("name")); 072 NodeList flowChartProcessList = chartElem.getElementsByTagName("preprocess"); 073 if (flowChartProcessList.getLength() == 1) { 074 readChartItems((Element)flowChartProcessList.item(0), flowCharts, preProcessChartItemSpecifications); 075 } else if (flowChartProcessList.getLength() > 1) { 076 throw new FlowException("The flow chart '"+getName()+"' has more than one preprocess elements. "); 077 } 078 079 flowChartProcessList = chartElem.getElementsByTagName("process"); 080 if (flowChartProcessList.getLength() == 1) { 081 readChartItems((Element)flowChartProcessList.item(0), flowCharts, processChartItemSpecifications); 082 } else if (flowChartProcessList.getLength() > 1) { 083 throw new FlowException("The flow chart '"+getName()+"' has more than one process elements. "); 084 } 085 086 flowChartProcessList = chartElem.getElementsByTagName("postprocess"); 087 if (flowChartProcessList.getLength() == 1) { 088 readChartItems((Element)flowChartProcessList.item(0), flowCharts, postProcessChartItemSpecifications); 089 } else if (flowChartProcessList.getLength() > 1) { 090 throw new FlowException("The flow chart '"+getName()+"' has more than one postprocess elements. "); 091 } 092 } 093 094 private void readChartItems(Element chartElem, FlowChartManager flowCharts, LinkedHashSet<ChartItemSpecification> chartItemSpecifications) throws MaltChainedException { 095 NodeList flowChartItemList = chartElem.getElementsByTagName("chartitem"); 096 for (int i = 0; i < flowChartItemList.getLength(); i++) { 097 ChartItemSpecification chartItemSpecification = new ChartItemSpecification(); 098 chartItemSpecification.read((Element)flowChartItemList.item(i), flowCharts); 099 chartItemSpecifications.add(chartItemSpecification); 100 } 101 } 102 103 public int hashCode() { 104 final int prime = 31; 105 int result = 1; 106 result = prime * result + ((name == null) ? 0 : name.hashCode()); 107 result = prime * result + ((postProcessChartItemSpecifications == null) ? 0 : postProcessChartItemSpecifications.hashCode()); 108 result = prime * result + ((preProcessChartItemSpecifications == null) ? 0 : preProcessChartItemSpecifications.hashCode()); 109 result = prime * result + ((processChartItemSpecifications == null) ? 0 : processChartItemSpecifications.hashCode()); 110 return result; 111 } 112 113 public boolean equals(Object obj) { 114 if (this == obj) 115 return true; 116 if (obj == null) 117 return false; 118 if (getClass() != obj.getClass()) 119 return false; 120 ChartSpecification other = (ChartSpecification) obj; 121 if (name == null) { 122 if (other.name != null) 123 return false; 124 } else if (!name.equals(other.name)) 125 return false; 126 if (postProcessChartItemSpecifications == null) { 127 if (other.postProcessChartItemSpecifications != null) 128 return false; 129 } else if (!postProcessChartItemSpecifications 130 .equals(other.postProcessChartItemSpecifications)) 131 return false; 132 if (preProcessChartItemSpecifications == null) { 133 if (other.preProcessChartItemSpecifications != null) 134 return false; 135 } else if (!preProcessChartItemSpecifications 136 .equals(other.preProcessChartItemSpecifications)) 137 return false; 138 if (processChartItemSpecifications == null) { 139 if (other.processChartItemSpecifications != null) 140 return false; 141 } else if (!processChartItemSpecifications 142 .equals(other.processChartItemSpecifications)) 143 return false; 144 return true; 145 } 146 147 public String toString() { 148 StringBuilder sb = new StringBuilder(); 149 sb.append(name);sb.append('\n'); 150 if (preProcessChartItemSpecifications.size() > 0) { 151 sb.append(" preprocess:");sb.append('\n'); 152 for (ChartItemSpecification key : preProcessChartItemSpecifications) { 153 sb.append(key);sb.append('\n'); 154 } 155 } 156 if (processChartItemSpecifications.size() > 0) { 157 sb.append(" process:");sb.append('\n'); 158 for (ChartItemSpecification key : processChartItemSpecifications) { 159 sb.append(key);sb.append('\n'); 160 } 161 } 162 if (postProcessChartItemSpecifications.size() > 0) { 163 sb.append(" postprocess:");sb.append('\n'); 164 for (ChartItemSpecification key : postProcessChartItemSpecifications) { 165 sb.append(key);sb.append('\n'); 166 } 167 } 168 return sb.toString(); 169 } 170}