1 10 package org.jgap.data.config; 11 12 import java.util.*; 13 import java.io.*; 14 15 22 public class MetaConfig { 23 private static final String METACON_FILENAME = "jgap-meta.con"; 25 26 private static final String CN = "MetaConfig"; 27 28 private static MetaConfig instance; 30 31 private Hashtable m_metaMap = new Hashtable(); 33 34 private int m_state; 36 37 private static final int INIT = 0; 38 39 private static final int CLASS = 1; 40 41 private static final int PROPERTY = 2; 42 43 private static final int VALUES = 3; 44 45 private String m_currName; 47 48 private ConfigProperty m_currProperty; 49 50 public static MetaConfig getInstance() 51 throws MetaConfigException, IOException { 52 if (null == instance) { 53 instance = new MetaConfig(); 54 } 55 return instance; 56 } 57 58 private MetaConfig() 59 throws MetaConfigException, IOException { 60 m_state = MetaConfig.INIT; 61 init(); 62 } 63 64 66 76 public List getConfigProperty(String className) { 77 return (List)m_metaMap.get(className); 78 } 79 80 91 protected void init() 92 throws MetaConfigException, IOException { 93 Reader fr = getReader(METACON_FILENAME); 94 LineNumberReader lr = new LineNumberReader(fr); 95 String line = lr.readLine(); 96 while (line != null) { 97 if (!this.isComment(line)) { 98 parseLine(line); 99 } 100 line = lr.readLine(); 101 } 102 endState(); 103 lr.close(); 104 } 105 106 115 public Reader getReader(String a_filename) throws IOException { 116 File metaFile = new File(a_filename); 117 FileReader fr = new FileReader(metaFile); 118 return fr; 119 } 120 121 129 private boolean isComment(String line) { 130 String tmpLine = line.trim(); 131 StringBuffer sb = new StringBuffer (tmpLine); 132 if (sb.charAt(0) == '#') { 133 return true; 134 } 135 return false; 136 } 137 138 146 private void parseLine(String a_line) 147 throws MetaConfigException { 148 String [] tokens = a_line.split("="); 149 if (tokens == null || tokens.length != 2) 150 throw new MetaConfigException(CN + ".parseLine():Exception while " + 151 "parsing " + METACON_FILENAME + " line " + 152 a_line + " is invalid"); 153 if (m_state == MetaConfig.INIT && tokens[0].equals("class")) { 154 handleClass(tokens[1]); 155 } 156 else if (m_state == MetaConfig.CLASS && tokens[0].equals("property")) { 157 handleProperty(tokens[1]); 158 } 159 else if (m_state == MetaConfig.PROPERTY && tokens[0].equals("values")) { 160 handleValues(tokens[1]); 161 } 162 else if (m_state == MetaConfig.PROPERTY && tokens[0].equals("class")) { 163 handleClass(tokens[1]); 164 } 165 else if (m_state == MetaConfig.VALUES && tokens[0].equals("class")) { 166 handleClass(tokens[1]); 167 } 168 else if (m_state == MetaConfig.VALUES && tokens[0].equals("property")) { 169 handleProperty(tokens[1]); 170 } 171 else { 172 throw new MetaConfigException(CN + ".parseLine():Exception while " 173 + "parsing " + METACON_FILENAME + " state " 174 + m_state 175 + " incompatible with line " + a_line); 176 } 177 } 178 179 184 private void handleClass(final String a_token) { 185 m_state = MetaConfig.CLASS; 186 if (m_currProperty != null) { 187 add(m_currName, m_currProperty); 188 } 189 m_currProperty = new ConfigProperty(); 190 m_currName = a_token; 191 } 192 193 200 private void handleProperty(final String a_token) 201 throws MetaConfigException { 202 int prevState = m_state; 203 if (prevState == MetaConfig.VALUES) { 204 if (m_currProperty != null) { 205 add(m_currName, m_currProperty); 206 } 207 } 208 m_currProperty = new ConfigProperty(); 209 m_state = MetaConfig.PROPERTY; 210 String [] tokens = a_token.split(","); 211 if (tokens.length < 2 || tokens.length > 3) { 212 throw new MetaConfigException("Invalid format of property line: " + 213 a_token); 214 } 215 m_currProperty.setName(tokens[0].trim()); 216 m_currProperty.setWidget(tokens[1].trim()); 217 if (tokens.length == 3) { 218 m_currProperty.setLabel(tokens[2]); 219 } 220 } 221 222 231 private void handleValues(final String a_token) 232 throws MetaConfigException { 233 m_state = MetaConfig.VALUES; 234 String [] tokens = a_token.split(","); 235 if (tokens.length == 0) { 236 throw new MetaConfigException("Invalid format of property line: " + 237 a_token); 238 } 239 for (int i = 0; i < tokens.length; i++) { 240 m_currProperty.addValue(tokens[i].trim()); 241 } 242 } 243 244 252 private void endState() 253 throws MetaConfigException { 254 if (m_state != MetaConfig.PROPERTY && m_state != MetaConfig.VALUES) { 255 throw new MetaConfigException("Invalid format of JGAP MetaConfig " 256 + "file: " + METACON_FILENAME 257 + "Ending in Invalid state : " 258 + m_state); 259 } 260 if (m_currProperty != null) { 261 add(m_currName, m_currProperty); 262 } 263 } 264 265 274 private void add(final String currName, ConfigProperty a_cp) { 275 List props = (List) m_metaMap.get(currName); 276 if (null == props) { 277 props = Collections.synchronizedList(new ArrayList()); 278 m_metaMap.put(currName, props); 279 } 280 props.add(a_cp); 281 } 282 } 283 | Popular Tags |