velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r563943 - in /velocity/tools/branches/2.x/src: main/java/org/apache/velocity/tools/generic/EscapeTool.java test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java
Date Wed, 08 Aug 2007 17:03:38 GMT
Author: nbubna
Date: Wed Aug  8 10:03:34 2007
New Revision: 563943

URL: http://svn.apache.org/viewvc?view=rev&rev=563943
Log:
add support for unicode 'escaping'

Modified:
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java
    velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java

Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java?view=diff&rev=563943&r1=563942&r2=563943
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java
(original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java
Wed Aug  8 10:03:34 2007
@@ -289,6 +289,34 @@
     }
 
     /**
+     * Converts the specified Unicode code point and/or escape sequence into
+     * the associated Unicode character.  This allows numeric
+     * code points or String versions of the numeric code point to be correctly
+     * translated within a template.  This is especially useful for those
+     * creating unicode from a reference value, or injecting a unicode character
+     * into a template with a version of Velocity prior to 1.6.
+     * @param code the code to be translated/escaped, may be null
+     * @return the unicode character for that code, {@code null} if input was null
+     * @see Character#toChars(int codePoint)
+     */
+    public String unicode(Object code)
+    {
+        if (code == null)
+        {
+            return null;
+        }
+
+        String s = String.valueOf(code);
+        if (s.startsWith("\\u"))
+        {
+            s = s.substring(2, s.length());
+        }
+        int codePoint = Integer.valueOf(s, 16);
+        return String.valueOf(Character.toChars(codePoint));
+    }
+            
+
+    /**
      * Renders a dollar sign ($).
      * @return a dollar sign ($).
      * @see #getD()

Modified: velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java?view=diff&rev=563943&r1=563942&r2=563943
==============================================================================
--- velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java
(original)
+++ velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java
Wed Aug  8 10:03:34 2007
@@ -121,6 +121,10 @@
         assertEquals("''",escapeTool.sql("'"));
         /* xml */
         assertEquals("&quot;&amp;&lt;&gt;",escapeTool.html("\"&<>"));
+        /* unicode */
+        assertEquals("\uf00b", escapeTool.unicode("f00b"));
+        assertEquals("\u1010", escapeTool.unicode("\\u1010"));
+        assertEquals("\u1111", escapeTool.unicode(1111));
     }
 
     public @Test void testMathTool() {



Mime
View raw message