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

com.github.resource4j.extras.config.ConfigMapParser Maven / Gradle / Ivy

There is a newer version: 3.3.1
Show newest version
package com.github.resource4j.extras.config;

import static com.github.resource4j.extras.config.ConfigParser.config;
import static com.typesafe.config.ConfigParseOptions.defaults;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.github.resource4j.ResourceObject;
import com.github.resource4j.ResourceObjectException;
import com.github.resource4j.objects.parsers.AbstractValueParser;
import com.github.resource4j.objects.parsers.BundleParser;
import com.github.resource4j.resources.discovery.ContentType;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigParseOptions;
import com.typesafe.config.ConfigSyntax;
import com.typesafe.config.ConfigValue;

@SuppressWarnings("WeakerAccess")
@ContentType(extension = ".config", mimeType = "text/x-java-properties")
public class ConfigMapParser extends AbstractValueParser> implements BundleParser {

	private ConfigParser parser;
	
	public static ConfigMapParser configMap() {
		return new ConfigMapParser();
	}
	
	public static ConfigMapParser configMap(ConfigParseOptions options) {
		return new ConfigMapParser(options);
	}
	

	public ConfigMapParser() {
		parser = config(defaults()
				.setSyntax(ConfigSyntax.CONF)
				.setAllowMissing(false));
	}
	
	public ConfigMapParser(ConfigParseOptions options) {
		parser = config(options);
	}


	@Override
	protected Map parse(ResourceObject file) throws IOException,
			ResourceObjectException {
		Config config = file.parsedTo(parser).asIs();

		Map result = new HashMap();
		Set> entrySet = config.entrySet();
		for (Entry entry : entrySet) {
			ConfigValue value = entry.getValue();
			switch (value.valueType()) {
			case BOOLEAN:
			case NUMBER:
			case STRING:
				result.put(entry.getKey(), String.valueOf(value.unwrapped()));
				break;
			default:
				result.put(entry.getKey(), value.render());
			}

		}
		return result;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy