2021-10-29 16:12:10 +02:00

120 lines
4.2 KiB
VB.net

Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Imports DigitalData.Modules.Logging
Namespace Schemas
Public Class SchemaLoader
Inherits BaseClass
Private ReadOnly ns As XNamespace = "http://www.w3.org/2001/XMLSchema"
Public SchemaList As List(Of FileInfo)
Public Sub New(pLogConfig As LogConfig)
MyBase.New(pLogConfig, pLogConfig.GetLogger)
End Sub
Public Function LoadFiles(pSchemaDirectory) As Boolean
If pSchemaDirectory = String.Empty Then
Throw New ArgumentNullException("SchemaDirectory")
End If
Logger.Info("Loading files from directory [{0}]", pSchemaDirectory)
Try
Dim oDirectory As New DirectoryInfo(pSchemaDirectory)
Dim oFiles = oDirectory.GetFiles()
Logger.Debug("Found [{0}] files in directory [{1}]", oFiles.Count, oDirectory)
SchemaList = oFiles.ToList()
Return True
Catch ex As Exception
Logger.Error(ex)
Throw New IO.IOException($"Could not load files from directory {pSchemaDirectory}", ex)
End Try
End Function
Public Function GetSchemaFromFile(pSchemaFilePath As String) As Schema
Dim oSchema As New Schema
Dim oFileInfo As New FileInfo(pSchemaFilePath)
Dim oElements = GetSchemaElements(pSchemaFilePath)
oSchema.Name = oFileInfo.Name
For Each oElement In oElements
Dim oColumns = GetElementColumns(oElement)
Dim oSchemaColumns As New List(Of Schema.Column)
For Each oColumn As XElement In oColumns
Dim oName = XmlData.GetElementAttribute(oColumn, "name")
Dim oMinOccurs = XmlData.GetElementAttribute(oColumn, "minOccurs")
Dim oMaxOccurs = XmlData.GetElementAttribute(oColumn, "maxOccurs")
Dim oType = GetElementType(oColumn)
Dim oRequired = False
If oMinOccurs = 1 And oMaxOccurs = 1 Then
oRequired = True
End If
Dim oSchemaColumn As New Schema.Column With {
.Name = oName,
.Required = oRequired,
.DataType = oType
}
oSchemaColumns.Add(oSchemaColumn)
Next
oSchema.Tables.Add(New Schema.Table With {
.Name = XmlData.GetElementAttribute(oElement, "name"),
.Columns = oSchemaColumns
})
Next
Return oSchema
End Function
Public Function GetElementType(pElement As XElement) As Constants.ColumnType
Dim oTypeString = XmlData.GetElementAttribute(pElement, "type")
If oTypeString Is Nothing Then
Dim oRestrictionElement As XElement = pElement.
Descendants(ns + "restriction").
FirstOrDefault()
oTypeString = XmlData.GetElementAttribute(oRestrictionElement, "base")
End If
Select Case oTypeString
Case "xs:date"
Return Constants.ColumnType.Date
Case "xs:integer"
Return Constants.ColumnType.Integer
Case "xs:decimal"
Return Constants.ColumnType.Decimal
Case "xs:boolean"
Return Constants.ColumnType.Boolean
Case Else
Return Constants.ColumnType.String
End Select
End Function
Public Function GetSchemaElements(pSchemaFilePath As String) As List(Of XElement)
Dim oText As String = IO.File.ReadAllText(pSchemaFilePath)
Dim oDoc = XDocument.Parse(oText)
Return XmlData.GetElementsFromElement(oDoc, "choice", ns)
End Function
Public Function GetElementColumns(pElement As XElement) As List(Of XElement)
Return XmlData.GetElementsFromElement(pElement, "sequence", ns)
End Function
End Class
End Namespace