001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.common.utils;
020
021import java.util.Map;
022import java.util.function.UnaryOperator;
023
024/**
025 * Yet Another variable resolver.
026 *
027 * @see TextTemplate
028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
029 */
030public class Vars {
031
032    public static String expand(String expression, final Map<?, ?> vars) {
033        int s = expression.indexOf("${", 0);
034        if (s == -1) {
035            return expression;
036        }
037        int e = expression.indexOf('}', s + 2);
038        if (e == -1) {
039            return expression;
040        }
041        return expand(expression, 0, s, e, key -> {
042            Object v = vars.get(key);
043            return v != null ? v.toString() : null;
044        });
045    }
046
047    private static String expand(String expression, int offset, int s, int e, UnaryOperator<String> resolver) {
048        StringBuilder sb = new StringBuilder();
049
050        do {
051            if (s > offset) {
052                sb.append(expression.substring(offset, s));
053            }
054            String v = resolveVar(expression.substring(s + 2, e), resolver);
055            if (v == null) {
056                sb.append(expression.substring(s, e + 1));
057            } else {
058                sb.append(v);
059            }
060            offset = e + 1;
061            s = expression.indexOf("${", offset);
062            if (s == -1) {
063                break;
064            }
065            e = expression.indexOf('}', s + 2);
066            if (e == -1) {
067                break;
068            }
069        } while (true);
070
071        if (offset < expression.length()) {
072            sb.append(expression.substring(offset));
073        }
074
075        return sb.toString();
076    }
077
078    private static String resolveVar(String var, UnaryOperator<String> resolver) {
079        String key = var;
080        int i = var.indexOf('?');
081        if (i > -1) {
082            key = key.substring(0, i);
083            Object v = resolver.apply(key);
084            if (v != null) {
085                return v.toString();
086            } else {
087                return var.substring(i + 1);
088            }
089        }
090        Object v = resolver.apply(key);
091        return v != null ? v.toString() : null;
092    }
093
094}