Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
HowToKBClustering/src/utils/ConfigReader.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
100 lines (84 sloc)
2.59 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.util.Map.Entry; | |
import java.util.Properties; | |
import util.Util; | |
public class ConfigReader { | |
protected static Properties properties; | |
protected static String configFile; | |
/******************************************* | |
* Load configuration e.g. params (enable parallel), file path Allows | |
* substitutions like these: $global-variable$ | |
* | |
* @param configFileIn | |
* "./data/config/config.properties" | |
* @throws IOException | |
* : configFile cannot be loaded. | |
*******************************************/ | |
public static boolean loadConfig(String configFileIn) throws IOException { | |
// load once as initialization | |
if (properties == null || configFile == null) { | |
if (!Util.exists(configFileIn)) | |
return false; | |
Properties firstPass = new Properties(); | |
properties = new Properties(); | |
firstPass.load(new FileInputStream(new File(configFileIn))); | |
// second pass to fill out constants. | |
for (Entry<Object, Object> e : firstPass.entrySet()) { | |
String key = e.getKey().toString(); | |
String val = firstPass.getProperty(key); | |
if (Util.countChar(val, '$', false) > 1) { | |
// won't create path inside writeFile if path doesn't exist | |
char[] chs = val.toCharArray(); | |
StringBuilder k2 = new StringBuilder(); | |
StringBuilder v2 = new StringBuilder(); | |
for (int i = 0; i < chs.length; i++) { | |
if (chs[i] == '$') { | |
for (int j = i + 1; j < chs.length; j++) { | |
if (chs[j] != '$') { | |
k2.append(chs[j]); | |
} else { | |
// only one indirection possible. | |
k2 = | |
new StringBuilder().append(firstPass | |
.getProperty(k2.toString())); | |
v2.append(k2); | |
k2 = new StringBuilder(); | |
i = j; | |
break; | |
} | |
} | |
} else | |
v2.append(chs[i]); | |
} | |
properties.put(key, v2.toString()); | |
} else | |
properties.put(key, val); | |
} | |
} | |
return properties != null; | |
} | |
public static String getProperty(String p) throws IOException { | |
return getProperty(p, ""); | |
} | |
public static String getProperty(String p, String defaultVal) { | |
try { | |
if (properties == null) { | |
boolean isLoaded = loadConfig(configFile); | |
if (!isLoaded) | |
return defaultVal; | |
} | |
return properties.getProperty(p); | |
} catch (Exception e) { | |
return defaultVal; | |
} | |
} | |
public static String setProperty(String propertykey, String value) | |
throws IOException { | |
if (properties == null) | |
loadConfig(configFile); | |
return (String) properties.setProperty(propertykey, value); | |
} | |
} |