michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.util; michael@0: michael@0: import android.text.TextUtils; michael@0: import android.util.Log; michael@0: michael@0: import java.io.BufferedWriter; michael@0: import java.io.IOException; michael@0: import java.util.Enumeration; michael@0: import java.util.Hashtable; michael@0: michael@0: public class INISection { michael@0: private static final String LOGTAG = "INIParser"; michael@0: michael@0: // default file to read and write to michael@0: private String mName = null; michael@0: public String getName() { return mName; } michael@0: public void setName(String name) { mName = name; } michael@0: michael@0: // show or hide debug logging michael@0: private boolean mDebug = false; michael@0: michael@0: // Global properties that aren't inside a section in the file michael@0: protected Hashtable mProperties = null; michael@0: michael@0: // create a parser. The file will not be read until you attempt to michael@0: // access sections or properties inside it. At that point its read synchronously michael@0: public INISection(String name) { michael@0: mName = name; michael@0: } michael@0: michael@0: // log a debug string to the console michael@0: protected void debug(String msg) { michael@0: if (mDebug) { michael@0: Log.i(LOGTAG, msg); michael@0: } michael@0: } michael@0: michael@0: // get a global property out of the hash table. will return null if the property doesn't exist michael@0: public Object getProperty(String key) { michael@0: getProperties(); // ensure that we have parsed the file michael@0: return mProperties.get(key); michael@0: } michael@0: michael@0: // get a global property out of the hash table. will return null if the property doesn't exist michael@0: public int getIntProperty(String key) { michael@0: Object val = getProperty(key); michael@0: if (val == null) michael@0: return -1; michael@0: michael@0: return Integer.parseInt(val.toString()); michael@0: } michael@0: michael@0: // get a global property out of the hash table. will return null if the property doesn't exist michael@0: public String getStringProperty(String key) { michael@0: Object val = getProperty(key); michael@0: if (val == null) michael@0: return null; michael@0: michael@0: return val.toString(); michael@0: } michael@0: michael@0: // get a hashtable of all the global properties in this file michael@0: public Hashtable getProperties() { michael@0: if (mProperties == null) { michael@0: try { michael@0: parse(); michael@0: } catch (IOException e) { michael@0: debug("Error parsing: " + e); michael@0: } michael@0: } michael@0: return mProperties; michael@0: } michael@0: michael@0: // do nothing for generic sections michael@0: protected void parse() throws IOException { michael@0: mProperties = new Hashtable(); michael@0: } michael@0: michael@0: // set a property. Will erase the property if value = null michael@0: public void setProperty(String key, Object value) { michael@0: getProperties(); // ensure that we have parsed the file michael@0: if (value == null) michael@0: removeProperty(key); michael@0: else michael@0: mProperties.put(key.trim(), value); michael@0: } michael@0: michael@0: // remove a property michael@0: public void removeProperty(String name) { michael@0: // ensure that we have parsed the file michael@0: getProperties(); michael@0: mProperties.remove(name); michael@0: } michael@0: michael@0: public void write(BufferedWriter writer) throws IOException { michael@0: if (!TextUtils.isEmpty(mName)) { michael@0: writer.write("[" + mName + "]"); michael@0: writer.newLine(); michael@0: } michael@0: michael@0: if (mProperties != null) { michael@0: for (Enumeration e = mProperties.keys(); e.hasMoreElements();) { michael@0: String key = e.nextElement(); michael@0: writeProperty(writer, key, mProperties.get(key)); michael@0: } michael@0: } michael@0: writer.newLine(); michael@0: } michael@0: michael@0: // Helper function to write out a property michael@0: private void writeProperty(BufferedWriter writer, String key, Object value) { michael@0: try { michael@0: writer.write(key + "=" + value); michael@0: writer.newLine(); michael@0: } catch (IOException e) { michael@0: e.printStackTrace(); michael@0: } michael@0: } michael@0: }