2008年4月3日 星期四

WjDateRecognizer_Slash_Y2008M04D03 ; [2008-04-03]

' -   -   -   -   -   -   -   -   -   -   -   -   -
' -   -   -   -   -   -   -   -   -   -   -   -   -
'     (著作權聲明) Copyright Statement:
'           Copyright 2000-2008, SoftHuman Corporation. All rights reserved.
'           善解科技股份有限公司
'           著作權 2000-2008。版權所有,保留一切權利。
'
'     (本段程式之目的) Purpose:
'           Object of this class can help you to automatically recognize a string with
'           date info, like "2008/04/01", embedded in it, and help to convert it to a date stamp like "2008-04-01".
'
'     (友善的模組名稱) Friendly name of this module:
'           [WJAtTSINT2008].[Wj Date Recognizer _Slash_Y2008M04D03]
'
'     (程式用模組名稱) Code name of this module:
'           WJAtTSINT2008.WjDateRecognizer_Slash_Y2008M04D03
'
'     (本段程式是否已經在被使用中) In use: Yes
'     (程式撰寫的進度) Developing: 100% (Mandatory)
'     (程式堪用的程度) Workable: 80% (Threshold at 80%)
'     (程式的測試程度) Code tested: 80% (max 99%)
'
'     WjDateRecognizer_Slash_Y2008M04D03 [2008-04-03-AM-08-09-14]
'
'     Version: 1.00.03
'     Last Updated: (2008 04 03 PM 06 29 42)
'     Version: 1.00.02
'     Last Updated: (2008 04 03 AM 08 08 27)
'     Version: 1.00.01
'     Last Updated: (2008 04 01 PM 10 02 29)
'
' -   -   -   -   -   -   -   -   -   -   -   -   -
' -   -   -   -   -   -   -   -   -   -   -   -   -

