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

com.neko233.skilltree.commons.configFile.ConfFile233 Maven / Gradle / Ivy

There is a newer version: 0.3.6
Show newest version
package com.neko233.skilltree.commons.configFile;

import com.neko233.skilltree.commons.configFile.utils.FilePahUtils;
import com.neko233.skilltree.commons.core.utils.ResourcesJdkUtils233;
import lombok.Getter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * xxx.conf 文件读取
 *
 * @author SolarisNeko on 2023-10-01
 */
public class ConfFile233 {


    public static void main(String[] args) {
        try {
            ConfFile233 confFile = new ConfFile233();
            confFile.reload(ResourcesJdkUtils233.getResourceFile("my.conf")); // 替换为您的 .conf 文件路径

            Map properties = confFile.getProperties();
            for (Map.Entry entry : properties.entrySet()) {
                System.out.println(entry.getKey() + " = " + entry.getValue());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static final Pattern PROPERTY_PATTERN = Pattern.compile("(\\w+)\\s*=\\s*(.*)");
    private static final Pattern CONTAINER_PATTERN = Pattern.compile("(\\w+)\\s*\\{");


    @Getter
    private Map properties = new HashMap<>();
    private Stack> contextStack = new Stack<>();

    public void reload(String filePath) throws IOException {
        File file = new File(filePath);
        reload(file);
    }

    private void reload(File file) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        Map currentContext = properties;

        while ((line = reader.readLine()) != null) {
            line = line.trim();

            if (line.startsWith("include")) {
                // Handle include directive
                String includePath = line.substring(8).trim();
                if (!includePath.isEmpty()) {
                    String includeReplacePath = includePath.replace("\"", "");
                    String includeRealPath = FilePahUtils.mergePathABtoB(file.getAbsolutePath(), includeReplacePath);
                    reload(includeRealPath);
                }
            } else if (line.endsWith(" {")) {
                // Start of a new container
                String containerName = line.substring(0, line.length() - 2).trim();
                Map newContext = new HashMap<>();
                currentContext.put(containerName, "");
                currentContext = newContext;
                contextStack.push(currentContext);
            } else if (line.equals("}")) {
                // End of the current container
                contextStack.pop();
                if (!contextStack.isEmpty()) {
                    currentContext = contextStack.peek();
                } else {
                    currentContext = properties;
                }
            } else {
                // Handle key-value pairs
                Matcher matcher = PROPERTY_PATTERN.matcher(line);
                if (matcher.matches()) {
                    String key = matcher.group(1);
                    String value = matcher.group(2);
                    currentContext.put(key, value);
                }
            }
        }

        reader.close();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy