You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ma...@apache.org on 2008/11/26 08:32:47 UTC

svn commit: r720758 - in /myfaces/trinidad/trunk_1.2.x: trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/ trinidad-api/src/test/java/org/apache/myfaces/trinidad/util/ trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/

Author: matzew
Date: Tue Nov 25 23:32:47 2008
New Revision: 720758

URL: http://svn.apache.org/viewvc?rev=720758&view=rev
Log:
TRINIDAD-1231 - Server and client message formatters should be consistent with each other.

Thanks to Cale Scholl for his patch

Added:
    myfaces/trinidad/trunk_1.2.x/trinidad-api/src/test/java/org/apache/myfaces/trinidad/util/FastMessageFormatTest.java
Modified:
    myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/FastMessageFormat.java
    myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/MessageFactory.java
    myfaces/trinidad/trunk_1.2.x/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/Locale.js

Modified: myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/FastMessageFormat.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/FastMessageFormat.java?rev=720758&r1=720757&r2=720758&view=diff
==============================================================================
--- myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/FastMessageFormat.java (original)
+++ myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/FastMessageFormat.java Tue Nov 25 23:32:47 2008
@@ -58,19 +58,20 @@
 
 
   /**
-   * Formats the given array of strings based on the initial
-   * pattern.   It is legal for this array to be shorter
-   * than that indicated by the pattern, or to have null
-   * entries - these will simply be ignored.
+   * This formatter will only replace patterns of the type "{[0-9]}"
+   * for which there is an associated token.
+   * Any other use of '{}' will be interpreted as literal text.
+   * This aims to have the same behavior as TrFastMessageFormatUtils.format
+   * on the client.
    * <p>
-   * @param source an array of strings
+   * @param source an array of strings (tokens)
    */
   public String format(Object[] source)
   {
     int formatLength = _formatText.length;
     int length = 0;
-    int sourceCount = source.length;
-    for (int i = 0; i < sourceCount; i++)
+    int tokenCount = source.length;
+    for (int i = 0; i < tokenCount; i++)
     {
       Object sourceString = source[i];
       if (sourceString != null)
@@ -79,87 +80,34 @@
       }
     }
 
-    StringBuffer buffer = new StringBuffer(length + formatLength);
+    // The following buffer size is just an initial estimate. It is legal for
+    // any given pattern, such as {0}, to occur more than once, in which case
+    // the buffer size will expand automatically if need be.
+    StringBuilder buffer = new StringBuilder(length + formatLength);
 
     int lastStart = 0;
-    boolean inQuote = false;
     for (int i = 0; i < formatLength; i++)
     {
       char ch = _formatText[i];
-      if (inQuote)
+      if (ch == '{')
       {
-        if (ch == '\'')
+        // Only check for single digit patterns that have an associated token.
+        if (i + 2 < formatLength && _formatText[i + 2] == '}')
         {
-          buffer.append(_formatText, lastStart, i - lastStart);
-          i++;
-          lastStart = i;
-          inQuote = false;
-        }
-      }
-      else
-      {
-        if (ch == '\'')
-        {
-          buffer.append(_formatText, lastStart, i - lastStart);
-          i++;
-          lastStart = i;
-
-          // Check for doubled-up quotes
-          if ((i < formatLength) && (_formatText[i] == '\''))
-          {
-            // Do nothing;  we'll add the doubled-up quote later
-            ;
-          }
-          else
-          {
-            inQuote = true;
-          }
-        }
-        else if (ch == '{')
-        {
-          buffer.append(_formatText, lastStart, i - lastStart);
-
-          int sourceIndex = 0;
-          int j = i + 1;
-          for (; j < formatLength; j++)
+          int tokenIndex = _formatText[i + 1] - '0';
+          if (tokenIndex >= 0 && tokenIndex < tokenCount)
           {
-            char patternChar = _formatText[j];
-            if (patternChar == '}')
-            {
-              break;
-            }
-            else
-            {
-              if ((patternChar < '0') ||
-                  (patternChar > '9'))
-                throw new IllegalArgumentException(_LOG.getLogger().getResourceBundle().getString(
-                  "FASTMESSAGEFORMAT_ONLY_SUPPORT_NUMERIC_ARGUMENTS"));
-              sourceIndex = (sourceIndex * 10) + (patternChar - '0');
-            }
-          }
-
-          if (j == formatLength)
-            throw new IllegalArgumentException(_LOG.getLogger().getResourceBundle().getString(
-              "END_OF_PATTERN_NOT_FOUND"));
-          if (j == i + 1)
-            throw new IllegalArgumentException(_LOG.getLogger().getResourceBundle().getString(
-              "FASTMESSAGEFORMAT_FIND_EMPTY_ARGUMENT"));
-          if (sourceIndex < sourceCount)
-          {
-            Object sourceString = source[sourceIndex];
+            buffer.append(_formatText, lastStart, i - lastStart);
+            Object sourceString = source[tokenIndex];
             if (sourceString != null)
               buffer.append(sourceString.toString());
+
+            i += 2;
+            lastStart = i + 1;
           }
-          
-          i = j;
-          lastStart = i + 1;
-        }
-        else
-        {
-           // Do nothing.  The character will be added in later
-          ;
         }
       }
+      // ELSE: Do nothing. The character will be added in later.
     }
 
     buffer.append(_formatText, lastStart, formatLength - lastStart);

