Home | History | Annotate | Download | only in util
      1 package com.xtremelabs.robolectric.util;
      2 
      3 import java.util.Enumeration;
      4 import java.util.Properties;
      5 import java.util.regex.Matcher;
      6 import java.util.regex.Pattern;
      7 
      8 public class PropertiesHelper {
      9 
     10     public static String doSingleSubstitution(String originalValue, Properties properties) {
     11         if (originalValue == null) {
     12             return null;
     13         }
     14 
     15         Pattern variablePattern = Pattern.compile("([^$]*)\\$\\{(.*?)\\}(.*)");
     16 
     17         String expandedValue = originalValue;
     18         Matcher variableMatcher = variablePattern.matcher(expandedValue);
     19         while (variableMatcher.matches()) {
     20             String propertyName = variableMatcher.group(2);
     21             String propertyValue = null;
     22             if (properties != null) {
     23                 propertyValue = properties.getProperty(propertyName);
     24             }
     25             if (propertyValue == null) {
     26                 propertyValue = System.getProperty(propertyName);
     27             }
     28             if (propertyValue == null) {
     29                 return originalValue;
     30             }
     31 
     32             String sdkPathStart = variableMatcher.group(1);
     33             String sdkPathEnd = variableMatcher.group(3);
     34             expandedValue = sdkPathStart + propertyValue + sdkPathEnd;
     35             variableMatcher = variablePattern.matcher(expandedValue);
     36         }
     37 
     38         return expandedValue;
     39     }
     40 
     41     public static void doSubstitutions(Properties properties) {
     42         Enumeration<?> propertyNames = properties.propertyNames();
     43         while (propertyNames.hasMoreElements()) {
     44             String propertyName = (String) propertyNames.nextElement();
     45             String propertyValue = properties.getProperty(propertyName);
     46             String expandedPropertyValue = doSingleSubstitution(propertyValue, properties);
     47             properties.setProperty(propertyName, expandedPropertyValue);
     48         }
     49     }
     50 }
     51