roller-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snoopd...@apache.org
Subject svn commit: r1515384 - in /roller/branches/roller_5.0/weblogger-web/src: main/java/org/apache/roller/weblogger/ui/struts2/util/ test/java/org/apache/roller/weblogger/ui/struts2/ test/java/org/apache/roller/weblogger/ui/struts2/util/
Date Mon, 19 Aug 2013 12:25:38 GMT
Author: snoopdave
Date: Mon Aug 19 12:25:38 2013
New Revision: 1515384

URL: http://svn.apache.org/r1515384
Log:
Backporting fixes from trunk

Added:
    roller/branches/roller_5.0/weblogger-web/src/test/java/org/apache/roller/weblogger/ui/struts2/
    roller/branches/roller_5.0/weblogger-web/src/test/java/org/apache/roller/weblogger/ui/struts2/util/
    roller/branches/roller_5.0/weblogger-web/src/test/java/org/apache/roller/weblogger/ui/struts2/util/UIActionTest.java
Modified:
    roller/branches/roller_5.0/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/util/UIAction.java

Modified: roller/branches/roller_5.0/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/util/UIAction.java
URL: http://svn.apache.org/viewvc/roller/branches/roller_5.0/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/util/UIAction.java?rev=1515384&r1=1515383&r2=1515384&view=diff
==============================================================================
--- roller/branches/roller_5.0/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/util/UIAction.java
(original)
+++ roller/branches/roller_5.0/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/util/UIAction.java
Mon Aug 19 12:25:38 2013
@@ -19,12 +19,7 @@
 package org.apache.roller.weblogger.ui.struts2.util;
 
 import com.opensymphony.xwork2.ActionSupport;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
+import org.apache.commons.lang.StringEscapeUtils;
 import org.apache.roller.weblogger.business.UserManager;
 import org.apache.roller.weblogger.business.WebloggerFactory;
 import org.apache.roller.weblogger.config.WebloggerConfig;
@@ -37,6 +32,12 @@ import org.apache.roller.weblogger.ui.co
 import org.apache.roller.weblogger.ui.core.util.menu.MenuHelper;
 import org.apache.struts2.interceptor.RequestAware;
 
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
 
 /**
  * Extends the Struts2 ActionSupport class to add in support for handling an
@@ -71,7 +72,7 @@ public abstract class UIAction extends A
     // the name of the menu this action wants to show, or null for no menu
     protected String desiredMenu = null;
     
-    // page title
+    // page title, called by some Tiles JSPs (e.g., tiles-simplepage.jsp)
     protected String pageTitle = null;
 
     protected String salt = null;
@@ -87,6 +88,16 @@ public abstract class UIAction extends A
 	public String getSalt() {
 		return salt;
 	}
+	
+	/**
+     * Sets the salt.
+     * 
+     * @param salt
+     *            the new salt
+     */
+    public void setSalt(String salt) {
+        this.salt = salt;
+    }
     
     // default action permissions, user is required
     public boolean isUserRequired() {
@@ -147,7 +158,7 @@ public abstract class UIAction extends A
             value = WebloggerRuntimeConfig.getProperty(key);
         }
         
-        return (value == null) ? false : (new Boolean(value)).booleanValue();
+        return (value == null) ? false : Boolean.valueOf(value);
     }
     
     public int getIntProp(String key) {
@@ -157,10 +168,60 @@ public abstract class UIAction extends A
             value = WebloggerRuntimeConfig.getProperty(key);
         }
         
-        return (value == null) ? 0 : (new Integer(value)).intValue();
+        return (value == null) ? 0 : new Integer(value);
     }
-    
-    
+
+    @Override
+    public String getText(String aTextName) {
+        return super.getText(cleanText(aTextName));
+    }
+
+    @Override
+    public String getText(String aTextName, String defaultValue) {
+        return super.getText(cleanText(aTextName), cleanText(defaultValue));
+    }
+
+    @Override
+    public String getText(String aTextName, String defaultValue, String obj) {
+        return super.getText(cleanText(aTextName), cleanText(defaultValue), cleanText(obj));
+    }
+
+    @Override
+    public String getText(String aTextName, List<?> args) {
+        List<Object> cleanedArgs = new ArrayList<Object>(args.size());
+        for (Object el : args) {
+            cleanedArgs.add(el instanceof String ? cleanText((String) el) : el);
+        }
+        return super.getText(cleanText(aTextName), cleanedArgs);
+    }
+
+    @Override
+    public String getText(String key, String[] args) {
+        String[] cleanedArgs = new String[args.length];
+        for (int i = 0; i < args.length; ++i) {
+            cleanedArgs[i] = cleanText(args[i]);
+        }
+        return super.getText(cleanText(key), cleanedArgs);
+    }
+
+    @Override
+    public String getText(String aTextName, String defaultValue, List<?> args) {
+        List<Object> cleanedArgs = new ArrayList<Object>(args.size());
+        for (Object el : args) {
+            cleanedArgs.add(el instanceof String ? cleanText((String) el) : el);
+        }
+        return super.getText(cleanText(aTextName), cleanText(defaultValue), cleanedArgs);
+    }
+
+    @Override
+    public String getText(String key, String defaultValue, String[] args) {
+        String[] cleanedArgs = new String[args.length];
+        for (int i = 0; i < args.length; ++i) {
+            cleanedArgs[i] = cleanText(args[i]);
+        }
+        return super.getText(cleanText(key), cleanText(defaultValue), cleanedArgs);
+    }
+
     public void addError(String errorKey) {
         addActionError(getText(errorKey));
     }
