Java Map Replace replaceProperties(final CharSequence string, final Map properties)

Here you can find the source of replaceProperties(final CharSequence string, final Map properties)

Description

replace Properties

License

Apache License

Declaration

static String replaceProperties(final CharSequence string, final Map<String, Object> properties) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Collection;

import java.util.Map;

public class Main {
    static String replaceProperties(final CharSequence string, final Map<String, Object> properties) {
        if (string == null) {
            return null;
        } else {/*from w w w. ja  v a  2  s  .  c o  m*/
            final StringBuilder buffer = new StringBuilder();
            for (int i = 0; i < string.length(); ++i) {
                char c = string.charAt(i);
                switch (c) {
                case '$':
                    ++i;
                    if (i < string.length()) {
                        c = string.charAt(i);
                        if (c == '{') {
                            ++i;
                            final StringBuilder propertyName = new StringBuilder();
                            for (; i < string.length() && c != '}'; ++i) {
                                c = string.charAt(i);
                                if (c != '}') {
                                    propertyName.append(c);
                                }
                            }
                            Object value = null;
                            if (propertyName.length() > 0) {
                                value = properties.get(propertyName.toString());

                            }
                            if (value == null) {
                                buffer.append("${");
                                buffer.append(propertyName);
                                buffer.append("}");
                            } else {
                                buffer.append(value);
                            }
                        }
                    }
                    break;

                default:
                    buffer.append(c);
                    break;
                }
            }
            return buffer.toString();
        }
    }

    static <T> T get(final Collection<T> collection, final int index) {
        if (collection != null) {
            int i = 0;
            for (final T object : collection) {
                if (i == index) {
                    return object;
                } else {
                    i++;
                }
            }
        }
        return null;
    }
}

Related

  1. replaceParameters(String query, Map parameters)
  2. replaceParameters(String str, Map parameters)
  3. replaceParams(String str, Map map)
  4. replacePlaceholders(String script, Map configurations)
  5. replacePlaceholders(String string, Map values)
  6. replaceQueryParameterInUrl(String fullUrl, Map newParameter)
  7. replaceSeparator(String value, String separator, Map map)
  8. replaceString(Map bodyToReplace, String replacingContent)
  9. replaceString(String source, Map args)