mobile/android/base/util/INISection.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/util/INISection.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,123 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.util;
     1.9 +
    1.10 +import android.text.TextUtils;
    1.11 +import android.util.Log;
    1.12 +
    1.13 +import java.io.BufferedWriter;
    1.14 +import java.io.IOException;
    1.15 +import java.util.Enumeration;
    1.16 +import java.util.Hashtable;
    1.17 +
    1.18 +public class INISection {
    1.19 +    private static final String LOGTAG = "INIParser";
    1.20 +
    1.21 +    // default file to read and write to
    1.22 +    private String mName = null;
    1.23 +    public String getName() { return mName; }
    1.24 +    public void setName(String name) { mName = name; }
    1.25 +
    1.26 +    // show or hide debug logging
    1.27 +    private  boolean mDebug = false;
    1.28 +
    1.29 +    // Global properties that aren't inside a section in the file
    1.30 +    protected Hashtable<String, Object> mProperties = null;
    1.31 +
    1.32 +    // create a parser. The file will not be read until you attempt to
    1.33 +    // access sections or properties inside it. At that point its read synchronously
    1.34 +    public INISection(String name) {
    1.35 +        mName = name;
    1.36 +    }
    1.37 +
    1.38 +    // log a debug string to the console
    1.39 +    protected void debug(String msg) {
    1.40 +        if (mDebug) {
    1.41 +            Log.i(LOGTAG, msg);
    1.42 +        }
    1.43 +    }
    1.44 +  
    1.45 +    // get a global property out of the hash table. will return null if the property doesn't exist
    1.46 +    public Object getProperty(String key) {
    1.47 +        getProperties(); // ensure that we have parsed the file
    1.48 +        return mProperties.get(key);
    1.49 +    }
    1.50 +
    1.51 +    // get a global property out of the hash table. will return null if the property doesn't exist
    1.52 +    public int getIntProperty(String key) {
    1.53 +        Object val = getProperty(key);
    1.54 +        if (val == null)
    1.55 +            return -1;
    1.56 +
    1.57 +        return Integer.parseInt(val.toString());
    1.58 +    }
    1.59 +
    1.60 +    // get a global property out of the hash table. will return null if the property doesn't exist
    1.61 +    public String getStringProperty(String key) {
    1.62 +        Object val = getProperty(key);
    1.63 +        if (val == null)
    1.64 +          return null;
    1.65 +
    1.66 +        return val.toString();
    1.67 +    }
    1.68 +
    1.69 +    // get a hashtable of all the global properties in this file
    1.70 +    public Hashtable<String, Object> getProperties() {
    1.71 +        if (mProperties == null) {
    1.72 +            try {
    1.73 +                parse();
    1.74 +            } catch (IOException e) {
    1.75 +                debug("Error parsing: " + e);
    1.76 +            }
    1.77 +        }
    1.78 +        return mProperties;
    1.79 +    }
    1.80 +
    1.81 +    // do nothing for generic sections
    1.82 +    protected void parse() throws IOException {
    1.83 +        mProperties = new Hashtable<String, Object>();
    1.84 +    }
    1.85 +
    1.86 +    // set a property. Will erase the property if value = null
    1.87 +    public void setProperty(String key, Object value) {
    1.88 +        getProperties(); // ensure that we have parsed the file
    1.89 +        if (value == null)
    1.90 +            removeProperty(key);
    1.91 +        else
    1.92 +            mProperties.put(key.trim(), value);     
    1.93 +    }   
    1.94 + 
    1.95 +    // remove a property
    1.96 +    public void removeProperty(String name) {
    1.97 +        // ensure that we have parsed the file
    1.98 +        getProperties();
    1.99 +        mProperties.remove(name);
   1.100 +    }
   1.101 +
   1.102 +    public void write(BufferedWriter writer) throws IOException {
   1.103 +        if (!TextUtils.isEmpty(mName)) {
   1.104 +            writer.write("[" + mName + "]");
   1.105 +            writer.newLine();
   1.106 +        }
   1.107 +
   1.108 +        if (mProperties != null) {
   1.109 +            for (Enumeration<String> e = mProperties.keys(); e.hasMoreElements();) {
   1.110 +                String key = e.nextElement();
   1.111 +                writeProperty(writer, key, mProperties.get(key));
   1.112 +            }
   1.113 +        }
   1.114 +        writer.newLine();
   1.115 +    }
   1.116 +
   1.117 +    // Helper function to write out a property
   1.118 +    private void writeProperty(BufferedWriter writer, String key, Object value) {
   1.119 +        try {
   1.120 +            writer.write(key + "=" + value);
   1.121 +            writer.newLine();
   1.122 +        } catch (IOException e) {
   1.123 +            e.printStackTrace();
   1.124 +        }
   1.125 +    }
   1.126 +}

mercurial