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