WW-2824 refactored altSyntax expression code

thanks to Gabriel Belingueres for patch.

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@726715 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Musachy Barroso
2008-12-15 15:39:15 +00:00
parent f4d486dbd3
commit bbdbe79929
8 changed files with 86 additions and 101 deletions
@@ -243,25 +243,84 @@ public class Component {
return null;
}
if (altSyntax()) {
// does the expression start with %{ and end with }? if so, just cut it off!
if (expr.startsWith("%{") && expr.endsWith("}")) {
expr = expr.substring(2, expr.length() - 1);
}
}
expr = stripExpressionIfAltSyntax(expr);
return getStack().findValue(expr);
}
/**
* If altsyntax (%{...}) is applied, simply strip the "%{" and "}" off.
* @param expr the expression (must be not null)
* @return the stripped expression if altSyntax is enabled. Otherwise
* the parameter expression is returned as is.
*/
protected String stripExpressionIfAltSyntax(String expr) {
return stripExpressionIfAltSyntax(stack, expr);
}
/**
* If altsyntax (%{...}) is applied, simply strip the "%{" and "}" off.
* @param stack the ValueStack where the context value is searched for.
* @param expr the expression (must be not null)
* @return the stripped expression if altSyntax is enabled. Otherwise
* the parameter expression is returned as is.
*/
public static String stripExpressionIfAltSyntax(ValueStack stack, String expr) {
if (altSyntax(stack)) {
// does the expression start with %{ and end with }? if so, just cut it off!
if (expr.startsWith("%{") && expr.endsWith("}")) {
return expr.substring(2, expr.length() - 1);
}
}
return expr;
}
/**
* Is the altSyntax enabled? [TRUE]
* <p/>
* @param stack the ValueStack where the context value is searched for.
* @return true if altSyntax is activated. False otherwise.
* See <code>struts.properties</code> where the altSyntax flag is defined.
*/
public static boolean altSyntax(ValueStack stack) {
return ContextUtil.isUseAltSyntax(stack.getContext());
}
/**
* Is the altSyntax enabled? [TRUE]
* <p/>
* See <code>struts.properties</code> where the altSyntax flag is defined.
*/
public boolean altSyntax() {
return ContextUtil.isUseAltSyntax(stack.getContext());
return altSyntax(stack);
}
/**
* Adds the sorrounding %{ } to the expression for proper processing.
* @param expr the expression.
* @return the modified expression if altSyntax is enabled, or the parameter
* expression otherwise.
*/
protected String completeExpressionIfAltSyntax(String expr) {
if (altSyntax()) {
return "%{" + expr + "}";
}
return expr;
}
/**
* This check is needed for backwards compatibility with 2.1.x
* @param expr the expression.
* @return the found string if altSyntax is enabled. The parameter
* expression otherwise.
*/
protected String findStringIfAltSyntax(String expr) {
if (altSyntax()) {
return findString(expr);
}
return expr;
}
/**
* Evaluates the OGNL stack to find an Object value.
* <p/>
@@ -309,14 +368,9 @@ public class Component {
*/
protected Object findValue(String expr, Class toType) {
if (altSyntax() && toType == String.class) {
return TextParseUtil.translateVariables('%', expr, stack);
return TextParseUtil.translateVariables('%', expr, stack);
} else {
if (altSyntax()) {
// does the expression start with %{ and end with }? if so, just cut it off!
if (expr.startsWith("%{") && expr.endsWith("}")) {
expr = expr.substring(2, expr.length() - 1);
}
}
expr = stripExpressionIfAltSyntax(expr);
return getStack().findValue(expr, toType);
}
@@ -143,13 +143,7 @@ public abstract class DoubleListUIBean extends ListUIBean {
}
if (doubleListValue != null) {
if (altSyntax()) {
// the same logic as with findValue(String)
// if value start with %{ and end with }, just cut it off!
if (doubleListValue.startsWith("%{") && doubleListValue.endsWith("}")) {
doubleListValue = doubleListValue.substring(2, doubleListValue.length() - 1);
}
}
doubleListValue = stripExpressionIfAltSyntax(doubleListValue);
addParameter("doubleListValue", doubleListValue);
}else if (tmpDoubleList instanceof Map) {
@@ -186,11 +180,7 @@ public abstract class DoubleListUIBean extends ListUIBean {
Form form = (Form) findAncestor(Form.class);
if (doubleId != null) {
// this check is needed for backwards compatibility with 2.1.x
if (altSyntax()) {
addParameter("doubleId", findString(doubleId));
} else {
addParameter("doubleId", doubleId);
}
addParameter("doubleId", findStringIfAltSyntax(doubleId));
} else if (form != null) {
addParameter("doubleId", form.getParameters().get("id") + "_" +escape(this.doubleName));
} else {
@@ -111,11 +111,7 @@ public abstract class FormButton extends ClosingUIBean {
String _tmp_id = "";
if (id != null) {
// this check is needed for backwards compatibility with 2.1.x
if (altSyntax()) {
_tmp_id = findString(id);
} else {
_tmp_id = id;
}
_tmp_id = findStringIfAltSyntax(id);
}
else {
if (form != null && form.getParameters().get("id") != null) {
@@ -88,10 +88,7 @@ public class Label extends UIBean {
String providedLabel = TextProviderHelper.getText(key, key, stack);
addParameter("nameValue", providedLabel);
} else if (name != null) {
String expr = name;
if (altSyntax()) {
expr = "%{" + expr + "}";
}
String expr = completeExpressionIfAltSyntax(name);
addParameter("nameValue", findString(expr));
}
}
@@ -103,19 +103,14 @@ public abstract class ListUIBean extends UIBean {
}
if (listKey != null) {
listKey = stripExpressionIfAltSyntax(listKey);
addParameter("listKey", listKey);
} else if (value instanceof Map) {
addParameter("listKey", "key");
}
if (listValue != null) {
if (altSyntax()) {
// the same logic as with findValue(String)
// if value start with %{ and end with }, just cut it off!
if (listValue.startsWith("%{") && listValue.endsWith("}")) {
listValue = listValue.substring(2, listValue.length() - 1);
}
}
listValue = stripExpressionIfAltSyntax(listValue);
addParameter("listValue", listValue);
} else if (value instanceof Map) {
addParameter("listValue", "value");
@@ -129,12 +129,8 @@ public class Property extends Component {
if (value == null) {
value = "top";
}
else if (altSyntax()) {
// the same logic as with findValue(String)
// if value start with %{ and end with }, just cut it off!
if (value.startsWith("%{") && value.endsWith("}")) {
value = value.substring(2, value.length() - 1);
}
else {
value = stripExpressionIfAltSyntax(value);
}
// exception: don't call findString(), since we don't want the
@@ -764,10 +764,7 @@ public abstract class UIBean extends Component {
if (value != null) {
addParameter("nameValue", findValue(value, valueClazz));
} else if (name != null) {
String expr = name;
if (altSyntax()) {
expr = "%{" + expr + "}";
}
String expr = completeExpressionIfAltSyntax(name);
addParameter("nameValue", findValue(expr, valueClazz));
}
@@ -859,7 +856,7 @@ public abstract class UIBean extends Component {
evaluateExtraParams();
}
protected String escape(String name) {
protected String escape(String name) {
// escape any possible values that can make the ID painful to work with in JavaScript
if (name != null) {
return name.replaceAll("[^a-zA-Z0-9_]", "_");
@@ -966,11 +963,7 @@ public abstract class UIBean extends Component {
String tryId;
if (id != null) {
// this check is needed for backwards compatibility with 2.1.x
if (altSyntax()) {
tryId = findString(id);
} else {
tryId = id;
}
tryId = findStringIfAltSyntax(id);
} else if (form != null) {
tryId = form.getParameters().get("id") + "_"
+ escape(name != null ? findString(name) : null);
@@ -25,9 +25,10 @@ import java.io.PrintWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.struts2.components.Component;
import org.apache.struts2.util.FastByteArrayOutputStream;
import org.apache.struts2.views.util.ContextUtil;
import com.opensymphony.xwork2.util.TextParseUtil;
import com.opensymphony.xwork2.util.ValueStack;
@@ -39,10 +40,6 @@ public class StrutsBodyTagSupport extends BodyTagSupport {
private static final long serialVersionUID = -1201668454354226175L;
protected boolean altSyntax() {
return ContextUtil.isUseAltSyntax(getStack().getContext());
}
protected ValueStack getStack() {
return TagUtils.getStack(pageContext);
}
@@ -52,26 +49,17 @@ public class StrutsBodyTagSupport extends BodyTagSupport {
}
protected Object findValue(String expr) {
if (altSyntax()) {
// does the expression start with %{ and end with }? if so, just cut it off!
if (expr.startsWith("%{") && expr.endsWith("}")) {
expr = expr.substring(2, expr.length() - 1);
}
}
expr = Component.stripExpressionIfAltSyntax(getStack(), expr);
return getStack().findValue(expr);
}
protected Object findValue(String expr, Class toType) {
if (altSyntax() && toType == String.class) {
return translateVariables(expr, getStack());
if (Component.altSyntax(getStack()) && toType == String.class) {
return TextParseUtil.translateVariables('%', expr, getStack());
//return translateVariables(expr, getStack());
} else {
if (altSyntax()) {
// does the expression start with %{ and end with }? if so, just cut it off!
if (expr.startsWith("%{") && expr.endsWith("}")) {
expr = expr.substring(2, expr.length() - 1);
}
}
expr = Component.stripExpressionIfAltSyntax(getStack(), expr);
return getStack().findValue(expr, toType);
}
@@ -93,28 +81,4 @@ public class StrutsBodyTagSupport extends BodyTagSupport {
return bodyContent.getString().trim();
}
}
public static String translateVariables(String expression, ValueStack stack) {
while (true) {
int x = expression.indexOf("%{");
int y = expression.indexOf("}", x);
if ((x != -1) && (y != -1)) {
String var = expression.substring(x + 2, y);
Object o = stack.findValue(var, String.class);
if (o != null) {
expression = expression.substring(0, x) + o + expression.substring(y + 1);
} else {
// the variable doesn't exist, so don't display anything
expression = expression.substring(0, x) + expression.substring(y + 1);
}
} else {
break;
}
}
return expression;
}
}