Uploaded image for project: 'Commons Text'
  1. Commons Text
  2. TEXT-74

StrSubstitutor: Ability to turn off substitution in values

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • 1.2

    Description

      StrSubstitutor replaces variables in values. And currently there's no way to turn this off.

      Why turn it off: I want to replace some variables in a simple template. Some of the replacement values are arbitrary user input.

      At the moment I escape all dollar signs in the replacement values with "$$". This is annoying. Especially as I use one template with variables as a value for another variable. Here I have to escape twice.

      Here's some example code. At the moment it prints:

      Hello Hamburg from Hamburg
      

      The commented line is my suggestion for this feature. If it works, it should print:

      Hello ${city} from Hamburg
      
      // untrusted user input
      String userInputName = "${city}";
      String userInputCity = "Hamburg";
      
      Map<String, String> valueMap = new HashMap<>();
      valueMap.put("name", userInputName);
      valueMap.put("city", userInputCity);
      
      String source = "Hello ${name} from ${city}";
      
      StrSubstitutor strSubstitutor = new StrSubstitutor(valueMap);
      // strSubstitutor.setEnableSubstitutionInValues(false);
      System.out.println(strSubstitutor.replace(source));
      

      Attachments

        Issue Links

          Activity

            People

              chtompki Rob Tompkins
              arendvr.com Arend v. Reinersdorff
              Votes:
              1 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: