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

com.aegisql.conveyor.config.TemplateEditor Maven / Gradle / Ivy

The newest version!
package com.aegisql.conveyor.config;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TemplateEditor {

    private final Map map = new HashMap<>();
    private final static Pattern PATTERN = Pattern.compile("\\$\\{.*?}");

    private void setEnvVariables() {
        Map env = System.getenv();
        map.putAll(env);
        Properties p = System.getProperties();
        p.forEach((key,value)-> map.put(""+key,""+value));
    }

    private String extractDefaultValue(String match) {
        String[] split = match.split(":");
        StringBuilder defaultVal = new StringBuilder(split[1]);
        if(split.length > 2) {
            for(int i = 2; i < split.length; i++) {
                defaultVal.append(":").append(split[i]);
            }
        }
        return defaultVal.toString();
    }

    public TemplateEditor() {
    }

    public String setVariables(String label, String value) {
        map.put(label,value);
        setEnvVariables();
        Objects.requireNonNull(value,"Property with name '"+label+"' is null");
        Matcher m = PATTERN.matcher(value);
        Map allMatches = new LinkedHashMap<>();
        while (m.find()) {
            String grouped = m.group();
            String match = grouped.substring(2, grouped.length() - 1);
            if(match.contains(":")){
                String[] split = match.split(":");
                allMatches.put(split[0],match);
            } else {
                allMatches.put(match,null);
            }
        }

        if( ! allMatches.isEmpty()) {
            String result = value;
            for(Map.Entry matchEntry : allMatches.entrySet()) {
                String matchKey = matchEntry.getKey();
                String fullMatch = matchEntry.getValue();
                if(! map.containsKey(matchKey) && fullMatch == null) {
                    throw new ConveyorConfigurationException("Expected property with name '"+matchKey+"'");
                }
                String property = map.get(matchKey);
                if(property == null && fullMatch != null) {
                    property = extractDefaultValue(matchEntry.getValue());
                }
                Objects.requireNonNull(property,"Property with name '"+matchKey+"' is null");
                String pattern = fullMatch == null ? "${"+matchKey+"}":"${"+fullMatch+"}";
                result = result.replace(pattern, property);
                map.put(label,result);
            }
            return result;
        } else {
            return value;
        }
    }

    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy