summaryrefslogtreecommitdiff
path: root/packages/fcl-mustache/src/fpexmustache.pp
blob: 6191e6bd78d568e3f4e2342b065f792dc5f5cbd4 (plain)
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
{
    This file is part of the Free Pascal Run time library.
    Copyright (c) 2021 by Michael Van Canneyt (michael@freepascal.org)

    This file contains a Mustache descendent with FPExpr parser expression support

    See the File COPYING.FPC, included in this distribution,
    for details about the copyright.

    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.

 **********************************************************************}

unit fpexmustache;

{$mode ObjFPC}{$H+}

interface

uses
  Classes, fpexprpars, fpmustache, fpjson;

Type

  { TMustacheExprElement }

  TMustacheExprElement = Class(TMustacheElement)
  private
    FNode: TFPExprNode;
    FExpr : TMustacheString;
  Protected
    Procedure SetNode(aNode : TFPExprNode); virtual;
    Function GetData : TMustacheString;override;
    Procedure SetData(const aValue : TMustacheString) ; override;
  Public
    Destructor Destroy; override;
    Procedure Render(aContext : TMustacheContext; aOutput : TMustacheOutput; const aPrefix : String = ''; aLast : Boolean = False); override;
    Property Node : TFPExprNode Read FNode;
  end;

  { TMustacheExprParser }

  TMustacheExprParser = class(TMustacheParser)
  private
    FExprEnd: Char;
    FExprParser: TFPExpressionParser;
    FExprStart: Char;
  Protected
    function CreateDefault(aParent: TMustacheElement; aPosition: Integer; const aName: String): TMustacheElement; override;
  Public
    Constructor Create(aTemplate : TMustacheString = '';aStart: TMustacheString='';aStop: TMustacheString = ''); override;
    // Default [
    Property ExprStart : Char Read FExprStart Write FExprStart;
    // Default ]
    Property ExprEnd : Char Read FExprEnd Write FExprEnd;
    // Our instance
    Property ExprParser : TFPExpressionParser Read FExprParser Write FExprParser;
  end;

  { TMustacheExpr }

  TMustacheExpr = Class(TMustache)
  private
    FExprEndChar: String;
    FExpressionParser: TFPExpressionParser;
    FExprStartChar: String;
    FCurrentContext : TMustacheContext;
    function GetResultType(aValue: TJSONData): TResultType;
    procedure SetExprEndChar(AValue: String);
    procedure SetExpressionParser(AValue: TFPExpressionParser);
    procedure SetExprStartChar(AValue: String);
    function DoGetExpressionParser : TFPExpressionParser;
  Protected
    procedure DoGetVariable(var Result: TFPExpressionResult; ConstRef  AName: ShortString); virtual;
    Procedure Notification(AComponent: TComponent; Operation: TOperation); override;
    Function CreateParser(aTemplate: TMustacheString): TMustacheParser; override;
    function GetExpressionParser(aOwner : TComponent): TFPExpressionParser; virtual;
  Public
    Constructor Create(aOwner : TComponent); override;
    Procedure Render(aContext : TMustacheContext; aOutput : TMustacheOutput); override; overload;
    // Register variables from JSON in the expression engine.
    // If UseEvent is true, the variables will be retrieved while parsing with an event.
    // If UseEvent is false, the variables will be registered as static values.
    Procedure RegisterVariables (aContext : TMustacheJSONContext; aPath : TJSONStringType = ''; UseEvent : Boolean = True);
    Procedure RegisterVariables (aJSON : String; aPath : TJSONStringType = ''; UseEvent : Boolean = True);
    Procedure RegisterVariables (aJSON : TJSONObject; aPath : TJSONStringType = ''; UseEvent : Boolean = True);
  Published
    // Default [
    Property ExprStartChar : String Read FExprStartChar Write SetExprStartChar;
    // Default ]
    Property ExprEndChar : String Read FExprEndChar Write SetExprEndChar;
    // An expression parser instance. If none is specified, then a default is created.
    Property ExpressionParser : TFPExpressionParser Read DoGetExpressionParser Write SetExpressionParser;
  end;

  { TMustacheExpressionParser }

  TMustacheExpressionParser = class(TFPExpressionParser)
  end;

implementation

uses sysutils;

Resourcestring
  SErrLengthStartMustBe1 = 'Length expression start delimiter must be 1';
  SErrLengthEndMustBe1 = 'Length expression end delimiter must be 1';

{ TMustacheExprElement }

procedure TMustacheExprElement.SetNode(aNode: TFPExprNode);
begin
  FNode:=aNode;
end;

function TMustacheExprElement.GetData: TMustacheString;
begin
  Result:=FExpr;
end;

procedure TMustacheExprElement.SetData(const aValue: TMustacheString);
begin
  FExpr:=aValue;
end;

procedure TMustacheExprElement.Render(aContext: TMustacheContext;
  aOutput: TMustacheOutput; const aPrefix: String; aLast: Boolean);

Var
  Res : TFPExpressionResult;
  S : TMustacheString;

begin
  Res:=Node.NodeValue;
  case Res.ResultType of
    rtString   : S:=Res.ResString;
    rtBoolean  : S:=BoolToStr(Res.ResBoolean,True);
    rtInteger  : S:=IntToStr(Res.ResInteger);
    rtFloat    : S:=FormatFloat('0.0#######',Res.ResFloat);
    rtCurrency : S:=CurrToStr(Res.ResCurrency);
    rtDateTime : S:=DateTimeToStr(Res.ResDateTime);
  end;
  aOutput.Output(aPrefix+S);
end;

destructor TMustacheExprElement.Destroy;
begin
  FreeAndNil(FNode);
  inherited Destroy;
end;

{ TMustacheExprParser }

function TMustacheExprParser.CreateDefault(aParent: TMustacheElement;
  aPosition: Integer; const aName: String): TMustacheElement;

Var
  L : Integer;
  N : TFPExprNode;

begin
  N:=Nil;
  L:=Length(aName);
  If (aName[1]=FExprStart) and (aName[L]=FExprEnd) then
    begin
    Result:=TMustacheExprElement.Create(metVariable,aParent,aPosition);
    Result.Data:=Copy(aName,2,L-2);
    ExprParser.Expression:=Result.Data;
    ExprParser.ExtractNode(N);
    TMustacheExprElement(Result).SetNode(N);
    aParent.AddChild(Result);
    end
  else
    Result:=Inherited CreateDefault(aParent,aPosition,aName);
end;

constructor TMustacheExprParser.Create(aTemplate: TMustacheString;
  aStart: TMustacheString; aStop: TMustacheString);
begin
  inherited Create(aTemplate, aStart, aStop);
  FExprStart:='[';
  FExprEnd:=']';
end;

{ TMustacheExpr }

procedure TMustacheExpr.SetExprEndChar(AValue: String);
begin
  if FExprEndChar=AValue then Exit;
  if Length(aValue)<>1 then
    EMustache.Create(SErrLengthStartMustBe1);
  FExprEndChar:=AValue;
end;

function TMustacheExpr.GetExpressionParser(aOwner : TComponent): TFPExpressionParser;
begin
  Result:=TMustacheExpressionParser.Create(AOwner);
end;

procedure TMustacheExpr.SetExpressionParser(AValue: TFPExpressionParser);

begin
  if FExpressionParser=AValue then Exit;
  If assigned(FExpressionParser) then
    FExpressionParser.RemoveFreeNotification(Self);
  FExpressionParser:=AValue;
  If assigned(FExpressionParser) then
    FExpressionParser.FreeNotification(Self);
end;

procedure TMustacheExpr.SetExprStartChar(AValue: String);
begin
  if FExprStartChar=AValue then Exit;
  if Length(aValue)<>1 then
    EMustache.Create(SErrLengthEndMustBe1);
  FExprStartChar:=AValue;
end;

function TMustacheExpr.DoGetExpressionParser: TFPExpressionParser;
begin
  if FExpressionParser=Nil then
    begin
    FExpressionParser:=GetExpressionParser(Self);
    FExpressionParser.SetSubComponent(True);
    FExpressionParser.FreeNotification(Self);
    end;
  Result:=FExpressionParser;
end;

procedure TMustacheExpr.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (Operation=opRemove) and (aComponent=FExpressionParser) then
    FExpressionParser:=Nil;
end;

function TMustacheExpr.CreateParser(aTemplate: TMustacheString ): TMustacheParser;

Var
  Exp : TMustacheExprParser;

begin
  Exp:=TMustacheExprParser.Create(aTemplate);
  Exp.ExprParser:=Self.ExpressionParser;
  Result:=Exp;
end;

constructor TMustacheExpr.Create(aOwner: TComponent);
begin
  inherited Create(aOwner);
  DoGetExpressionParser;
end;

