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

io.basestar.jackson.conf.SpringLikeTextProcessor Maven / Gradle / Ivy

package io.basestar.jackson.conf;

import io.basestar.util.Nullsafe;

import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpringLikeTextProcessor implements Function {

    private static final Pattern PATTERN = Pattern.compile("\\$\\{([^:}]+):([^}]+)?}");

    @Override
    public String apply(final String str) {

        final StringBuffer result = new StringBuffer();
        final Matcher matcher = PATTERN.matcher(str);
        while (matcher.find()) {
            final String key = matcher.group(1);
            final String def = matcher.group(2);
            final String rep = lookup(key, def);
            matcher.appendReplacement(result, Nullsafe.option(rep));
        }
        matcher.appendTail(result);
        return result.toString();
    }

    protected String lookup(final String key, final String defaultValue) {

        final String result = System.getenv(key);
        if(result == null) {
            return defaultValue;
        } else {
            return result;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy