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

tools.codebuilder.ConfigParser Maven / Gradle / Ivy

The newest version!
package tools.codebuilder;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.configuration2.ex.ConfigurationException;

import java.util.*;

public class ConfigParser {
    private static final String INPUT_FILE = "./src/main/java/tools/codebuilder/config.properties";
    private Set services;
    private Map> serviceProperties;
    private Map> propertyValues;

    public ConfigParser() {
        services = new HashSet<>();
        serviceProperties = new HashMap<>();
        propertyValues = new HashMap<>();
    }

    public void parseConfigFile(String filePath) {
        Configurations configs = new Configurations();
        try {
            Configuration config = configs.properties(filePath);

            // Extract services
            String serviceList = config.getString("Service");
            services.addAll(Arrays.asList(serviceList.split(",")));

            // Extract properties and service associations
            Iterator keys = config.getKeys();
            while (keys.hasNext()) {
                String key = keys.next();
                if (key.startsWith("Service.") && key.endsWith(".Properties")) {
                    String serviceName = key.split("\\.")[1];
                    String propertiesList = config.getString(key);
                    if (!propertiesList.equals("*")) {
                        serviceProperties.put(serviceName, new HashSet<>(Arrays.asList(propertiesList.split(","))));
                    } else {
                        serviceProperties.put(serviceName, new HashSet<>()); // Will be populated later
                    }
                } else if (key.startsWith("Property.")) {
                    String propertyName = key.substring("Property.".length());
                    List values = Arrays.asList(config.getString(key).split(","));
                    propertyValues.put(propertyName, values);
                }
            }

            // Populate wildcard properties
            for (Map.Entry> entry : serviceProperties.entrySet()) {
                if (entry.getValue().isEmpty()) {
                    entry.getValue().addAll(propertyValues.keySet());
                }
            }
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }

    public Set getServices() {
        return services;
    }

    public Map> getServiceProperties() {
        return serviceProperties;
    }

    public Map> getPropertyValues() {
        return propertyValues;
    }

    // Method to create nested EnumClass object
    public EnumClass createNestedEnum() {
        EnumClass rootEnum = new EnumClass("RootEnum",true);

        for (Map.Entry> entry : propertyValues.entrySet()) {
            String key = entry.getKey();
            List values = entry.getValue();

            if (values.contains("open")) {
                rootEnum.addOpenValueToNestedEnum(key);
            } else {
                rootEnum.addNestedEnum(key, new HashSet<>(values));
            }
        }

        return rootEnum;
    }

    // New method to generate service enum with associated properties as variables within the enum
    public String generateServiceEnum() {
        StringBuilder sb = new StringBuilder();
        sb.append("public enum Service {\n");

        for (String service : services) {
            sb.append("    ").append(service).append(",\n");
        }

        sb.setLength(sb.length() - 2); // Remove the last comma and newline
        sb.append(";\n\n");

        for (String service : services) {
            sb.append("    public static final Object[] ").append(service).append("_Properties = {");
            Set props = serviceProperties.get(service);
            if (props != null) {
                for (String prop : props) {
                    sb.append(prop.toUpperCase()).append(",");
                }
                sb.setLength(sb.length() - 1); // Remove the last comma
            }
            sb.append(" };\n");
        }

        sb.append("}\n");

        return sb.toString();
    }

    public static void main(String[] args) {
        ConfigParser parser = new ConfigParser();
        parser.parseConfigFile(INPUT_FILE);

        System.out.println("Services: " + parser.getServices());
        System.out.println("Service Properties: " + parser.getServiceProperties());
        System.out.println("Property Values: " + parser.getPropertyValues());

        // Create nested EnumClass object
        EnumClass nestedEnum = parser.createNestedEnum();
        System.out.println(nestedEnum.exportAsJavaEnum());

        // Generate and print service enum
        String serviceEnum = parser.generateServiceEnum();
        System.out.println(serviceEnum);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy