001 package org.maltparser.core.io.dataformat; 002 003 import org.maltparser.core.exception.MaltChainedException; 004 import org.maltparser.core.symbol.SymbolTable; 005 import org.maltparser.core.symbol.SymbolTableHandler; 006 007 /** 008 * 009 * 010 * @author Johan Hall 011 * @since 1.0 012 **/ 013 public class ColumnDescription implements Comparable<ColumnDescription> { 014 // Categories 015 public static final int INPUT = 1; 016 public static final int HEAD = 2; 017 public static final int DEPENDENCY_EDGE_LABEL = 3; 018 public static final int PHRASE_STRUCTURE_EDGE_LABEL = 4; 019 public static final int PHRASE_STRUCTURE_NODE_LABEL = 5; 020 public static final int SECONDARY_EDGE_LABEL = 6; 021 022 // Types 023 public static final int STRING = 1; 024 public static final int INTEGER = 2; 025 public static final int BOOLEAN = 3; 026 public static final int ECHO = 4; 027 public static final int IGNORE = 5; 028 029 private int position; 030 private String name; 031 private int category; 032 private int type; 033 private String defaultOutput; 034 private SymbolTable symbolTable; 035 private int cachedHash; 036 037 public ColumnDescription(int position, String name, String category, String type, String defaultOutput, SymbolTableHandler symbolTables, String specialSymbolsfileName, String rootLabel) throws MaltChainedException { 038 setPosition(position); 039 setName(name); 040 setCategory(category); 041 setType(type); 042 setDefaultOutput(defaultOutput); 043 createSymbolTable(symbolTables, specialSymbolsfileName, rootLabel); 044 } 045 046 private void createSymbolTable(SymbolTableHandler symbolTables, String nullValueStrategy, String rootLabel) throws MaltChainedException { 047 if (type == ColumnDescription.STRING || type == ColumnDescription.INTEGER || type == ColumnDescription.BOOLEAN) { 048 if (category == ColumnDescription.DEPENDENCY_EDGE_LABEL) { 049 symbolTable = symbolTables.addSymbolTable(name, category, nullValueStrategy, rootLabel); 050 } else { 051 symbolTable = symbolTables.addSymbolTable(name, category, nullValueStrategy); 052 } 053 } else { 054 symbolTable = null; 055 } 056 } 057 058 public int getPosition() { 059 return position; 060 } 061 062 public String getName() { 063 return name; 064 } 065 066 public int getCategory() { 067 return category; 068 } 069 070 public int getType() { 071 return type; 072 } 073 074 public String getDefaultOutput() { 075 return defaultOutput; 076 } 077 078 public SymbolTable getSymbolTable() { 079 return symbolTable; 080 } 081 082 private void setPosition(int position) throws MaltChainedException { 083 if (position >= 0) { 084 this.position = position; 085 } else { 086 throw new DataFormatException("Position value for column must be a non-negative value. "); 087 } 088 } 089 090 private void setName(String name) { 091 this.name = name.toUpperCase(); 092 } 093 094 private void setCategory(String category) throws MaltChainedException { 095 if (category.toUpperCase().equals("INPUT")) { 096 this.category = ColumnDescription.INPUT; 097 } else if (category.toUpperCase().equals("HEAD")) { 098 this.category = ColumnDescription.HEAD; 099 } else if (category.toUpperCase().equals("OUTPUT")) { 100 this.category = ColumnDescription.DEPENDENCY_EDGE_LABEL; 101 } else if (category.toUpperCase().equals("DEPENDENCY_EDGE_LABEL")) { 102 this.category = ColumnDescription.DEPENDENCY_EDGE_LABEL; 103 } else if (category.toUpperCase().equals("PHRASE_STRUCTURE_EDGE_LABEL")) { 104 this.category = ColumnDescription.PHRASE_STRUCTURE_EDGE_LABEL; 105 } else if (category.toUpperCase().equals("PHRASE_STRUCTURE_NODE_LABEL")) { 106 this.category = ColumnDescription.PHRASE_STRUCTURE_NODE_LABEL; 107 } else if (category.toUpperCase().equals("SECONDARY_EDGE_LABEL")) { 108 this.category = ColumnDescription.SECONDARY_EDGE_LABEL; 109 } else { 110 throw new DataFormatException("The category '"+category+"' is not allowed. "); 111 } 112 } 113 114 private void setType(String type) throws MaltChainedException { 115 if (type.toUpperCase().equals("STRING")) { 116 this.type = ColumnDescription.STRING; 117 } else if (type.toUpperCase().equals("INTEGER")) { 118 this.type = ColumnDescription.INTEGER; 119 } else if (type.toUpperCase().equals("BOOLEAN")) { 120 this.type = ColumnDescription.BOOLEAN; 121 } else if (type.toUpperCase().equals("ECHO")) { 122 this.type = ColumnDescription.ECHO; 123 //this.type = ColumnDescription.STRING; 124 } else if (type.toUpperCase().equals("IGNORE")) { 125 this.type = ColumnDescription.IGNORE; 126 } else { 127 throw new DataFormatException("The column type '"+type+"' is not allowed. "); 128 } 129 } 130 131 public void setSymbolTable(SymbolTable symbolTable) { 132 if (type == ColumnDescription.STRING) { 133 this.symbolTable = symbolTable; 134 } 135 } 136 137 public void setDefaultOutput(String defaultOutput) { 138 this.defaultOutput = defaultOutput; 139 } 140 141 public int compareTo(ColumnDescription that) { 142 final int BEFORE = -1; 143 final int EQUAL = 0; 144 final int AFTER = 1; 145 if (this == that) return EQUAL; 146 if (this.position < that.position) return BEFORE; 147 if (this.position > that.position) return AFTER; 148 return EQUAL; 149 } 150 151 public boolean equals(Object obj) { 152 if (this == obj) 153 return true; 154 if (obj == null) 155 return false; 156 if (getClass() != obj.getClass()) 157 return false; 158 ColumnDescription objC = (ColumnDescription)obj; 159 return type == objC.type && category == objC.category &&((name == null) ? objC.name == null : name.equals(objC.name)); 160 } 161 162 public int hashCode() { 163 if (cachedHash == 0) { 164 int hash = 31*7 + type; 165 hash = 31*hash + category; 166 hash = 31*hash + (null == name ? 0 : name.hashCode()); 167 cachedHash = hash; 168 } 169 return cachedHash; 170 } 171 172 173 public String toString() { 174 final StringBuilder sb = new StringBuilder(); 175 sb.append(name); 176 sb.append('\t'); 177 sb.append(category); 178 sb.append('\t'); 179 sb.append(type); 180 if (defaultOutput != null) { 181 sb.append('\t'); 182 sb.append(defaultOutput); 183 } 184 return sb.toString(); 185 } 186 }