Java Utililty Methods Map Substitute

List of utility methods to do Map Substitute

Description

The list of methods to do Map Substitute are organized into topic(s).

Method

StringsubstituteSql(String template, Map map)
substitute Sql
Iterator<Map.Entry<String, String>> itMap = map.entrySet().iterator();
while (itMap.hasNext()) {
    Map.Entry<String, String> entry = itMap.next();
    template = template.replace("{" + entry.getKey() + "}", "" + entry.getValue());
return template;
StringsubstituteTokens(String pattern, Map tokens)
substitute Tokens
Map tokensCopy = new HashMap(tokens);
if (tokensCopy.containsKey(ORGANISATION_KEY) && !tokensCopy.containsKey(ORGANISATION_KEY2)) {
    tokensCopy.put(ORGANISATION_KEY2, tokensCopy.get(ORGANISATION_KEY));
if (tokensCopy.containsKey(ORGANISATION_KEY) && !tokensCopy.containsKey(ORGANISATION_PATH_KEY)) {
    String org = (String) tokensCopy.get(ORGANISATION_KEY);
    tokensCopy.put(ORGANISATION_PATH_KEY, org == null ? "" : org.replace('.', '/'));
StringBuffer buffer = new StringBuffer();
char[] chars = pattern.toCharArray();
StringBuffer optionalPart = null;
StringBuffer tokenBuffer = null;
boolean insideOptionalPart = false;
boolean insideToken = false;
boolean tokenSeen = false;
boolean tokenHadValue = false;
for (int i = 0; i < chars.length; i++) {
    switch (chars[i]) {
    case '(':
        if (insideOptionalPart) {
            throw new IllegalArgumentException(
                    "invalid start of optional part at position " + i + " in pattern " + pattern);
        optionalPart = new StringBuffer();
        insideOptionalPart = true;
        tokenSeen = false;
        tokenHadValue = false;
        break;
    case ')':
        if (!insideOptionalPart || insideToken) {
            throw new IllegalArgumentException(
                    "invalid end of optional part at position " + i + " in pattern " + pattern);
        if (tokenHadValue) {
            buffer.append(optionalPart.toString());
        } else if (!tokenSeen) {
            buffer.append('(').append(optionalPart.toString()).append(')');
        insideOptionalPart = false;
        break;
    case '[':
        if (insideToken) {
            throw new IllegalArgumentException(
                    "invalid start of token at position " + i + " in pattern " + pattern);
        tokenBuffer = new StringBuffer();
        insideToken = true;
        break;
    case ']':
        if (!insideToken) {
            throw new IllegalArgumentException(
                    "invalid end of token at position " + i + " in pattern " + pattern);
        String token = tokenBuffer.toString();
        Object tokenValue = tokensCopy.get(token);
        String value = (tokenValue == null) ? null : tokenValue.toString();
        if (insideOptionalPart) {
            tokenHadValue = (value != null) && (value.length() > 0);
            optionalPart.append(value);
        } else {
            if (value == null) { 
                value = "[" + token + "]";
            buffer.append(value);
        insideToken = false;
        tokenSeen = true;
        break;
    default:
        if (insideToken) {
            tokenBuffer.append(chars[i]);
        } else if (insideOptionalPart) {
            optionalPart.append(chars[i]);
        } else {
            buffer.append(chars[i]);
        break;
if (insideToken) {
    throw new IllegalArgumentException("last token hasn't been closed in pattern " + pattern);
if (insideOptionalPart) {
    throw new IllegalArgumentException("optional part hasn't been closed in pattern " + pattern);
return buffer.toString();
StringsubstituteTokens(String pattern, Map tokens)
substitute Tokens
StringBuffer buffer = new StringBuffer();
char[] chars = pattern.toCharArray();
StringBuffer optionalPart = null;
StringBuffer tokenBuffer = null;
boolean insideOptionalPart = false;
boolean insideToken = false;
boolean tokenHadValue = false;
for (int i = 0; i < chars.length; i++) {
...
StringsubstituteUserConfiguration(String originalContent, Map paramAnchorTextAndValues)
substitute User Configuration
for (Entry<String, String> entry : paramAnchorTextAndValues.entrySet()) {
    String anchorText = entry.getKey();
    String value = entry.getValue();
    originalContent = originalContent.replace(anchorText, value);
return originalContent;