File: fr3tolrf.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (566 lines) | stat: -rw-r--r-- 17,117 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
{ A LazReport loader for FastReport 3 formats

  Copyright (C) 2013 Jesus Reyes Aguilar jesusrmx@yahoo.com.mx

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version with the following modification:

  As a special exception, the copyright holders of this library give you
  permission to link this library with independent modules to produce an
  executable, regardless of the license terms of these independent modules,and
  to copy and distribute the resulting executable under terms of your choice,
  provided that you also meet, for each linked independent module, the terms
  and conditions of the license of that module. An independent module is a
  module which is not derived from or based on this library. If you modify
  this library, you may extend this exception to your version of the library,
  but you are not obligated to do so. If you do not wish to do so, delete this
  exception statement from your version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}
unit fr3tolrf;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Laz2_DOM, Laz2_XMLRead, LConvEncoding, FileUtil, Graphics, lr_class;

type
  EFR3ReaderException = class(Exception);

  function LoadFastReport3(Report: TfrReport; aFileName: string; out log:string): Integer;

implementation

type

  { TAttributeEnumerator }

  TAttributeEnumerator = class
  private
    fIndex: Integer;
    fNode: TDOMNode;
    function GetCurrent: TDOMNode;
  public
    constructor Create(const A: TDOMNode);
    property Current: TDOMNode read GetCurrent;
    function MoveNext: Boolean;
  end;

  operator Enumerator(const A: TDOMNode): TAttributeEnumerator;
   begin
     Result := TAttributeEnumerator.Create(A);
   end;

type
  TClassName = string[30];
  TKnownObjects = record
    frClass: TClassName;
    lrClass: TClassName;
    Typ: Integer;
    BandType: TfrBandType;
  end;

  { Tfr3Reader }

  Tfr3Reader = class
  private
    fReport: TfrReport;
    fLog: TStringList;
    fObj: array of TKnownObjects;
    fUsedNodes: TFPList;
    function GetMessages: string;
    function MMToPt(AValue: Extended): Integer;
    function MMToScrPt(AValue: Extended): Integer;
    function CreateView(Page: TfrPage; frClass: string): TfrView;
    function NodeValToCutStr(Node: TDOMNode): string;
    function NodeValToFloat(Node:TDOMNode; defValue:Extended=0.0): Extended;
    function MatchAttribute(Attr: TDOMNode; AttrName:string): boolean;
    function EncodeUTF8Checking(s:string): string;
    //
    procedure PopulateKnownObjects;
    function IndexOfFrClass(frClass: TClassName): Integer;
    procedure AddObj(frClass,lrClass:TClassName; Typ:Integer; BandType: TfrBandType);
  protected
    procedure LoadReport(Node: TDOMNode); virtual;
    procedure LoadPages(Node: TDOMNode); virtual;
    procedure LoadPage(Node: TDOMNode; Page: TfrPage); virtual;
    procedure LoadView(Node: TDOMNode; Page: TfrPage; View, ParentView: TfrView);
    procedure LoadCommonView(Node: TDOMNode; Page: TfrPage; ParentView, View: TfrView);
    procedure LoadMemoView(Node: TDOMNode; Page:TfrPage; View: TfrMemoView);
    procedure LoadBandView(Node: TDOMNode; Page:TfrPage; View: TfrBandView);
    procedure ProcessObject(Page:TfrPage; Node: TDOMNode; ParentView: TfrView = nil);
    procedure Log(s:string);
  public
    destructor destroy; override;
    procedure LoadFromFile(aFileName: string);
    property Report: TfrReport read fReport write fReport;
    property Messages: string read GetMessages;
  end;

function LoadFastReport3(Report: TfrReport; aFileName: string; out Log:string): Integer;
var
  Reader: Tfr3Reader;
begin
  Result := 0;
  Reader := Tfr3Reader.Create;
  Reader.Report := Report;
  try
    Reader.LoadFromFile(aFileName);
    log := Reader.Messages;
  finally
    Reader.Free;
  end;
end;

