vCard Reader Class

类别:.NET开发 点击:0 评论:0 推荐:

Imports System.IO
Public Class vCardReader
#Region "Define"
    Public vCards As vCard()
#End Region
#Region "Subs"

    Sub New(ByVal vCardFileStream As FileStream)
        DecodeVCardFromFileStream(vCardFileStream)
    End Sub

    Private Function Split(ByVal Content As String) As CardProperty
        Dim CardProperty As CardProperty
        Dim PropertyAndParametreString As String = Content.Split(":")(0)
        Dim ValueString As String = Content.Split(":")(1)
        Dim Temp As String() = PropertyAndParametreString.Split(";")
        CardProperty.Name = Temp(0)
        If Temp.Length > 1 Then
            ReDim CardProperty.Parametres(Temp.GetUpperBound(0) - 1)
            Temp.Copy(Temp, 1, CardProperty.Parametres, 0, Temp.Length - 1)
        Else
            CardProperty.Parametres = Nothing
        End If
        CardProperty.Values = ValueString.Split(";")
        Return CardProperty
    End Function

    Public Sub DecodeVCardFromFileStream(ByVal vCardFileStream As FileStream)
        Dim Reader As New StreamReader(vCardFileStream)
        ReDim vCards(0)
        Dim CurrentVcard As Integer
        Do
            'Search for vCard
            ReDim vCards(CurrentVcard).Properties(-1)
            If InStr(Reader.ReadLine, "BEGIN:VCARD") Then
                'Start Processing vCard object
                Dim Content As String
                Dim cardProperty As CardProperty
                Do
                    Content = Reader.ReadLine
                    If Content = "END:VCARD" Then Exit Do
                    'The "=" in then end of a stream indicates message is not complete. Read next line and contact them.
                    Do

                        If Content.LastIndexOf("=") = Content.Length - 1 Then
                            Content = Content.Substring(1, Content.Length - 2) + Reader.ReadLine
                        Else : Exit Do
                        End If
                    Loop
                    'Split properties , parametres and values.
                    cardProperty = Split(Content)
                    Dim PropertyNumber As Integer = vCards(CurrentVcard).Properties.Length
                    ReDim Preserve vCards(CurrentVcard).Properties(PropertyNumber)
                    vCards(CurrentVcard).Properties(PropertyNumber) = cardProperty
                Loop
            End If
            If Reader.Peek() < 0 Then
                Return
            End If
            CurrentVcard += 1
            ReDim Preserve vCards(CurrentVcard)
        Loop
    End Sub
#End Region

#Region "Structures"
    Public Structure vCard
        Public Properties As CardProperty()
    End Structure

    Public Structure CardProperty
        Public Name As String
        Public Parametres() As String
        Public Values() As String
    End Structure

#End Region

End Class

本文地址:http://com.8s8s.com/it/it42856.htm