Advertisement
scotty92

Untitled

Oct 11th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. package varekd.util;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.lang.reflect.Constructor;
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import java.util.Iterator;
  13. import java.util.List;
  14. import java.util.Map;
  15. import java.util.zip.GZIPInputStream;
  16.  
  17. public class Config {
  18. private Map<String,Object> props;
  19. private List<ConfigChangeListener> listeners = new ArrayList<ConfigChangeListener>();
  20.  
  21. public void load(String file) throws IOException {
  22. props = new HashMap<String,Object>();
  23. File f = new File(file);
  24. InputStream in = null;
  25. if(file.endsWith(".gz")) {
  26. in = new GZIPInputStream(new FileInputStream(f));
  27. } else {
  28. in = new FileInputStream(f);
  29. }
  30. loadFrom(in);
  31. for(ConfigChangeListener ccl : listeners)
  32. ccl.configChanged(this);
  33. }
  34.  
  35. private void loadFrom(InputStream in) throws IOException {
  36. BufferedReader brIn = new BufferedReader(new InputStreamReader(in));
  37. StringBuilder keyvalue = new StringBuilder();
  38. String line = null;
  39. while((line = brIn.readLine()) != null) {
  40. line = line.trim();
  41. if(line.startsWith("#") || line.length() == 0)
  42. continue;
  43. if(line.endsWith("\\"))
  44. keyvalue.append((line.substring(0, line.length() - 1).trim()));
  45. else if(keyvalue.length() > 0)
  46. line = keyvalue.toString();
  47. processLine(line);
  48. }
  49. }
  50.  
  51. private void processLine(String s) {
  52. Class type = null;
  53. String key = s.substring(0, s.indexOf('='));
  54. String value = s.substring(s.indexOf('=')+1);
  55. for(TypeMatching tm : types) {
  56. if(value.matches(tm.getMatcher())) {
  57. type = tm.getType();
  58. break;
  59. }
  60. }
  61. if(type == null)
  62. throw new IllegalArgumentException("Bad line: "+s);
  63. Object obj = null;
  64. if(type == Integer.class)
  65. obj = new Integer(Integer.parseInt(value));
  66. else if(type == Double.class)
  67. obj = new Double(Double.parseDouble(value));
  68. else if(type == Integer[].class) {
  69. String[] ints = value.substring(1, value.length()-1).split(",");
  70. int[] vals = new int[ints.length];
  71. for(int i=0;i<vals.length; i++) {
  72. vals[i] = Integer.parseInt(ints[i].trim());
  73. }
  74. obj = vals;
  75. } else if(type == String.class) {
  76. obj = value;
  77. if(value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"))
  78. obj = new Boolean(true);
  79. else if(value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no"))
  80. obj = new Boolean(false);
  81. }
  82. //System.out.println(key+"="+obj+" [type="+obj.getClass()+";"+type+"]");
  83. props.put(key, obj);
  84. }
  85.  
  86. public void addChangeListener(ConfigChangeListener ccl) {
  87. listeners.add(ccl);
  88. ccl.configChanged(this);
  89. }
  90.  
  91. public static void main(String[] args) throws Exception {
  92. Config c = new Config();
  93. c.load("conf/spkd.properties");
  94. }
  95.  
  96. public String get(String s) { return (String)props.get(s); }
  97. public int getInt(String s) { return (Integer)props.get(s); }
  98. public double getDouble(String s) { return (Double)props.get(s); }
  99. public boolean getBool(String s) { return (Boolean)props.get(s); }
  100. public int[] getIntArray(String s) { return (int[])props.get(s); }
  101. public Iterator<Map.Entry<String,Object>> getProperties() { return props.entrySet().iterator(); }
  102.  
  103. static class TypeMatching {
  104. String matcher;
  105. Class type;
  106. TypeMatching(String matcher, Class type) {
  107. this.matcher = matcher;
  108. this.type = type;
  109. }
  110. String getMatcher() { return matcher; }
  111. Class getType() { return type; }
  112. }
  113.  
  114. private static final List<TypeMatching> types = new ArrayList<TypeMatching>();
  115. static {
  116. types.add(new TypeMatching("\\d+", Integer.class));
  117. types.add(new TypeMatching("[\\d\\.]+", Double.class));
  118. types.add(new TypeMatching("\\[[\\d\\s,]*\\]", Integer[].class));
  119. types.add(new TypeMatching(".*", String.class));
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement