Advertisement
gocha

Expand environment variables in text (bash style)

Mar 4th, 2013
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. /**
  2.  * Expand environment variables in text (bash style)
  3.  * @author gocha <http://twitter.com/gochaism> (I release it into public domain, though)
  4.  */
  5. import java.util.Map;
  6. import java.util.regex.Pattern;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.MatchResult;
  9.  
  10. public class VariablePath
  11. {
  12.     /**
  13.      * Expand environment variables in text
  14.      * @param path input path string
  15.      * @param userVariableMap user-defined variable name/value map (null if not needed)
  16.      * @param useEnvironmentVariables true if expand environment variables
  17.      * @param throwException true if throw Exception if a variable is not defined
  18.      * @return expanded path string
  19.      * @throws IllegalArgumentException if a variable is not defined
  20.      */
  21.     private static String expand(String path, Map<String, String> userVariableMap, boolean useEnvironmentVariables, boolean throwException) throws IllegalArgumentException
  22.     {
  23.         Pattern variablePattern = Pattern.compile("\\$\\{(.*)\\}|\\$([0-9]+|[A-Za-z_][0-9A-Za-z_]*)");
  24.         Matcher variableMatcher = variablePattern.matcher(path);
  25.         StringBuffer pathBuffer = new StringBuffer();
  26.         int lastMatchEnd = 0;
  27.         while (variableMatcher.find())
  28.         {
  29.             // get variable name
  30.             MatchResult matchResult = variableMatcher.toMatchResult();
  31.             boolean variableHasPathensis = (matchResult.group(1) != null);
  32.             String variableName = (variableHasPathensis ? matchResult.group(1) : matchResult.group(2));
  33.             String variableContent = null;
  34.  
  35.             // get environment variable
  36.             if (useEnvironmentVariables)
  37.             {
  38.                 variableContent = System.getenv(variableName);
  39.             }
  40.             // if not available, get user-defined variable
  41.             if (variableContent == null && userVariableMap != null)
  42.             {
  43.                 variableContent = userVariableMap.get(variableName);
  44.             }
  45.             // if the variable is not defined
  46.             if (variableContent == null)
  47.             {
  48.                 if (throwException)
  49.                 {
  50.                     // throw Exception if needed
  51.                     throw new IllegalArgumentException(path.substring(matchResult.start(), matchResult.end()) + " is not defined.");
  52.                 }
  53.                 else
  54.                 {
  55.                     // otherwise, use empty string
  56.                     variableContent = "";
  57.                 }
  58.             }
  59.  
  60.             // put the value, and a part of string just before the variable
  61.             pathBuffer.append(path.substring(lastMatchEnd, matchResult.start()));
  62.             pathBuffer.append(variableContent);
  63.             lastMatchEnd = matchResult.end();
  64.         }
  65.         // put the rest, and return the result
  66.         pathBuffer.append(path.substring(lastMatchEnd, path.length()));
  67.         return pathBuffer.toString();
  68.     }
  69.  
  70.     /**
  71.      * Expand environment variables in text
  72.      * @param path input path string
  73.      * @return expanded path string
  74.      */
  75.     public static String expand(String path)
  76.     {
  77.         return expand(path, null, true, false);
  78.     }
  79.  
  80.     /**
  81.      * VariablePath test method
  82.      * @param args path string
  83.      */
  84.     public static void main(String[] args)
  85.     {
  86.         if (args.length > 0)
  87.         {
  88.             try
  89.             {
  90.                 String path = args[0];
  91.                 System.out.println("Input : " + path);
  92.                 System.out.println("Output: " + VariablePath.expand(path));
  93.             }
  94.             catch (Exception e)
  95.             {
  96.                 e.printStackTrace();
  97.             }
  98.         }
  99.         else
  100.         {
  101.             System.out.println("Usage: VariablePath PathString");
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement