myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mmarinsc...@apache.org
Subject svn commit: r306498 - in /myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource: Test.java date.js
Date Thu, 06 Oct 2005 02:17:32 GMT
Author: mmarinschek
Date: Wed Oct  5 19:17:25 2005
New Revision: 306498

URL: http://svn.apache.org/viewcvs?rev=306498&view=rev
Log:
new SimpleDateFormat

Modified:
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/Test.java
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/date.js

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/Test.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/Test.java?rev=306498&r1=306497&r2=306498&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/Test.java
(original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/Test.java
Wed Oct  5 19:17:25 2005
@@ -13,6 +13,8 @@
 {
     private String pattern;
 
+    private String[] ampms = new String[]{"AM","PM"};
+
     public Test(String pattern)
     {
         this.pattern = pattern;
@@ -20,25 +22,49 @@
 
     private Date parse(String dateStr)
     {
+        Context context = handle(dateStr, null, true);
+
+        return new Date(context.year, context.month,
+                context.day,context.hour,context.min,context.sec);
+    }
+
+    private Context handle(String dateStr, Date date, boolean parse)
+    {
         int patternIndex = 0;
         int dateIndex = 0;
         boolean commentMode = false;
         char lastChar = 0;
-        char c=0;
+        char currentChar=0;
+        char nextChar=0;
         String patternSub = null;
 
-        int year;
-        int month;
-        int day;
-        int hour;
-        int min;
-        int sec;
+        Context context = new Context();
+
+        if(date != null)
+        {
+            context.year = date.getYear();
+            context.month = date.getMonth();
+            context.day = date.getDate();
+            context.hour = date.getHours();
+            context.min = date.getMinutes();
+            context.sec = date.getSeconds();
+        }
 
         while (patternIndex < pattern.length())
         {
-            c = pattern.charAt(patternIndex);
+            currentChar = pattern.charAt(patternIndex);
 
-            if (c == '\'' && lastChar!='\\')
+            if(patternIndex<(pattern.length()-1))
+            {
+                nextChar = pattern.charAt(patternIndex+1);
+            }
+            else
+            {
+                nextChar = 0;
+            }
+
+
+            if (currentChar == '\'' && lastChar!='\\')
             {
                 commentMode = !commentMode;
                 patternIndex++;
@@ -47,41 +73,46 @@
             {
                 if(!commentMode)
                 {
-                    if (c == '\\' && lastChar!='\\')
-                    {
-                        patternIndex++;
-                    }
-                    else if(c == lastChar)
-                    {
-                        patternSub+=c;
-                        patternIndex++;
-                    }
-                    else if(patternSub == null)
+                    if (currentChar == '\\' && lastChar!='\\')
                     {
-                        patternSub = "";
-                        patternSub+=c;
                         patternIndex++;
                     }
                     else
                     {
-                        /*dateStr.substring(dateIndex-patternSub.length(),dateIndex)*/
+                        if(patternSub == null)
+                            patternSub = "";
 
-                        handlePatternSub(patternSub, dateStr, patternIndex, dateIndex);
+                        patternSub+=currentChar;
 
-                        if(patternSub.equals("yyyy") || patternSub.equals("yy")
-                                || patternSub.equals("y"))
+                        if(currentChar != nextChar)
                         {
+                            handlePatternSub(context, patternSub,
+                                    dateStr, dateIndex, parse);
+
+                            patternSub = null;
 
+                            if(context.newIndex<0)
+                                break;
+
+                            dateIndex = context.newIndex;
                         }
-                        patternSub = "";
+
+                        patternIndex++;
                     }
                 }
                 else
                 {
-                    if(pattern.charAt(patternIndex)!=dateStr.charAt(dateIndex))
+                    if(parse)
+                    {
+                        if(pattern.charAt(patternIndex)!=dateStr.charAt(dateIndex))
+                        {
+                            //invalid character in dateString
+                            return null;
+                        }
+                    }
+                    else
                     {
-                        //invalid character in dateString
-                        return null;
+                        context.dateStr+=pattern.charAt(patternIndex);
                     }
 
                     patternIndex++;
@@ -89,25 +120,203 @@
                 }
             }
 
-            lastChar = c;
+            lastChar = currentChar;
         }
 
-        return null;
+        handlePatternSub(context, patternSub,
+                dateStr, dateIndex, parse);
+
+        return context;
     }
 
-    private void handlePatternSub(String patternSub, String dateStrSub,
-                                  int patternIndex, int dateIndex)
+    private Context parseString(Context context, String dateStr, int dateIndex, String[][]
strings)
     {
-        if(patternSub.equals("yyyy"))
+        for(int i=0; i<strings.length;i++)
         {
-            System.out.println(patternSub);
+            String currentStrings[] = strings[0];
+
+            for (int j = 0; j < currentStrings.length; j++)
+            {
+                String currentString = currentStrings[j];
+
+                if(dateStr.substring(dateIndex,dateIndex+currentString.length()).equals(currentString))
+                {
+                    context.newIndex=dateIndex+currentString.length();
+                    context.retValue=j;
+                    return context;
+                }
+            }
         }
+
+        context.newIndex=-1;
+        return context;
+    }
+
+    private Context parseNum(Context context, String dateStr, int posCount, int dateIndex)
+    {
+        for(int i=posCount;i>0;i--)
+        {
+            String numStr = dateStr.substring(dateIndex,dateIndex+posCount);
+
+            try
+            {
+                context.retValue = Integer.parseInt(numStr);
+                context.newIndex = dateIndex+posCount;
+                return context;
+            }
+            catch(NumberFormatException ex)
+            {
+            }
+        }
+
+        context.newIndex = -1;
+        return context;
+    }
+
+    private void handlePatternSub(Context context, String patternSub, String dateStr,
+                                  int dateIndex, boolean parse)
+    {
+        if(patternSub==null || patternSub.length()==0)
+            return;
+
+        if(patternSub.charAt(0)=='y')
+        {
+            if(parse)
+            {
+                parseNum(context, dateStr,4,dateIndex);
+                context.year = context.retValue;
+            }
+            else
+            {
+                formatNum(context,context.year,patternSub.length());
+            }
+        }
+        else if(patternSub.charAt(0)=='M')
+        {
+            if(parse)
+            {
+                parseNum(context, dateStr,2,dateIndex);
+                context.month = context.retValue-1;
+            }
+            else
+            {
+                formatNum(context,context.month+1,patternSub.length());
+            }
+        }
+        else if(patternSub.charAt(0)=='d')
+        {
+            if(parse)
+            {
+                parseNum(context, dateStr,2,dateIndex);
+                context.day = context.retValue;
+            }
+            else
+            {
+                formatNum(context,context.day,patternSub.length());
+            }
+        }
+        else if(patternSub.charAt(0)=='H' ||
+                patternSub.charAt(0)=='h')
+        {
+            if(parse)
+            {
+                parseNum(context, dateStr,2,dateIndex);
+                context.hour = context.retValue;
+            }
+            else
+            {
+                formatNum(context,context.hour,patternSub.length());
+            }
+        }
+        else if(patternSub.charAt(0)=='m')
+        {
+            if(parse)
+            {
+                parseNum(context, dateStr,2,dateIndex);
+                context.min = context.retValue;
+            }
+            else
+            {
+                formatNum(context,context.min,patternSub.length());
+            }
+        }
+        else if(patternSub.charAt(0)=='s')
+        {
+            if(parse)
+            {
+                parseNum(context, dateStr,2,dateIndex);
+                context.sec = context.retValue;
+            }
+            else
+            {
+                formatNum(context,context.sec,patternSub.length());
+            }
+        }
+        else if(patternSub.charAt(0)=='a')
+        {
+            if(parse)
+            {
+                parseString(context, dateStr,dateIndex,new String[][]{ampms});
+                context.ampm = context.retValue;
+            }
+            else
+            {
+                formatNum(context,context.ampm,patternSub.length());
+            }
+        }
+        else
+        {
+            if(parse)
+            {
+                context.newIndex=dateIndex+patternSub.length();
+            }
+            else
+            {
+                context.dateStr+=patternSub;
+            }
+
+        }
+    }
+
+    private void formatNum(Context context, int num, int length)
+    {
+        String str = num+"";
+
+        while(str.length()<length)
+            str="0"+str;
+
+        context.dateStr+=str;
+    }
+
+    private String format(Date date)
+    {
+        Context context = handle(null, date, false);
+
+        return context.dateStr;
+    }
+
+
+    private static class Context
+    {
+        public int newIndex;
+        public int retValue;
+        public int year;
+        public int month;
+        public int day;
+        public int hour;
+        public int min;
+        public int sec;
+        public int ampm=0;
+        String dateStr="";
     }
 
     public static void main(String[] args)
     {
-        Test test = new Test("dd.MM.yyyy");
+        Test test = new Test(" dd/MM/yyyy \\'yyyy\\' HH:mm");
+
+        Date date = test.parse(" 11/06/2005 yyyy 00:30");
 
-        test.parse("11.06.2005");
+        System.out.println(test.format(date));
     }
+
 }

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/date.js
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/date.js?rev=306498&r1=306497&r2=306498&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/date.js (original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/date.js Wed
Oct  5 19:17:25 2005
@@ -18,6 +18,23 @@
     },
 }
 
+var SimpleDateFormatParserContext = Class.create();
+SimpleDateFormatParserContext.prototype = {
+    initialize: function()
+    {
+        this.newIndex=0;
+        this.retValue=0;
+        this.year=0;
+        this.month=0;
+        this.day=0;
+        this.hour=0;
+        this.min=0;
+        this.sec=0;
+        this.ampm=0;
+        this.dateStr="";
+    },
+}
+
 var SimpleDateFormat = Class.create();
 SimpleDateFormat.prototype = {
     initialize: function(pattern, dateFormatSymbols)
@@ -26,31 +43,51 @@
         this.dateFormatSymbols = dateFormatSymbols;
     },
 
-    format: function(date)
-    {
 
-    },
-
-    parse: function(dateStr)
+    _handle: function(dateStr, date, parse)
     {
         var patternIndex = 0;
         var dateIndex = 0;
         var commentMode = false;
-        var lastChar = -1;
+        var lastChar = 0;
+        var currentChar=0;
+        var nextChar=0;
         var patternSub = null;
 
-        var year;
-        var month;
-        var day;
-        var hour;
-        var min;
-        var sec;
+        var context = new SimpleDateFormatParserContext();
+
+        if(date != null)
+        {
+            var yearStr = date.getYear()+"";
 
-        while (patternIndex < pattern.length)
+            if (yearStr.length < 4)
+            {
+                  yearStr=(parseInt(yearStr)+1900)+"";
+            }
+
+            context.year = parseInt(yearStr);
+            context.month = date.getMonth();
+            context.day = date.getDate();
+            context.hour = date.getHours();
+            context.min = date.getMinutes();
+            context.sec = date.getSeconds();
+        }
+
+        while (patternIndex < this.pattern.length)
         {
-            c = pattern.charAt(patternIndex);
+            currentChar = this.pattern.charAt(patternIndex);
 
-            if (c == '\'' && lastChar!='\\')
+            if(patternIndex<(this.pattern.length-1))
+            {
+                nextChar = this.pattern.charAt(patternIndex+1);
+            }
+            else
+            {
+                nextChar = 0;
+            }
+
+
+            if (currentChar == '\'' && lastChar!='\\')
             {
                 commentMode = !commentMode;
                 patternIndex++;
@@ -59,44 +96,231 @@
             {
                 if(!commentMode)
                 {
-                    if (c == '\\' && lastChar!='\\')
+                    if (currentChar == '\\' && lastChar!='\\')
                     {
                         patternIndex++;
                     }
-                    else if(c == lastChar)
-                    {
-                        patternSub+=c;
-                        patternIndex++;
-                        dateIndex++;
-                    }
                     else
                     {
-                        if(patternSub != null)
+                        if(patternSub == null)
+                            patternSub = "";
+
+                        patternSub+=currentChar;
+
+                        if(currentChar != nextChar)
                         {
-                            handlePatternSub(patternSub, dateStr.substring(dateIndex-patternSub.length,dateIndex))
+                            this._handlePatternSub(context, patternSub,
+                                    dateStr, dateIndex, parse);
+
                             patternSub = null;
+
+                            if(context.newIndex<0)
+                                break;
+
+                            dateIndex = context.newIndex;
                         }
-                        else
-                        {
-                            if(pattern.charAt(patternIndex)!=dateStr.charAt(dateIndex))
-                            {
-                                //invalid character in dateString
-                                return null;
-                            }
-                        }
+
+                        patternIndex++;
                     }
                 }
                 else
                 {
-                    if(pattern.charAt(patternIndex)!=dateStr.charAt(dateIndex))
+                    if(parse)
                     {
-                        //invalid character in dateString
-                        return null;
+                        if(this.pattern.charAt(patternIndex)!=dateStr.charAt(dateIndex))
+                        {
+                            //invalid character in dateString
+                            return null;
+                        }
                     }
+                    else
+                    {
+                        context.dateStr+=this.pattern.charAt(patternIndex);
+                    }
+
+                    patternIndex++;
+                    dateIndex++;
+                }
+            }
+
+            lastChar = currentChar;
+        }
+
+        this._handlePatternSub(context, patternSub,
+                dateStr, dateIndex, parse);
+
+        return context;
+    },
+
+    parse: function(dateStr)
+    {
+        var context = this._handle(dateStr, null, true);
+
+        return new Date(context.year, context.month,
+                context.day,context.hour,context.min,context.sec);
+    },
+
+    format: function(date)
+    {
+        var context = this._handle(null, date, false);
+
+        return context.dateStr;
+    },
+
+    parseString: function(context, dateStr, dateIndex, strings)
+    {
+        for(var i=0; i<strings.length;i++)
+        {
+            var currentStrings = strings[0];
+
+            for (var j = 0; j < currentStrings.length; j++)
+            {
+                var currentString = currentStrings[j];
+
+                if(dateStr.substring(dateIndex,dateIndex+currentString.length).equals(currentString))
+                {
+                    context.newIndex=dateIndex+currentString.length;
+                    context.retValue=j;
+                    return context;
                 }
             }
+        }
+
+        context.newIndex=-1;
+        return context;
+    },
+
+    _parseNum: function(context, dateStr, posCount, dateIndex)
+    {
+        for(var i=posCount;i>0;i--)
+        {
+            var numStr = dateStr.substring(dateIndex,dateIndex+posCount);
+
+            context.retValue = parseInt(numStr);
+
+            if(context.retValue == Number.NaN)
+                continue;
+
+            context.newIndex = dateIndex+posCount;
+            return context;
+        }
+
+        context.newIndex = -1;
+        return context;
+    },
+
+    _handlePatternSub: function(context, patternSub, dateStr,
+                                  dateIndex, parse)
+    {
+        if(patternSub==null || patternSub.length==0)
+            return;
+
+        if(patternSub.charAt(0)=='y')
+        {
+            if(parse)
+            {
+                this._parseNum(context, dateStr,4,dateIndex);
+                context.year = context.retValue;
+            }
+            else
+            {
+                this._formatNum(context,context.year,patternSub.length);
+            }
+        }
+        else if(patternSub.charAt(0)=='M')
+        {
+            if(parse)
+            {
+                this._parseNum(context, dateStr,2,dateIndex);
+                context.month = context.retValue-1;
+            }
+            else
+            {
+                this._formatNum(context,context.month+1,patternSub.length);
+            }
+        }
+        else if(patternSub.charAt(0)=='d')
+        {
+            if(parse)
+            {
+                this._parseNum(context, dateStr,2,dateIndex);
+                context.day = context.retValue;
+            }
+            else
+            {
+                this._formatNum(context,context.day,patternSub.length);
+            }
+        }
+        else if(patternSub.charAt(0)=='H' ||
+                patternSub.charAt(0)=='h')
+        {
+            if(parse)
+            {
+                this._parseNum(context, dateStr,2,dateIndex);
+                context.hour = context.retValue;
+            }
+            else
+            {
+                this._formatNum(context,context.hour,patternSub.length);
+            }
+        }
+        else if(patternSub.charAt(0)=='m')
+        {
+            if(parse)
+            {
+                this._parseNum(context, dateStr,2,dateIndex);
+                context.min = context.retValue;
+            }
+            else
+            {
+                this._formatNum(context,context.min,patternSub.length);
+            }
+        }
+        else if(patternSub.charAt(0)=='s')
+        {
+            if(parse)
+            {
+                this._parseNum(context, dateStr,2,dateIndex);
+                context.sec = context.retValue;
+            }
+            else
+            {
+                this._formatNum(context,context.sec,patternSub.length);
+            }
+        }
+        else if(patternSub.charAt(0)=='a')
+        {
+            if(parse)
+            {
+                parseString(context, dateStr,dateIndex,new Array(ampms));
+                context.ampm = context.retValue;
+            }
+            else
+            {
+                this._formatNum(context,context.ampm,patternSub.length);
+            }
+        }
+        else
+        {
+            if(parse)
+            {
+                context.newIndex=dateIndex+patternSub.length;
+            }
+            else
+            {
+                context.dateStr+=patternSub;
+            }
 
-            lastChar = c;
         }
     },
+
+    _formatNum: function (context, num, length)
+    {
+        var str = num+"";
+
+        while(str.length<length)
+            str="0"+str;
+
+        context.dateStr+=str;
+    }
 }



Mime
View raw message