Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.commons.util;
  
  import java.io.File;
  
A utility class for replacing properties in strings.

Author(s):
Jason Dillon
Scott Stark
Claudio Vesco
Adrian Brock
Dimitris Andreadis
Since:
4.2
 
 public class StringPropertyReplacer {
    private static final Log log = LogFactory.getLog(StringPropertyReplacer.class);
   
New line string constant
 
    public static final String NEWLINE = SecurityActions.getProperty("line.separator""\n");

   
File separator value
 
    private static final String FILE_SEPARATOR = .;

   
Path separator value
 
    private static final String PATH_SEPARATOR = .;

   
File separator alias
 
    private static final String FILE_SEPARATOR_ALIAS = "/";

   
Path separator alias
 
    private static final String PATH_SEPARATOR_ALIAS = ":";
 
    // States used in property parsing
    private static final int NORMAL = 0;
    private static final int SEEN_DOLLAR = 1;
    private static final int IN_BRACKET = 2;

   
Go through the input string and replace any occurance of ${p} with the System.getProperty(p) value. If there is no such property p defined, then the ${p} reference will remain unchanged.

If the property reference is of the form ${p:v} and there is no such property p, then the default value v will be returned.

If the property reference is of the form ${p1,p2} or ${p1,p2:v} then the primary and the secondary properties will be tried in turn, before returning either the unchanged input, or the default value.

The property ${/} is replaced with System.getProperty("file.separator") value and the property ${:} is replaced with System.getProperty("path.separator").

Parameters:
string - the string with possible ${} references
Returns:
the input string with all property references replaced if any. If there are no valid references the input string will be returned.
 
    public static String replaceProperties(final String string) {
       return replaceProperties(stringnull);
    }

   
Go through the input string and replace any occurance of ${p} with the props.getProperty(p) value. If there is no such property p defined, then the ${p} reference will remain unchanged.

If the property reference is of the form ${p:v} and there is no such property p, then the default value v will be returned.

If the property reference is of the form ${p1,p2} or ${p1,p2:v} then the primary and the secondary properties will be tried in turn, before returning either the unchanged input, or the default value.

The property ${/} is replaced with System.getProperty("file.separator") value and the property ${:} is replaced with System.getProperty("path.separator").

Parameters:
string - the string with possible ${} references
props - the source for ${x} property ref values, null means use System.getProperty()
Returns:
the input string with all property references replaced if any. If there are no valid references the input string will be returned.
 
    public static String replaceProperties(final String stringfinal Properties props) {
       if (string == null)
          return null;
       final char[] chars = string.toCharArray();
       StringBuilder buffer = new StringBuilder();
      boolean properties = false;
      int state = ;
      int start = 0;
      for (int i = 0; i < chars.length; ++i) {
         char c = chars[i];
         // Dollar sign outside brackets
         if (c == '$' && state != )
            state = ;
            // Open bracket immediatley after dollar
         else if (c == '{' && state == ) {
            buffer.append(string.substring(starti - 1));
            state = ;
            start = i - 1;
         }
         // No open bracket after dollar
         else if (state == )
            state = ;
            // Closed bracket after open bracket
         else if (c == '}' && state == ) {
            // No content
            if (start + 2 == i) {
               buffer.append("${}"); // REVIEW: Correct?
            } else // Collect the system property
            {
               String value;
               String key = string.substring(start + 2, i);
               // check for alias
               if (.equals(key)) {
                  value = ;
               } else if (.equals(key)) {
                  value = ;
               } else {
                  // check from the properties
                  if (props != null)
                     value = props.getProperty(key);
                  else
                     value = SecurityActions.getProperty(key);
                  if (value == null) {
                     // Check for a default value ${key:default}
                     int colon = key.indexOf(':');
                     if (colon > 0) {
                        String realKey = key.substring(0, colon);
                        if (props != null)
                           value = props.getProperty(realKey);
                        else
                           value = SecurityActions.getProperty(realKey);
                        if (value == null) {
                           // Check for a composite key, "key1,key2"
                           value = resolveCompositeKey(realKeyprops);
                           // Not a composite key either, use the specified default
                           if (value == null)
                              value = key.substring(colon + 1);
                        }
                     } else {
                        // No default, check for a composite key, "key1,key2"
                        value = resolveCompositeKey(keyprops);
                     }
                  }
               }
               if (value != null) {
                  properties = true;
                  buffer.append(value);
               } else {
                  buffer.append("${");
                  buffer.append(key);
                  buffer.append('}');
                  .propertyCouldNotBeReplaced(key);
               }
            }
            start = i + 1;
            state = ;
         }
      }
      // No properties
      if (!properties)
         return string;
      // Collect the trailing characters
      if (start != chars.length)
         buffer.append(string.substring(startchars.length));
      // Done
      return buffer.toString();
   }

   
Try to resolve a "key" from the provided properties by checking if it is actually a "key1,key2", in which case try first "key1", then "key2". If all fails, return null.

It also accepts "key1," and ",key2".

Parameters:
key the key to resolve
props the properties to use
Returns:
the resolved key or null
   private static String resolveCompositeKey(String keyProperties props) {
      String value = null;
      // Look for the comma
      int comma = key.indexOf(',');
      if (comma > -1) {
         // If we have a first part, try resolve it
         if (comma > 0) {
            // Check the first part
            String key1 = key.substring(0, comma);
            if (props != null)
               value = props.getProperty(key1);
            else
               value = SecurityActions.getProperty(key1);
         }
         // Check the second part, if there is one and first lookup failed
         if (value == null && comma < key.length() - 1) {
            String key2 = key.substring(comma + 1);
            if (props != null)
               value = props.getProperty(key2);
            else
               value = SecurityActions.getProperty(key2);
         }
      }
      // Return whatever we've found or null
      return value;
   }
New to GrepCode? Check out our FAQ X