Changeset 251
- Timestamp:
- 09/02/08 17:57:01 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
bbtracker/trunk/src/org/bbtracker/mobile/config/ConfigFile.java
r232 r251 30 30 import javax.microedition.io.file.FileConnection; 31 31 32 import org.bbtracker.CsvReader; 32 33 import org.bbtracker.CsvWriter; 33 34 … … 159 160 160 161 /** 162 * Create a configuration object and read data from an InputStream in CSV 163 * format. 164 * 165 * @param in 166 * InputStream 167 * @return config object 168 * @throws IOException 169 * io error 170 */ 171 public static ConfigFile openCSVConfig(final InputStream in) throws IOException { 172 final ConfigFile self = new ConfigFile(); 173 174 try { 175 final InputStreamReader reader = new InputStreamReader(in); 176 final CsvReader csv = new CsvReader(reader); 177 String[] fields; 178 while ((fields = csv.nextLine()) != null) { 179 if (fields.length != 2) { 180 throw new IOException("Malformed CSV!"); 181 182 } 183 self.params.put(fields[0], fields[1]); 184 } 185 } finally { 186 in.close(); 187 } 188 189 return self; 190 } 191 192 /** 161 193 * Save the content of this config file back to a file. 162 194 * … … 173 205 } finally { 174 206 connection.close(); 207 } 208 } 209 210 /** 211 * Save the content of this config back to a OutputStream in CSV format. 212 * 213 * @param out 214 * OutputStream 215 * @throws IOException 216 * io error 217 */ 218 public void saveCSVConfig(final OutputStream out) throws IOException { 219 final OutputStreamWriter writer = new OutputStreamWriter(out); 220 final CsvWriter csv = new CsvWriter(); 221 try { 222 final Enumeration keys = params.keys(); 223 final Enumeration values = params.elements(); 224 while (keys.hasMoreElements()) { 225 final String key = (String) keys.nextElement(); 226 final String value = (String) values.nextElement(); 227 csv.append(key); 228 csv.append(value); 229 csv.nl(); 230 writer.write(csv.toString()); 231 csv.reset(); 232 } 233 } finally { 234 writer.close(); 235 out.close(); 175 236 } 176 237 } … … 302 363 303 364 /** 304 * Get matching integer value (or null). throws NumberFormatException on305 * invalidvalue.365 * Get matching integer value. throws NumberFormatException on invalid 366 * value. 306 367 * 307 368 * @param key … … 321 382 322 383 /** 323 * Get matching double value (or null). throws NumberFormatException on 324 * invalid value. 384 * Get matching double value. throws NumberFormatException on invalid value. 325 385 * 326 386 * @param key … … 328 388 * @param defaultValue 329 389 * value 330 * @return integervalue or default value if key not found390 * @return double value or default value if key not found 331 391 */ 332 392 public double getDouble(final String key, final double defaultValue) { … … 340 400 341 401 /** 402 * Get matching boolean value. 403 * 404 * @param key 405 * key 406 * @param defaultValue 407 * value 408 * @return boolean value or default value if key not found 409 */ 410 public boolean getBoolean(final String key, final boolean defaultValue) { 411 final String value = get(key); 412 if (value != null) { 413 return "true".equals(value); 414 } else { 415 return defaultValue; 416 } 417 } 418 419 /** 342 420 * Add name / value pair. 343 421 * … … 361 439 public void put(final String key, final int value) { 362 440 put(key, Integer.toString(value)); 441 } 442 443 /** 444 * Add name / value pair. 445 * 446 * @param key 447 * key 448 * @param value 449 * value 450 */ 451 public void put(final String key, final boolean value) { 452 put(key, value ? "true" : "false"); 363 453 } 364 454
Note: See TracChangeset
for help on using the changeset viewer.