summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/INISection.java
blob: af91ad4108169411e10a92d61532535c1e0aff52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.util;

import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;

public class INISection {
    private static final String LOGTAG = "INIParser";

    // default file to read and write to
    private String mName;
    public String getName() { return mName; }
    public void setName(String name) { mName = name; }

    // show or hide debug logging
    private  boolean mDebug;

    // Global properties that aren't inside a section in the file
    protected Hashtable<String, Object> mProperties;

    // create a parser. The file will not be read until you attempt to
    // access sections or properties inside it. At that point its read synchronously
    public INISection(String name) {
        mName = name;
    }

    // log a debug string to the console
    protected void debug(String msg) {
        if (mDebug) {
            Log.i(LOGTAG, msg);
        }
    }

    // get a global property out of the hash table. will return null if the property doesn't exist
    public Object getProperty(String key) {
        getProperties(); // ensure that we have parsed the file
        return mProperties.get(key);
    }

    // get a global property out of the hash table. will return null if the property doesn't exist
    public int getIntProperty(String key) {
        Object val = getProperty(key);
        if (val == null)
            return -1;

        return Integer.parseInt(val.toString());
    }

    // get a global property out of the hash table. will return null if the property doesn't exist
    public String getStringProperty(String key) {
        Object val = getProperty(key);
        if (val == null)
          return null;

        return val.toString();
    }

    // get a hashtable of all the global properties in this file
    public Hashtable<String, Object> getProperties() {
        if (mProperties == null) {
            try {
                parse();
            } catch (IOException e) {
                debug("Error parsing: " + e);
            }
        }
        return mProperties;
    }

    // do nothing for generic sections
    protected void parse() throws IOException {
        mProperties = new Hashtable<String, Object>();
    }

    // set a property. Will erase the property if value = null
    public void setProperty(String key, Object value) {
        getProperties(); // ensure that we have parsed the file
        if (value == null)
            removeProperty(key);
        else
            mProperties.put(key.trim(), value);
    }

    // remove a property
    public void removeProperty(String name) {
        // ensure that we have parsed the file
        getProperties();
        mProperties.remove(name);
    }

    public void write(BufferedWriter writer) throws IOException {
        if (!TextUtils.isEmpty(mName)) {
            writer.write("[" + mName + "]");
            writer.newLine();
        }

        if (mProperties != null) {
            for (Enumeration<String> e = mProperties.keys(); e.hasMoreElements();) {
                String key = e.nextElement();
                writeProperty(writer, key, mProperties.get(key));
            }
        }
        writer.newLine();
    }

    // Helper function to write out a property
    private void writeProperty(BufferedWriter writer, String key, Object value) {
        try {
            writer.write(key + "=" + value);
            writer.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}