Modified: myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/MessageFactory.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/MessageFactory.java?rev=720758&r1=720757&r2=720758&view=diff
==============================================================================
--- myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/MessageFactory.java (original)
+++ myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/MessageFactory.java Tue Nov 25 23:32:47 2008
@@ -631,7 +631,10 @@
         
         // Since that string will get parsed by FastMessageFormat, the { } 
         // of the EL must be escaped
-        detailMsgPattern = '\'' + detailMsgPattern + '\'';
+        //detailMsgPattern = '\'' + detailMsgPattern + '\'';
+        
+        // No need to format this string, just return it here.
+        return detailMsgPattern;
       }
       
       Object[] params = super.getParameters();

Added: myfaces/trinidad/trunk_1.2.x/trinidad-api/src/test/java/org/apache/myfaces/trinidad/util/FastMessageFormatTest.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk_1.2.x/trinidad-api/src/test/java/org/apache/myfaces/trinidad/util/FastMessageFormatTest.java?rev=720758&view=auto
==============================================================================
--- myfaces/trinidad/trunk_1.2.x/trinidad-api/src/test/java/org/apache/myfaces/trinidad/util/FastMessageFormatTest.java (added)
+++ myfaces/trinidad/trunk_1.2.x/trinidad-api/src/test/java/org/apache/myfaces/trinidad/util/FastMessageFormatTest.java Tue Nov 25 23:32:47 2008
@@ -0,0 +1,59 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  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.
+ */
+package org.apache.myfaces.trinidad.util;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.myfaces.trinidad.util.FastMessageFormat;
+
+public class FastMessageFormatTest
+  extends TestCase
+{
+  public static final Test suite()
+  {
+    return new TestSuite(FastMessageFormatTest.class);
+  }
+
+  public static void main(String[] args)
+    throws Throwable
+  {
+    junit.textui.TestRunner.run(suite());
+  }
+
+  public FastMessageFormatTest(String testName)
+  {
+    super(testName);
+  }
+
+  public void testGet()
+  {
+    // {0} and {1} should be replaced.
+    // Param for {2} is null, so remove {2}.
+    // The rest is interpreted literally.
+    // Expected result: "beef {{3} isn't {} {a} {12a}kosher {"
+    FastMessageFormat fmf =
+      new FastMessageFormat("{0} {{3} isn't {} {a} {12a}{2}{1} {");
+    String[] params = { "beef", "kosher", null };
+    String result = fmf.format(params);
+    assertEquals(result, "beef {{3} isn't {} {a} {12a}kosher {");
+  }
+}
+

Modified: myfaces/trinidad/trunk_1.2.x/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/Locale.js
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk_1.2.x/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/Locale.js?rev=720758&r1=720757&r2=720758&view=diff
==============================================================================
--- myfaces/trinidad/trunk_1.2.x/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/Locale.js (original)
+++ myfaces/trinidad/trunk_1.2.x/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/Locale.js Tue Nov 25 23:32:47 2008
@@ -824,8 +824,11 @@
 var TrFastMessageFormatUtils = new Object();
 
  /**
-  * Formats the given array of strings based on the initial
-  * pattern.  
+  * This formatter will only replace patterns of the type "{[0-9]}" 
+  * for which there is an associated token.
+  * Any other use of '{}' will be interpreted as literal text.
+  * This aims to have the same behavior as FastMessageFormat.format 
+  * on the server.
   * @param {String} String to format
   * @param {any...:undefined} Varargs objects to substitute for positional parameters.
   * Each parameter will be converted to a String and substituted into the format.
@@ -835,17 +838,50 @@
   parameters    // {any...:undefined} Varargs objects to substitute for positional parameters.
   )
 {
-  // I need to create an array here because I have to strip the first arg
-  var tempArray = new Array();
+  // There are arguments.length - 1 tokens:
+  // arguments[1], ..., arguments[arguments.length-1]
+  var formatLength = formatString.length;
+  var tokenCount = arguments.length - 1;
   
-  for ( var i = 1; i < arguments.length; i++)
+  // Use the javascript StringBuffer technique.
+  var buffer = [];
+
+  var lastStart = 0;
+  for (var i = 0; i < formatLength; i++)
   {
-    tempArray[i -1] = arguments[i];
+    var ch = formatString[i];
+    if (ch == '{')
+    {
+      // Only check for single digit patterns that have an associated token.
+      if (i + 2 < formatLength && formatString[i+2] == '}')
+      {
+        var tokenIndex = formatString[i+1] - '0';
+        if (tokenIndex >= 0 && tokenIndex < tokenCount)
+        {            
+          // Use the javascript StringBuffer technique for append(string)
+          var substr = formatString.substring(lastStart, i);
+          buffer.push(substr);
+          
+          var token = arguments[tokenIndex+1];
+          if (token != null)
+            buffer.push(token);
+
+          i += 2;
+          lastStart = i + 1;
+        }
+      }
+    }
+    // ELSE: Do nothing. The character will be added in later.
   }
   
-  // TODO - move the code of the function below into here after 
-  //        simplifying it
-  return _formatErrorString(formatString, tempArray);
+  if (lastStart < formatLength)
+  {
+    var substr = formatString.substring(lastStart);
+    buffer.push(substr);
+  }
+
+  // Use the javascript StringBuffer technique for toString()
+  return buffer.join("");
 }
 
 var TrMessageFactory = new Object();
@@ -938,4 +974,4 @@
     message = TrFastMessageFormatUtils.format(customMessage, param1, param2);
   }
   return message;
-}
\ No newline at end of file
+}