下面列出了org.apache.log4j.helpers.OptionConverter#substVars ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public
void varSubstTest1() {
String r;
r = OptionConverter.substVars("hello world.", null);
assertEquals("hello world.", r);
r = OptionConverter.substVars("hello ${TOTO} world.", null);
assertEquals("hello wonderful world.", r);
}
public
void varSubstTest2() {
String r;
r = OptionConverter.substVars("Test2 ${key1} mid ${key2} end.", null);
assertEquals("Test2 value1 mid value2 end.", r);
}
public
void varSubstTest3() {
String r;
r = OptionConverter.substVars(
"Test3 ${unset} mid ${key1} end.", null);
assertEquals("Test3 mid value1 end.", r);
}
public
void varSubstTest4() {
String res;
String val = "Test4 ${incomplete ";
try {
res = OptionConverter.substVars(val, null);
}
catch(IllegalArgumentException e) {
String errorMsg = e.getMessage();
//System.out.println('['+errorMsg+']');
assertEquals('"'+val
+ "\" has no closing brace. Opening brace at position 6.",
errorMsg);
}
}
public
void varSubstTest5() {
Properties props = new Properties();
props.put("p1", "x1");
props.put("p2", "${p1}");
String res = OptionConverter.substVars("${p2}", props);
System.out.println("Result is ["+res+"].");
assertEquals(res, "x1");
}
/**
* Substitutes property value for any references in expression.
*
* @param value value from configuration file, may contain
* literal text, property references or both
* @param props properties.
* @return evaluated expression, may still contain expressions
* if unable to expand.
* @since 1.2.15
*/
public static String subst(final String value, final Properties props) {
try {
return OptionConverter.substVars(value, props);
} catch (IllegalArgumentException e) {
LogLog.warn("Could not perform variable substitution.", e);
return value;
}
}
/**
* Substitutes property value for any references in expression.
*
* @param value value from configuration file, may contain
* literal text, property references or both
* @param props properties.
* @return evaluated expression, may still contain expressions
* if unable to expand.
*/
public String subst(final String value, final Properties props) {
try {
return OptionConverter.substVars(value, props);
} catch (IllegalArgumentException e) {
LOGGER.warn("Could not perform variable substitution.", e);
return value;
}
}
/**
* Substitutes property value for any references in expression.
*
* @param value value from configuration file, may contain
* literal text, property references or both
* @param props properties.
* @return evaluated expression, may still contain expressions
* if unable to expand.
*/
public static String subst(final String value, final Properties props) {
try {
return OptionConverter.substVars(value, props);
} catch (IllegalArgumentException e) {
LOGGER.warn("Could not perform variable substitution.", e);
return value;
}
}