Java Map Replace replace(String template, Map funcAndDic)

Here you can find the source of replace(String template, Map funcAndDic)

Description

replace

License

Open Source License

Declaration

public static String replace(String template, Map<String, String> funcAndDic) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Map;
import java.util.Map.Entry;

public class Main {
    private static final String PRE_VAR = "$Var{";

    public static String replace(String template, Map<String, String> funcAndDic) {
        if (funcAndDic == null) {
            return template;
        }//from  w ww .ja va2s. c  o  m
        for (Entry<String, String> entry : funcAndDic.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            String[] items = key.split("-");

            String replaceStr = items[1].replaceAll("\\$", "\\\\\\$").replaceAll("\\{", "\\\\\\{")
                    .replaceAll("\\}", "\\\\\\}").replaceAll("\\(", "\\\\\\(").replaceAll("\\)", "\\\\\\)");
            value = value.replaceAll("\\$", "\\\\\\$").replaceAll("\\{", "\\\\\\{").replaceAll("\\}", "\\\\\\}")
                    .replaceAll("\\(", "\\\\\\(").replaceAll("\\)", "\\\\\\)");

            if (items[1].startsWith(PRE_VAR)) {
                //System.out.println(items[1] + "+++++++++++++++++++" + replaceStr);
                template = template.replaceAll(replaceStr + "=", "");
                template = template.replaceAll(replaceStr, value);
            } else {
                template = template.replaceFirst(replaceStr, value);
            }

        }
        funcAndDic.clear();
        funcAndDic = null;

        return template;
    }
}

Related

  1. replace(String src, String prefix, String suffix, Map props)
  2. replace(String str, Map values)
  3. replace(String string, Map values)
  4. replace(String target, Map arguments)
  5. replace(String temp, Map tags)
  6. replace(String text, Map pairs)
  7. replace2(String str, Map data)
  8. replaceAll(Map globals, String value)
  9. replaceAll(String inTemplate, Map inVars)