This is an automated email from the ASF dual-hosted git repository.

amashchenko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/struts.git


The following commit(s) were added to refs/heads/master by this push:
     new cd434da  WW-4995 Add trimBody attribute to set tag
cd434da is described below

commit cd434da6f0f28153d5446e1f7849bd4740da4023
Author: JCgH4164838Gh792C124B5 
<43964333+jcgh4164838gh792c12...@users.noreply.github.com>
AuthorDate: Fri Dec 21 14:33:31 2018 -0500

    WW-4995 Add trimBody attribute to set tag
---
 .../java/org/apache/struts2/components/Set.java    |  6 ++++
 .../java/org/apache/struts2/views/jsp/SetTag.java  | 14 ++++++++
 .../org/apache/struts2/views/jsp/SetTagTest.java   | 38 ++++++++++++++++++++++
 3 files changed, 58 insertions(+)

diff --git a/core/src/main/java/org/apache/struts2/components/Set.java 
b/core/src/main/java/org/apache/struts2/components/Set.java
index 6549454..a8233b9 100644
--- a/core/src/main/java/org/apache/struts2/components/Set.java
+++ b/core/src/main/java/org/apache/struts2/components/Set.java
@@ -83,6 +83,7 @@ import com.opensymphony.xwork2.util.ValueStack;
 public class Set extends ContextBean {
     protected String scope;
     protected String value;
+    protected boolean trimBody = true;
 
     public Set(ValueStack stack) {
         super(stack);
@@ -136,6 +137,11 @@ public class Set extends ContextBean {
         this.value = value;
     }
 
+    @StrutsTagAttribute(description="Set to false to prevent the default 
whitespace-trim of this tag's body content", type="Boolean", 
defaultValue="true")
+    public void setTrimBody(boolean trimBody) {
+        this.trimBody = trimBody;
+    }
+
     @Override
     public boolean usesBody() {
         return true;
diff --git a/core/src/main/java/org/apache/struts2/views/jsp/SetTag.java 
b/core/src/main/java/org/apache/struts2/views/jsp/SetTag.java
index 161ed259..a1b04e5 100644
--- a/core/src/main/java/org/apache/struts2/views/jsp/SetTag.java
+++ b/core/src/main/java/org/apache/struts2/views/jsp/SetTag.java
@@ -35,6 +35,7 @@ public class SetTag extends ContextBeanTag {
 
     protected String scope;
     protected String value;
+    protected boolean trimBody = true;
 
     public Component getBean(ValueStack stack, HttpServletRequest req, 
HttpServletResponse res) {
         return new Set(stack);
@@ -59,4 +60,17 @@ public class SetTag extends ContextBeanTag {
     public void setValue(String value) {
         this.value = value;
     }
+
+    public void setTrimBody(boolean trimBody) {
+        this.trimBody = trimBody;
+}
+
+    @Override
+    protected String getBody() {
+        if (trimBody) {
+            return super.getBody();
+        } else {
+            return (bodyContent == null ? "" : bodyContent.getString());
+        }
+    }
 }
diff --git a/core/src/test/java/org/apache/struts2/views/jsp/SetTagTest.java 
b/core/src/test/java/org/apache/struts2/views/jsp/SetTagTest.java
index e5eba5e..16e56f2 100644
--- a/core/src/test/java/org/apache/struts2/views/jsp/SetTagTest.java
+++ b/core/src/test/java/org/apache/struts2/views/jsp/SetTagTest.java
@@ -18,6 +18,8 @@
  */
 package org.apache.struts2.views.jsp;
 
+import com.mockobjects.servlet.MockJspWriter;
+import java.io.IOException;
 import javax.servlet.jsp.JspException;
 
 
@@ -83,6 +85,42 @@ public class SetTagTest extends AbstractUITagTest {
         assertEquals(chewie, context.get("chewie"));
     }
 
+    public void testSetTrimBody() throws JspException, IOException {
+        final String beginEndSpaceString = "  Preceding and trailing spaces.  
";
+        final String trimmedBeginEndSpaceString = beginEndSpaceString.trim();
+        StrutsMockBodyContent mockBodyContent;
+
+        tag.setName("foo");
+        tag.setValue(null);
+        // Do not set any value - default for tag should be true
+        mockBodyContent = new StrutsMockBodyContent(new MockJspWriter());
+        mockBodyContent.setString(beginEndSpaceString);
+        tag.setBodyContent(mockBodyContent);
+        tag.doStartTag();
+        tag.doEndTag();
+        assertEquals(trimmedBeginEndSpaceString, context.get("foo"));
+
+        tag.setName("foo");
+        tag.setValue(null);
+        tag.setTrimBody(true);
+        mockBodyContent = new StrutsMockBodyContent(new MockJspWriter());
+        mockBodyContent.setString(beginEndSpaceString);
+        tag.setBodyContent(mockBodyContent);
+        tag.doStartTag();
+        tag.doEndTag();
+        assertEquals(trimmedBeginEndSpaceString, context.get("foo"));
+
+        tag.setName("foo");
+        tag.setValue(null);
+        tag.setTrimBody(false);
+        mockBodyContent = new StrutsMockBodyContent(new MockJspWriter());
+        mockBodyContent.setString(beginEndSpaceString);
+        tag.setBodyContent(mockBodyContent);
+        tag.doStartTag();
+        tag.doEndTag();
+        assertEquals(beginEndSpaceString, context.get("foo"));
+    }
+
     protected void setUp() throws Exception {
         super.setUp();
 

Reply via email to