blob: 0f0cbaea4e3063afcd24a7ca4d9712eea38aca2a [file] [log] [blame]
package org.eclipse.epp.installer.core;
/**
* Variables is used to resolve variables in strings.
*/
public class Variables {
private static final String VARIABLE_TOKEN_BEGIN = "${";
private static final String VARIABLE_TOKEN_END = "}";
/**
* If there is a platform-dependent variable with the same name. Otherwise return value of variable in options.
*
* @param varName
* variable name.
* @param options
* InstallOptions from where variable value will be taken.
* @return variable value.
*/
private static String getString(String varName, InstallOptions options) {
String varValue = options.getString(varName);
if (varValue == null) {
varValue = Platform.getVarString(varName);
}
return varValue;
}
/**
* Returns a string where all variables are replaced with their values.
*
* @param string
* string to resolve.
* @param options
* InstallOptions from where variable values will be taken.
* @return resolved string.
*/
public static String resolve(String string, InstallOptions options) {
if (string == null) {
return null;
}
StringBuffer resultString = new StringBuffer("");
String curString = string;
while (curString.length() > 0) {
if (curString.indexOf(VARIABLE_TOKEN_BEGIN) > -1) {
int indexBegin = curString.indexOf(VARIABLE_TOKEN_BEGIN);
// append part of string before variable to result
resultString.append(curString.substring(0, indexBegin));
curString = curString.substring(indexBegin + VARIABLE_TOKEN_BEGIN.length());
if (curString.indexOf(VARIABLE_TOKEN_END) <= -1) {
break;
}
int indexEnd = curString.indexOf(VARIABLE_TOKEN_END);
String varName = curString.substring(0, indexEnd);
String varValue = getString(varName, options);
if (varValue != null) {
// append variable value to result, do recursive variable replacement
resultString.append(resolve(varValue, options));
} else {
// if not found, leave as it was
resultString.append(VARIABLE_TOKEN_BEGIN);
resultString.append(varName);
resultString.append(VARIABLE_TOKEN_END);
}
curString = curString.substring(indexEnd + VARIABLE_TOKEN_END.length());
} else {
// append rest part of string to result
resultString.append(curString);
break;
}
}
return resultString.toString();
}
}