aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wireless-transport/code-Carbon-SR1/apps/devicemanager/impl/src/main/java/org/opendaylight/mwtn/base/internalTypes/IniConfigurationFile.java
blob: 95d6a1370c773d2245d8321a1142708ff4426341 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package org.opendaylight.mwtn.base.internalTypes;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IniConfigurationFile {

	private static final Logger LOG = LoggerFactory.getLogger(IniConfigurationFile.class);

	public static class ConfigurationException extends Exception {
		/**
		 *
		 */
		private static final long serialVersionUID = 733061908616404383L;

		public ConfigurationException(String m) {
			super(m);
		}
	}

	public static class ConversionException extends Exception {
		/**
		 *
		 */
		private static final long serialVersionUID = 5179891576029923079L;

		public ConversionException(String m) {
			super(m);
		}
	}

	public static final String SECTIONNAME_ROOT = "";
	public static final String DELIMITER = "=";

	private static class SectionValue {
		public String Value;
		public final List<String> Comments;
		public boolean IsUncommented;

		public SectionValue(String value) {
			this(value, new ArrayList<String>(), false);
		}

		public SectionValue(String value, List<String> commentsForValue, boolean isuncommented) {
			this.Comments = commentsForValue;
			this.Value = value;
			this.IsUncommented = isuncommented;
		}
	}

	public static class Section {
		public final String Name;
		public final List<String> rawLines;
		public final LinkedHashMap<String, SectionValue> values;

		public Section(String name) {
			LOG.debug("new section created:" + name);
			this.Name = name;
			this.rawLines = new ArrayList<String>();
			this.values = new LinkedHashMap<String, SectionValue>();
		}

		public void addLine(String line) {
			LOG.trace("adding raw line:" + line);
			this.rawLines.add(line);
		}

		public String getProperty(String key) {
			return this.getProperty(key, null);
		}

		public String getProperty(String key, String defValue) {
			if (values.containsKey(key))
				return values.get(key).Value;
			return defValue;
		}

		public void setProperty(String key, String value) {
			boolean isuncommented = this.isCommentLine(key);
			if(isuncommented)
				key=key.substring(1);
			if (this.values.containsKey(key)) {
				this.values.get(key).Value = value;
				this.values.get(key).IsUncommented = isuncommented;
			} else {
				SectionValue sv = new SectionValue(value);
				sv.IsUncommented = isuncommented;
				this.values.put(key, sv);
			}
		}

		public void parseLines() {
			this.values.clear();
			List<String> commentsForValue = new ArrayList<String>();
			boolean uncommented = false;
			for (String line : rawLines) {

				if (this.isCommentLine(line)) {
					if (!line.contains(DELIMITER)) {
						commentsForValue.add(line);
						continue;
					} else {
						uncommented = true;
						line = line.substring(1);
					}
				}
				if (!line.contains(DELIMITER))
					continue;
				String hlp[] = line.split(DELIMITER);
				if(hlp.length>1)
				{
					String key=hlp[0];
					String value=line.length()>(key+DELIMITER).length()?line.substring((key+DELIMITER).length()):"";
					if (this.values.containsKey(key))
						this.values.get(key).Value = value;
					else {
						this.values.put(key, new SectionValue(value, commentsForValue, uncommented));
						commentsForValue = new ArrayList<String>();
					}
				}
				else
				{
					LOG.warn("ignoring unknown formatted line:"+line);
				}
				uncommented = false;
			}
		}

		private boolean isCommentLine(String line) {
			for (String c : commentChars) {
				if (line.startsWith(c))
					return true;
			}
			return false;
		}

		public String[] toLines() {
			List<String> lines = new ArrayList<String>();
			if (!this.Name.isEmpty())
				lines.add("[" + this.Name + "]");
			for (Entry<String, SectionValue> entry : this.values.entrySet()) {
				if (entry.getValue().Comments.size() > 0) {
					for (String comment : entry.getValue().Comments)
						lines.add(comment);
				}
				lines.add((entry.getValue().IsUncommented ? commentChars[0] : "") + entry.getKey() + DELIMITER
						+ entry.getValue().Value);
			}
			String[] alines = new String[lines.size()];
			return lines.toArray(alines);
		}

		public String getString(String key, String def) {
			return this.getProperty(key, def);
		}

		public boolean getBoolean(String key, boolean def) throws ConversionException {
			String v = this.getProperty(key);
			if (v == null || v.isEmpty())
				return def;
			if (v.equals("true"))
				return true;
			if (v.equals("false"))
				return false;
			throw new ConversionException("invalid value for key " + key);
		}

		public int getInt(String key, int def) throws ConversionException {
			String v = this.getProperty(key);
			if (v == null || v.isEmpty())
				return def;
			try {
				return Integer.parseInt(v);
			} catch (NumberFormatException e) {
				throw new ConversionException(e.getMessage());
			}
		}

		public boolean hasValues() {
			return this.values.size()>0;
		}

		public boolean hasKey(String key) {
			return this.values.containsKey(key);
		}

	}

	private final File mFile;
	private final List<Section> sections;
	private static final String commentChars[] = { "#", ";" };

	public IniConfigurationFile(File f) {
		this.mFile = f;
		this.sections = new ArrayList<Section>();
		this.sections.add(new Section(SECTIONNAME_ROOT));

	}

	public void load() throws ConfigurationException {
		String curSectionName = SECTIONNAME_ROOT;
		LOG.debug("loading file");
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(this.mFile));
			for (String line; (line = br.readLine()) != null;) {
				line = line.trim();
				if (line.isEmpty())
					continue;
				if (line.startsWith("[") && line.endsWith("]")) {
					curSectionName = line.substring(1, line.length() - 1);
					this.addSection(curSectionName);
				} else
					this.getSection(curSectionName).addLine(line);
			}

		} catch (Exception e) {
			throw new ConfigurationException(e.getMessage());
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (IOException e) {
			}
		}
		LOG.debug("finished loading file");
		LOG.debug("start parsing sections");
		for (Section section : this.sections)
			section.parseLines();
		LOG.debug("finished parsing " + this.sections.size() + " sections");
	}

	private Section getSection(String name) {
		for (Section s : this.sections) {
			if (s.Name.equals(name))
				return s;
		}
		return this.addSection(name);

	}

	private Section addSection(String name) {

		Section s = new Section(name);
		this.sections.add(s);
		return s;
	}

	public void reLoad() throws ConfigurationException {
		this.sections.clear();
		this.sections.add(new Section(SECTIONNAME_ROOT));
		this.load();
	}

	public String getProperty(String key, String defValue) {
		Section s;
		if (key.contains(".")) {
			s = this.getSection(key.substring(0, key.indexOf(".")));
			key = key.substring(key.indexOf(".") + 1);
		} else
			s = this.getSection(SECTIONNAME_ROOT);

		String v = s.getProperty(key);
		if (v == null || v.isEmpty())
			return defValue;
		return v;
	}

	public void setProperty(String key, String value) {
		Section s;
		if (key.contains(".")) {
			s = this.getSection(key.substring(0, key.indexOf(".")));
			key = key.substring(key.indexOf(".") + 1);
		} else
			s = this.getSection(SECTIONNAME_ROOT);
		s.setProperty(key, value);
	}

	public int getProperty(String key, int defValue) throws ConversionException {
		Section s;
		if (key.contains(".")) {
			s = this.getSection(key.substring(0, key.indexOf(".")));
			key = key.substring(key.indexOf(".") + 1);
		} else
			s = this.getSection(SECTIONNAME_ROOT);

		return s.getInt(key, defValue);
	}

	public void setProperty(String key, int value) {
		Section s;
		if (key.contains(".")) {
			s = this.getSection(key.substring(0, key.indexOf(".")));
			key = key.substring(key.indexOf(".") + 1);
		} else
			s = this.getSection(SECTIONNAME_ROOT);
		s.setProperty(key, String.format("%d", value));
	}

	public boolean getProperty(String key, boolean defValue) throws ConversionException {
		Section s;
		if (key.contains(".")) {
			s = this.getSection(key.substring(0, key.indexOf(".")));
			key = key.substring(key.indexOf(".") + 1);
		} else
			s = this.getSection(SECTIONNAME_ROOT);

		return s.getBoolean(key, defValue);
	}

	public void setProperty(String key, boolean value) {
		Section s;
		if (key.contains(".")) {
			s = this.getSection(key.substring(0, key.indexOf(".")));
			key = key.substring(key.indexOf(".") + 1);
		} else
			s = this.getSection(SECTIONNAME_ROOT);
		s.setProperty(key, value ? "true" : "false");
	}

	public void setProperty(String key, Object value) {
		this.setProperty(key, value==null?"null":value.toString());
	}

	public void save() {
		final String LR = "\n";
		BufferedWriter bw;
		try {
			bw = new BufferedWriter(new FileWriter(this.mFile, false));
			for (Section section : this.sections) {
				if(section.hasValues())
					bw.write(String.join(LR, section.toLines()) + LR + LR);
			}
			bw.close();
		} catch (Exception e) {

		}

	}

	public Section subset(String section) {
		return this.getSection(section);
	}

}