1 10 package org.jgap.data.config; 11 12 import java.util.*; 13 import java.io.*; 14 15 22 public class ConfigFileReader { 23 private final static String CVS_REVISION = "$Revision: 1.5 $"; 24 25 private String m_fileName; 27 28 private Properties m_props; 30 31 private String m_ns; 33 34 37 private static ConfigFileReader m_cfReader; 38 39 46 public static ConfigFileReader instance() { 47 if (m_cfReader == null) { 48 m_cfReader = new ConfigFileReader(); 49 } 50 return m_cfReader; 51 } 52 53 59 private ConfigFileReader() { 60 m_props = new Properties(); 61 } 62 63 72 public String getValue(final String a_name) { 73 String tmpName = m_ns + "." + a_name; 74 String val = m_props.getProperty(tmpName); 75 return val; 76 } 77 78 87 public List getValues(final String a_name) { 88 String val = ""; 89 boolean done = false; 90 String tmpName = ""; 91 int idx = 0; 92 List values = Collections.synchronizedList(new ArrayList()); 93 while (!done) { 94 tmpName = m_ns + "." + a_name + "[" + idx + "]"; 95 val = m_props.getProperty(tmpName); 96 if (val == null) { 97 done = true; 98 } 99 else { 100 values.add(val); 101 idx++; 102 } 103 } 104 if (idx == 0) { 105 return null; 106 } 107 else { 108 return values; 109 } 110 } 111 112 120 public void setNS(final String a_ns) { 121 m_ns = a_ns; 122 } 123 124 134 public void setFileName(final String a_fileName) 135 throws ConfigException { 136 m_fileName = a_fileName; 137 load(); 138 } 139 140 147 private void load() 148 throws ConfigException { 149 try { 150 m_props.load(new FileInputStream(m_fileName)); 151 } 152 catch (Exception ex) { 153 String dir = new File(".").getAbsolutePath(); 154 throw new ConfigException("Error reading Config file " + m_fileName 155 + " in directory " + dir); 156 } 157 } 158 } 159 | Popular Tags |