diff --git a/libraries-files/pom.xml b/libraries-files/pom.xml new file mode 100644 index 0000000000..7e21d127bb --- /dev/null +++ b/libraries-files/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + libraries-files + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + + + + org.ini4j + ini4j + ${org.ini4j.version} + + + org.apache.commons + commons-configuration2 + ${commons-configuration2} + + + com.fasterxml.jackson.core + jackson-annotations + ${jackson.version} + + + com.fasterxml.jackson.core + jackson-core + ${jackson.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + + 0.5.4 + 2.8.0 + 2.13.1 + + + diff --git a/libraries-files/src/main/java/com/baeldung/ini/CommonsParser.java b/libraries-files/src/main/java/com/baeldung/ini/CommonsParser.java new file mode 100644 index 0000000000..77a544db16 --- /dev/null +++ b/libraries-files/src/main/java/com/baeldung/ini/CommonsParser.java @@ -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> parseIniFile(File fileToParse) throws IOException, ConfigurationException { + Map> iniFileContents = new HashMap<>(); + + INIConfiguration iniConfiguration = new INIConfiguration(); + try (FileReader fileReader = new FileReader(fileToParse)) { + iniConfiguration.read(fileReader); + } + + for (String section : iniConfiguration.getSections()) { + Map subSectionMap = new HashMap<>(); + SubnodeConfiguration confSection = iniConfiguration.getSection(section); + Iterator 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(); + } +} diff --git a/libraries-files/src/main/java/com/baeldung/ini/Ini4JParser.java b/libraries-files/src/main/java/com/baeldung/ini/Ini4JParser.java new file mode 100644 index 0000000000..dacb559787 --- /dev/null +++ b/libraries-files/src/main/java/com/baeldung/ini/Ini4JParser.java @@ -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> 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); + } +} diff --git a/libraries-files/src/main/java/com/baeldung/ini/MyConfiguration.java b/libraries-files/src/main/java/com/baeldung/ini/MyConfiguration.java new file mode 100644 index 0000000000..3d9af04655 --- /dev/null +++ b/libraries-files/src/main/java/com/baeldung/ini/MyConfiguration.java @@ -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; + } +} diff --git a/libraries-files/src/test/java/com/baeldung/ini/CommonsParserUnitTest.java b/libraries-files/src/test/java/com/baeldung/ini/CommonsParserUnitTest.java new file mode 100644 index 0000000000..b6645da884 --- /dev/null +++ b/libraries-files/src/test/java/com/baeldung/ini/CommonsParserUnitTest.java @@ -0,0 +1,32 @@ +package com.baeldung.ini; + +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.nio.file.Paths; +import java.util.Map; + +import static com.baeldung.ini.CommonsParser.parseIniFile; +import static com.baeldung.ini.CommonsParser.readIniFileValue; +import static org.assertj.core.api.Assertions.assertThat; + +class CommonsParserUnitTest { + + private static final File TEST_FILE = Paths.get("src", "test", "resources", "sample.ini").toFile(); + + @Test + void givenIniFileThenCanParseWithIni4j() throws Exception { + Map> result = + parseIniFile(TEST_FILE); + + assertThat(result.get("fonts")) + .containsEntry("letter", "bold") + .containsEntry("text-size", "28"); + } + + @Test + void givenIniFileThenCanReadKeyWithIni4j() throws Exception { + assertThat(readIniFileValue(TEST_FILE, "fonts", "letter")) + .isEqualTo("bold"); + } +} \ No newline at end of file diff --git a/libraries-files/src/test/java/com/baeldung/ini/Ini4JParserUnitTest.java b/libraries-files/src/test/java/com/baeldung/ini/Ini4JParserUnitTest.java new file mode 100644 index 0000000000..c974e92a87 --- /dev/null +++ b/libraries-files/src/test/java/com/baeldung/ini/Ini4JParserUnitTest.java @@ -0,0 +1,32 @@ +package com.baeldung.ini; + +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.nio.file.Paths; +import java.util.Map; + +import static com.baeldung.ini.Ini4JParser.readIniFileValue; +import static com.baeldung.ini.Ini4JParser.parseIniFile; +import static org.assertj.core.api.Assertions.assertThat; + +class Ini4JParserUnitTest { + + private static final File TEST_FILE = Paths.get("src", "test", "resources", "sample.ini").toFile(); + + @Test + void givenIniFileThenCanParseWithIni4j() throws Exception { + Map> result = + parseIniFile(TEST_FILE); + + assertThat(result.get("fonts")) + .containsEntry("letter", "bold") + .containsEntry("text-size", "28"); + } + + @Test + void givenIniFileThenCanReadKeyFromIt() throws Exception { + assertThat(readIniFileValue(TEST_FILE, "fonts", "letter")) + .isEqualTo("bold"); + } +} \ No newline at end of file diff --git a/libraries-files/src/test/java/com/baeldung/ini/SerializeIntoPojoUnitTest.java b/libraries-files/src/test/java/com/baeldung/ini/SerializeIntoPojoUnitTest.java new file mode 100644 index 0000000000..a91abcb8b3 --- /dev/null +++ b/libraries-files/src/test/java/com/baeldung/ini/SerializeIntoPojoUnitTest.java @@ -0,0 +1,31 @@ +package com.baeldung.ini; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.nio.file.Paths; +import java.util.Map; + +import static com.baeldung.ini.Ini4JParser.parseIniFile; +import static org.assertj.core.api.Assertions.assertThat; + +class SerializeIntoPojoUnitTest { + + private static final File TEST_FILE = Paths.get("src", "test", "resources", "sample.ini").toFile(); + + @Test + void givenAnIniFileThenCanLoadAsPojo() throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + + Map> iniKeys = parseIniFile(TEST_FILE); + + MyConfiguration config = objectMapper.convertValue(iniKeys, MyConfiguration.class); + + assertThat(config.getFonts().getLetter()).isEqualTo("bold"); + assertThat(config.getFonts().getTextSize()).isEqualTo(28); + assertThat(config.getBackground().getColor()).isEqualTo("white"); + assertThat(config.getRequestResult().getRequestCode()).isEqualTo(1); + assertThat(config.getResponseResult().getResultCode()).isZero(); + } +} diff --git a/libraries-files/src/test/resources/sample.ini b/libraries-files/src/test/resources/sample.ini new file mode 100644 index 0000000000..687cf1daed --- /dev/null +++ b/libraries-files/src/test/resources/sample.ini @@ -0,0 +1,14 @@ +; for 16-bit app support + +[fonts] +letter=bold +text-size=28 + +[background] +color=white + +[RequestResult] +RequestCode=1 + +[ResponseResult] +ResultCode=0 \ No newline at end of file