All Downloads are FREE. Search and download functionalities are using the official Maven repository.

generator.server.springboot.mvc.internationalized-errors.main.ArgumentsReplacer.mustache Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
package {{ packageName }}.shared.error.infrastructure.primary;

import java.util.Map;

final class ArgumentsReplacer {

  private static final String OPEN = "{{";
  private static final String CLOSE = "}}";

  private final Map arguments;

  private ArgumentsReplacer(Map arguments) {
    this.arguments = arguments;
  }

  public static String replaceParameters(String message, Map arguments) {
    if (message == null || arguments == null) {
      return message;
    }

    return new ArgumentsReplacer(arguments).format(message);
  }

  private String format(String message) {
    StringBuilder result = new StringBuilder(message);

    int lastMustaches = result.indexOf(OPEN);
    while (lastMustaches != -1) {
      int end = result.indexOf(CLOSE, lastMustaches);
      String key = result.substring(lastMustaches + OPEN.length(), end).trim();

      result.replace(lastMustaches, end + CLOSE.length(), getArgumentValue(key));

      lastMustaches = result.indexOf(OPEN, lastMustaches + 2);
    }

    return result.toString();
  }

  private String getArgumentValue(String key) {
    Object value = arguments.get(key);
    if (value == null) {
      return "{{ " + key + " }}";
    }

    return value.toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy