You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by ah...@apache.org on 2020/01/23 23:43:32 UTC

[royale-asjs] branch develop updated: implement DateFormatter

This is an automated email from the ASF dual-hosted git repository.

aharui pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git


The following commit(s) were added to refs/heads/develop by this push:
     new 462df29  implement DateFormatter
462df29 is described below

commit 462df29dbf897891acdac1a52eaebd23ddb952ff
Author: Alex Harui <ah...@apache.org>
AuthorDate: Thu Jan 23 15:43:21 2020 -0800

    implement DateFormatter
---
 .../src/main/royale/mx/formatters/DateFormatter.as |  19 +-
 .../main/royale/mx/formatters/StringFormatter.as   | 195 +++++++++++++++++++++
 2 files changed, 204 insertions(+), 10 deletions(-)

diff --git a/frameworks/projects/MXRoyale/src/main/royale/mx/formatters/DateFormatter.as b/frameworks/projects/MXRoyale/src/main/royale/mx/formatters/DateFormatter.as
index 5592d46..a4cfc19 100644
--- a/frameworks/projects/MXRoyale/src/main/royale/mx/formatters/DateFormatter.as
+++ b/frameworks/projects/MXRoyale/src/main/royale/mx/formatters/DateFormatter.as
@@ -75,9 +75,8 @@ use namespace mx_internal;
  *  @playerversion AIR 1.1
  *  @productversion Royale 0.9.3
  */
-public class DateFormatter 
+public class DateFormatter extends Formatter
 {
-/* extends Formatter */
     //include "../core/Version.as";
 
     //--------------------------------------------------------------------------
@@ -125,7 +124,7 @@ public class DateFormatter
      */
     public static function parseDateString(str:String, format:String = null):Date
     {
-        /* if (!str || str == "")
+        if (!str || str == "")
             return null;
 
         var year:int = -1;
@@ -413,7 +412,7 @@ public class DateFormatter
         if (day != newDate.getDate() || mon != newDate.getMonth())
             return null;
 
-        return newDate; */ var newDate:Date;  return newDate;
+        return newDate;
     }
 
     //--------------------------------------------------------------------------
@@ -643,12 +642,12 @@ public class DateFormatter
      */
     public function set formatString(value:String):void
     {
-        /* formatStringOverride = value;
+        //formatStringOverride = value;
 
         _formatString = value != null ?
                         value :
-                        resourceManager.getString(
-                            "SharedResources", "dateFormat"); */
+                        /*resourceManager.getString(
+                            "SharedResources", "dateFormat");*/ "MM/DD/YYYY";
     }
 
     //--------------------------------------------------------------------------
@@ -685,9 +684,9 @@ public class DateFormatter
      *  @playerversion AIR 1.1
      *  @productversion Royale 0.9.3
      */
-    /* override */ public function format(value:Object):String
+    override public function format(value:Object):String
     {       
-      /*   // Reset any previous errors.
+        // Reset any previous errors.
         if (error)
             error = null;
 
@@ -755,7 +754,7 @@ public class DateFormatter
             formatString, VALID_PATTERN_CHARS,
             DateBase.extractTokenDate);
 
-        return dataFormatter.formatValue(value); */ return "";
+        return dataFormatter.formatValue(value);
     }
 }
 
diff --git a/frameworks/projects/MXRoyale/src/main/royale/mx/formatters/StringFormatter.as b/frameworks/projects/MXRoyale/src/main/royale/mx/formatters/StringFormatter.as
new file mode 100644
index 0000000..7d25de7
--- /dev/null
+++ b/frameworks/projects/MXRoyale/src/main/royale/mx/formatters/StringFormatter.as
@@ -0,0 +1,195 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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 mx.formatters
+{
+
+[ExcludeClass]
+
+/**
+ *  @private
+ *  The StringFormatter class provides a mechanism for displaying
+ *  and saving data in the specified format.
+ *  The constructor accepts the format and an Array of tokens,
+ *  and uses these values to create the data structures to support
+ *  the formatting during data retrieval and saving. 
+ *  
+ *  <p>This class is used internally by other formatters,
+ *  and is typically not used directly.</p>
+ *  
+ *  @see mx.formatters.DateFormatter
+ */
+public class StringFormatter
+{
+//    include "../core/Version.as";
+
+	//--------------------------------------------------------------------------
+	//
+	//  Constructor
+	//
+	//--------------------------------------------------------------------------
+
+	/**
+	 *  Constructor.
+	 *
+ 	 *  @param format String that contains the desired format.
+	 *
+	 *  @param tokens String that contains the character tokens
+	 *  within the specified format String that is replaced
+	 *  during data formatting operations.
+	 *
+	 *  @param extractTokenFunc The token 
+	 *  accessor method to call when formatting for display.
+	 *  The method signature is
+	 *  value: anything, tokenInfo: {token: id, begin: start, end: finish}.
+	 *  This method must return a String representation of value
+	 *  for the specified <code>tokenInfo</code>.
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 9
+	 *  @playerversion AIR 1.1
+	 *  @productversion Flex 3
+	 */
+	public function StringFormatter(format:String, tokens:String,
+									extractTokenFunc:Function)
+	{
+		super();
+
+		formatPattern(format, tokens);
+		extractToken = extractTokenFunc;
+	}
+
+	//--------------------------------------------------------------------------
+	//
+	//  Variables
+	//
+	//--------------------------------------------------------------------------
+
+	/**
+	 *  @private
+	 */
+	private var extractToken:Function;
+
+	/**
+	 *  @private
+	 */
+	private var reqFormat:String;
+
+	/**
+	 *  @private
+	 */
+	private var patternInfo:Array;
+
+	//--------------------------------------------------------------------------
+	//
+	//  Methods
+	//
+	//--------------------------------------------------------------------------
+
+	/**
+	 *  Returns the formatted String using the format, tokens,
+	 *  and extraction function passed to the constructor.
+	 *
+	 *  @param value String to be formatted.
+	 *
+	 *  @return Formatted String.
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 9
+	 *  @playerversion AIR 1.1
+	 *  @productversion Flex 3
+	 */
+	public function formatValue(value:Object):String
+	{
+		var curTokenInfo:Object = patternInfo[0];
+		
+		var result:String = reqFormat.substring(0, curTokenInfo.begin) +
+							extractToken(value, curTokenInfo);
+
+		var lastTokenInfo:Object = curTokenInfo;
+		
+		var n:int = patternInfo.length;
+		for (var i:int = 1; i < n; i++)
+		{
+			curTokenInfo = patternInfo[i];
+			
+			result += reqFormat.substring(lastTokenInfo.end,
+										  curTokenInfo.begin) +
+					  extractToken(value, curTokenInfo);
+			
+			lastTokenInfo = curTokenInfo;
+		}
+		if (lastTokenInfo.end > 0 && lastTokenInfo.end != reqFormat.length)
+			result += reqFormat.substring(lastTokenInfo.end);
+		
+		return result;
+	}
+	
+	/**
+	 *  @private
+	 *  Formats a user-defined pattern String into a more usable object.
+	 *
+	 *  @param format String that defines the user-requested pattern.
+	 *
+	 *  @param tokens List of valid patttern characters.
+	 */
+	private function formatPattern(format:String, tokens:String):void
+	{
+		var start:int = 0;
+		var finish:int = 0;
+		var index:int = 0;
+		
+		var tokenArray:Array = tokens.split(",");
+		
+		reqFormat = format;
+		
+		patternInfo = [];
+		
+		var n:int = tokenArray.length;
+		for (var i:int = 0; i < n; i++)
+		{
+			start = reqFormat.indexOf(tokenArray[i]);
+			if (start >= 0  && start < reqFormat.length)
+			{
+				finish = reqFormat.lastIndexOf(tokenArray[i]);
+				finish = finish >= 0 ? finish + 1 : start + 1;
+				patternInfo.splice(index, 0,
+					{ token: tokenArray[i], begin: start, end: finish });
+				index++;
+			}
+		}
+		
+		patternInfo.sort(compareValues);
+	}
+
+	/**
+	 *  @private
+	 */
+	private function compareValues(a:Object, b:Object):int
+	{
+		if (a.begin > b.begin)
+			return 1;
+		else if (a.begin < b.begin)
+			return -1;
+		else
+			return 0;
+	} 
+}
+
+}