@@ -234,7 +295,8 @@ public abstract class UIAction extends A
     }
 
     public void setPageTitle(String pageTitle) {
-        this.pageTitle = pageTitle;
+        //this.pageTitle = pageTitle;
+        // no-op as mucks up the page titles on chains etc
     }
     
     
@@ -324,5 +386,41 @@ public abstract class UIAction extends A
         
         return opts;
     }
-    
-}
+
+    private static String cleanDollarExpressions(String s) {
+        // Remove ${ } expressions; handcoded automaton
+        StringBuilder cleaned = new StringBuilder(s.length());
+        boolean skipping = false;
+        int braceDepth = 0;
+        int p = 0;
+        char prior = ' ';
+        while (p < s.length()) {
+            char c = s.charAt(p);
+            switch (c) {
+                case '{':
+                    ++braceDepth;
+                    skipping = skipping || (prior == '$');
+                    break;
+                case '}':
+                    if (braceDepth > 0) --braceDepth;
+                    break;
+                default:
+            }
+            if (!skipping) {
+                if (prior == '$') cleaned.append(prior);
+                if (c != '$')  cleaned.append(c);
+            }
+            skipping = skipping && (braceDepth > 0);
+            prior = c;
+            ++p;
+        }
+        if (prior == '$') cleaned.append(prior);  // string had final $ held in prior
+        return cleaned.toString();
+    }
+
+    public static String cleanText(String s) {
+        if (s == null || s.isEmpty()) return s;
+        // escape HTML
+        return StringEscapeUtils.escapeHtml(cleanDollarExpressions(s));
+    }
+}
\ No newline at end of file

Added: roller/branches/roller_5.0/weblogger-web/src/test/java/org/apache/roller/weblogger/ui/struts2/util/UIActionTest.java
URL: http://svn.apache.org/viewvc/roller/branches/roller_5.0/weblogger-web/src/test/java/org/apache/roller/weblogger/ui/struts2/util/UIActionTest.java?rev=1515384&view=auto
==============================================================================
--- roller/branches/roller_5.0/weblogger-web/src/test/java/org/apache/roller/weblogger/ui/struts2/util/UIActionTest.java
(added)
+++ roller/branches/roller_5.0/weblogger-web/src/test/java/org/apache/roller/weblogger/ui/struts2/util/UIActionTest.java
Mon Aug 19 12:25:38 2013
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  The ASF licenses this file to You
+ * under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.weblogger.ui.struts2.util;
+
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+public class UIActionTest extends TestCase {
+
+    public UIActionTest() {
+    }
+
+    public static Test suite() {
+        return new TestSuite(UIActionTest.class);
+    }
+
+    public void testCleanTextEmpty() {
+        assertEquals(null,UIAction.cleanText(null));
+        assertEquals("",UIAction.cleanText(""));
+    }
+
+    public void testCleanTextDollarExpressions() {
+        assertEquals(null,UIAction.cleanText(null));
+        assertEquals("",UIAction.cleanText(""));
+        assertEquals("a",UIAction.cleanText("a"));
+        assertEquals("$",UIAction.cleanText("$"));
+        assertEquals("{",UIAction.cleanText("{"));
+        assertEquals("}",UIAction.cleanText("}"));
+        assertEquals("",UIAction.cleanText("${"));
+        assertEquals("text$",UIAction.cleanText("text$"));
+        assertEquals("text something   more", UIAction.cleanText("text something ${ more
} ${ and more } more"));
+        assertEquals("text  more", UIAction.cleanText("text ${ something ${ more } ${ and
more } } more"));
+        assertEquals("text %{1} text %{2} more", UIAction.cleanText("text %{1} text${2} %{2}
more"));
+        assertEquals("already { clean }", UIAction.cleanText("already { clean }"));
+        assertEquals("$signs but not immediately followed by { braces }", UIAction.cleanText("$signs
but not immediately followed by { braces }"));
+        assertEquals("clean", UIAction.cleanText("${part { } }clean${anything}"));
+    }
+
+    public void testCleanTextHtml() {
+        assertEquals("&lt;i&gt;some text&lt;/i&gt;",UIAction.cleanText("<i>some
text</i>"));
+        assertEquals("&lt;i&gt;some &lt;/i&gt;",UIAction.cleanText("<i>some
${text}</i>"));   // combined
+    }
+
+}
\ No newline at end of file



Mime
View raw message