1 package org.opendaylight.mwtn.base.internalTypes;
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
6 import java.io.FileReader;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.util.ArrayList;
10 import java.util.LinkedHashMap;
11 import java.util.List;
12 import java.util.Map.Entry;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 public class IniConfigurationFile {
19 private static final Logger LOG = LoggerFactory.getLogger(IniConfigurationFile.class);
21 public static class ConfigurationException extends Exception {
25 private static final long serialVersionUID = 733061908616404383L;
27 public ConfigurationException(String m) {
32 public static class ConversionException extends Exception {
36 private static final long serialVersionUID = 5179891576029923079L;
38 public ConversionException(String m) {
43 public static final String SECTIONNAME_ROOT = "";
44 public static final String DELIMITER = "=";
46 private static class SectionValue {
48 public final List<String> Comments;
49 public boolean IsUncommented;
51 public SectionValue(String value) {
52 this(value, new ArrayList<String>(), false);
55 public SectionValue(String value, List<String> commentsForValue, boolean isuncommented) {
56 this.Comments = commentsForValue;
58 this.IsUncommented = isuncommented;
62 public static class Section {
63 public final String Name;
64 public final List<String> rawLines;
65 public final LinkedHashMap<String, SectionValue> values;
67 public Section(String name) {
68 LOG.debug("new section created:" + name);
70 this.rawLines = new ArrayList<String>();
71 this.values = new LinkedHashMap<String, SectionValue>();
74 public void addLine(String line) {
75 LOG.trace("adding raw line:" + line);
76 this.rawLines.add(line);
79 public String getProperty(String key) {
80 return this.getProperty(key, null);
83 public String getProperty(String key, String defValue) {
84 if (values.containsKey(key))
85 return values.get(key).Value;
89 public void setProperty(String key, String value) {
90 boolean isuncommented = this.isCommentLine(key);
93 if (this.values.containsKey(key)) {
94 this.values.get(key).Value = value;
95 this.values.get(key).IsUncommented = isuncommented;
97 SectionValue sv = new SectionValue(value);
98 sv.IsUncommented = isuncommented;
99 this.values.put(key, sv);
103 public void parseLines() {
105 List<String> commentsForValue = new ArrayList<String>();
106 boolean uncommented = false;
107 for (String line : rawLines) {
109 if (this.isCommentLine(line)) {
110 if (!line.contains(DELIMITER)) {
111 commentsForValue.add(line);
115 line = line.substring(1);
118 if (!line.contains(DELIMITER))
120 String hlp[] = line.split(DELIMITER);
124 String value=line.length()>(key+DELIMITER).length()?line.substring((key+DELIMITER).length()):"";
125 if (this.values.containsKey(key))
126 this.values.get(key).Value = value;
128 this.values.put(key, new SectionValue(value, commentsForValue, uncommented));
129 commentsForValue = new ArrayList<String>();
134 LOG.warn("ignoring unknown formatted line:"+line);
140 private boolean isCommentLine(String line) {
141 for (String c : commentChars) {
142 if (line.startsWith(c))
148 public String[] toLines() {
149 List<String> lines = new ArrayList<String>();
150 if (!this.Name.isEmpty())
151 lines.add("[" + this.Name + "]");
152 for (Entry<String, SectionValue> entry : this.values.entrySet()) {
153 if (entry.getValue().Comments.size() > 0) {
154 for (String comment : entry.getValue().Comments)
157 lines.add((entry.getValue().IsUncommented ? commentChars[0] : "") + entry.getKey() + DELIMITER
158 + entry.getValue().Value);
160 String[] alines = new String[lines.size()];
161 return lines.toArray(alines);
164 public String getString(String key, String def) {
165 return this.getProperty(key, def);
168 public boolean getBoolean(String key, boolean def) throws ConversionException {
169 String v = this.getProperty(key);
170 if (v == null || v.isEmpty())
172 if (v.equals("true"))
174 if (v.equals("false"))
176 throw new ConversionException("invalid value for key " + key);
179 public int getInt(String key, int def) throws ConversionException {
180 String v = this.getProperty(key);
181 if (v == null || v.isEmpty())
184 return Integer.parseInt(v);
185 } catch (NumberFormatException e) {
186 throw new ConversionException(e.getMessage());
190 public boolean hasValues() {
191 return this.values.size()>0;
194 public boolean hasKey(String key) {
195 return this.values.containsKey(key);
200 private final File mFile;
201 private final List<Section> sections;
202 private static final String commentChars[] = { "#", ";" };
204 public IniConfigurationFile(File f) {
206 this.sections = new ArrayList<Section>();
207 this.sections.add(new Section(SECTIONNAME_ROOT));
211 public void load() throws ConfigurationException {
212 String curSectionName = SECTIONNAME_ROOT;
213 LOG.debug("loading file");
214 BufferedReader br = null;
216 br = new BufferedReader(new FileReader(this.mFile));
217 for (String line; (line = br.readLine()) != null;) {
221 if (line.startsWith("[") && line.endsWith("]")) {
222 curSectionName = line.substring(1, line.length() - 1);
223 this.addSection(curSectionName);
225 this.getSection(curSectionName).addLine(line);
228 } catch (Exception e) {
229 throw new ConfigurationException(e.getMessage());
234 } catch (IOException e) {
237 LOG.debug("finished loading file");
238 LOG.debug("start parsing sections");
239 for (Section section : this.sections)
240 section.parseLines();
241 LOG.debug("finished parsing " + this.sections.size() + " sections");
244 private Section getSection(String name) {
245 for (Section s : this.sections) {
246 if (s.Name.equals(name))
249 return this.addSection(name);
253 private Section addSection(String name) {
255 Section s = new Section(name);
256 this.sections.add(s);
260 public void reLoad() throws ConfigurationException {
261 this.sections.clear();
262 this.sections.add(new Section(SECTIONNAME_ROOT));
266 public String getProperty(String key, String defValue) {
268 if (key.contains(".")) {
269 s = this.getSection(key.substring(0, key.indexOf(".")));
270 key = key.substring(key.indexOf(".") + 1);
272 s = this.getSection(SECTIONNAME_ROOT);
274 String v = s.getProperty(key);
275 if (v == null || v.isEmpty())
280 public void setProperty(String key, String value) {
282 if (key.contains(".")) {
283 s = this.getSection(key.substring(0, key.indexOf(".")));
284 key = key.substring(key.indexOf(".") + 1);
286 s = this.getSection(SECTIONNAME_ROOT);
287 s.setProperty(key, value);
290 public int getProperty(String key, int defValue) throws ConversionException {
292 if (key.contains(".")) {
293 s = this.getSection(key.substring(0, key.indexOf(".")));
294 key = key.substring(key.indexOf(".") + 1);
296 s = this.getSection(SECTIONNAME_ROOT);
298 return s.getInt(key, defValue);
301 public void setProperty(String key, int value) {
303 if (key.contains(".")) {
304 s = this.getSection(key.substring(0, key.indexOf(".")));
305 key = key.substring(key.indexOf(".") + 1);
307 s = this.getSection(SECTIONNAME_ROOT);
308 s.setProperty(key, String.format("%d", value));
311 public boolean getProperty(String key, boolean defValue) throws ConversionException {
313 if (key.contains(".")) {
314 s = this.getSection(key.substring(0, key.indexOf(".")));
315 key = key.substring(key.indexOf(".") + 1);
317 s = this.getSection(SECTIONNAME_ROOT);
319 return s.getBoolean(key, defValue);
322 public void setProperty(String key, boolean value) {
324 if (key.contains(".")) {
325 s = this.getSection(key.substring(0, key.indexOf(".")));
326 key = key.substring(key.indexOf(".") + 1);
328 s = this.getSection(SECTIONNAME_ROOT);
329 s.setProperty(key, value ? "true" : "false");
332 public void setProperty(String key, Object value) {
333 this.setProperty(key, value==null?"null":value.toString());
337 final String LR = "\n";
340 bw = new BufferedWriter(new FileWriter(this.mFile, false));
341 for (Section section : this.sections) {
342 if(section.hasValues())
343 bw.write(String.join(LR, section.toLines()) + LR + LR);
346 } catch (Exception e) {
352 public Section subset(String section) {
353 return this.getSection(section);