function DelphiIntToFpcFontStyle(aStyle: Integer): TFontStyles;
begin
  result := TFontStyles(aStyle);
end;

function DelphiIntToFPCFrameBorders(aFrameTyp: Integer): TfrFrameBorders;
begin
  result := [];
  if (aFrameTyp and 1) <> 0 then include(result, frbLeft);
  if (aFrameTyp and 2) <> 0 then include(result, frbRight);
  if (aFrameTyp and 4) <> 0 then include(result, frbTop);
  if (aFrameTyp and 8) <> 0 then include(result, frbBottom);
end;


{ TAttributeEnumerator }

function TAttributeEnumerator.GetCurrent: TDOMNode;
begin
  result := fNode.Attributes.Item[fIndex];
end;

constructor TAttributeEnumerator.Create(const A: TDOMNode);
begin
  inherited create;
  fIndex := -1;
  fNode := A;
end;

function TAttributeEnumerator.MoveNext: Boolean;
begin
  inc(fIndex);
  result := (fNode<>nil) and (fIndex<fNode.Attributes.Length);
end;

function Tfr3Reader.GetMessages: string;
begin
  if fLog<>nil then
    result := fLog.Text
  else
    result := '';
end;

function Tfr3Reader.MMToPt(AValue: Extended): Integer;
begin
  result := round(AValue * 25.4 / 72);
end;

function Tfr3Reader.MMToScrPt(AValue: Extended): Integer;
begin
  result := round(AValue * 18 / 5);
end;

function Tfr3Reader.NodeValToCutStr(Node: TDOMNode): string;
begin
  result := Copy(Node.NodeValue, 1, 40);
  if Length(Node.NodeValue)>40 then
    result := result + '...';
  result := '"'+result+'"';
end;

function Tfr3Reader.NodeValToFloat(Node: TDOMNode; defValue:Extended=0.0): Extended;
var
  s: string;
  settings: TFormatSettings;
begin
  s := Node.NodeValue;
  Settings.ThousandSeparator:=',';
  Settings.DecimalSeparator:='.';
  if not tryStrToFloat(s, result, settings) then begin
    if pos(',',s)>0 then begin
      Settings.ThousandSeparator:='.';
      settings.DecimalSeparator:=',';
      if not TryStrToFloat(s, result, settings) then
        result := defValue;
    end else
      result := defValue;
  end;
end;

function Tfr3Reader.MatchAttribute(Attr: TDOMNode; AttrName: string): boolean;
var
  i: Integer;
begin
  result := Attr.NodeName=AttrName;
  if result then begin
    if fUsedNodes=nil then
      fUsedNodes := TFPList.Create;
    i := fUsedNodes.IndexOf(Attr);
    if i<0 then
      fUsedNodes.Add(Attr);
  end;
end;

function Tfr3Reader.EncodeUTF8Checking(s: string): string;
var
  enc: String;
begin
  enc := GuessEncoding(s);
  result :=  ConvertEncoding(s, Enc, EncodingUTF8);
end;

procedure Tfr3Reader.PopulateKnownObjects;
begin
  AddObj('TfrxReportTitle',   'TfrBandView',    gtBand,     btReportTitle);
  AddObj('TfrxPageHeader',    'TfrBandView',    gtBand,     btPageHeader);
  AddObj('TfrxMasterData',    'TfrBandView',    gtBand,     btMasterData);
  AddObj('TfrxPageFooter',    'TfrBandView',    gtBand,     btPageFooter);

  AddObj('TfrxDetailData',    'TfrBandView',    gtBand,     btDetailData);
  AddObj('TfrxSubdetailData', 'TfrBandView',    gtBand,     btSubDetailData);

  AddObj('TfrxMemoView',      'TfrMemoView',    gtMemo,     btNone);
  AddObj('TfrxLineView',      'TfrLineView',    gtLine,     btNone);
  AddObj('TfrxShapeView',     'TfrShapeView',   gtaddIn,    btNone);
  AddObj('TfrxPictureView',   'TfrPictureView', gtPicture,  btNone);