procedure TMustacheExpr.Render(aContext: TMustacheContext; aOutput: TMustacheOutput);

begin
  FCurrentContext:=aContext;
  try
    inherited Render(aContext, aOutput);
  finally
    FCurrentContext:=nil;
  end;
end;

procedure TMustacheExpr.DoGetVariable(var Result: TFPExpressionResult; ConstRef
  AName: ShortString);

Var
  S : TMustacheString;
  V : Double;
  C : Integer;

begin
  If not Assigned(FCurrentContext) then
    case result.ResultType of
      rtInteger : Result.ResInteger:=0;
      rtDateTime : Result.ResDateTime:=0.0;
      rtString : Result.ResString:='';
      rtFloat: Result.ResFloat:=0.0;
      rtCurrency: Result.ResCurrency:=0.0;
      rtBoolean: Result.ResBoolean:=False;
    end
  else
    begin
    S:=FCurrentContext.GetTextValue(aName);
    case result.ResultType of
      rtInteger : Result.ResInteger:=StrToInt64Def(S,0);
      rtDateTime : if Not TryStrToDateTime(S,Result.ResDateTime) then
                     Result.ResDateTime:=0.0;
      rtString : Result.ResString:=S;
      rtFloat: begin
               Val(S,V,C);
               if C<>0 then
                 Result.ResFloat:=0.0
               else
                 Result.ResFloat:=V;
               end;
      rtCurrency:
               begin
               Val(S,V,C);
               if (C<>0) then
                 Result.ResCurrency:=0.0
               else
                 Result.ResCurrency:=V;
               end;
      rtBoolean: Result.ResBoolean:=StrToBoolDef(S,False);
    end;
    end;
end;

function TMustacheExpr.GetResultType(aValue: TJSONData): TResultType;

begin
  Case aValue.JSONType of
    jtBoolean : Result:=rtBoolean;
    jtString,
    jtArray,
    jtObject,
    jtNull : Result:=rtString;
    jtNumber :
       begin
       Case TJSONNumber(aValue).NumberType of
         ntFloat : Result:=rtFloat;
         ntInteger,
         ntInt64 : Result:=rtInteger;
         ntQWord : Raise EMustache.Create('Unsupported JSON type');
       end;
       end;
  end;
end;

procedure TMustacheExpr.RegisterVariables(aContext: TMustacheJSONContext;
  aPath: TJSONStringType; UseEvent: Boolean);

begin
  RegisterVariables(aContext.RootData as TJSONObject,aPath,UseEvent);
end;

procedure TMustacheExpr.RegisterVariables(aJSON: String;
  aPath: TJSONStringType; UseEvent: Boolean);

Var
  aData : TJSONData;
  aObj : TJSONObject absolute aData;


begin
  aData:=getJSON(aJSON,True);
  try
    if aData is TJSONObject then
      RegisterVariables(aObj,aPath,useEvent)
    else
      Raise EMustache.Create('Invalid JSON data to register variables');
  finally
    aData.Free;
  end;
end;

procedure TMustacheExpr.RegisterVariables(aJSON: TJSONObject; aPath: TJSONStringType; UseEvent: Boolean);

Var
  aData,aValue : TJSONData;
  aEnum : TJSONEnum;
  aKey : TJSONStringType;
  rt : TResultType;
  aParser : TFPExpressionParser;

begin
  aParser:=ExpressionParser;
  aData:=aJSON.FindPath(aPath);
  if aData is TJSONObject then
    for aEnum in aData do
      begin
      aKey:=aEnum.Key;
      aValue:=aEnum.Value;
      rt:=GetResultType(aValue);
      if UseEvent then
        aParser.Identifiers.AddVariable(aKey,rt,@DoGetVariable)
      else
        case rt of
          rtBoolean: aParser.Identifiers.AddBooleanVariable(aKey,aValue.AsBoolean);
          rtFloat: aParser.Identifiers.AddFloatVariable(aKey,aValue.AsFloat);
          rtInteger: aParser.Identifiers.AddIntegerVariable(aKey,aValue.AsInteger);
          rtString: Case aValue.JSONType of
                      jtNull: aParser.Identifiers.AddStringVariable(aKey,'');
                      jtArray,
                      jtObject: aParser.Identifiers.AddStringVariable(aKey, aValue.AsJSON);
                    else
                      aParser.Identifiers.AddStringVariable(aKey,aValue.AsString);
                    end;
          end;
      end;
end;

end.