| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.inifile; |
| 17 |
|
| 18 |
import java.io.BufferedReader; |
| 19 |
import java.io.IOException; |
| 20 |
import java.io.Reader; |
| 21 |
import java.io.Writer; |
| 22 |
import java.util.*; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class IniFile |
| 33 |
{ |
| 34 |
private List sections; |
| 35 |
private Map sectionMap; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public IniFile() |
| 41 |
{ |
| 42 |
super(); |
| 43 |
this.sections = new ArrayList(); |
| 44 |
this.sectionMap = new HashMap(); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public List getSections() |
| 52 |
{ |
| 53 |
return sections; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public IniSection getSection(String name) |
| 62 |
{ |
| 63 |
return (IniSection)sectionMap.get(name); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public void setSection(IniSection section) |
| 72 |
{ |
| 73 |
IniSection sec = (IniSection)sectionMap.get(section.getName()); |
| 74 |
if (sec != null) |
| 75 |
{ |
| 76 |
int idx = sections.indexOf(sec); |
| 77 |
sections.set(idx, section); |
| 78 |
} |
| 79 |
else |
| 80 |
{ |
| 81 |
sections.add(section); |
| 82 |
} |
| 83 |
|
| 84 |
sectionMap.put(section.getName(), section); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public void removeSection(String name) |
| 92 |
{ |
| 93 |
IniSection sec = (IniSection)sectionMap.get(name); |
| 94 |
if (sec != null) |
| 95 |
{ |
| 96 |
int idx = sections.indexOf(sec); |
| 97 |
sections.remove(idx); |
| 98 |
sectionMap.remove(name); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public String getProperty(String section, String property) |
| 110 |
{ |
| 111 |
String value = null; |
| 112 |
IniSection sec = getSection(section); |
| 113 |
if (sec != null) |
| 114 |
{ |
| 115 |
IniProperty prop = sec.getProperty(property); |
| 116 |
if (prop != null) |
| 117 |
{ |
| 118 |
value = prop.getValue(); |
| 119 |
} |
| 120 |
} |
| 121 |
return value; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public void setProperty(String section, String property, String value) |
| 132 |
{ |
| 133 |
IniSection sec = getSection(section); |
| 134 |
if (sec == null) |
| 135 |
{ |
| 136 |
sec = new IniSection(section); |
| 137 |
setSection(sec); |
| 138 |
} |
| 139 |
|
| 140 |
sec.setProperty(new IniProperty(property, value)); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public void removeProperty(String section, String property) |
| 149 |
{ |
| 150 |
IniSection sec = getSection(section); |
| 151 |
if (sec != null) |
| 152 |
{ |
| 153 |
sec.removeProperty(property); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public void write(Writer writer) |
| 164 |
throws IOException |
| 165 |
{ |
| 166 |
Iterator it = sections.iterator(); |
| 167 |
IniSection section = null; |
| 168 |
while (it.hasNext()) |
| 169 |
{ |
| 170 |
section = (IniSection)it.next(); |
| 171 |
section.write(writer); |
| 172 |
writer.write(System.getProperty("line.separator")); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public void read(Reader reader) |
| 184 |
throws IOException |
| 185 |
{ |
| 186 |
BufferedReader br = new BufferedReader(reader); |
| 187 |
String line = null; |
| 188 |
|
| 189 |
IniSection currentSection = new IniSection("NONE"); |
| 190 |
|
| 191 |
while ((line = br.readLine()) != null) |
| 192 |
{ |
| 193 |
line = line.trim(); |
| 194 |
if (line.length() > 0 && !line.startsWith("#") && !line.startsWith(";")) |
| 195 |
{ |
| 196 |
if(line.startsWith("[") && line.endsWith("]")) |
| 197 |
{ |
| 198 |
String secName = line.substring(1, line.length()-1); |
| 199 |
currentSection = getSection(secName); |
| 200 |
if (currentSection == null) |
| 201 |
{ |
| 202 |
currentSection = new IniSection(secName); |
| 203 |
setSection(currentSection); |
| 204 |
} |
| 205 |
} |
| 206 |
else |
| 207 |
{ |
| 208 |
String name = line; |
| 209 |
String value = ""; |
| 210 |
int pos = line.indexOf("="); |
| 211 |
if (pos != -1) |
| 212 |
{ |
| 213 |
name = line.substring(0,pos); |
| 214 |
value = line.substring(pos+1); |
| 215 |
} |
| 216 |
|
| 217 |
currentSection.setProperty(new IniProperty(name,value)); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
} |
| 223 |
} |
| 224 |
} |