You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by ch...@apache.org on 2010/09/29 06:46:12 UTC

svn commit: r1002469 - in /shindig/trunk/java/common/src: main/java/org/apache/shindig/expressions/OpensocialFunctions.java test/java/org/apache/shindig/expressions/OpensocialFunctionsTest.java

Author: chirag
Date: Wed Sep 29 04:46:12 2010
New Revision: 1002469

URL: http://svn.apache.org/viewvc?rev=1002469&view=rev
Log:
SHINDIG-1434 | Fix default EL functions
Code Review: http://codereview.appspot.com/1885042/

Modified:
    shindig/trunk/java/common/src/main/java/org/apache/shindig/expressions/OpensocialFunctions.java
    shindig/trunk/java/common/src/test/java/org/apache/shindig/expressions/OpensocialFunctionsTest.java

Modified: shindig/trunk/java/common/src/main/java/org/apache/shindig/expressions/OpensocialFunctions.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/common/src/main/java/org/apache/shindig/expressions/OpensocialFunctions.java?rev=1002469&r1=1002468&r2=1002469&view=diff
==============================================================================
--- shindig/trunk/java/common/src/main/java/org/apache/shindig/expressions/OpensocialFunctions.java (original)
+++ shindig/trunk/java/common/src/main/java/org/apache/shindig/expressions/OpensocialFunctions.java Wed Sep 29 04:46:12 2010
@@ -19,6 +19,7 @@
 package org.apache.shindig.expressions;
 
 import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.lang.StringEscapeUtils;
 import org.apache.shindig.common.util.Utf8UrlCoder;
 
 import org.json.JSONArray;
@@ -30,7 +31,8 @@ import java.io.UnsupportedEncodingExcept
 import javax.el.ELException;
 
 /**
- * Default functions in the "os:" namespace prefix.
+ * Default functions in the OpenSocial-Templating spec are prefixed with "os:"
+ * All other functions are prefixed with "osx:"
  */
 public final class OpensocialFunctions {
   private OpensocialFunctions() {
@@ -78,7 +80,7 @@ public final class OpensocialFunctions {
   /**
    * Form encode a string
    */
-  @Functions.Expose(prefix = "osx", names = {"urlEncode"})
+  @Functions.Expose(prefix = "os", names = {"urlEncode"})
   public static String formEncode(String text) {
     if (text == null) {
       return null;
@@ -92,7 +94,7 @@ public final class OpensocialFunctions {
    * @param text
    * @return
    */
-  @Functions.Expose(prefix = "osx", names = {"urlDecode"})
+  @Functions.Expose(prefix = "os", names = {"urlDecode"})
   public static String formDecode(String text) {
     if (text == null) {
       return null;
@@ -100,4 +102,30 @@ public final class OpensocialFunctions {
     
     return Utf8UrlCoder.decode(text);
   }
+
+  /**
+   * Escape HTML entities in a string
+   */
+  @Functions.Expose(prefix = "os", names = {"htmlEncode"})
+  public static String htmlEncode(String text) {
+    if (text == null) {
+      return null;
+    }
+
+    return StringEscapeUtils.escapeHtml(text);
+  }
+
+  /**
+   * Unescape HTML entities in a string
+   * @param text
+   * @return
+   */
+  @Functions.Expose(prefix = "os", names = {"htmlDecode"})
+  public static String htmlDecode(String text) {
+    if (text == null) {
+      return null;
+    }
+
+    return StringEscapeUtils.unescapeHtml(text);
+  }
 }

Modified: shindig/trunk/java/common/src/test/java/org/apache/shindig/expressions/OpensocialFunctionsTest.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/common/src/test/java/org/apache/shindig/expressions/OpensocialFunctionsTest.java?rev=1002469&r1=1002468&r2=1002469&view=diff
==============================================================================
--- shindig/trunk/java/common/src/test/java/org/apache/shindig/expressions/OpensocialFunctionsTest.java (original)
+++ shindig/trunk/java/common/src/test/java/org/apache/shindig/expressions/OpensocialFunctionsTest.java Wed Sep 29 04:46:12 2010
@@ -74,7 +74,7 @@ public class OpensocialFunctionsTest ext
     vars.put("test", test);
 
     ValueExpression testUrlEncode =
-      expressions.parse("${osx:urlEncode(test)}", String.class);
+      expressions.parse("${os:urlEncode(test)}", String.class);
     assertEquals("He+He", testUrlEncode.getValue(context));
   }
 
@@ -84,11 +84,31 @@ public class OpensocialFunctionsTest ext
     vars.put("encoded", test);
 
     ValueExpression testUrlDecode =
-      expressions.parse("${osx:urlDecode(encoded)}", String.class);
+      expressions.parse("${os:urlDecode(encoded)}", String.class);
     assertEquals("He He", testUrlDecode.getValue(context));
   }
 
   @Test
+  public void testHtmlEncode() throws Exception {
+    String test = "<test>";
+    vars.put("test", test);
+
+    ValueExpression testHtmlEncode =
+      expressions.parse("${os:htmlEncode(test)}", String.class);
+    assertEquals("&lt;test&gt;", testHtmlEncode.getValue(context));
+  }
+
+  @Test
+  public void testHtmlDecode() throws Exception {
+    String test = "&lt;1+1>3&gt;";
+    vars.put("encoded", test);
+
+    ValueExpression testHtmlDecode =
+      expressions.parse("${os:htmlDecode(encoded)}", String.class);
+    assertEquals("<1+1>3>", testHtmlDecode.getValue(context));
+  }
+
+  @Test
   public void testParseJsonNull() throws Exception {
     ValueExpression testUrlEncode =
       expressions.parse("${osx:parseJson(null)}", String.class);
@@ -105,14 +125,14 @@ public class OpensocialFunctionsTest ext
   @Test
   public void testUrlEncodeNull() throws Exception {
     ValueExpression testUrlEncode =
-      expressions.parse("${osx:urlEncode(null)}", String.class);
+      expressions.parse("${os:urlEncode(null)}", String.class);
     assertEquals("", testUrlEncode.getValue(context));
   }
 
   @Test
   public void testUrlDecodeNull() throws Exception {
     ValueExpression testUrlDecode =
-      expressions.parse("${osx:urlDecode(null)}", String.class);
+      expressions.parse("${os:urlDecode(null)}", String.class);
     assertEquals("", testUrlDecode.getValue(context));
   }
 }