1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
/* ------------------------------------ JavaOnTracks Thibaut Colar tcolar-jot AT colar DOT net Artistic Licence 2.0 http://www.javaontracks.net ------------------------------------ */ package net.jot.prefs; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Iterator; import java.util.Properties; /** * Represents/manage preferences stored in a propert file. * @author tcolar */ public class JOTPropertiesPreferences implements JOTPreferenceInterface { protected Properties props = new Properties(); public Boolean getBoolean(String key) { String val = getString(key); return val == null ? null : new Boolean(val.equalsIgnoreCase("true") || val.equalsIgnoreCase("CHECKED")); } public Boolean getDefaultedBoolean(String key, Boolean defaultValue) { Boolean b = getBoolean(key); return b == null ? defaultValue : b; } public Long getDefaultedLong(String key, Long defaultValue) { Long l = (Long) props.get(key); return l == null ? defaultValue : l; } public String getDefaultedString(String key, String defaultValue) { String s = (String) props.get(key); return s == null ? defaultValue : s; } public String getString(String key) { return (String) props.get(key); } public Integer getInt(String key) { return new Integer((String) props.get(key)); } public Integer getDefaultedInt(String key, Integer defaultValue) { Integer i = null; try { i = getInt(key); } catch (Exception e) { } if (i == null) { i = defaultValue; } return i; } /** * load prefs from given props file. * @param f * @throws java.io.IOException */ public void loadFrom(File f) throws IOException { IOException exception = null; FileInputStream input = null; input = new FileInputStream(f); try { loadFrom(input); } catch (IOException e) { exception = e; } finally { if (input != null) { input.close(); } } if (exception != null) { throw (exception); } } public void loadFrom(InputStream input) throws IOException { props.clear(); props.load(input); } /** * Save prefs to stream * @param output * @throws java.io.IOException */ public void saveTo(OutputStream output) throws IOException { props.store(output, null); } public void saveTo(File f) throws IOException { IOException exception = null; FileOutputStream output = null; output = new FileOutputStream(f); try { saveTo(output); } catch (IOException e) { exception = e; } finally { if (output != null) { output.close(); } } if (exception != null) { throw (exception); } } public void setBoolean(String key, Boolean value) { props.setProperty(key, value.toString()); } public void setString(String key, String value) { props.setProperty(key, value); } }