Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / XML

Validating data with Flat File Checker

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
30 Oct 2009GPL32 min read 43.8K   881   17  
An article on data validation with a flat file schema generated in Flat File Checker.
Imports System.Xml.XPath
Imports System.Xml

''' <summary>
''' Represents the column of the value separated flat file.
''' Also defines the parent class for FixedPositionFileColumn.
''' </summary>
''' <remarks></remarks>
Public Class FileColumn
    Inherits DataColumn

    Private _file As FlatFile
    '--------------------------------------------------------------------
    ''' <summary>
    ''' Create new column in the file from XML
    ''' </summary>
    ''' <param name="xmlDefinition">XML node with column definition</param>
    ''' <param name="file">File that column belongs to</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal xmlDefinition As IXPathNavigable, ByVal file As FlatFile)
        MyBase.New(file, -1)

        Dim navigator As XPathNavigator = xmlDefinition.CreateNavigator
        If xmlDefinition Is Nothing Then Exit Sub



        If Not String.IsNullOrEmpty(navigator.GetAttribute("Name", "")) Then Me.Name = navigator.GetAttribute("Name", "")
        If Not String.IsNullOrEmpty(navigator.GetAttribute("Index", "")) Then Me.IndexInFile = Convert.ToInt32(navigator.GetAttribute("Index", ""), System.Globalization.CultureInfo.InvariantCulture)

        If Not String.IsNullOrEmpty(Me.Name) Then
            If Not Me.Name Like "*.*" Then
                ' Column is in the same file
                Me.File = file

            Else
                Me.File = file.Datasources.File(FlatFile.GetFileAlias(Me.Name))
                Me.Name = DataColumn.GetColumnName(Me.Name)
            End If
        Else
            Me.File = file
        End If

        If Me.Name Is Nothing AndAlso Me.IndexInFile = -1 Then
            Throw New XmlException("Either Name or Index attribute should be provided in Column Node")
        End If
        Dim req As String = navigator.GetAttribute("Required", "")
        If Not String.IsNullOrEmpty(req) Then
            If Not Boolean.TryParse(req, Me.Required) Then
                Throw New XmlException("Required attribute in Column element must have value of either False are True.")
            End If
        End If

        Me.Format = navigator.GetAttribute("Format", "")
        If navigator.MoveToFirstChild() Then
            Do
                Select Case navigator.Name
                    Case "Comment"
                        Me.Comment = navigator.InnerXml
                    Case Else
                        Try
                            Me.AddRule(GeneralChecker.Create(navigator, Me.Checks, Me))
                        Catch ex As XmlException
                            Throw New XmlException("Failed to process xml node with column definition", ex)
                        End Try
                End Select
            Loop While navigator.MoveToNext
        End If


    End Sub

    

    Public Function Initiate() As Boolean

        If Not Me.File.ColumnForLoadExists(Me.Name) Then
            ' add column to column load list if it has checks
            ' If Me.Checks.Count > 0 Then
            _file.AddColumnToLoad(Me)
        Else : _file.ColumnToLoad(Me.Name) = Me
        End If
        Return True
    End Function
   
    Public Sub New(ByVal file As FlatFile, ByVal index As Integer)
        MyBase.New(file, index)
        Me.File = file
        Me.IndexInFile = index
    End Sub
    Public Shared Shadows Operator <>(ByVal col1 As FileColumn, ByVal col2 As FileColumn) As Boolean
        Return Not col1 = col2
    End Operator
    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        If Not obj Is Nothing AndAlso TypeOf obj Is FileColumn Then

            Return CType(obj, FileColumn) = Me
        Else
            Return False
        End If
    End Function
    Public Overrides Function GetHashCode() As Integer
        Return MyBase.GetHashCode()
    End Function
    Public Shared Shadows Operator =(ByVal col1 As FileColumn, ByVal col2 As FileColumn) As Boolean
        If col1.File = col2.File Then
            If Not (String.IsNullOrEmpty(col1.Name) OrElse String.IsNullOrEmpty(col2.Name)) Then
                Return UCase(col1.FullName) = UCase(col2.FullName)
            Else
                If col1.IndexInFile >= 0 AndAlso col2.IndexInFile > 0 Then
                    Return col1.IndexInFile = col2.IndexInFile
                Else
                    Return False
                End If
            End If
        Else : Return False
        End If
    End Operator
    Public Sub New(ByVal file As FlatFile, ByVal name As String)
        MyBase.New(file, -1)
        Me.File = file
        Me.Checks = New AndChecksCollection(Me)
        Me.Table = file
        MyBase.Name = name
    End Sub
    Public Property File() As FlatFile
        Get
            Return _file
        End Get
        Set(ByVal value As FlatFile)
            _file = value
            Me.Datasource = File
        End Set
    End Property
    
   
    ''' <summary>
    ''' Function gets full path for the file by evaluating path string with column specific fuctions
    ''' </summary>
    ''' <param name="pathExpression"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetFullPath(ByVal pathExpression As String) As String

        Dim evFuncs As New EvaluateActionExpression(Me)
        Return _file.GetFullPath(ReplaceExpression.Evaluate(pathExpression, evFuncs))

    End Function

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Database Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions