Welcome to DetlefGrohs.com Sign in | Join | Help

Host Application Code for Old XMLDocumentSplitter Class

I am including the code from my original host application for my XMLDocumentSplitter class. I will be updating this along with the utility class as well. It is interesting to see some of my coding habits from years ago.

XMLSplitter 


Public Class frmMain
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "    
' VS Generated Code Removed
#End Region

    Private m_XMLSplitter As New XMLDocumentSplitter()

    Private Sub btnSplit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSplit.Click
        If rbSplitByDocumentSize.Checked Then
            m_XMLSplitter.SplitType = XMLDocumentSplitter.SplitTypes.ByDocumentSize
            m_XMLSplitter.SplitSize = CType(txtDocumentSize.Text, Long)
        Else
            m_XMLSplitter.SplitType = XMLDocumentSplitter.SplitTypes.ByElementCount
            m_XMLSplitter.SplitSize = CType(txtElementCount.Text, Long)
        End If

        m_XMLSplitter.Split(New System.IO.StreamReader(txtSourceDocument.Text), AddressOf SplitHandler)

        MessageBox.Show(String.Format("Split '{0}' into {1} files.", txtSourceDocument.Text, m_XMLSplitter.SplitCount))
    End Sub

    Private Sub SplitHandler(ByVal count As Long, ByVal document As String)
        Dim stream_writer As New System.IO.StreamWriter(txtDestinationPath.Text & String.Format(txtDestinationFilenamePattern.Text, count))
        stream_writer.Write(document)
        stream_writer.Close()
    End Sub

    Private Sub FieldFields_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles txtSourceDocument.TextChanged, txtDestinationPath.TextChanged, txtDestinationFilenamePattern.TextChanged
        AdjustSplitButton()
    End Sub

    Private Sub AdjustSplitButton()
        If System.IO.File.Exists(txtSourceDocument.Text) And _
            System.IO.Directory.Exists(txtDestinationPath.Text) And _
            ValidPattern(txtDestinationFilenamePattern.Text) Then
            btnSplit.Enabled = True
        Else
            btnSplit.Enabled = False
        End If
    End Sub

    Private Function ValidPattern(ByVal pattern As String) As Boolean
        ' Pattern needs to contain '{0.*}' - very basic checking...
        Dim regexp As New System.Text.RegularExpressions.Regex("\{0.*\}")
        Return regexp.IsMatch(pattern)
    End Function

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AdjustSplitButton()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub RadioButtons_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles rbSplitByDocumentSize.CheckedChanged, rbSplitByElementCount.CheckedChanged
        If rbSplitByDocumentSize.Checked Then
            txtDocumentSize.Enabled = True : txtElementCount.Enabled = False
        Else
            txtDocumentSize.Enabled = False : txtElementCount.Enabled = True
        End If
    End Sub

    Private Sub btnSourceDocument_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSourceDocument.Click
        ' Get Source File Information
        dlgOpenFile.FileName = txtSourceDocument.Text
        If dlgOpenFile.ShowDialog() = DialogResult.OK Then
            txtSourceDocument.Text = dlgOpenFile.FileName
        End If
    End Sub

    Private Sub btnDestinationPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDestinationPath.Click
        ' Get Destination Path Information
        dlgSaveFile.InitialDirectory = txtDestinationPath.Text
        If dlgSaveFile.ShowDialog = DialogResult.OK Then
            txtDestinationPath.Text = dlgSaveFile.FileName
        End If
    End Sub
End Class
Published Wednesday, October 03, 2007 9:13 AM by detlef
Filed Under: ,

Comments

No Comments
Anonymous comments are disabled