From e117dd6dc4b9e2687ed4b0c593f0740be4334b41 Mon Sep 17 00:00:00 2001 From: Sven Harazim Date: Thu, 26 Nov 2020 13:43:22 +0100 Subject: [PATCH] intf.XRechnungValidationHelperJava added --- .gitignore | 12 +- {Tools => Distribution}/README.md | 0 {Tools => Distribution}/installtools.ps1 | 15 +- Samples/XRechnungUnit1.dfm | 2 +- Samples/XRechnungUnit1.pas | 301 ++++++++++------------- intf.XRechnungValidationHelperJava.pas | 287 +++++++++++++++++++++ 6 files changed, 422 insertions(+), 195 deletions(-) rename {Tools => Distribution}/README.md (100%) rename {Tools => Distribution}/installtools.ps1 (80%) create mode 100644 intf.XRechnungValidationHelperJava.pas diff --git a/.gitignore b/.gitignore index 54e5db5..fcbca68 100644 --- a/.gitignore +++ b/.gitignore @@ -30,13 +30,11 @@ Dokumentation Samples/Win32 Samples/Win64 SaxonHE-Unmanaged -Tools/openxrechnungtoolbox -Tools/validator -Tools/validator-configuration-122 -Tools/validator-configuration-200 -Tools/jre -Tools/visualization -Tools/saxon +Distribution/validator +Distribution/validator-configuration-122 +Distribution/validator-configuration-200 +Distribution/visualization +Distribution/java Schema # Delphi compiler-generated binaries (safe to delete) diff --git a/Tools/README.md b/Distribution/README.md similarity index 100% rename from Tools/README.md rename to Distribution/README.md diff --git a/Tools/installtools.ps1 b/Distribution/installtools.ps1 similarity index 80% rename from Tools/installtools.ps1 rename to Distribution/installtools.ps1 index 6235480..eb529a7 100644 --- a/Tools/installtools.ps1 +++ b/Distribution/installtools.ps1 @@ -2,10 +2,6 @@ If (Test-Path validator){ Remove-Item validator -Recurse } -If (Test-Path openxrechnungtoolbox){ - Remove-Item openxrechnungtoolbox -Recurse -} - If (Test-Path validator-configuration-122){ Remove-Item validator-configuration-122 -Recurse } @@ -18,23 +14,23 @@ If (Test-Path visualization){ Remove-Item visualization -Recurse } -If (Test-Path jre){ - Remove-Item jre -Recurse +If (Test-Path java){ + Remove-Item java -Recurse } Invoke-WebRequest -Uri https://github.com/itplr-kosit/validator/releases/download/v1.4.0/validationtool-1.4.0.zip -OutFile validator.zip Invoke-WebRequest -Uri https://github.com/itplr-kosit/validator-configuration-xrechnung/releases/download/release-2019-12-30/validator-configuration-xrechnung_1.2.2_2019-12-30.zip -OutFile validator-configuration-122.zip Invoke-WebRequest -Uri https://github.com/itplr-kosit/validator-configuration-xrechnung/releases/download/release-2020-07-31/validator-configuration-xrechnung_2.0.0_2020-07-31.zip -OutFile validator-configuration-200.zip Invoke-WebRequest -Uri https://github.com/itplr-kosit/xrechnung-visualization/releases/download/v2020-07-31/xrechnung-2.0.0-xrechnung-visualization-2020-07-31.zip -OutFile visualization.zip -Invoke-WebRequest -Uri https://github.com/jcthiele/OpenXRechnungToolbox/releases/download/2020-08-28/OpenXRechnungToolbox_v1.0_Windows_64bit.zip -OutFile openxrechnungtoolbox.zip Invoke-WebRequest -Uri https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u265-b01/OpenJDK8U-jre_x64_windows_hotspot_8u265b01.zip -OutFile jre.zip Expand-Archive validator.zip Expand-Archive validator-configuration-122.zip Expand-Archive validator-configuration-200.zip Expand-Archive visualization.zip -Expand-Archive openxrechnungtoolbox.zip Expand-Archive jre.zip +Move-Item .\jre\jdk8u265-b01-jre .\java +Remove-Item jre -Recurse If (Test-Path validator.zip){ Remove-Item validator.zip @@ -48,9 +44,6 @@ If (Test-Path validator-configuration-200.zip){ If (Test-Path visualization.zip){ Remove-Item visualization.zip } -If (Test-Path openxrechnungtoolbox.zip){ - Remove-Item openxrechnungtoolbox.zip -} If (Test-Path jre.zip){ Remove-Item jre.zip } diff --git a/Samples/XRechnungUnit1.dfm b/Samples/XRechnungUnit1.dfm index 4f85c4c..ed411b6 100644 --- a/Samples/XRechnungUnit1.dfm +++ b/Samples/XRechnungUnit1.dfm @@ -1,7 +1,7 @@ object Form1: TForm1 Left = 0 Top = 0 - Caption = 'XRechnung 2.0.0' + Caption = 'XRechnung for Delphi v1.2.0' ClientHeight = 1183 ClientWidth = 1910 Color = clBtnFace diff --git a/Samples/XRechnungUnit1.pas b/Samples/XRechnungUnit1.pas index 6f8f9ef..51a83ca 100644 --- a/Samples/XRechnungUnit1.pas +++ b/Samples/XRechnungUnit1.pas @@ -8,6 +8,7 @@ interface ,System.IOUtils,System.Win.COMObj,System.UITypes ,Xml.xmldom,Xml.XMLDoc,Xml.XMLIntf,Xml.XMLSchema ,intf.XRechnung,intf.Invoice, Vcl.OleCtrls, SHDocVw, Vcl.ExtCtrls + ,intf.XRechnungValidationHelperJava ; type @@ -38,11 +39,14 @@ TForm1 = class(TForm) procedure btX200ConvertHTMLClick(Sender: TObject); procedure Button1Click(Sender: TObject); private - function ExecAndWait(Filename, Params: string): Boolean; procedure Generate122(inv : TInvoice); procedure Generate200(inv : TInvoice); public - toolsPath : String; + JavaRuntimeEnvironmentPath : String; + ValidatorLibPath : String; + ValidatorConfiguration122Path : String; + ValidatorConfiguration200Path : String; + VisualizationLibPath : String; end; var @@ -52,68 +56,19 @@ implementation {$R *.dfm} -procedure TForm1.btX122ConvertHTMLClick(Sender: TObject); -var - Doc : Variant; -begin - Memo3.Clear; - if FileExists(toolsPath+'validator\libs\Saxon-HE-9.9.1-7.jar') then - begin - ExecAndWait(toolsPath+'jre\jdk8u265-b01-jre\bin\java','-jar '+toolsPath+'validator\libs\Saxon-HE-9.9.1-7.jar '+ - '-s:'+ExtractFilePath(Application.ExeName)+'XRechnung-UBL-122.xml '+ - '-xsl:'+toolsPath+'visualization\xsl\ubl-invoice-xr.xsl '+ - '-o:'+ExtractFilePath(Application.ExeName)+'XRechnung-UBL-122-xr.xml'); - ExecAndWait(toolsPath+'jre\jdk8u265-b01-jre\bin\java','-jar '+toolsPath+'validator\libs\Saxon-HE-9.9.1-7.jar '+ - '-s:'+ExtractFilePath(Application.ExeName)+'XRechnung-UBL-122-xr.xml '+ - '-xsl:'+toolsPath+'visualization\xsl\xrechnung-html.xsl '+ - '-o:'+ExtractFilePath(Application.ExeName)+'XRechnung-UBL-122.html'); - WebBrowser1.Navigate2('file:\\\'+ExtractFilePath(Application.ExeName)+'XRechnung-UBL-122.html'); - end else - begin - Doc := WebBrowser1.Document; - Doc.Clear; - Doc.Write('Validation Tool nicht installiert. Siehe Verzeichnis ./Tools/Read.Me'); - Doc.Close; - end; -end; - -procedure TForm1.btX200ConvertHTMLClick(Sender: TObject); +procedure TForm1.FormCreate(Sender: TObject); var - Doc : Variant; + hstr : String; begin - Memo3.Clear; - if FileExists(toolsPath+'validator\libs\Saxon-HE-9.9.1-7.jar') then - begin - if rbFormat.ItemIndex = 0 then - begin - ExecAndWait(toolsPath+'jre\jdk8u265-b01-jre\bin\java','-jar '+toolsPath+'validator\libs\Saxon-HE-9.9.1-7.jar '+ - '-s:'+ExtractFilePath(Application.ExeName)+'XRechnung-UBL-200.xml '+ - '-xsl:'+toolsPath+'visualization\xsl\ubl-invoice-xr.xsl '+ - '-o:'+ExtractFilePath(Application.ExeName)+'XRechnung-UBL-200-xr.xml'); - ExecAndWait(toolsPath+'jre\jdk8u265-b01-jre\bin\java','-jar '+toolsPath+'validator\libs\Saxon-HE-9.9.1-7.jar '+ - '-s:'+ExtractFilePath(Application.ExeName)+'XRechnung-UBL-200-xr.xml '+ - '-xsl:'+toolsPath+'visualization\xsl\xrechnung-html.xsl '+ - '-o:'+ExtractFilePath(Application.ExeName)+'XRechnung-UBL-200.html'); - WebBrowser2.Navigate2('file:\\\'+ExtractFilePath(Application.ExeName)+'XRechnung-UBL-200.html'); - end - else begin - ExecAndWait(toolsPath+'jre\jdk8u265-b01-jre\bin\java','-jar '+toolsPath+'validator\libs\Saxon-HE-9.9.1-7.jar '+ - '-s:'+ExtractFilePath(Application.ExeName)+'XRechnung-UNCEFACT.xml '+ - '-xsl:'+toolsPath+'visualization\xsl\cii-xr.xsl '+ - '-o:'+ExtractFilePath(Application.ExeName)+'XRechnung-UNCEFACT-xr.xml'); - ExecAndWait(toolsPath+'jre\jdk8u265-b01-jre\bin\java','-jar '+toolsPath+'validator\libs\Saxon-HE-9.9.1-7.jar '+ - '-s:'+ExtractFilePath(Application.ExeName)+'XRechnung-UNCEFACT-xr.xml '+ - '-xsl:'+toolsPath+'visualization\xsl\xrechnung-html.xsl '+ - '-o:'+ExtractFilePath(Application.ExeName)+'XRechnung-UNCEFACT.html'); - WebBrowser2.Navigate2('file:\\\'+ExtractFilePath(Application.ExeName)+'XRechnung-UNCEFACT.html'); - end; - end else - begin - Doc := WebBrowser2.Document; - Doc.Clear; - Doc.Write('Validation Tool nicht installiert. Siehe Verzeichnis ./Tools/Read.Me'); - Doc.Close; - end; + hstr := ExtractFileDir(Application.ExeName); + hstr := ExtractFileDir(hstr); + hstr := ExtractFileDir(hstr); + hstr := ExtractFileDir(hstr)+PathDelim+'Distribution'+PathDelim; + JavaRuntimeEnvironmentPath := hstr +'java'+PathDelim; + ValidatorLibPath := hstr +'validator'+PathDelim; + ValidatorConfiguration122Path := hstr +'validator-configuration-122'+PathDelim; + ValidatorConfiguration200Path := hstr +'validator-configuration-200'+PathDelim; + VisualizationLibPath := hstr +'visualization'+PathDelim; end; procedure TForm1.Button1Click(Sender: TObject); @@ -683,136 +638,130 @@ procedure TForm1.Button4Click(Sender: TObject); end; end; -function TForm1.ExecAndWait(Filename, Params: string): Boolean; +procedure TForm1.Generate122(inv: TInvoice); var - SA: TSecurityAttributes; - SI: TStartupInfoA; - PI: TProcessInformation; - StdOutPipeRead, StdOutPipeWrite: THandle; - WasOK: Boolean; - Buffer: array[0..255] of AnsiChar; - BytesRead: Cardinal; - Handle:Boolean; - ProcessExitCode : DWORD; - ReadLine : AnsiString; + xml,cmdoutput,xmlresult,htmlresult : String; + Doc : Variant; begin - Result := false; + Memo3.Clear; + if rbFormat.itemindex <> 0 then + exit; + + TXRechnungInvoiceAdapter.SaveToXMLStr(inv,XRechnungVersion_122,xml); + + GetXRechnungValidationHelperJava.SetJavaRuntimeEnvironmentPath(JavaRuntimeEnvironmentPath) + .SetValidatorLibPath(ValidatorLibPath) + .SetValidatorConfigurationPath(ValidatorConfiguration122Path) + .Validate(xml,cmdoutput,xmlresult,htmlresult); + + Doc := WebBrowser1.Document; + Doc.Clear; + if htmlresult <> '' then + Doc.Write(htmlresult) + else + Doc.Write('Validation nicht erfolgreich. Siehe Verzeichnis ./Distribution/Read.Me'); + Doc.Close; + + Memo1.Lines.Text := xml; + btX122ConvertHTML.Visible := true; +end; - SA.nLength := SizeOf(SA); - SA.bInheritHandle := True; - SA.lpSecurityDescriptor := nil; +procedure TForm1.Generate200(inv: TInvoice); +var + xml,cmdoutput,xmlresult,htmlresult : String; + Doc : Variant; +begin + Memo3.Clear; + if rbFormat.itemindex = 0 then + begin + TXRechnungInvoiceAdapter.SaveToXMLStr(inv,XRechnungVersion_200_UBL,xml); - CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0); - try + GetXRechnungValidationHelperJava.SetJavaRuntimeEnvironmentPath(JavaRuntimeEnvironmentPath) + .SetValidatorLibPath(ValidatorLibPath) + .SetValidatorConfigurationPath(ValidatorConfiguration200Path) + .Validate(xml,cmdoutput,xmlresult,htmlresult); - FillChar(SI, SizeOf(SI), 0); - SI.cb := SizeOf(SI); - SI.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES; - SI.wShowWindow := SW_HIDE; - SI.hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin - SI.hStdOutput := StdOutPipeWrite; - SI.hStdError := StdOutPipeWrite; - - Handle := CreateProcessA(nil, PAnsiChar(AnsiString(Filename+ ' ' + Params)), - nil, nil, True, 0, nil, - PAnsiChar(AnsiString(ExtractFileDir(Application.ExeName))), SI, PI); - CloseHandle(StdOutPipeWrite); - if Handle then - try - repeat - WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil); - if BytesRead > 0 then - begin - ReadLine := Copy(Buffer,0,BytesRead); - Memo3.Lines.add(Trim(String(ReadLine))); - end; - until not WasOK or (BytesRead = 0); - WaitForSingleObject(PI.hProcess, INFINITE); - Result := GetExitCodeProcess(pi.hProcess, ProcessExitCode); - if Result then - Result := ProcessExitCode = 0; - finally - CloseHandle(PI.hThread); - CloseHandle(PI.hProcess); - end; - finally - CloseHandle(StdOutPipeRead); - end; -end; + Memo3.Lines.Text := cmdoutput; -procedure TForm1.FormCreate(Sender: TObject); -begin - toolsPath := ExtractFileDir(Application.ExeName); - toolsPath := ExtractFileDir(toolsPath); - toolsPath := ExtractFileDir(toolsPath); - toolsPath := ExtractFileDir(toolsPath)+PathDelim+'Tools'+PathDelim; + Doc := WebBrowser2.Document; + Doc.Clear; + if htmlresult <> '' then + Doc.Write(htmlresult) + else + Doc.Write('Validation nicht erfolgreich. Siehe Verzeichnis ./Distribution/Read.Me'); + Doc.Close; + + Memo2.Lines.Text := xml; + btX200ConvertHTML.Visible := true; + end + else + begin + TXRechnungInvoiceAdapter.SaveToXMLStr(inv,XRechnungVersion_200_UNCEFACT,xml); + + GetXRechnungValidationHelperJava.SetJavaRuntimeEnvironmentPath(JavaRuntimeEnvironmentPath) + .SetValidatorLibPath(ValidatorLibPath) + .SetValidatorConfigurationPath(ValidatorConfiguration200Path) + .Validate(xml,cmdoutput,xmlresult,htmlresult); + + Memo3.Lines.Text := cmdoutput; + + Doc := WebBrowser2.Document; + Doc.Clear; + if htmlresult <> '' then + Doc.Write(htmlresult) + else + Doc.Write('Validation nicht erfolgreich. Siehe Verzeichnis ./Distribution/Read.Me'); + Doc.Close; + + Memo2.Lines.Text := xml; + btX200ConvertHTML.Visible := true; + end; end; -procedure TForm1.Generate122(inv: TInvoice); +procedure TForm1.btX122ConvertHTMLClick(Sender: TObject); var - hstr : String; + xml,cmdoutput,htmlresult : String; Doc : Variant; begin - if rbFormat.itemindex = 0 then - begin - TXRechnungInvoiceAdapter.SaveToXMLStr(inv,XRechnungVersion_122,hstr); - TXRechnungInvoiceAdapter.SaveToFile(inv,XRechnungVersion_122,ExtractFilePath(Application.ExeName)+'XRechnung-UBL-122.xml'); - if FileExists(toolsPath+'validator\validationtool-1.4.0-java8-standalone.jar') then - begin - ExecAndWait(toolsPath+'jre\jdk8u265-b01-jre\bin\java','-jar '+toolsPath+'validator\validationtool-1.4.0-java8-standalone.jar -s '+toolsPath+'validator-configuration-122\scenarios.xml -h '+ExtractFilePath(Application.ExeName)+'XRechnung-UBL-122.xml'); - WebBrowser1.Navigate2('file:\\\'+ExtractFilePath(Application.ExeName)+'XRechnung-UBL-122-report.html'); - end else - begin - Doc := WebBrowser1.Document; - Doc.Clear; - Doc.Write('Validation Tool nicht installiert. Siehe Verzeichnis ./Tools/Read.Me'); - Doc.Close; - end; - Memo1.Lines.Text := hstr; - btX122ConvertHTML.Visible := FileExists(ExtractFilePath(Application.ExeName)+'XRechnung-UBL-122.xml'); - end; + xml := Memo1.Lines.Text; + + GetXRechnungValidationHelperJava.SetJavaRuntimeEnvironmentPath(JavaRuntimeEnvironmentPath) + .SetValidatorLibPath(ValidatorLibPath) + .SetVisualizationLibPath(VisualizationLibPath) + .Visualize(xml,true,cmdoutput,htmlresult); + + Memo3.Lines.Text := cmdoutput; + + Doc := WebBrowser1.Document; + Doc.Clear; + if htmlresult <> '' then + Doc.Write(htmlresult) + else + Doc.Write('Visualisierung nicht erfolgreich. Siehe Verzeichnis ./Distribution/Read.Me'); + Doc.Close; end; -procedure TForm1.Generate200(inv: TInvoice); +procedure TForm1.btX200ConvertHTMLClick(Sender: TObject); var - hstr : String; + xml,cmdoutput,htmlresult : String; Doc : Variant; begin - if rbFormat.itemindex = 0 then - begin - TXRechnungInvoiceAdapter.SaveToXMLStr(inv,XRechnungVersion_200_UBL,hstr); - TXRechnungInvoiceAdapter.SaveToFile(inv,XRechnungVersion_200_UBL,ExtractFilePath(Application.ExeName)+'XRechnung-UBL-200.xml'); - if FileExists(toolsPath+'validator\validationtool-1.4.0-java8-standalone.jar') then - begin - ExecAndWait(toolsPath+'jre\jdk8u265-b01-jre\bin\java','-jar '+toolsPath+'validator\validationtool-1.4.0-java8-standalone.jar -s '+toolsPath+'validator-configuration-200\scenarios.xml -h '+ExtractFilePath(Application.ExeName)+'XRechnung-UBL-200.xml'); - WebBrowser2.Navigate2('file:\\\'+ExtractFilePath(Application.ExeName)+'XRechnung-UBL-200-report.html'); - end else - begin - Doc := WebBrowser2.Document; - Doc.Clear; - Doc.Write('Validation Tool nicht installiert. Siehe Verzeichnis ./Tools/Read.Me'); - Doc.Close; - end; - Memo2.Lines.Text := hstr; - btX200ConvertHTML.Visible := FileExists(ExtractFilePath(Application.ExeName)+'XRechnung-UBL-200.xml'); - end else - begin - TXRechnungInvoiceAdapter.SaveToXMLStr(inv,XRechnungVersion_200_UNCEFACT,hstr); - TXRechnungInvoiceAdapter.SaveToFile(inv,XRechnungVersion_200_UNCEFACT,ExtractFilePath(Application.ExeName)+'XRechnung-UNCEFACT.xml'); - if FileExists(toolsPath+'validator\validationtool-1.4.0-java8-standalone.jar') then - begin - ExecAndWait(toolsPath+'jre\jdk8u265-b01-jre\bin\java','-jar '+toolsPath+'validator\validationtool-1.4.0-java8-standalone.jar -s '+toolsPath+'validator-configuration-200\scenarios.xml -h '+ExtractFilePath(Application.ExeName)+'XRechnung-UNCEFACT.xml'); - WebBrowser2.Navigate2('file:\\\'+ExtractFilePath(Application.ExeName)+'XRechnung-UNCEFACT-report.html'); - end else - begin - Doc := WebBrowser2.Document; - Doc.Clear; - Doc.Write('Validation Tool nicht installiert. Siehe Verzeichnis ./Tools/Read.Me'); - Doc.Close; - end; - Memo2.Lines.Text := hstr; - btX200ConvertHTML.Visible := FileExists(ExtractFilePath(Application.ExeName)+'XRechnung-UNCEFACT.xml'); - end; + xml := Memo2.Lines.Text; + + GetXRechnungValidationHelperJava.SetJavaRuntimeEnvironmentPath(JavaRuntimeEnvironmentPath) + .SetValidatorLibPath(ValidatorLibPath) + .SetVisualizationLibPath(VisualizationLibPath) + .Visualize(xml,rbFormat.itemindex=0,cmdoutput,htmlresult); + + Memo3.Lines.Text := cmdoutput; + + Doc := WebBrowser2.Document; + Doc.Clear; + if htmlresult <> '' then + Doc.Write(htmlresult) + else + Doc.Write('Visualisierung nicht erfolgreich. Siehe Verzeichnis ./Distribution/Read.Me'); + Doc.Close; end; end. diff --git a/intf.XRechnungValidationHelperJava.pas b/intf.XRechnungValidationHelperJava.pas new file mode 100644 index 0000000..fd4a6a5 --- /dev/null +++ b/intf.XRechnungValidationHelperJava.pas @@ -0,0 +1,287 @@ +{ +Copyright (C) 2020 Landrix Software GmbH & Co. KG +Sven Harazim, info@landrix.de +Version 1.1.0 + +License +This file is not official part of the package XRechnung-for-Delphi. + +This is provided as is, expressly without a warranty of any kind. +You use it at your own risc. +} + +unit intf.XRechnungValidationHelperJava; + +interface + +uses + Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes + ,System.IOUtils,System.Win.COMObj,System.UITypes + ,Xml.xmldom,Xml.XMLDoc,Xml.XMLIntf,Xml.XMLSchema + ; + +type + IXRechnungValidationHelperJava = interface + ['{6DCEC6AF-1B1B-4C65-B004-B335397CF10D}'] + function SetJavaRuntimeEnvironmentPath(const _Path : String) : IXRechnungValidationHelperJava; + function SetValidatorLibPath(const _Path : String) : IXRechnungValidationHelperJava; + function SetValidatorConfigurationPath(const _Path : String) : IXRechnungValidationHelperJava; + function SetVisualizationLibPath(const _Path : String) : IXRechnungValidationHelperJava; + function Validate(const _InvoiceXMLData : String; out _CmdOutput,_ValidationResultAsXML,_ValidationResultAsHTML : String) : Boolean; + function Visualize(const _InvoiceXMLData : String; _TrueIfUBL_FalseIfCII : Boolean; out _CmdOutput,_VisualizationAsHTML : String) : Boolean; + end; + + function GetXRechnungValidationHelperJava : IXRechnungValidationHelperJava; + +implementation + +type + TXRechnungValidationHelperJava = class(TInterfacedObject,IXRechnungValidationHelperJava) + private + JavaRuntimeEnvironmentPath : String; + ValidatorLibPath : String; + ValidatorConfigurationPath : String; + VisualizationLibPath : String; + CmdOutput : TStringList; + function ExecAndWait(_Filename, _Params: string): Boolean; + public + constructor Create; + destructor Destroy; override; + function SetJavaRuntimeEnvironmentPath(const _Path : String) : IXRechnungValidationHelperJava; + function SetValidatorLibPath(const _Path : String) : IXRechnungValidationHelperJava; + function SetValidatorConfigurationPath(const _Path : String) : IXRechnungValidationHelperJava; + function SetVisualizationLibPath(const _Path : String) : IXRechnungValidationHelperJava; + function Validate(const _InvoiceXMLData : String; out _CmdOutput,_ValidationResultAsXML,_ValidationResultAsHTML : String) : Boolean; + function Visualize(const _InvoiceXMLData : String; _TrueIfUBL_FalseIfCII : Boolean; out _CmdOutput,_VisualizationAsHTML : String) : Boolean; + end; + +function GetXRechnungValidationHelperJava : IXRechnungValidationHelperJava; +begin + Result := TXRechnungValidationHelperJava.Create; +end; + +{ TXRechnungValidationHelperJava } + +constructor TXRechnungValidationHelperJava.Create; +begin + CmdOutput := TStringList.Create; +end; + +destructor TXRechnungValidationHelperJava.Destroy; +begin + if Assigned(CmdOutput) then begin CmdOutput.Free; CmdOutput := nil; end; + inherited; +end; + +function TXRechnungValidationHelperJava.ExecAndWait(_Filename, _Params: string): Boolean; +var + SA: TSecurityAttributes; + SI: TStartupInfoA; + PI: TProcessInformation; + StdOutPipeRead, StdOutPipeWrite: THandle; + WasOK: Boolean; + Buffer: array[0..255] of AnsiChar; + BytesRead: Cardinal; + Handle:Boolean; + ProcessExitCode : DWORD; + ReadLine : AnsiString; +begin + Result := false; + CmdOutput.Clear; + + SA.nLength := SizeOf(SA); + SA.bInheritHandle := True; + SA.lpSecurityDescriptor := nil; + + CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0); + try + + FillChar(SI, SizeOf(SI), 0); + SI.cb := SizeOf(SI); + SI.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES; + SI.wShowWindow := SW_HIDE; + SI.hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin + SI.hStdOutput := StdOutPipeWrite; + SI.hStdError := StdOutPipeWrite; + + Handle := CreateProcessA(nil, PAnsiChar(AnsiString(_Filename+ ' ' + _Params)), + nil, nil, True, 0, nil, + PAnsiChar(AnsiString(ExtractFileDir(ParamStr(0)))), SI, PI); + CloseHandle(StdOutPipeWrite); + if Handle then + try + repeat + WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil); + if BytesRead > 0 then + begin + ReadLine := Copy(Buffer,0,BytesRead); + CmdOutput.add(Trim(String(ReadLine))); + end; + until not WasOK or (BytesRead = 0); + WaitForSingleObject(PI.hProcess, INFINITE); + Result := GetExitCodeProcess(pi.hProcess, ProcessExitCode); + if Result then + Result := ProcessExitCode = 0; + finally + CloseHandle(PI.hThread); + CloseHandle(PI.hProcess); + end; + finally + CloseHandle(StdOutPipeRead); + end; +end; + +function TXRechnungValidationHelperJava.SetJavaRuntimeEnvironmentPath( + const _Path: String): IXRechnungValidationHelperJava; +begin + JavaRuntimeEnvironmentPath := IncludeTrailingPathDelimiter(_Path); + Result := self; +end; + +function TXRechnungValidationHelperJava.SetValidatorConfigurationPath( + const _Path: String): IXRechnungValidationHelperJava; +begin + ValidatorConfigurationPath := IncludeTrailingPathDelimiter(_Path); + Result := self; +end; + +function TXRechnungValidationHelperJava.SetValidatorLibPath(const _Path: String): IXRechnungValidationHelperJava; +begin + ValidatorLibPath := IncludeTrailingPathDelimiter(_Path); + Result := self; +end; + +function TXRechnungValidationHelperJava.SetVisualizationLibPath(const _Path: String): IXRechnungValidationHelperJava; +begin + VisualizationLibPath := IncludeTrailingPathDelimiter(_Path); + Result := self; +end; + +function TXRechnungValidationHelperJava.Validate(const _InvoiceXMLData: String; out _CmdOutput, + _ValidationResultAsXML, _ValidationResultAsHTML: String): Boolean; +var + hstrl,cmd: TStringList; + tmpFilename : String; +begin + Result := false; + if _InvoiceXMLData = '' then + exit; + if not FileExists(JavaRuntimeEnvironmentPath+'bin\java.exe') then + exit; + if not FileExists(ValidatorLibPath+'validationtool-1.4.0-java8-standalone.jar') then + exit; + if not FileExists(ValidatorConfigurationPath+'scenarios.xml') then + exit; + + tmpFilename := TPath.GetTempFileName; + + hstrl := TStringList.Create; + cmd := TStringList.Create; + try + hstrl.Text := _InvoiceXMLData; + hstrl.SaveToFile(tmpFilename,TEncoding.UTF8); + + cmd.Add('pushd '+ExtractFilePath(tmpFilename)); + cmd.Add(JavaRuntimeEnvironmentPath+'bin\java -jar '+ + ValidatorLibPath+'validationtool-1.4.0-java8-standalone.jar -s '+ + ValidatorConfigurationPath+'scenarios.xml -h '+ + tmpFilename); + cmd.SaveToFile(tmpFilename+'.bat',TEncoding.ANSI); + + Result := ExecAndWait(tmpFilename+'.bat',''); + + _CmdOutput := CmdOutput.Text; + + DeleteFile(tmpFilename+'.bat'); + DeleteFile(tmpFilename); + + if FileExists(ChangeFileExt(tmpFilename,'-report.xml')) then + begin + hstrl.LoadFromFile(ChangeFileExt(tmpFilename,'-report.xml'),TEncoding.UTF8); + _ValidationResultAsXML := hstrl.Text; + DeleteFile(ChangeFileExt(tmpFilename,'-report.xml')); + end; + + if FileExists(ChangeFileExt(tmpFilename,'-report.html')) then + begin + hstrl.LoadFromFile(ChangeFileExt(tmpFilename,'-report.html'),TEncoding.UTF8); + _ValidationResultAsHTML := hstrl.Text; + DeleteFile(ChangeFileExt(tmpFilename,'-report.html')); + end; + + finally + hstrl.Free; + end; +end; + +function TXRechnungValidationHelperJava.Visualize(const _InvoiceXMLData: String; + _TrueIfUBL_FalseIfCII : Boolean; + out _CmdOutput, _VisualizationAsHTML: String): Boolean; +var + hstrl,cmd: TStringList; + tmpFilename : String; +begin + Result := false; + if _InvoiceXMLData = '' then + exit; + if not FileExists(JavaRuntimeEnvironmentPath+'bin\java.exe') then + exit; + if not FileExists(ValidatorLibPath+'libs\Saxon-HE-9.9.1-7.jar') then + exit; + if _TrueIfUBL_FalseIfCII then + if not FileExists(VisualizationLibPath+'xsl\ubl-invoice-xr.xsl') then + exit; + if not _TrueIfUBL_FalseIfCII then + if not FileExists(VisualizationLibPath+'xsl\cii-xr.xsl') then + exit; + + if not FileExists(VisualizationLibPath+'xsl\xrechnung-html.xsl') then + exit; + + tmpFilename := TPath.GetTempFileName; + + hstrl := TStringList.Create; + cmd := TStringList.Create; + try + hstrl.Text := _InvoiceXMLData; + hstrl.SaveToFile(tmpFilename,TEncoding.UTF8); + + cmd.Add('pushd '+ExtractFilePath(tmpFilename)); + if _TrueIfUBL_FalseIfCII then + cmd.Add(JavaRuntimeEnvironmentPath+'bin\java -jar '+ + ValidatorLibPath+'libs\Saxon-HE-9.9.1-7.jar -s:'+tmpFilename+ + ' -xsl:'+VisualizationLibPath+'xsl\ubl-invoice-xr.xsl'+ + ' -o:'+ChangeFileExt(tmpFilename,'-xr.xml')) + else + cmd.Add(JavaRuntimeEnvironmentPath+'bin\java -jar '+ + ValidatorLibPath+'libs\Saxon-HE-9.9.1-7.jar -s:'+tmpFilename+ + ' -xsl:'+VisualizationLibPath+'xsl\cii-xr.xsl'+ + ' -o:'+ChangeFileExt(tmpFilename,'-xr.xml')); + cmd.Add(JavaRuntimeEnvironmentPath+'bin\java -jar '+ + ValidatorLibPath+'libs\Saxon-HE-9.9.1-7.jar -s:'+ChangeFileExt(tmpFilename,'-xr.xml')+ + ' -xsl:'+VisualizationLibPath+'xsl\xrechnung-html.xsl'+ + ' -o:'+ChangeFileExt(tmpFilename,'-.html')); + + cmd.SaveToFile(tmpFilename+'.bat',TEncoding.ANSI); + + Result := ExecAndWait(tmpFilename+'.bat',''); + + _CmdOutput := CmdOutput.Text; + + DeleteFile(tmpFilename+'.bat'); + DeleteFile(tmpFilename); + DeleteFile(ChangeFileExt(tmpFilename,'-xr.xml')); + + if FileExists(ChangeFileExt(tmpFilename,'-.html')) then + begin + hstrl.LoadFromFile(ChangeFileExt(tmpFilename,'-.html'),TEncoding.UTF8); + _VisualizationAsHTML := hstrl.Text; + DeleteFile(ChangeFileExt(tmpFilename,'-.html')); + end; + + finally + hstrl.Free; + end; +end; + +end.