Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.util; |
michael@0 | 6 | |
michael@0 | 7 | import android.text.TextUtils; |
michael@0 | 8 | import android.util.Log; |
michael@0 | 9 | |
michael@0 | 10 | import java.io.BufferedWriter; |
michael@0 | 11 | import java.io.IOException; |
michael@0 | 12 | import java.util.Enumeration; |
michael@0 | 13 | import java.util.Hashtable; |
michael@0 | 14 | |
michael@0 | 15 | public class INISection { |
michael@0 | 16 | private static final String LOGTAG = "INIParser"; |
michael@0 | 17 | |
michael@0 | 18 | // default file to read and write to |
michael@0 | 19 | private String mName = null; |
michael@0 | 20 | public String getName() { return mName; } |
michael@0 | 21 | public void setName(String name) { mName = name; } |
michael@0 | 22 | |
michael@0 | 23 | // show or hide debug logging |
michael@0 | 24 | private boolean mDebug = false; |
michael@0 | 25 | |
michael@0 | 26 | // Global properties that aren't inside a section in the file |
michael@0 | 27 | protected Hashtable<String, Object> mProperties = null; |
michael@0 | 28 | |
michael@0 | 29 | // create a parser. The file will not be read until you attempt to |
michael@0 | 30 | // access sections or properties inside it. At that point its read synchronously |
michael@0 | 31 | public INISection(String name) { |
michael@0 | 32 | mName = name; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | // log a debug string to the console |
michael@0 | 36 | protected void debug(String msg) { |
michael@0 | 37 | if (mDebug) { |
michael@0 | 38 | Log.i(LOGTAG, msg); |
michael@0 | 39 | } |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | // get a global property out of the hash table. will return null if the property doesn't exist |
michael@0 | 43 | public Object getProperty(String key) { |
michael@0 | 44 | getProperties(); // ensure that we have parsed the file |
michael@0 | 45 | return mProperties.get(key); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | // get a global property out of the hash table. will return null if the property doesn't exist |
michael@0 | 49 | public int getIntProperty(String key) { |
michael@0 | 50 | Object val = getProperty(key); |
michael@0 | 51 | if (val == null) |
michael@0 | 52 | return -1; |
michael@0 | 53 | |
michael@0 | 54 | return Integer.parseInt(val.toString()); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | // get a global property out of the hash table. will return null if the property doesn't exist |
michael@0 | 58 | public String getStringProperty(String key) { |
michael@0 | 59 | Object val = getProperty(key); |
michael@0 | 60 | if (val == null) |
michael@0 | 61 | return null; |
michael@0 | 62 | |
michael@0 | 63 | return val.toString(); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | // get a hashtable of all the global properties in this file |
michael@0 | 67 | public Hashtable<String, Object> getProperties() { |
michael@0 | 68 | if (mProperties == null) { |
michael@0 | 69 | try { |
michael@0 | 70 | parse(); |
michael@0 | 71 | } catch (IOException e) { |
michael@0 | 72 | debug("Error parsing: " + e); |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | return mProperties; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | // do nothing for generic sections |
michael@0 | 79 | protected void parse() throws IOException { |
michael@0 | 80 | mProperties = new Hashtable<String, Object>(); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | // set a property. Will erase the property if value = null |
michael@0 | 84 | public void setProperty(String key, Object value) { |
michael@0 | 85 | getProperties(); // ensure that we have parsed the file |
michael@0 | 86 | if (value == null) |
michael@0 | 87 | removeProperty(key); |
michael@0 | 88 | else |
michael@0 | 89 | mProperties.put(key.trim(), value); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | // remove a property |
michael@0 | 93 | public void removeProperty(String name) { |
michael@0 | 94 | // ensure that we have parsed the file |
michael@0 | 95 | getProperties(); |
michael@0 | 96 | mProperties.remove(name); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | public void write(BufferedWriter writer) throws IOException { |
michael@0 | 100 | if (!TextUtils.isEmpty(mName)) { |
michael@0 | 101 | writer.write("[" + mName + "]"); |
michael@0 | 102 | writer.newLine(); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | if (mProperties != null) { |
michael@0 | 106 | for (Enumeration<String> e = mProperties.keys(); e.hasMoreElements();) { |
michael@0 | 107 | String key = e.nextElement(); |
michael@0 | 108 | writeProperty(writer, key, mProperties.get(key)); |
michael@0 | 109 | } |
michael@0 | 110 | } |
michael@0 | 111 | writer.newLine(); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | // Helper function to write out a property |
michael@0 | 115 | private void writeProperty(BufferedWriter writer, String key, Object value) { |
michael@0 | 116 | try { |
michael@0 | 117 | writer.write(key + "=" + value); |
michael@0 | 118 | writer.newLine(); |
michael@0 | 119 | } catch (IOException e) { |
michael@0 | 120 | e.printStackTrace(); |
michael@0 | 121 | } |
michael@0 | 122 | } |
michael@0 | 123 | } |