Public Class WjDateRecognizer_Slash_Y2008M04D03

    Private Class DateInfoVerifier_Slash_Y2008M04D03
        Private Const TextWithTheOnlyValidDecimalChars As String = "0123456789"
        Private Const LengthOfValidDateInfo As Integer = 10
        Private Const PositionOfFirstSlashChar As Integer = 4
        Private Const PositionOfSecondSlashChar As Integer = 7
        Private Const CharacteristicString_Slash As String = "/"

        Private m_IsAValidDateInfo As Boolean = False
        Private m_TextWithPossibleDateInfo As String
        Private m_DateParsedFromGivenDateInfo As Date = Nothing

        Public Sub New(ByVal TextWithPossibleDateInfo As String)
            ' (本段程式之目的) Purpose:
            '     Explanation
            ' (本段程式是否已經在被使用中) In use: Yes
            ' (程式撰寫的進度) Developing: 100% (Mandatory)
            ' (程式堪用的程度) Workable: 80% (Threshold at 80%)
            ' (程式的測試程度) Code tested: 70% (max 99%)
            ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            ' (修改程式的日期) Revision Date: (2008 04 03 AM 07 09 25)
            ' (修改程式的人員) Revised by: WeiJin Tang (湯偉晉)
            '     OK
            ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

            Dim T As String
            T = TextWithPossibleDateInfo

            ' If it is not a valid Date Info string
            If T.Length <> Me.LengthOfValidDateInfo Then
                Me.m_IsAValidDateInfo = False
                Me.m_DateParsedFromGivenDateInfo = Now.Today
                Exit Sub
            End If

            Dim myPositionOfFirstSlashChar As Integer
            Dim myPositionOfSecondSlashChar As Integer

            myPositionOfFirstSlashChar = T.IndexOf(Me.CharacteristicString_Slash)
            If myPositionOfFirstSlashChar <> Me.PositionOfFirstSlashChar Then
                Me.m_IsAValidDateInfo = False
                Me.m_DateParsedFromGivenDateInfo = Now.Today
                Exit Sub
            End If

            myPositionOfSecondSlashChar = T.LastIndexOf(Me.CharacteristicString_Slash)
            If myPositionOfSecondSlashChar <> Me.PositionOfSecondSlashChar Then
                Me.m_IsAValidDateInfo = False
                Me.m_DateParsedFromGivenDateInfo = Now.Today
                Exit Sub
            End If

            ' Text with year's data, text with month's data, and text with day's data; all as string
            Dim TY, TM, TD As String
            ' number with year's data, text with month's data, and text with day's data; all as integer
            Dim Y, M, D As Integer
            TY = T.Substring(0, 4)
            TM = T.Substring(Me.PositionOfFirstSlashChar + 1, 2)
            TD = T.Substring(Me.PositionOfSecondSlashChar + 1, 2)

            ' Begin_[2008_04_03_PM_04_54_48]
            ' Comments by WeiJin Tang (湯偉晉) :
            '   Make sure every character in TY, TM, TD are valid decimal characters, i.e. from 0, 1, ... , 8, 9.
            '  
            Dim T3 As String
            T3 = TY & TM & TD
            Dim C3 As Char

            For i As Integer = 0 To T3.Length - 1
                C3 = T3.Chars(i)

                ' If character C3 is not a valid decimal character
                If Me.TextWithTheOnlyValidDecimalChars.IndexOf(C3) < 0 Then
                    Me.m_IsAValidDateInfo = False
                    Me.m_DateParsedFromGivenDateInfo = Now.Today
                    Exit Sub
                End If
            Next
            '  
            ' End_[2008_04_03_PM_04_54_48]

            Try
                Y = CType(TY, Integer)
                M = CType(TM, Integer)
                D = CType(TD, Integer)
            Catch ex As Exception
                Me.m_IsAValidDateInfo = False
                Me.m_DateParsedFromGivenDateInfo = Now.Today
                Exit Sub
            End Try

            Dim myDate As Date

            ' wjNote:
            '   Use this trick to make sure the generated date object is reasonable.
            Try
                myDate = New Date(Y, M, D)

                Me.m_IsAValidDateInfo = True
                Me.m_DateParsedFromGivenDateInfo = myDate
            Catch ex As Exception
                Me.m_IsAValidDateInfo = False
                Me.m_DateParsedFromGivenDateInfo = Now.Today
                Exit Sub
            End Try
        End Sub

        Public ReadOnly Property IsAValidDateInfo() As Boolean
            ' ok
            Get
                Return Me.m_IsAValidDateInfo
            End Get
        End Property

        Public Function GetParsedDateOrDefaultDate() As Date
            ' OK
            Return Me.m_DateParsedFromGivenDateInfo
        End Function

        Public Function GetParsedDateOrDefaultDateAsOneString() As String
            ' OK

            Dim D As Date
            D = Me.m_DateParsedFromGivenDateInfo

            Dim SB As New System.Text.StringBuilder

            With SB
                .Append(D.Year.ToString.PadLeft(4, "0"c))
                .Append("-")
                .Append(D.Month.ToString.PadLeft(2, "0"c))
                .Append("-")
                .Append(D.Day.ToString.PadLeft(2, "0"c))
            End With

            Return SB.ToString

        End Function

    End Class ' DateInfoVerifier_Slash_Y2008M04D03

    ' Begin_[2008_04_01_PM_09_43_19]
    ' Comments by WeiJin Tang (湯偉晉) :
    '   Constants used by this object
    '  
    Private Const LengthOfValidDateInfo As Integer = 10
    Private Const InitialValue_for_PositionOfFirstSlash As Integer = -1
    Private Const InitialValue_for_PositionOfSecondSlash As Integer = -1
    Private Const InitialValue_for_PositionOfTheFirstOtherSlashChar As Integer = -1
    Private Const MinLength_for_TextWithPossibleDateInfo As Integer = 10
    '  
    ' End_[2008_04_01_PM_09_43_19]

    Private m_TextWithPossibleDateInfo As String

    Private m_RecognizedDate As Date

    Private m_PositionOfFirstSlash As Integer = Me.InitialValue_for_PositionOfFirstSlash ' Initial value
    Private m_PositionOfSecondSlash As Integer = Me.InitialValue_for_PositionOfSecondSlash ' Initial value
    Private m_PositionOfTheFirstOtherSlashChar As Integer = Me.InitialValue_for_PositionOfTheFirstOtherSlashChar ' Initial value

    ' Begin_[2008_04_01_PM_08_33_53]
    ' Comments by WeiJin Tang (湯偉晉) :
    '   Characteristic features of the to-be-recognized date info
    '  
    Private Const DistanceBetweenThe2SlashChars As Integer = 3
    Private Const MinNumberOfCharactersBeforeFirstSlash As Integer = 4
    Private Const MinNumberOfCharactersAfterSecondSlash As Integer = 2
    Private Const CharacteristicString_Slash As String = "/"
    '  
    ' End_[2008_04_01_PM_08_33_53]

    Public Sub New(ByVal AnyText As String)
        ' OK
        Me.m_TextWithPossibleDateInfo = AnyText

        Me.Do_ParseTextForPossibleDateInfo(Me.m_TextWithPossibleDateInfo)

    End Sub

    Private ReadOnly Property TextWithPossibleDateInfo() As String
        ' Ok
        Get
            Return Me.m_TextWithPossibleDateInfo
        End Get
    End Property

    Public Function GetRecognizedDate() As Date
        Return Me.m_RecognizedDate
    End Function

    Public Function GetRecognizedDateAsOneString() As String
        ' (本段程式之目的) Purpose:
        '     Explanation
        ' (本段程式是否已經在被使用中) In use: Yes
        ' (重要等級) Importance rating:  60%
        ' (再利用的可能性) Chances of reuse:  40%
        ' (程式撰寫的進度) Developing: 100% (Mandatory)
        ' (程式堪用的程度) Workable: 80% (Threshold at 80%)
        ' (程式的測試程度) Code tested: 70% (max 99%)
        ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        ' (修改程式的日期) Revision Date: (2008 04 03 AM 05 48 58)
        ' (修改程式的人員) Revised by: WeiJin Tang (湯偉晉)
        '     OK
        ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        Dim D As Date
        D = Me.GetRecognizedDate

        Dim SB As New System.Text.StringBuilder

        With SB
            .Append(D.Year.ToString.PadLeft(4, "0"c))
            .Append("-")
            .Append(D.Month.ToString.PadLeft(2, "0"c))
            .Append("-")
            .Append(D.Day.ToString.PadLeft(2, "0"c))
        End With

        ' Return a string like "2008-03-29"
        Return SB.ToString

    End Function ' GetRecognizedDateAsOneString
    ' -   -   -   -   -   -   -   -   -   -   -   -   -
    ' -   -   -   -   -   -   -   -   -   -   -   -   -

    Private Sub Do_ParseTextForPossibleDateInfo(ByVal TextWithPossibleDateInfo As String)
        ' (本段程式之目的) Purpose:
        '     Explanation
        ' (本段程式是否已經在被使用中) In use: Yes
        ' (重要等級) Importance rating:  90%
        ' (再利用的可能性) Chances of reuse:  85%
        ' (程式撰寫的進度) Developing: 100% (Mandatory)
        ' (程式堪用的程度) Workable: 80% (Threshold at 80%)
        ' (程式的測試程度) Code tested: 75% (max 99%)
        ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        ' (修改程式的日期) Revision Date: (2008 04 01 PM 07 37 56)
        ' (修改程式的人員) Revised by: WeiJin Tang (湯偉晉)
        '     OK
        ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        Dim T As String

        If TextWithPossibleDateInfo.Length >= Me.MinLength_for_TextWithPossibleDateInfo Then
            T = TextWithPossibleDateInfo
        Else
            ' Length of   TextWithPossibleDateInfo   is too short to hold a valid date info
            Me.m_RecognizedDate = Now.Today
            Exit Sub
        End If

        Me.m_PositionOfFirstSlash = T.IndexOf(Me.CharacteristicString_Slash)

        ' if we can't find any "/" char in working string T
        If Me.m_PositionOfFirstSlash < 0 Then
            Me.m_RecognizedDate = Now.Today
            Exit Sub
        End If

        ' Position of first slash char violates the required position constraints
        If Me.m_PositionOfFirstSlash < Me.MinNumberOfCharactersBeforeFirstSlash Then
            Dim myStartPositionOfRemainingTextToParse As Integer
            myStartPositionOfRemainingTextToParse = Me.m_PositionOfFirstSlash + 1

            Dim myRemainingTextToParse As String
            Dim myLengthOfRemainingTextToParse As Integer

            ' if there is no enough room to hold a valid date info string then simply use the default date.
            If myStartPositionOfRemainingTextToParse + Me.MinLength_for_TextWithPossibleDateInfo > TextWithPossibleDateInfo.Length Then
                Me.m_RecognizedDate = Now.Today
                Exit Sub
            Else

                myLengthOfRemainingTextToParse = TextWithPossibleDateInfo.Length - myStartPositionOfRemainingTextToParse

                myRemainingTextToParse = TextWithPossibleDateInfo.Substring(myStartPositionOfRemainingTextToParse, myLengthOfRemainingTextToParse)

            End If

            Me.m_PositionOfFirstSlash = Me.InitialValue_for_PositionOfFirstSlash
            Me.m_PositionOfSecondSlash = Me.InitialValue_for_PositionOfSecondSlash

            Me.Do_ParseTextForPossibleDateInfo(myRemainingTextToParse)
            Exit Sub

        End If ' Me.m_PositionOfFirstSlash < Me.MinNumberOfCharactersBeforeFirstSlash

        Me.m_PositionOfSecondSlash = T.IndexOf(Me.CharacteristicString_Slash, Me.m_PositionOfFirstSlash + 1)

        If Me.m_PositionOfSecondSlash - Me.m_PositionOfFirstSlash = Me.DistanceBetweenThe2SlashChars Then
            ' If T is a string like
            '      "This2008/04/01Happy"   ,   "This2008/??/01Happy"  , or  "This????/??/??Happy"
            '
            ' We might have found a possible Date Info to recognize

            ' Begin_[2008_04_01_PM_09_50_01]
            ' Comments by WeiJin Tang (湯偉晉) :
            '   Check position constraints imposed on the to-be-recognized date info
            '  

            ' Position of first slash char violates the required position constraints
            If Me.m_PositionOfFirstSlash < Me.MinNumberOfCharactersBeforeFirstSlash Then

                Dim myStartPositionOfRemainingTextToParse As Integer
                myStartPositionOfRemainingTextToParse = Me.m_PositionOfFirstSlash + 1

                Dim myRemainingTextToParse As String
                Dim myLengthOfRemainingTextToParse As Integer

                ' if there is no enough room to hold a valid date info string then simply use the default date.
                If myStartPositionOfRemainingTextToParse + Me.MinLength_for_TextWithPossibleDateInfo > TextWithPossibleDateInfo.Length Then
                    Me.m_RecognizedDate = Now.Today
                    Exit Sub
                Else

                    myLengthOfRemainingTextToParse = TextWithPossibleDateInfo.Length - myStartPositionOfRemainingTextToParse

                    myRemainingTextToParse = TextWithPossibleDateInfo.Substring(myStartPositionOfRemainingTextToParse, myLengthOfRemainingTextToParse)

                End If

                Me.m_PositionOfFirstSlash = Me.InitialValue_for_PositionOfFirstSlash
                Me.m_PositionOfSecondSlash = Me.InitialValue_for_PositionOfSecondSlash

                Me.Do_ParseTextForPossibleDateInfo(myRemainingTextToParse)
                Exit Sub

                '' Position of second slash char also violates the required position constraints
                'If Me.m_PositionOfSecondSlash < Me.MinNumberOfCharactersBeforeFirstSlash Then

                'Else

                'End If

            Else
                ' Position of first slash char satisfies the required position constraints
                '
                ' Continue to check whether there are enough characters after the second slash character.
                If Me.m_PositionOfSecondSlash + Me.MinNumberOfCharactersAfterSecondSlash < T.Length Then

                    Dim myTextWithPossibleDateInfo As String
                    myTextWithPossibleDateInfo = T.Substring(Me.m_PositionOfFirstSlash - Me.MinNumberOfCharactersBeforeFirstSlash, Me.LengthOfValidDateInfo)

                    ' we are almost done, if we are lucky
                    'MsgBox("myTextWithPossibleDateInfo = " & myTextWithPossibleDateInfo)

                    Dim H As WjDateRecognizer_Slash_Y2008M04D03.DateInfoVerifier_Slash_Y2008M04D03
                    H = New WjDateRecognizer_Slash_Y2008M04D03.DateInfoVerifier_Slash_Y2008M04D03(myTextWithPossibleDateInfo)

                    If H.IsAValidDateInfo Then
                        'MsgBox("H.GetParsedDateOrDefaultDateAsOneString() = " & H.GetParsedDateOrDefaultDateAsOneString())
                        Me.m_RecognizedDate = H.GetParsedDateOrDefaultDate
                        Exit Sub
                    Else
                        ' Keep working ..., since this is not a valid Date Info string

                        Dim myStartPositionOfRemainingTextToParse As Integer
                        myStartPositionOfRemainingTextToParse = Me.m_PositionOfSecondSlash + 1

                        Dim myRemainingTextToParse As String
                        Dim myLengthOfRemainingTextToParse As Integer

                        ' if there is no enough room to hold a valid date info string then simply use the default date.
                        If myStartPositionOfRemainingTextToParse + Me.MinLength_for_TextWithPossibleDateInfo > TextWithPossibleDateInfo.Length Then
                            Me.m_RecognizedDate = Now.Today
                            Exit Sub
                        Else
                            myLengthOfRemainingTextToParse = TextWithPossibleDateInfo.Length - myStartPositionOfRemainingTextToParse

                            myRemainingTextToParse = TextWithPossibleDateInfo.Substring(myStartPositionOfRemainingTextToParse, myLengthOfRemainingTextToParse)

                        End If

                        Me.m_PositionOfFirstSlash = Me.InitialValue_for_PositionOfFirstSlash
                        Me.m_PositionOfSecondSlash = Me.InitialValue_for_PositionOfSecondSlash

                        Me.Do_ParseTextForPossibleDateInfo(myRemainingTextToParse)
                        Exit Sub
                    End If
                    Exit Sub
                Else
                    ' this string T can't contain a recognizable date info since there's no enough room at the end, i.e. after the 2nd slash char
                    Me.m_RecognizedDate = Now.Today
                    Exit Sub
                End If
            End If
            '  
            ' End_[2008_04_01_PM_09_50_01]
        Else
            ' If T is a string like
            '     "This2008/04ABC/01Happy"
            '
            ' It is not a valid characteristic feature, so we need to update the position of the first slash.
            Me.m_PositionOfFirstSlash = Me.m_PositionOfSecondSlash
            Me.m_PositionOfSecondSlash = Me.InitialValue_for_PositionOfSecondSlash

            Dim myRemainingTextToParse As String
            Dim myStartPositionOfRemainingTextToParse As Integer
            myStartPositionOfRemainingTextToParse = Me.m_PositionOfFirstSlash - Me.MinNumberOfCharactersBeforeFirstSlash ' me.MinNumberOfCharactersBeforeFirstSlash = 4

            Dim myLengthOfRemainingTextToParse As Integer
            myLengthOfRemainingTextToParse = TextWithPossibleDateInfo.Length - myStartPositionOfRemainingTextToParse

            myRemainingTextToParse = TextWithPossibleDateInfo.Substring(myStartPositionOfRemainingTextToParse, myLengthOfRemainingTextToParse)

            Me.Do_ParseTextForPossibleDateInfo(myRemainingTextToParse)
            Exit Sub
        End If

    End Sub ' Do_ParseTextForPossibleDateInfo
    ' -   -   -   -   -   -   -   -   -   -   -   -   -
    ' -   -   -   -   -   -   -   -   -   -   -   -   -
    Private Function Get_PositionOfTheFirstOtherSlashChar() As Integer
        ' (本段程式之目的) Purpose:
        '     Explanation
        ' (本段程式是否已經在被使用中) In use: Yes
        ' (重要等級) Importance rating:  60%
        ' (再利用的可能性) Chances of reuse:  40%
        ' (程式撰寫的進度) Developing: 100% (Mandatory)
        ' (程式堪用的程度) Workable: 0% (Threshold at 80%)
        ' (程式的測試程度) Code tested: 0% (max 99%)
        ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        ' (修改程式的日期) Revision Date: (2008 04 01 PM 09 04 26)
        ' (修改程式的人員) Revised by: WeiJin Tang (湯偉晉)
        '     OK
        ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        Return Me.m_PositionOfTheFirstOtherSlashChar

    End Function ' Get_PositionOfTheFirstOtherSlashChar
    ' -   -   -   -   -   -   -   -   -   -   -   -   -
    ' -   -   -   -   -   -   -   -   -   -   -   -   -

End Class ' WjDateRecognizer_Slash_Y2008M04D03


 

沒有留言: