92 lines
2.9 KiB
VB.net
92 lines
2.9 KiB
VB.net
Imports System.Xml
|
|
Imports System.Xml.Serialization
|
|
Imports System.Xml.XPath
|
|
Imports System.Xml.Xsl
|
|
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 XPathDocument
|
|
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 XPathDocument
|
|
Dim oProcessOutput, oProcessError, oFileContent As String
|
|
Dim oXmlDocument As XPathDocument
|
|
Dim oTempFile = System.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
|
|
oXmlDocument = New XPath.XPathDocument(oTempFile)
|
|
|
|
Catch ex As Exception
|
|
_logger.Error(ex)
|
|
Return Nothing
|
|
End Try
|
|
|
|
Return oXmlDocument
|
|
End Function
|
|
|
|
Public Function ParseXMLDocument(doc As XPathDocument)
|
|
Dim nav As XPathNavigator = doc.CreateNavigator()
|
|
Dim reader = nav.ReadSubtree()
|
|
|
|
Dim serializer As New XmlSerializer(GetType(CrossIndustryDocumentType))
|
|
Dim cross As CrossIndustryDocumentType = serializer.Deserialize(reader)
|
|
End Function
|
|
End Class
|