Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package varekd.util;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.lang.reflect.Constructor;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.zip.GZIPInputStream;
- public class Config {
- private Map<String,Object> props;
- private List<ConfigChangeListener> listeners = new ArrayList<ConfigChangeListener>();
- public void load(String file) throws IOException {
- props = new HashMap<String,Object>();
- File f = new File(file);
- InputStream in = null;
- if(file.endsWith(".gz")) {
- in = new GZIPInputStream(new FileInputStream(f));
- } else {
- in = new FileInputStream(f);
- }
- loadFrom(in);
- for(ConfigChangeListener ccl : listeners)
- ccl.configChanged(this);
- }
- private void loadFrom(InputStream in) throws IOException {
- BufferedReader brIn = new BufferedReader(new InputStreamReader(in));
- StringBuilder keyvalue = new StringBuilder();
- String line = null;
- while((line = brIn.readLine()) != null) {
- line = line.trim();
- if(line.startsWith("#") || line.length() == 0)
- continue;
- if(line.endsWith("\\"))
- keyvalue.append((line.substring(0, line.length() - 1).trim()));
- else if(keyvalue.length() > 0)
- line = keyvalue.toString();
- processLine(line);
- }
- }
- private void processLine(String s) {
- Class type = null;
- String key = s.substring(0, s.indexOf('='));
- String value = s.substring(s.indexOf('=')+1);
- for(TypeMatching tm : types) {
- if(value.matches(tm.getMatcher())) {
- type = tm.getType();
- break;
- }
- }
- if(type == null)
- throw new IllegalArgumentException("Bad line: "+s);
- Object obj = null;
- if(type == Integer.class)
- obj = new Integer(Integer.parseInt(value));
- else if(type == Double.class)
- obj = new Double(Double.parseDouble(value));
- else if(type == Integer[].class) {
- String[] ints = value.substring(1, value.length()-1).split(",");
- int[] vals = new int[ints.length];
- for(int i=0;i<vals.length; i++) {
- vals[i] = Integer.parseInt(ints[i].trim());
- }
- obj = vals;
- } else if(type == String.class) {
- obj = value;
- if(value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"))
- obj = new Boolean(true);
- else if(value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no"))
- obj = new Boolean(false);
- }
- //System.out.println(key+"="+obj+" [type="+obj.getClass()+";"+type+"]");
- props.put(key, obj);
- }
- public void addChangeListener(ConfigChangeListener ccl) {
- listeners.add(ccl);
- ccl.configChanged(this);
- }
- public static void main(String[] args) throws Exception {
- Config c = new Config();
- c.load("conf/spkd.properties");
- }
- public String get(String s) { return (String)props.get(s); }
- public int getInt(String s) { return (Integer)props.get(s); }
- public double getDouble(String s) { return (Double)props.get(s); }
- public boolean getBool(String s) { return (Boolean)props.get(s); }
- public int[] getIntArray(String s) { return (int[])props.get(s); }
- public Iterator<Map.Entry<String,Object>> getProperties() { return props.entrySet().iterator(); }
- static class TypeMatching {
- String matcher;
- Class type;
- TypeMatching(String matcher, Class type) {
- this.matcher = matcher;
- this.type = type;
- }
- String getMatcher() { return matcher; }
- Class getType() { return type; }
- }
- private static final List<TypeMatching> types = new ArrayList<TypeMatching>();
- static {
- types.add(new TypeMatching("\\d+", Integer.class));
- types.add(new TypeMatching("[\\d\\.]+", Double.class));
- types.add(new TypeMatching("\\[[\\d\\s,]*\\]", Integer[].class));
- types.add(new TypeMatching(".*", String.class));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement