Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Expand environment variables in text (bash style)
- * @author gocha <http://twitter.com/gochaism> (I release it into public domain, though)
- */
- import java.util.Map;
- import java.util.regex.Pattern;
- import java.util.regex.Matcher;
- import java.util.regex.MatchResult;
- public class VariablePath
- {
- /**
- * Expand environment variables in text
- * @param path input path string
- * @param userVariableMap user-defined variable name/value map (null if not needed)
- * @param useEnvironmentVariables true if expand environment variables
- * @param throwException true if throw Exception if a variable is not defined
- * @return expanded path string
- * @throws IllegalArgumentException if a variable is not defined
- */
- private static String expand(String path, Map<String, String> userVariableMap, boolean useEnvironmentVariables, boolean throwException) throws IllegalArgumentException
- {
- Pattern variablePattern = Pattern.compile("\\$\\{(.*)\\}|\\$([0-9]+|[A-Za-z_][0-9A-Za-z_]*)");
- Matcher variableMatcher = variablePattern.matcher(path);
- StringBuffer pathBuffer = new StringBuffer();
- int lastMatchEnd = 0;
- while (variableMatcher.find())
- {
- // get variable name
- MatchResult matchResult = variableMatcher.toMatchResult();
- boolean variableHasPathensis = (matchResult.group(1) != null);
- String variableName = (variableHasPathensis ? matchResult.group(1) : matchResult.group(2));
- String variableContent = null;
- // get environment variable
- if (useEnvironmentVariables)
- {
- variableContent = System.getenv(variableName);
- }
- // if not available, get user-defined variable
- if (variableContent == null && userVariableMap != null)
- {
- variableContent = userVariableMap.get(variableName);
- }
- // if the variable is not defined
- if (variableContent == null)
- {
- if (throwException)
- {
- // throw Exception if needed
- throw new IllegalArgumentException(path.substring(matchResult.start(), matchResult.end()) + " is not defined.");
- }
- else
- {
- // otherwise, use empty string
- variableContent = "";
- }
- }
- // put the value, and a part of string just before the variable
- pathBuffer.append(path.substring(lastMatchEnd, matchResult.start()));
- pathBuffer.append(variableContent);
- lastMatchEnd = matchResult.end();
- }
- // put the rest, and return the result
- pathBuffer.append(path.substring(lastMatchEnd, path.length()));
- return pathBuffer.toString();
- }
- /**
- * Expand environment variables in text
- * @param path input path string
- * @return expanded path string
- */
- public static String expand(String path)
- {
- return expand(path, null, true, false);
- }
- /**
- * VariablePath test method
- * @param args path string
- */
- public static void main(String[] args)
- {
- if (args.length > 0)
- {
- try
- {
- String path = args[0];
- System.out.println("Input : " + path);
- System.out.println("Output: " + VariablePath.expand(path));
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- else
- {
- System.out.println("Usage: VariablePath PathString");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement