1 33 34 package edu.rice.cs.drjava.config; 35 import edu.rice.cs.util.UnexpectedException; 36 37 import java.util.Properties ; 38 import java.util.Iterator ; 39 import java.io.*; 40 import java.lang.reflect.*; 41 public class OptionMapLoader implements OptionConstants { 42 43 44 private static DefaultOptionMap DEFAULTS = new DefaultOptionMap(); 45 private static Properties DEFAULT_STRINGS = new Properties (); 46 47 static { 48 Field[] fields = OptionConstants.class.getDeclaredFields(); 50 for (int i = 0; i < fields.length; i++) { 51 Field field = fields[i]; 52 int mods = field.getModifiers(); 53 if (Modifier.isStatic(mods) && Modifier.isPublic(mods) && Modifier.isFinal(mods)) { 54 Option<?> option; 56 try { 57 Object o = field.get(null); if (o == null || !(o instanceof Option)) { 60 continue; } 62 63 option = (Option<?>) o; 64 } 65 catch(IllegalAccessException e) { 66 throw new UnexpectedException(e); 68 } 69 70 String sval = option.getDefaultString(); 71 DEFAULT_STRINGS.setProperty(option.name,sval); 72 DEFAULTS.setString(option,sval); 73 } 74 } 75 } 76 77 78 public static final OptionMapLoader DEFAULT = new OptionMapLoader(DEFAULT_STRINGS); 79 80 83 public OptionMapLoader(InputStream is) throws IOException { 84 this(new Properties (DEFAULT_STRINGS)); 85 try { prop.load(is); } 86 finally { is.close(); } 87 } 88 89 private final Properties prop; 90 91 private OptionMapLoader(Properties prop) { 92 this.prop = prop; 93 } 94 95 public void loadInto(OptionMap map) { 96 Iterator <OptionParser<?>> options = DEFAULTS.keys(); 97 while(options.hasNext()) { 98 OptionParser<?> option = options.next(); 99 String val = prop.getProperty(option.name); 100 map.setString(option,val); 101 } 102 } 103 } 104 | Popular Tags |