mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-3875 extracts static util methods into separated class
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1381599 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -28,9 +28,9 @@ import org.apache.struts2.StrutsConstants;
|
||||
import org.apache.struts2.StrutsException;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapper;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapping;
|
||||
import org.apache.struts2.util.ComponentUtils;
|
||||
import org.apache.struts2.util.FastByteArrayOutputStream;
|
||||
import org.apache.struts2.views.jsp.TagUtils;
|
||||
import org.apache.struts2.views.util.ContextUtil;
|
||||
import org.apache.struts2.views.util.UrlHelper;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -265,35 +265,7 @@ public class Component {
|
||||
* 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 (isExpression(expr)) {
|
||||
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());
|
||||
return ComponentUtils.stripExpressionIfAltSyntax(stack, expr);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -302,7 +274,7 @@ public class Component {
|
||||
* See <code>struts.properties</code> where the altSyntax flag is defined.
|
||||
*/
|
||||
public boolean altSyntax() {
|
||||
return altSyntax(stack);
|
||||
return ComponentUtils.altSyntax(stack);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -518,9 +490,4 @@ public class Component {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isExpression(Object value) {
|
||||
String expr = value.toString();
|
||||
return expr.startsWith("%{") && expr.endsWith("}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.apache.struts2.util;
|
||||
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import org.apache.struts2.views.util.ContextUtil;
|
||||
|
||||
/**
|
||||
* Various static methods used with components
|
||||
*/
|
||||
public class ComponentUtils {
|
||||
|
||||
/**
|
||||
* 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 (isExpression(expr)) {
|
||||
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 <code>struts.properties</code> where the altSyntax flag is defined.
|
||||
*/
|
||||
public static boolean altSyntax(ValueStack stack) {
|
||||
return ContextUtil.isUseAltSyntax(stack.getContext());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if object is expression base on altSyntax
|
||||
*
|
||||
* @param value to treat as an expression
|
||||
* @return true if it is an expression
|
||||
*/
|
||||
public static boolean isExpression(Object value) {
|
||||
String expr = value.toString();
|
||||
return expr.startsWith("%{") && expr.endsWith("}");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,7 +25,7 @@ import java.io.PrintWriter;
|
||||
|
||||
import javax.servlet.jsp.tagext.BodyTagSupport;
|
||||
|
||||
import org.apache.struts2.components.Component;
|
||||
import org.apache.struts2.util.ComponentUtils;
|
||||
import org.apache.struts2.util.FastByteArrayOutputStream;
|
||||
|
||||
import com.opensymphony.xwork2.util.TextParseUtil;
|
||||
@@ -49,17 +49,17 @@ public class StrutsBodyTagSupport extends BodyTagSupport {
|
||||
}
|
||||
|
||||
protected Object findValue(String expr) {
|
||||
expr = Component.stripExpressionIfAltSyntax(getStack(), expr);
|
||||
expr = ComponentUtils.stripExpressionIfAltSyntax(getStack(), expr);
|
||||
|
||||
return getStack().findValue(expr);
|
||||
}
|
||||
|
||||
protected Object findValue(String expr, Class toType) {
|
||||
if (Component.altSyntax(getStack()) && toType == String.class) {
|
||||
if (ComponentUtils.altSyntax(getStack()) && toType == String.class) {
|
||||
return TextParseUtil.translateVariables('%', expr, getStack());
|
||||
//return translateVariables(expr, getStack());
|
||||
} else {
|
||||
expr = Component.stripExpressionIfAltSyntax(getStack(), expr);
|
||||
expr = ComponentUtils.stripExpressionIfAltSyntax(getStack(), expr);
|
||||
|
||||
return getStack().findValue(expr, toType);
|
||||
}
|
||||
|
||||
@@ -22,8 +22,8 @@
|
||||
package org.apache.struts2.views.jsp.ui;
|
||||
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.struts2.components.Component;
|
||||
import org.apache.struts2.components.UIBean;
|
||||
import org.apache.struts2.util.ComponentUtils;
|
||||
import org.apache.struts2.views.jsp.ComponentTagSupport;
|
||||
|
||||
import javax.servlet.jsp.JspException;
|
||||
@@ -292,7 +292,7 @@ public abstract class AbstractUITag extends ComponentTagSupport implements Dynam
|
||||
}
|
||||
|
||||
public void setDynamicAttribute(String uri, String localName, Object value) throws JspException {
|
||||
if (Component.altSyntax(getStack()) && Component.isExpression(value)) {
|
||||
if (ComponentUtils.altSyntax(getStack()) && ComponentUtils.isExpression(value)) {
|
||||
dynamicAttributes.put(localName, String.valueOf(ObjectUtils.defaultIfNull(findValue(value.toString()), value)));
|
||||
} else {
|
||||
dynamicAttributes.put(localName, value);
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
package org.apache.struts2.util;
|
||||
|
||||
import com.opensymphony.xwork2.config.Configuration;
|
||||
import com.opensymphony.xwork2.config.ConfigurationException;
|
||||
import com.opensymphony.xwork2.config.ConfigurationProvider;
|
||||
import com.opensymphony.xwork2.inject.ContainerBuilder;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.util.ValueStackFactory;
|
||||
import com.opensymphony.xwork2.util.location.LocatableProperties;
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
import org.apache.struts2.StrutsTestCase;
|
||||
|
||||
public class ComponentUtilsTest extends StrutsTestCase {
|
||||
|
||||
public void testStripExpression() throws Exception {
|
||||
// given
|
||||
ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
|
||||
String anExpression = "%{foo}";
|
||||
|
||||
// when
|
||||
String actual = ComponentUtils.stripExpressionIfAltSyntax(stack, anExpression);
|
||||
|
||||
// then
|
||||
assertEquals(actual, "foo");
|
||||
}
|
||||
|
||||
public void testNoStripExpressionIfNoAltSyntax() throws Exception {
|
||||
// given
|
||||
loadConfigurationProviders(new MockConfigurationProvider());
|
||||
ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
|
||||
String anExpression = "%{foo}";
|
||||
|
||||
// when
|
||||
String actual = ComponentUtils.stripExpressionIfAltSyntax(stack, anExpression);
|
||||
|
||||
// then
|
||||
assertEquals(actual, "%{foo}");
|
||||
}
|
||||
|
||||
public void testAltSyntaxIsTrue() throws Exception {
|
||||
// given
|
||||
ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
|
||||
|
||||
// when
|
||||
boolean actual = ComponentUtils.altSyntax(stack);
|
||||
|
||||
// then
|
||||
assertTrue(actual);
|
||||
}
|
||||
|
||||
public void testAltSyntaxIsFalse() throws Exception {
|
||||
// given
|
||||
loadConfigurationProviders(new MockConfigurationProvider());
|
||||
ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
|
||||
|
||||
// when
|
||||
boolean actual = ComponentUtils.altSyntax(stack);
|
||||
|
||||
// then
|
||||
assertFalse(actual);
|
||||
}
|
||||
|
||||
public void testIsExpressionIsTrue() throws Exception {
|
||||
// given
|
||||
String anExpression = "%{foo}";
|
||||
|
||||
// when
|
||||
boolean actual = ComponentUtils.isExpression(anExpression);
|
||||
|
||||
// then
|
||||
assertTrue(actual);
|
||||
}
|
||||
|
||||
public void testIsExpressionIsFalse() throws Exception {
|
||||
// given
|
||||
String anExpression = "foo";
|
||||
|
||||
// when
|
||||
boolean actual = ComponentUtils.isExpression(anExpression);
|
||||
|
||||
// then
|
||||
assertFalse(actual);
|
||||
}
|
||||
}
|
||||
|
||||
class MockConfigurationProvider implements ConfigurationProvider {
|
||||
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
public void init(Configuration configuration) throws ConfigurationException {
|
||||
}
|
||||
|
||||
public boolean needsReload() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void loadPackages() throws ConfigurationException {
|
||||
}
|
||||
|
||||
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
|
||||
builder.constant(StrutsConstants.STRUTS_TAG_ALTSYNTAX, "false");
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -23,7 +23,7 @@ package org.apache.struts2.views.java.simple;
|
||||
import com.opensymphony.xwork2.util.TextParseUtil;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import org.apache.struts2.components.template.TemplateRenderingContext;
|
||||
import org.apache.struts2.components.Component;
|
||||
import org.apache.struts2.util.ComponentUtils;
|
||||
import org.apache.struts2.views.java.Attributes;
|
||||
import org.apache.struts2.views.java.TagHandler;
|
||||
import org.apache.struts2.views.util.ContextUtil;
|
||||
@@ -83,7 +83,7 @@ public abstract class AbstractTagHandler implements TagHandler {
|
||||
}
|
||||
|
||||
ValueStack stack = context.getStack();
|
||||
return stack.findValue(Component.stripExpressionIfAltSyntax(stack, expr));
|
||||
return stack.findValue(ComponentUtils.stripExpressionIfAltSyntax(stack, expr));
|
||||
}
|
||||
|
||||
private Object findValue(String expr, Class toType) {
|
||||
@@ -92,7 +92,7 @@ public abstract class AbstractTagHandler implements TagHandler {
|
||||
if (altSyntax && toType == String.class) {
|
||||
return TextParseUtil.translateVariables('%', expr, stack);
|
||||
} else {
|
||||
return stack.findValue(Component.stripExpressionIfAltSyntax(stack, expr), toType);
|
||||
return stack.findValue(ComponentUtils.stripExpressionIfAltSyntax(stack, expr), toType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user