Utility class for text parsing.
| Method from com.opensymphony.xwork2.util.TextParseUtil Detail: |
public static Set commaDelimitedStringToSet(String s) {
Set set = new HashSet();
String[] split = s.split(",");
for (int i = 0; i < split.length; i++) {
String trimmed = split[i].trim();
if (trimmed.length() > 0)
set.add(trimmed);
}
return set;
}
Returns a set from comma delimted Strings. |
public static String translateVariables(String expression,
ValueStack stack) {
return translateVariables('$", expression, stack, String.class, null).toString();
}
Converts all instances of ${...} in expression to the value returned
by a call to ValueStack#findValue(java.lang.String) . If an item cannot
be found on the stack (null is returned), then the entire variable ${...} is not
displayed, just as if the item was on the stack but returned an empty string. |
public static String translateVariables(String expression,
ValueStack stack,
TextParseUtil.ParsedValueEvaluator evaluator) {
return translateVariables('$", expression, stack, String.class, evaluator).toString();
}
Function similarly as #translateVariables(char, String, ValueStack)
except for the introduction of an additional evaluator that allows
the parsed value to be evaluated by the evaluator. The evaluator
could be null, if it is it will just be skipped as if it is just calling
#translateVariables(char, String, ValueStack) .
A typical use-case would be when we need to URL Encode the parsed value. To do so
we could just supply a URLEncodingEvaluator for example. |
public static String translateVariables(char open,
String expression,
ValueStack stack) {
return translateVariables(open, expression, stack, String.class, null).toString();
}
Converts all instances of ${...} in expression to the value returned
by a call to ValueStack#findValue(java.lang.String) . If an item cannot
be found on the stack (null is returned), then the entire variable ${...} is not
displayed, just as if the item was on the stack but returned an empty string. |
public static Object translateVariables(char open,
String expression,
ValueStack stack,
Class asType) {
return translateVariables(open, expression, stack, asType, null);
}
Converted object from variable translation. |
public static Object translateVariables(char open,
String expression,
ValueStack stack,
Class asType,
TextParseUtil.ParsedValueEvaluator evaluator) {
return translateVariables(open, expression, stack, asType, evaluator, MAX_RECURSION);
}
Converted object from variable translation. |
public static Object translateVariables(char open,
String expression,
ValueStack stack,
Class asType,
TextParseUtil.ParsedValueEvaluator evaluator,
int maxLoopCount) {
// deal with the "pure" expressions first!
//expression = expression.trim();
Object result = expression;
int loopCount = 1;
int pos = 0;
while (true) {
int start = expression.indexOf(open + "{", pos);
if (start == -1) {
pos = 0;
loopCount++;
start = expression.indexOf(open + "{");
}
if (loopCount > maxLoopCount) {
// translateVariables prevent infinite loop / expression recursive evaluation
break;
}
int length = expression.length();
int x = start + 2;
int end;
char c;
int count = 1;
while (start != -1 && x < length && count != 0) {
c = expression.charAt(x++);
if (c == '{") {
count++;
} else if (c == '}") {
count--;
}
}
end = x - 1;
if ((start != -1) && (end != -1) && (count == 0)) {
String var = expression.substring(start + 2, end);
Object o = stack.findValue(var, asType);
if (evaluator != null) {
o = evaluator.evaluate(o);
}
String left = expression.substring(0, start);
String right = expression.substring(end + 1);
String middle = null;
if (o != null) {
middle = o.toString();
if (!TextUtils.stringSet(left)) {
result = o;
} else {
result = left + middle;
}
if (TextUtils.stringSet(right)) {
result = result + right;
}
expression = left + middle + right;
} else {
// the variable doesn't exist, so don't display anything
result = left + right;
expression = left + right;
}
pos = (left != null && left.length() > 0 ? left.length() - 1: 0) +
(middle != null && middle.length() > 0 ? middle.length() - 1: 0) +
1;
pos = Math.max(pos, 1);
} else {
break;
}
}
XWorkConverter conv = ((Container)stack.getContext().get(ActionContext.CONTAINER)).getInstance(XWorkConverter.class);
return conv.convertValue(stack.getContext(), result, asType);
}
Converted object from variable translation. |