70 lines
2.3 KiB
VB.net
70 lines
2.3 KiB
VB.net
Imports System.IO
|
|
Imports System.Xml
|
|
Imports System.Xml.Serialization
|
|
Imports System.Xml.XPath
|
|
Imports DigitalData.Modules.Logging
|
|
Imports EDIDocumentImport.DocumentInfo
|
|
|
|
Public Class FileLoader
|
|
Inherits Base
|
|
|
|
Public Config As Config
|
|
Public Files As New List(Of FileInfo)
|
|
|
|
Public Sub New(pLogConfig As LogConfig, pConfig As Config)
|
|
MyBase.New(pLogConfig, pLogConfig.GetLogger())
|
|
Config = pConfig
|
|
End Sub
|
|
|
|
Public Function LoadFiles() As Boolean
|
|
If Config.InputDirectory = String.Empty Then
|
|
Throw New ArgumentNullException("InputDirectory")
|
|
End If
|
|
|
|
Logger.Info("Loading files from directory [{0}]", Config.InputDirectory)
|
|
|
|
Try
|
|
Dim oDirectory As New DirectoryInfo(Config.InputDirectory)
|
|
Dim oFiles = oDirectory.GetFiles()
|
|
|
|
Logger.Debug("Found [{0}] files in directory [{1}]", oFiles.Count, oDirectory)
|
|
|
|
Files = oFiles.ToList()
|
|
|
|
Return True
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Throw New IO.IOException($"Could not load files from directory {Config.InputDirectory}", ex)
|
|
End Try
|
|
End Function
|
|
|
|
Public Function LoadFile(pFullName) As Object
|
|
Dim oFile As FileInfo = Files.
|
|
Where(Function(f) f.FullName = pFullName).
|
|
SingleOrDefault()
|
|
|
|
Using oFileStream As New FileStream(oFile.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)
|
|
Dim oXmlDocument = New XPathDocument(oFileStream)
|
|
Dim oNavigator = oXmlDocument.CreateNavigator()
|
|
|
|
Using oReader = oNavigator.ReadSubtree()
|
|
Dim oTemplateName = GetTemplateName(oNavigator)
|
|
Dim oDocumentType = GetDocumentTypeFromTemplateName(oTemplateName)
|
|
Dim oSchemaType = GetDocumentSchemaFromDocumentType(oDocumentType)
|
|
Dim oSerializer As New XmlSerializer(oSchemaType)
|
|
Dim oObject = oSerializer.Deserialize(oReader)
|
|
|
|
Return New Tuple(Of Object, DocumentType)(oObject, oDocumentType)
|
|
End Using
|
|
End Using
|
|
End Function
|
|
|
|
Public Function GetTemplateName(pDocument As XPathNavigator) As String
|
|
Dim oTemplateName = pDocument.
|
|
SelectSingleNode("//MESOWebService").
|
|
GetAttribute("Template", "")
|
|
|
|
Return oTemplateName
|
|
End Function
|
|
End Class
|