Skip to content

Commit 292135c

Browse files
committed
toml file support
1 parent 07003aa commit 292135c

File tree

3 files changed

+107
-7
lines changed

3 files changed

+107
-7
lines changed

src/main/java/dev/jsinco/abstractjavafilelib/AbstractFileManager.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import org.slf4j.Logger;
44

55
import java.io.File;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.OutputStream;
9+
import java.nio.file.Files;
610

711
public abstract class AbstractFileManager extends ConfigurationSection {
812

@@ -17,7 +21,29 @@ public AbstractFileManager(File file) {
1721
}
1822

1923

20-
public abstract AbstractFileManager generateFile();
24+
public AbstractFileManager generateFile() {
25+
if (!file.exists()) {
26+
try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(file.getName())) {
27+
file.getParentFile().mkdirs();
28+
file.createNewFile();
29+
30+
if (inputStream != null) {
31+
OutputStream outputStream = Files.newOutputStream(file.toPath());
32+
byte[] buffer = new byte[1024];
33+
int bytesRead;
34+
while ((bytesRead = inputStream.read(buffer)) != -1) {
35+
outputStream.write(buffer, 0, bytesRead);
36+
}
37+
outputStream.flush();
38+
outputStream.close();
39+
}
40+
41+
} catch (IOException e) {
42+
logger.error("Error creating file: " + e.getMessage());
43+
}
44+
}
45+
return loadFile();
46+
}
2147

2248
public abstract AbstractFileManager loadFile();
2349

src/main/java/dev/jsinco/abstractjavafilelib/ConfigurationSection.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.AbstractMap;
77
import java.util.ArrayList;
88
import java.util.Collections;
9+
import java.util.HashMap;
910
import java.util.LinkedHashMap;
1011
import java.util.List;
1112

@@ -32,12 +33,16 @@ public boolean contains(String key) {
3233

3334
public ConfigurationSection getConfigurationSection(String key) {
3435
data.computeIfAbsent(key, k -> new LinkedHashMap<String, Object>());
35-
if (data.get(key) instanceof LinkedHashMap<?, ?>) { // SNAKEYAML + GSON
36-
return new ConfigurationSection((LinkedHashMap<String, Object>) data.get(key));
37-
} else if (data.get(key) instanceof LinkedTreeMap<?, ?>) { // GSON
38-
return new ConfigurationSection((LinkedTreeMap<String, Object>) data.get(key));
36+
Object object = data.get(key);
37+
38+
if (object instanceof LinkedHashMap<?, ?>) { // SNAKEYAML + GSON
39+
return new ConfigurationSection((LinkedHashMap<String, Object>) object);
40+
} else if (object instanceof LinkedTreeMap<?, ?>) { // GSON
41+
return new ConfigurationSection((LinkedTreeMap<String, Object>) object);
42+
} else if (object instanceof HashMap<?,?>) {
43+
return new ConfigurationSection((HashMap<String, Object>) object);
3944
}
40-
return new ConfigurationSection((AbstractMap<String, Object>) data.get(key)); // UNKNOWN
45+
return new ConfigurationSection((AbstractMap<String, Object>) object); // UNKNOWN
4146
}
4247

4348
private Object getLastFromSection(String path) {
Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,73 @@
11
package dev.jsinco.abstractjavafilelib.schemas;
22

3-
public class TomlConfig {
3+
import com.moandjiezana.toml.Toml;
4+
import com.moandjiezana.toml.TomlWriter;
5+
import dev.jsinco.abstractjavafilelib.AbstractFileManager;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.util.HashMap;
11+
import java.util.LinkedHashMap;
12+
13+
public class TomlConfig extends AbstractFileManager {
14+
15+
private final Toml toml = new Toml();
16+
17+
public TomlConfig(String fileName) {
18+
this(new File(dataFolder, fileName), false);
19+
}
20+
21+
public TomlConfig(String fileName, boolean isImaginary) {
22+
this(new File(dataFolder, fileName), isImaginary);
23+
}
24+
25+
public TomlConfig(File file, boolean isImaginary) {
26+
super(file);
27+
if (isImaginary) {
28+
loadImaginaryFile();
29+
} else {
30+
generateFile();
31+
}
32+
}
33+
34+
@Override
35+
public TomlConfig loadFile() {
36+
if (isToml(file)) {
37+
data = (HashMap<String, Object>) toml.read(file).toMap();
38+
}
39+
if (data == null) {
40+
data = new LinkedHashMap<>();
41+
}
42+
return this;
43+
}
44+
45+
@Override
46+
public AbstractFileManager loadImaginaryFile() {
47+
if (isToml(file)) {
48+
try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(file.getName())) {
49+
data = (HashMap<String, Object>) toml.read(inputStream).toMap();
50+
if (data == null) {
51+
data = new LinkedHashMap<>();
52+
}
53+
} catch (IOException e) {
54+
logger.error("Error loading imaginary file: " + e.getMessage());
55+
}
56+
}
57+
return this;
58+
}
59+
60+
@Override
61+
public void save() {
62+
TomlWriter writer = new TomlWriter();
63+
try {
64+
writer.write(data, file);
65+
} catch (IOException e) {
66+
logger.error("Error saving file: " + e.getMessage());
67+
}
68+
}
69+
70+
public static boolean isToml(File file) {
71+
return file.isFile() && file.getName().endsWith(".toml");
72+
}
473
}

0 commit comments

Comments
 (0)