Author: scottbw
Date: Tue Feb 15 14:41:17 2011
New Revision: 1070923

URL: http://svn.apache.org/viewvc?rev=1070923&view=rev
Log:
Added methods for getting unicode encoded versions of strings for presentation 
models that don't use HTML <span> tags, using the algorithm presented in the 
current W3C Widgets Interface spec (see 
http://dev.w3.org/2006/waf/widgets-api/#getting-localizable-strings-). This 
contributes to both WOOKIE-20 and WOOKIE-128

Added:
    
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/FormattingUtilsTest.java
Modified:
    
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/FormattingUtils.java

Added: 
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/FormattingUtilsTest.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/FormattingUtilsTest.java?rev=1070923&view=auto
==============================================================================
--- 
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/FormattingUtilsTest.java
 (added)
+++ 
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/FormattingUtilsTest.java
 Tue Feb 15 14:41:17 2011
@@ -0,0 +1,70 @@
+/*
+ *  Licensed 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.
+ */
+package org.apache.wookie.w3c.test;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.wookie.w3c.util.FormattingUtils;
+import org.junit.Test;
+
+/**
+ * Tests for some simple cases of encoding text containing direction 
instructions
+ * 
+ * Some of these tests are derived from examples ones used in 
http://dev.w3.org/2006/waf/widgets-api/#getting-localizable-strings-
+ * 
+ * @author [email protected]
+ *
+ */
+public class FormattingUtilsTest {
+       
+       @Test
+       public void noDirection(){
+               assertEquals("Hello World", FormattingUtils.getEncoded(null, 
"Hello World"));
+       }
+
+       @Test
+       public void ltr(){
+               assertEquals("\u202aHello World\u202c", 
FormattingUtils.getEncoded("ltr", "Hello World"));
+       }
+       
+       @Test
+       public void rtl(){
+               assertEquals("\u202bHello World\u202c", 
FormattingUtils.getEncoded("rtl", "Hello World"));
+       }
+       
+       @Test
+       public void rlo(){
+               assertEquals("\u202eHello World\u202c", 
FormattingUtils.getEncoded("rlo", "Hello World"));
+       }
+       
+       @Test
+       public void lro(){
+               assertEquals("\u202dHello World\u202c", 
FormattingUtils.getEncoded("lro", "Hello World"));
+       }
+       
+       @Test
+       public void embeddedOnly(){
+               assertEquals("\u202aGoodbye \u202bleurc\u202c World\u202c", 
FormattingUtils.getEncoded(null, "Goodbye <span dir=\"rtl\">leurc</span> 
World"));
+       }
+       
+       @Test
+       public void rtlembedded(){
+               assertEquals("\u202bGoodbye \u202bleurc\u202c World\u202c", 
FormattingUtils.getEncoded("rtl", "Goodbye <span dir=\"rtl\">leurc</span> 
World"));
+       }
+       
+       @Test
+       public void ltrembedded(){
+               assertEquals("\u202A\u202EolleH\u202C\u202C", 
FormattingUtils.getEncoded("ltr", "<span dir=\"rlo\">olleH</span>"));
+       }
+}

Modified: 
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/FormattingUtils.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/FormattingUtils.java?rev=1070923&r1=1070922&r2=1070923&view=diff
==============================================================================
--- 
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/FormattingUtils.java
 (original)
+++ 
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/FormattingUtils.java
 Tue Feb 15 14:41:17 2011
@@ -22,13 +22,27 @@ import org.apache.wookie.w3c.W3CWidget;
 /**
  * i18n formatting utilities
  * 
- * The methods in this class can be used to generate i18n strings using
+ * <p>The methods in this class can be used to obtain strings
+ * that have been processed for i18n content to be presented 
+ * in different ways, for example using CSS styling or unicode
+ * control characters</p>
+ * 
+ * <p>The <em>getFormattedXYZ</em> methods generate i18n strings using
  * CSS bidi properties for use in display. This involves inserting HTML 
- * <span> tags containing CSS styling properties for text direction
+ * &lt;span&gt; tags containing CSS styling properties for text direction</p>
+ * 
+ * <p>The <em>getEncodedXYZ</em> methods generate unicode Strings
+ * using unicode control characters, and remove any embedded &lt;span&gt; 
tags</p>
  * 
  */
 public class FormattingUtils {
        
+       public final static String LTR = "\u202a";
+       public final static String RTL = "\u202b";
+       public final static String LRO = "\u202d";
+       public final static String RLO = "\u202e";
+       public final static String END = "\u202c";
+       
        /**
         * Returns the CSS formatted i18n string for the widget name
         * @param name the Widget's Name entity
@@ -114,6 +128,37 @@ public class FormattingUtils {
                return "<span style=\"unicode-bidi:"+mode+"; 
direction:"+dir+"\">"+value+"</span>";
        }
        
+       public static String getEncoded(String dir, String value){              
+               // Encode any embedded SPAN tags into unicode control characters
+               String checkSpans = encodeSpan(value);
+               // If no changes, and no dir property, return original string 
unmodified
+               if (checkSpans.equals(value) && dir == null) return value;
+               value = checkSpans;
+               // Prepend direction control character
+               if (dir == null) dir = "ltr";
+               if (dir.equals("ltr")) dir = LTR;
+               if (dir.equals("lro")) dir = LRO;
+               if (dir.equals("rlo")) dir = RLO;
+               if (dir.equals("rtl")) dir = RTL;
+               // Append marker
+               return dir+value+END;
+       }
+       
+       /**
+        * Replace any embedded <span> tags with
+        * control characters
+        * @param value
+        * @return
+        */
+       private static String encodeSpan(String value){
+               value = value.replace("<span dir=\"ltr\">", LTR);
+               value = value.replace("<span dir=\"rtl\">", RTL);
+               value = value.replace("<span dir=\"lro\">", LRO);
+               value = value.replace("<span dir=\"rlo\">", RLO);
+               value = value.replace("</span>", END);
+               return value;
+       }
+       
        /**
         * Reformats any embedded <span dir="xyz"> tags to use
         * CSS BIDI properties


Reply via email to