jj: ZUGFeRD
This commit is contained in:
114
Modules.Interfaces/ZUGFeRDInterface.vb
Normal file
114
Modules.Interfaces/ZUGFeRDInterface.vb
Normal file
@@ -0,0 +1,114 @@
|
||||
Imports System.Xml
|
||||
Imports DigitalData.Modules.Interfaces.Exceptions
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Public Class ZUGFeRDInterface
|
||||
Private _logConfig As LogConfig
|
||||
Private _logger As Logger
|
||||
|
||||
Private Const ZUGFERD_CONVERTER_EXE = ".\pdf_zugferd_test.exe"
|
||||
Private Const ZUGFERD_CONVERTER_SUCCESS_MESSAGE = "Document contains ZUGFeRD data."
|
||||
|
||||
Public Enum ErrorType
|
||||
NoValidFile
|
||||
ExtractionFailed
|
||||
End Enum
|
||||
|
||||
Public Sub New(LogConfig As LogConfig)
|
||||
_logConfig = LogConfig
|
||||
_logger = _logConfig.GetLogger()
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' Validates a ZUGFeRD File and extracts the XML Document from it
|
||||
''' </summary>
|
||||
''' <param name="Path"></param>
|
||||
''' <exception cref="ZUGFeRDExecption"></exception>
|
||||
''' <returns></returns>
|
||||
Public Function ExtractXMLFile(Path As String) As XmlDocument
|
||||
Dim oException As New Exception
|
||||
Dim oXmlDocument = ExtractZugferd(Path)
|
||||
|
||||
If IsNothing(oXmlDocument) Then
|
||||
Throw New ZUGFeRDExecption(ErrorType.ExtractionFailed, "Datei ist kein gültiges ZUGFeRD Format.")
|
||||
End If
|
||||
|
||||
Return oXmlDocument
|
||||
End Function
|
||||
|
||||
Public Function ExtractZugferd(Path As String) As XmlDocument
|
||||
Dim oProcessOutput, oProcessError, oFileContent As String
|
||||
Dim oXmlDocument As New XmlDocument()
|
||||
Dim oTempFile = IO.Path.GetTempFileName()
|
||||
|
||||
Dim oProcessStartInfo As New ProcessStartInfo() With {
|
||||
.FileName = ZUGFERD_CONVERTER_EXE,
|
||||
.RedirectStandardError = True,
|
||||
.RedirectStandardOutput = True,
|
||||
.UseShellExecute = False,
|
||||
.Arguments = $"-i ""{Path}"" -o ""{oTempFile}"""
|
||||
}
|
||||
Dim oProcess As New Process() With {
|
||||
.StartInfo = oProcessStartInfo
|
||||
}
|
||||
|
||||
Try
|
||||
oProcess.Start()
|
||||
oProcessOutput = oProcess.StandardOutput.ReadToEnd()
|
||||
oProcessError = oProcess.StandardError.ReadToEnd()
|
||||
oProcess.WaitForExit()
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Return Nothing
|
||||
End Try
|
||||
|
||||
If Not oProcessOutput.Contains(ZUGFERD_CONVERTER_SUCCESS_MESSAGE) Then
|
||||
_logger.Warn("File {0} is not a valid ZUGFeRD File!", Path)
|
||||
Return Nothing
|
||||
End If
|
||||
|
||||
Try
|
||||
oFileContent = IO.File.ReadAllText(oTempFile)
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Return Nothing
|
||||
End Try
|
||||
|
||||
Try
|
||||
oXmlDocument.LoadXml(oFileContent)
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Return Nothing
|
||||
End Try
|
||||
|
||||
Return oXmlDocument
|
||||
End Function
|
||||
|
||||
Private Function CheckZugferd(Path As String) As Boolean
|
||||
Dim oProcessOutput, oProcessError As String
|
||||
Dim oProcessStartInfo As New ProcessStartInfo() With {
|
||||
.FileName = ZUGFERD_CONVERTER_EXE,
|
||||
.RedirectStandardError = True,
|
||||
.RedirectStandardOutput = True,
|
||||
.UseShellExecute = False,
|
||||
.Arguments = $"-i {Path}"
|
||||
}
|
||||
Dim oProcess As New Process() With {
|
||||
.StartInfo = oProcessStartInfo
|
||||
}
|
||||
|
||||
Try
|
||||
oProcess.Start()
|
||||
oProcessOutput = oProcess.StandardOutput.ReadToEnd()
|
||||
oProcessError = oProcess.StandardError.ReadToEnd()
|
||||
oProcess.WaitForExit()
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Return False
|
||||
End Try
|
||||
|
||||
|
||||
|
||||
Return True
|
||||
End Function
|
||||
End Class
|
||||
Reference in New Issue
Block a user