end;

function Tfr3Reader.IndexOfFrClass(frClass: TClassName): Integer;
var
  i: Integer;
begin
  result := -1;
  for i:=0 to Length(fObj)-1 do
    if fObj[i].frClass=frClass then begin
      result := i;
      break;
    end;
end;

procedure Tfr3Reader.AddObj(frClass, lrClass: TClassName; Typ: Integer;
  BandType: TfrBandType);
var
  i: Integer;
begin
  i := IndexOfFrClass(frClass);
  if i<0 then begin
    i := Length(fObj);
    SetLength(fObj, i+1);
    fObj[i].frClass:=frClass;
    fObj[i].lrClass:=lrClass;
    fObj[i].Typ := Typ;
    fObj[i].BandType:=BandType;
  end;
end;

procedure Tfr3Reader.LoadReport(Node: TDOMNode);
var
  Attr: TDOMNode;
begin
  Report.Clear;

  PopulateKnownObjects;

  for Attr in Node do begin
    if Attr.NodeName='ReportOptions.CreateDate' then
      Report.ReportCreateDate := NodeValToFloat(Attr)
    else
    if Attr.NodeName='ReportOptions.Description.Text' then
      Report.Subject := Attr.NodeValue
    else
    if Attr.NodeName='ReportOptions.LastChange' then
      Report.ReportLastChange := NodeValToFloat(Attr)
    else
    if Attr.NodeName='ScriptLanguage' then
      Report.Script.Insert(0, '// ScriptLanguage='+Attr.NodeValue)
    else
    if Attr.NodeName='ScriptText.Text' then begin
      Report.Script.Text := Report.Script.Text + '{'^M + Attr.NodeValue + ^M'}';
    end
    else
      Log('Ignored: TfrxReport.'+Attr.NodeName+'='+NodeValToCutStr(Attr));
  end;

  LoadPages(Node);
end;

procedure Tfr3Reader.LoadPages(Node: TDOMNode);
var
  i: Integer;
begin
  Node := Node.FirstChild;
  while Node<>nil do begin
    if Node.NodeName='TfrxReportPage' then begin
      i := Report.Pages.Count;
      Report.Pages.Add;
      LoadPage(Node, Report.Pages[i]);
    end else
      Log('Unknown Page class: '+Node.NodeName);
    Node := Node.NextSibling;
  end;
end;

procedure Tfr3Reader.LoadPage(Node: TDOMNode; Page: TfrPage);
var
  Attr: TDOMNode;
begin

  for Attr in Node do begin
    if Attr.NodeName='Name' then
      Page.Name:= Attr.NodeValue
    else if Attr.NodeName='PaperWidth' then
      Page.Width := MMToPt(NodeValToFloat(Attr))
    else if Attr.NodeNAme='PaperHeight' then
      Page.Height := MMToPt(NodeValToFloat(Attr))
    else if Attr.NodeName='PaperSize' then
      Page.pgSize := StrToIntDef(Attr.NodeValue, 9)
    else if Attr.NodeName='LeftMargin' then
      Page.Margins.Left := MMToScrPt(NodeValToFloat(Attr))
    else if Attr.NodeName='TopMargin' then
      Page.Margins.Top := MMToScrPt(NodeValToFloat(Attr))
    else if Attr.NodeName='BottomMargin' then
      Page.Margins.Bottom := MMToScrPt(NodeValToFloat(Attr))
    else if Attr.NodeName='RightMargin' then
      Page.Margins.Right := MMToScrPt(NodeValToFloat(Attr))
    else
      Log('Ignored: TfrxPageReport.'+Attr.NodeName+'='+NodeValToCutStr(Attr));
  end;

  // objects
  Node := Node.FirstChild;
  while Node<>nil do begin
    ProcessObject(Page, Node);
    Node := Node.NextSibling;
  end;
end;

procedure Tfr3Reader.LoadView(Node: TDOMNode; Page: TfrPage; View, ParentView: TfrView);
var
  Attr: TDOMNode;
