From bbdbe79929d477bd65643c841202c89401666bf8 Mon Sep 17 00:00:00 2001 From: Musachy Barroso Date: Mon, 15 Dec 2008 15:39:15 +0000 Subject: [PATCH] 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 --- .../apache/struts2/components/Component.java | 82 +++++++++++++++---- .../struts2/components/DoubleListUIBean.java | 14 +--- .../apache/struts2/components/FormButton.java | 6 +- .../org/apache/struts2/components/Label.java | 5 +- .../apache/struts2/components/ListUIBean.java | 9 +- .../apache/struts2/components/Property.java | 8 +- .../org/apache/struts2/components/UIBean.java | 13 +-- .../views/jsp/StrutsBodyTagSupport.java | 50 ++--------- 8 files changed, 86 insertions(+), 101 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/components/Component.java b/core/src/main/java/org/apache/struts2/components/Component.java index bab20fcb6..f86579f81 100644 --- a/core/src/main/java/org/apache/struts2/components/Component.java +++ b/core/src/main/java/org/apache/struts2/components/Component.java @@ -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] + *

+ * @param stack the ValueStack where the context value is searched for. + * @return true if altSyntax is activated. False otherwise. + * See struts.properties where the altSyntax flag is defined. + */ + public static boolean altSyntax(ValueStack stack) { + return ContextUtil.isUseAltSyntax(stack.getContext()); + } + /** * Is the altSyntax enabled? [TRUE] *

* See struts.properties 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. *

@@ -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); } diff --git a/core/src/main/java/org/apache/struts2/components/DoubleListUIBean.java b/core/src/main/java/org/apache/struts2/components/DoubleListUIBean.java index 50198c02e..cf1b1cf48 100644 --- a/core/src/main/java/org/apache/struts2/components/DoubleListUIBean.java +++ b/core/src/main/java/org/apache/struts2/components/DoubleListUIBean.java @@ -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 { diff --git a/core/src/main/java/org/apache/struts2/components/FormButton.java b/core/src/main/java/org/apache/struts2/components/FormButton.java index ab672c446..b5189e1ec 100644 --- a/core/src/main/java/org/apache/struts2/components/FormButton.java +++ b/core/src/main/java/org/apache/struts2/components/FormButton.java @@ -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) { diff --git a/core/src/main/java/org/apache/struts2/components/Label.java b/core/src/main/java/org/apache/struts2/components/Label.java index e35e46877..926cedbdf 100644 --- a/core/src/main/java/org/apache/struts2/components/Label.java +++ b/core/src/main/java/org/apache/struts2/components/Label.java @@ -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)); } } diff --git a/core/src/main/java/org/apache/struts2/components/ListUIBean.java b/core/src/main/java/org/apache/struts2/components/ListUIBean.java index 80c2f935e..fe61c1199 100644 --- a/core/src/main/java/org/apache/struts2/components/ListUIBean.java +++ b/core/src/main/java/org/apache/struts2/components/ListUIBean.java @@ -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"); diff --git a/core/src/main/java/org/apache/struts2/components/Property.java b/core/src/main/java/org/apache/struts2/components/Property.java index 7ab804fa8..820613513 100644 --- a/core/src/main/java/org/apache/struts2/components/Property.java +++ b/core/src/main/java/org/apache/struts2/components/Property.java @@ -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 diff --git a/core/src/main/java/org/apache/struts2/components/UIBean.java b/core/src/main/java/org/apache/struts2/components/UIBean.java index 05720134e..4f806f4fc 100644 --- a/core/src/main/java/org/apache/struts2/components/UIBean.java +++ b/core/src/main/java/org/apache/struts2/components/UIBean.java @@ -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); diff --git a/core/src/main/java/org/apache/struts2/views/jsp/StrutsBodyTagSupport.java b/core/src/main/java/org/apache/struts2/views/jsp/StrutsBodyTagSupport.java index 49ac66afb..0cc31fcbe 100644 --- a/core/src/main/java/org/apache/struts2/views/jsp/StrutsBodyTagSupport.java +++ b/core/src/main/java/org/apache/struts2/views/jsp/StrutsBodyTagSupport.java @@ -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; - } }