Changeset 251


Ignore:
Timestamp:
09/02/08 17:57:01 (5 years ago)
Author:
saua
Message:
  • support loading&saving CSV files (2 values per line, key and value)
  • support boolean values
File:
1 edited

Legend:

Unmodified
Added
Removed
  • bbtracker/trunk/src/org/bbtracker/mobile/config/ConfigFile.java

    r232 r251  
    3030import javax.microedition.io.file.FileConnection; 
    3131 
     32import org.bbtracker.CsvReader; 
    3233import org.bbtracker.CsvWriter; 
    3334 
     
    159160 
    160161        /** 
     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        /** 
    161193         * Save the content of this config file back to a file. 
    162194         *  
     
    173205                } finally { 
    174206                        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(); 
    175236                } 
    176237        } 
     
    302363 
    303364        /** 
    304          * Get matching integer value (or null). throws NumberFormatException on 
    305          * invalid value. 
     365         * Get matching integer value. throws NumberFormatException on invalid 
     366         * value. 
    306367         *  
    307368         * @param key 
     
    321382 
    322383        /** 
    323          * Get matching double value (or null). throws NumberFormatException on 
    324          * invalid value. 
     384         * Get matching double value. throws NumberFormatException on invalid value. 
    325385         *  
    326386         * @param key 
     
    328388         * @param defaultValue 
    329389         *            value 
    330          * @return integer value or default value if key not found 
     390         * @return double value or default value if key not found 
    331391         */ 
    332392        public double getDouble(final String key, final double defaultValue) { 
     
    340400 
    341401        /** 
     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        /** 
    342420         * Add name / value pair. 
    343421         *  
     
    361439        public void put(final String key, final int value) { 
    362440                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"); 
    363453        } 
    364454 
Note: See TracChangeset for help on using the changeset viewer.