begin
  fUsedNodes := nil;

  LoadCommonView(Node, Page, ParentView, View);

  if View is TfrMemoView then
    LoadMemoView(Node, Page, TfrMemoView(View))
  else
  if View is TfrBandView then
    LoadBandView(Node, Page, TfrBandView(View));

  // dump ignored attributes
  for Attr in Node do begin
    // attributes that we know and don't support
    if Attr.NodeName='ParentFont' then
      continue;
    // check the rest
    if (fUsedNodes=nil) or (fUsedNodes.IndexOf(Attr)<0) then
      Log(format('Ignored Attribute: %s:%s %s=%s',[View.ClassName, View.Name, Attr.Nodename,NodeValToCutStr(Attr)]));
  end;

  fUsedNodes.Free;
end;

procedure Tfr3Reader.LoadCommonView(Node: TDOMNode; Page: TfrPage;
  ParentView, View: TfrView );
var
  attr: TDOMNode;
  k: double;
begin
  k := 0.945; // TODO: value obtained by try, needs to check real value
  for attr in Node do begin
    if MatchAttribute(Attr, 'Name') then
      View.Name := attr.NodeValue
    else if MatchAttribute(Attr, 'Visible') then
      View.Visible := StrToBoolDef(attr.NodeValue, true)
    else if MatchAttribute(Attr, 'Left') then
      View.Left := Page.Margins.Left + NodeValToFloat(Attr) * k
    else if MatchAttribute(Attr, 'Top') then begin
      View.Top := Page.Margins.Top + NodeValToFloat(Attr) * k;
      if ParentView<>nil then
        View.Top := ParentView.Top + View.Top - Page.Margins.Top;
    end else if MatchAttribute(Attr, 'Width') then
      View.Width := NodeValToFloat(Attr) * k
    else if MatchAttribute(Attr, 'Height') then
      View.Height := NodeValToFloat(Attr) * k
    else if MatchAttribute(Attr, 'Color') then
      View.FillColor := StrToIntDef(attr.NodeValue, clWhite)
    else if MatchAttribute(Attr, 'Frame.Typ') then
      View.Frames := DelphiIntToFPCFrameBorders(StrToIntDef(attr.NodeValue, 0))
    else if MatchAttribute(Attr, 'Frame.Width') then
      View.FrameWidth := NodeValToFloat(Attr, 1.0);
  end;
end;

procedure Tfr3Reader.LoadMemoView(Node: TDOMNode; Page: TfrPage;
  View: TfrMemoView);
var
  attr: TDOMNode;
  DFDecimalSeparator,DFFormatStr,DFKind: string;
