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

com.github.zhengframework.configuration.parser.INIConfigurationParser Maven / Gradle / Ivy

There is a newer version: 1.8.0
Show newest version
package com.github.zhengframework.configuration.parser;

import com.github.zhengframework.configuration.ex.ConfigurationSourceException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.ini4j.Ini;
import org.ini4j.Profile.Section;

public class INIConfigurationParser implements ConfigurationParser, FileConfigurationParser {

  @Override
  public Map parse(InputStream inputStream) {
    try {
      Map map = new HashMap<>();
      Ini ini = new Ini(inputStream);
      for (Section section : ini.values()) {
        for (String s : section.keySet()) {
          map.put(section.getName() + "." + s, section.fetch(s));
        }
      }
      return map;
    } catch (IOException e) {
      throw new ConfigurationSourceException("fail load configuration from inputStream", e);
    }
  }

  @Override
  public String[] supportFileTypes() {
    return new String[]{".properties"};
  }

  @Override
  public Map parse(String fileName, InputStream inputStream) {
    checkSupportFileTypes(fileName);
    try {
      Map map = new HashMap<>();
      Ini ini = new Ini(inputStream);
      for (Section section : ini.values()) {
        for (String s : section.keySet()) {
          map.put(section.getName() + "." + s, section.fetch(s));
        }
      }
      return map;
    } catch (IOException e) {
      throw new ConfigurationSourceException("fail load configuration from file: " + fileName, e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy