BAEL-5676 Implement ini file parsing and unit tests
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.ini;
|
||||
|
||||
import org.apache.commons.configuration2.INIConfiguration;
|
||||
import org.apache.commons.configuration2.SubnodeConfiguration;
|
||||
import org.apache.commons.configuration2.ex.ConfigurationException;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
public class CommonsParser {
|
||||
public static Map<String, Map<String, String>> parseIniFile(File fileToParse) throws IOException, ConfigurationException {
|
||||
Map<String, Map<String, String>> iniFileContents = new HashMap<>();
|
||||
|
||||
INIConfiguration iniConfiguration = new INIConfiguration();
|
||||
try (FileReader fileReader = new FileReader(fileToParse)) {
|
||||
iniConfiguration.read(fileReader);
|
||||
}
|
||||
|
||||
for (String section : iniConfiguration.getSections()) {
|
||||
Map<String, String> subSectionMap = new HashMap<>();
|
||||
SubnodeConfiguration confSection = iniConfiguration.getSection(section);
|
||||
Iterator<String> keyIterator = confSection.getKeys();
|
||||
while (keyIterator.hasNext()) {
|
||||
String key = keyIterator.next();
|
||||
String value = confSection.getProperty(key)
|
||||
.toString();
|
||||
subSectionMap.put(key, value);
|
||||
}
|
||||
iniFileContents.put(section, subSectionMap);
|
||||
}
|
||||
return iniFileContents;
|
||||
}
|
||||
|
||||
public static String readIniFileValue(File fileToParse, String section, String value) throws IOException, ConfigurationException {
|
||||
INIConfiguration iniConfiguration = new INIConfiguration();
|
||||
try (FileReader fileReader = new FileReader(fileToParse)) {
|
||||
iniConfiguration.read(fileReader);
|
||||
}
|
||||
|
||||
return iniConfiguration.getSection(section).getProperty(value).toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.ini;
|
||||
|
||||
import org.ini4j.Ini;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
import static java.util.stream.Collectors.toMap;
|
||||
|
||||
public class Ini4JParser {
|
||||
|
||||
public static Map<String, Map<String, String>> parseIniFile(File fileToParse) throws IOException {
|
||||
Ini ini = new Ini(fileToParse);
|
||||
return ini.entrySet().stream()
|
||||
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
}
|
||||
|
||||
public static String readIniFileValue(File fileToParse, String section, String key) throws IOException {
|
||||
Ini ini = new Ini(fileToParse);
|
||||
return ini.get(section, key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.baeldung.ini;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
|
||||
public class MyConfiguration {
|
||||
|
||||
@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class)
|
||||
public static class Fonts {
|
||||
private String letter;
|
||||
private int textSize;
|
||||
|
||||
public String getLetter() {
|
||||
return letter;
|
||||
}
|
||||
|
||||
public void setLetter(String letter) {
|
||||
this.letter = letter;
|
||||
}
|
||||
|
||||
public int getTextSize() {
|
||||
return textSize;
|
||||
}
|
||||
|
||||
public void setTextSize(int textSize) {
|
||||
this.textSize = textSize;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Background {
|
||||
private String color;
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
|
||||
public static class RequestResult {
|
||||
private int requestCode;
|
||||
|
||||
public int getRequestCode() {
|
||||
return requestCode;
|
||||
}
|
||||
|
||||
public void setRequestCode(int requestCode) {
|
||||
this.requestCode = requestCode;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
|
||||
public static class ResponseResult {
|
||||
private int resultCode;
|
||||
|
||||
public int getResultCode() {
|
||||
return resultCode;
|
||||
}
|
||||
|
||||
public void setResultCode(int resultCode) {
|
||||
this.resultCode = resultCode;
|
||||
}
|
||||
}
|
||||
|
||||
private Fonts fonts;
|
||||
private Background background;
|
||||
|
||||
@JsonProperty("RequestResult")
|
||||
private RequestResult requestResult;
|
||||
|
||||
@JsonProperty("ResponseResult")
|
||||
private ResponseResult responseResult;
|
||||
|
||||
public Fonts getFonts() {
|
||||
return fonts;
|
||||
}
|
||||
|
||||
public void setFonts(Fonts fonts) {
|
||||
this.fonts = fonts;
|
||||
}
|
||||
|
||||
public Background getBackground() {
|
||||
return background;
|
||||
}
|
||||
|
||||
public void setBackground(Background background) {
|
||||
this.background = background;
|
||||
}
|
||||
|
||||
public RequestResult getRequestResult() {
|
||||
return requestResult;
|
||||
}
|
||||
|
||||
public void setRequestResult(RequestResult requestResult) {
|
||||
this.requestResult = requestResult;
|
||||
}
|
||||
|
||||
public ResponseResult getResponseResult() {
|
||||
return responseResult;
|
||||
}
|
||||
|
||||
public void setResponseResult(ResponseResult responseResult) {
|
||||
this.responseResult = responseResult;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user