begin

  DFDecimalSeparator := '';
  DFFormatStr := '';
  DFKind := '';

  for Attr in Node do begin

    if MatchAttribute(Attr, 'Font.Color') then
      View.Font.Color := StrToIntDef(attr.NodeValue, 0)
    else if MatchAttribute(Attr, 'Font.Charset') then
      View.Font.CharSet := StrToIntDef(attr.NodeValue, 1)
    else if MatchAttribute(Attr, 'Font.Height') then
      View.Font.Size := Round(-StrToIntDef(attr.NodeValue, -10)*72/96)
    else if MatchAttribute(Attr, 'Font.Name') then
      View.Font.Name := attr.NodeValue
    else if MatchAttribute(Attr, 'Font.Style') then
      View.Font.Style := DelphiIntToFPCFontStyle(StrToIntDef(attr.NodeValue, 0))
    else if MatchAttribute(Attr, 'HAlign') then begin
      if attr.NodeValue='haCenter' then View.Alignment := taCenter;
      if attr.NodeValue='haRight' then View.Alignment := taRightJustify;
    end else if MatchAttribute(Attr, 'VAlign') then begin
      if attr.NodeValue='vaCenter' then View.Layout := tlCenter;
    end else if MatchAttribute(Attr, 'Memo.Text') then
      View.Memo.Text := EncodeUTF8Checking(attr.NodeValue)
    else if MatchAttribute(Attr, 'Text') then
      View.Memo.Text := EncodeUTF8Checking(attr.NodeValue)
    else if MatchAttribute(Attr, 'WordWrap') then
      View.WordWrap := StrToBoolDef(attr.NodeValue, false)
    else if MatchAttribute(Attr, 'AutoWidth') then begin
      View.AutoSize := StrToBoolDef(attr.NodeValue, false);
      if View.AutoSize then
        View.Width := 150; // a default value
    end else if MatchAttribute(Attr, 'DisplayFormat.DecimalSeparator') then
      DFDecimalSeparator := attr.NodeValue
    else if MatchAttribute(Attr, 'DisplayFormat.FormatStr') then
      DFFormatStr := attr.NodeValue
    else if MatchAttribute(Attr, 'DisplayFormat.Kind') then begin
      DFKind := attr.NodeValue;
    end;
  end;

  if (DFFormatStr<>'') then begin
    if DFKind='fkNumeric' then begin
      if DFDecimalSeparator='' then
        DFDecimalSeparator := '.';
      View.Format := $01040000 or ord(DFDecimalSeparator[1]);
      View.FormatStr := DFFormatStr;
      if DFFormatStr='%2.2n' then
        View.FormatStr := '##.00'
      else begin
        View.FormatStr := '#';
        Log(Format('Warning: %s:%s changed default numeric format from %s to %s',
          [View.ClassName,View.Name,DFFormatStr,'#']));
      end;
    end else
      Log(format('Warning: %s:%s don''t know how to handle %s format using %s pattern',
        [View.ClassName, View.Name, DFKind, DFFormatStr]));
  end;
end;

procedure Tfr3Reader.LoadBandView(Node: TDOMNode; Page: TfrPage;
  View: TfrBandView);
var
  Attr: TDOMNode;
begin
  for Attr in Node do begin
    //if MatchAttribute(Attr, 'DataSet') then
    //  View.DataSet := Attr.NodeValue
    if MatchAttribute(Attr, 'DataSetName') then
      View.DataSet := Attr.NodeValue
    else if MatchAttribute(Attr, 'Stretched') then
      View.Stretched := StrToBoolDef(Attr.NodeValue, false);
  end;
end;

function Tfr3Reader.CreateView(Page: TfrPage; frClass: string): TfrView;
var
  i: Integer;
begin
  i := IndexOfFrClass(frClass);
  if i>=0 then begin
    result := frCreateObject(fObj[i].Typ, fObj[i].lrClass, Page);
    if result is TfrBandView then
      TfrBandView(result).BandType:=fObj[i].BandType;
  end else begin
    result := nil;
    Log(format('Found unknown class %s ',[frClass]));
  end;
end;

procedure Tfr3Reader.ProcessObject(Page:TfrPage; Node: TDOMNode;
  ParentView: TfrView = nil);
var
  cNode: TDOMNode;
  View: TfrView;
begin
  View := CreateView(Page, Node.NodeName);
  if View<>nil then
  begin
    View.BeginUpdate;
//    Page.Objects.Add(View);
    LoadView(Node, Page, View, ParentView);
    View.EndUpdate;
    // process any child
    cNode := Node.FirstChild;
    while cNode<>nil do
    begin
      ProcessObject(Page, cNode, View);
      cNode := cNode.NextSibling;
    end;
  end;
end;

procedure Tfr3Reader.Log(s: string);
begin
  if fLog=nil then
    fLog := TStringList.Create;
  fLog.Add(s);
end;

destructor Tfr3Reader.destroy;
begin
  fLog.Free;
  inherited destroy;
end;

procedure Tfr3Reader.LoadFromFile(aFileName: string);
var
  Node: TDOMNode;
  aDoc: TXMLDocument;
begin
  ReadXMLFile(aDoc, aFileName);

  try

    Node := aDoc.FindNode('TfrxReport');
    if Node=nil then
      raise EFR3ReaderException.Create('TfrxReport not found');

  LoadReport(Node);

  finally
    aDoc.Free;
  end;

end;

end.