laz2fpreport.pp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. program laz2fpreport;
  2. {$mode objfpc}{$H+}
  3. uses
  4. Classes, SysUtils, CustApp, fpjson, fpreport, fplazreport, fpreportstreamer;
  5. type
  6. { TLazToFPReport }
  7. TLazToFPReport = class(TCustomApplication)
  8. Private
  9. FLazReport : TFPLazReport;
  10. FInputFile,
  11. FOutputFile : String;
  12. FFormatOutput : Boolean;
  13. FVerbose : Boolean;
  14. procedure Convert;
  15. procedure DoVerbose(Sender: TOBject; const Msg: String);
  16. protected
  17. procedure DoRun; override;
  18. public
  19. constructor Create(TheOwner: TComponent); override;
  20. destructor Destroy; override;
  21. procedure WriteHelp(Const aMsg :String); virtual;
  22. end;
  23. { TLazToFPReport }
  24. procedure TLazToFPReport.DoRun;
  25. var
  26. ErrorMsg: String;
  27. begin
  28. // quick check parameters
  29. ErrorMsg:=CheckOptions('hi:o:vf', ['help','input:','output:','verbose','format']);
  30. if (ErrorMsg<>'') or HasOption('h','help') then
  31. WriteHelp(ErrorMsg);
  32. FInputFile:=GetOptionValue('i','input');
  33. if FInputFile='' then
  34. WriteHelp('No input file specified.');
  35. FOutputFile:=GetOptionValue('o','output');
  36. If FOutputFile='' then
  37. FOutputFile:=ChangeFileExt(FinputFile,'.json');
  38. FFormatOutput:=HasOption('f','format');
  39. FVerbose:=HasOption('v','verbose');
  40. if FVerbose then
  41. FLazReport.OnLog:=@DoVerbose;
  42. Convert;
  43. Terminate;
  44. end;
  45. procedure TLazToFPReport.Convert;
  46. Var
  47. S : TFPReportJSONStreamer;
  48. F : TFileStream;
  49. J : TJSONStringType;
  50. begin
  51. FLazReport.LoadFromFile(FInputFile);
  52. F:=Nil;
  53. S:=TFPReportJSONStreamer.Create(Self);
  54. try
  55. FLazReport.WriteElement(S);
  56. if FFormatOutput then
  57. J:=S.JSON.FormatJSON()
  58. else
  59. J:=S.JSON.AsJSON;
  60. F:=TFileStream.Create(FOutputFile,fmCreate);
  61. F.Write(J[1],Length(J)); // Single byte type.
  62. finally
  63. F.Free;
  64. S.Free;
  65. end;
  66. end;
  67. procedure TLazToFPReport.DoVerbose(Sender: TOBject; const Msg: String);
  68. begin
  69. if FVerbose then
  70. Writeln(StdErr,Msg);
  71. end;
  72. constructor TLazToFPReport.Create(TheOwner: TComponent);
  73. begin
  74. inherited Create(TheOwner);
  75. StopOnException:=True;
  76. FLazReport:=TFPLazReport.Create(Self);
  77. end;
  78. destructor TLazToFPReport.Destroy;
  79. begin
  80. FreeAndNil(FLazReport);
  81. inherited Destroy;
  82. end;
  83. procedure TLazToFPReport.WriteHelp(const aMsg: String);
  84. begin
  85. if (aMsg<>'') then
  86. Writeln('Error : ',aMsg);
  87. writeln('Usage: ', ExeName, ' [options] -i filename');
  88. Writeln('Where options are: ');
  89. Writeln('-f --format Write formatted JSON to output file');
  90. Writeln('-h --help This help message');
  91. Writeln('-i --input=filename input file name, must be a .lrf file, in XML format.');
  92. Writeln('-o --output=filename output file name.');
  93. Writeln(' If not specified, input file with extension changed to .json is used.');
  94. Writeln('-v --verbose Print some diagnostic information');
  95. Halt(Ord(aMsg<>''));
  96. end;
  97. var
  98. Application: TLazToFPReport;
  99. begin
  100. Application:=TLazToFPReport.Create(nil);
  101. Application.Title:='LazReport to FPReport Converter';
  102. Application.Run;
  103. Application.Free;
  104. end.