com.syntaxphoenix.syntaxapi.config.toml.TomlConfigSection Maven / Gradle / Ivy
package com.syntaxphoenix.syntaxapi.config.toml;
import java.io.IOException;
import java.util.Set;
import java.util.HashMap;
import java.util.Map.Entry;
import com.electronwill.toml.Toml;
import com.syntaxphoenix.syntaxapi.config.BaseSection;
public class TomlConfigSection extends BaseSection {
public TomlConfigSection() {
super("");
}
public TomlConfigSection(String name) {
super(name);
}
@Override
protected BaseSection initSection(String name) {
return new TomlConfigSection(name);
}
@Override
protected boolean isSectionInstance(BaseSection section) {
return section instanceof TomlConfigSection;
}
/*
*
* TO TOML
*
*/
public String toTomlString() {
try {
return Toml.writeToString(toMap());
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
/*
*
* FROM TOML
*
*/
public void fromTomlString(String toml) {
fromMap((HashMap) Toml.read(toml));
}
@SuppressWarnings("unchecked")
public void fromMap(HashMap input) {
clear();
Set> set = input.entrySet();
if(set.isEmpty()) {
return;
}
for(Entry entry : set) {
Object obj = entry.getValue();
if(obj instanceof HashMap) {
((TomlConfigSection) createSection(entry.getKey())).fromMap((HashMap) obj);
} else {
set(entry.getKey(), obj);
}
}
}
}