Java Map Replace replaceVariables(String chart, Map variables)

Here you can find the source of replaceVariables(String chart, Map variables)

Description

replace Variables

License

Mozilla Public License

Declaration

public static String replaceVariables(String chart,
            Map<String, String> variables) 

Method Source Code

//package com.java2s;
//License from project: Mozilla Public License 

import java.util.Map;

public class Main {
    public static String replaceVariables(String chart,
            Map<String, String> variables) {
        //look for special rangetype variable first
        modifyVariables(variables, true);

        //replace comments first so if there are variables in the comment, they will not be replaced since
        //they are removed from the file anyways.
        chart = chart.replaceAll("\\*\\{[\\w\\W]*?\\}\\*", "");

        for (String key : variables.keySet()) {
            String val = variables.get(key);
            chart = chart.replaceAll("\\$\\{" + key + "\\}", val);
        }/*from   www .j  a v  a  2 s . co  m*/

        return chart;
    }

    public static void modifyVariables(Map<String, String> variables,
            boolean webRequest) {
        String type = variables.get("_daterangetype");
        if (type != null) {
            String url = variables.get("url");
            String shortUrl = addCallbackParam(webRequest, url);
            variables.put("shortUrl", shortUrl);
            if ("daterange".equals(type)) {
                String from = variables.get("_fromepoch");
                String to = variables.get("_toepoch");
                url += "?start=" + from + "&end=" + to;
            } else {
                String number = variables.get("_numberpoints");
                String ending = url.substring("/api".length());
                if (ending.contains("?"))
                    url = "/api/firstvaluesV1/" + number + ending
                            + "&reverse=true";
                else
                    url = "/api/firstvaluesV1/" + number + ending
                            + "?reverse=true";
            }

            url = addCallbackParam(webRequest, url);

            variables.put("url", url);
        }
    }

    private static String addCallbackParam(boolean webRequest, String url) {
        if (webRequest) {
            //add stuff for clean json callback
            if (url.contains("?"))
                url = url + "&callback=?";
            else
                url = url + "?callback=?";
        }
        return url;
    }
}

Related

  1. replaceTokens(String inputString, Map tokenMap)
  2. replaceToStringBuilder(String s, String begin, String end, Map values)
  3. replaceValues(String s, String begin, String end, Map values)
  4. replaceValuesToSynonyms(Map dic, Map synonims)
  5. replaceVariable(final String src, final Map value)
  6. replaceVars(String origString, Map vars)
  7. replaceVars(String s, Map m)
  8. stringReplace(Map map, String line)
  9. unescape(String cooked, char escapeChar, Map replacements)