Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-596

StrSubstitutor should also handle the default properties of a java.util.Properties class

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.5
    • 3.0
    • lang.text.*
    • None

    Description

      The following program show a problem with a shortcoming of the java.util.Properties class.
      The default properties are not substituted by the StrSubstitutor.

      StrSubstTest.java
      import org.apache.commons.lang.text.StrSubstitutor;
      
      public class StrSubstTest
      {
          public static void main(String[] args)
          {
              String org = "${doesnotwork}";
              System.setProperty("doesnotwork", "It work's!");
      
              // create a new Poperties object with the System.getProperties as default
              Properties props = new Properties(System.getProperties());
      
              String subst = StrSubstitutor.replace(org, props);
              // is ${doesnotwork} substituted?
              System.out.println(subst);
      
          }
      }
      

      The following method could be added to the StrSubstitutor class to fix this problem in an easy way:

          /**
           * Replaces all the occurrences of variables in the given source object with their matching
           * values from the properties.
           * 
           * @param source the source text containing the variables to substitute, null returns null
           * @param properties the properties with values, may be null
           * @return the result of the replace operation
           */
          public static String replace(Object source, Properties valueProperties)
          {
              if (valueProperties == null) {
                  return source;
              }
              Map valueMap = new HashMap();
              Enumeration propNames = valueProperties.propertyNames();
              while (propNames.hasMoreElements())
              {
                  String propName = (String)propNames.nextElement();
                  String propValue = valueProperties.getProperty(propName);
                  valueMap.put(propName, propValue);
              }
              return StrSubstitutor.replace(source, valueMap);
          }
      

      Attachments

        1. Patch_LANG-596_1.0
          3 kB
          Tomas de Priede

        Activity

          People

            Unassigned Unassigned
            boente Ulrich Voigt
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 0.5h
                0.5h
                Remaining:
                Remaining Estimate - 0.5h
                0.5h
                Logged:
                Time Spent - Not Specified
                Not Specified