struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1381599 - in /struts/struts2/trunk: core/src/main/java/org/apache/struts2/components/ core/src/main/java/org/apache/struts2/util/ core/src/main/java/org/apache/struts2/views/jsp/ core/src/main/java/org/apache/struts2/views/jsp/ui/ core/src...
Date Thu, 06 Sep 2012 13:44:34 GMT
Author: lukaszlenart
Date: Thu Sep  6 13:44:33 2012
New Revision: 1381599

URL: http://svn.apache.org/viewvc?rev=1381599&view=rev
Log:
WW-3875 extracts static util methods into separated class

Added:
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/ComponentUtils.java
    struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/ComponentUtilsTest.java
Modified:
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Component.java
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/StrutsBodyTagSupport.java
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/ui/AbstractUITag.java
    struts/struts2/trunk/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AbstractTagHandler.java

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Component.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Component.java?rev=1381599&r1=1381598&r2=1381599&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Component.java (original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Component.java Thu
Sep  6 13:44:33 2012
@@ -28,9 +28,9 @@ import org.apache.struts2.StrutsConstant
 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("}");
-    }
-
 }

Added: struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/ComponentUtils.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/ComponentUtils.java?rev=1381599&view=auto
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/ComponentUtils.java (added)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/ComponentUtils.java Thu
Sep  6 13:44:33 2012
@@ -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("}");
+    }
+
+}

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/StrutsBodyTagSupport.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/StrutsBodyTagSupport.java?rev=1381599&r1=1381598&r2=1381599&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/StrutsBodyTagSupport.java
(original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/StrutsBodyTagSupport.java
Thu Sep  6 13:44:33 2012
@@ -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 extend
     }
 
     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);
         }

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/ui/AbstractUITag.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/ui/AbstractUITag.java?rev=1381599&r1=1381598&r2=1381599&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/ui/AbstractUITag.java
(original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/ui/AbstractUITag.java
Thu Sep  6 13:44:33 2012
@@ -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 exte
     }
 
     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);

Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/ComponentUtilsTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/ComponentUtilsTest.java?rev=1381599&view=auto
==============================================================================
--- struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/ComponentUtilsTest.java
(added)
+++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/ComponentUtilsTest.java
Thu Sep  6 13:44:33 2012
@@ -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");
+    }
+}

Modified: struts/struts2/trunk/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AbstractTagHandler.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AbstractTagHandler.java?rev=1381599&r1=1381598&r2=1381599&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AbstractTagHandler.java
(original)
+++ struts/struts2/trunk/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AbstractTagHandler.java
Thu Sep  6 13:44:33 2012
@@ -23,7 +23,7 @@ package org.apache.struts2.views.java.si
 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
         }
 
         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
         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);
         }
     }
 }



Mime
View raw message