All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.syntaxphoenix.syntaxapi.config.toml.TomlConfigSection Maven / Gradle / Ivy

There is a newer version: 2.0.12
Show newest version
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);
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy