-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadpparser.pp
More file actions
581 lines (511 loc) · 13.7 KB
/
Copy pathadpparser.pp
File metadata and controls
581 lines (511 loc) · 13.7 KB
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
unit adpparser;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, adpdata, adputils;
Type
TparseMode = (process_catalog,process_page);
{ TADPParser }
TADPParser = class
private
FDatasources: TDatasources;
Fmaster_template: String;
FMessages: TMessages;
FOptions: TOptions;
FParseMode: TParseMode;
FProperties: TProperties;
Frecognize_properties: boolean;
Fslave_html: unicodestring;
last_expr_result:boolean;
adp : Unicodestring;
function do_else_tag(const content: Unicodestring): Unicodestring;
function do_if_tag(const params, content : Unicodestring): Unicodestring;
function do_master_tag(const params: Unicodestring): Unicodestring;
function do_multiple_tag(const params, content: Unicodestring): Unicodestring;
function do_property_tag(const params, content: Unicodestring): Unicodestring;
function do_slave_tag: Unicodestring;
function do_tag(const tag, params, content, closetag: Unicodestring): Unicodestring;
function do_trn_tag(const params, content: Unicodestring): Unicodestring;
function parse_at(var pos: longint; stoptag: Unicodestring): Unicodestring;
procedure parse_param(var p: Unicodestring; out key, value: Unicodestring);
function parse_tag(var pos: longint): Unicodestring;
function parse_tag_recursively(const tag: Unicodestring): boolean;
procedure skip_whitespace(var pos: longint);
public
constructor create(aOptions : TOptions; aMessages : TMessages; aProperties : TProperties; aData : TDatasources);
function Parse(aSource:Unicodestring):Unicodestring;
Property Options : TOptions read FOptions;
Property Messages : TMessages Write FMessages;
Property Properties : TProperties Read FProperties;
Property Data : TDatasources Read FDatasources;
property mode : TParseMode Read FParseMode Write FParseMode;
property master_template : String Read Fmaster_template Write Fmaster_template;
property slave_html : unicodestring Read Fslave_html Write Fslave_html;
property recognize_properties:boolean Read Frecognize_properties Write Frecognize_properties;
end;
implementation
procedure TADPParser.skip_whitespace(var pos:longint);
begin
while (pos<=length(adp)) and (adp[pos] in whitespace) do
inc(pos);
end;
function TADPParser.parse_tag_recursively(const tag:Unicodestring):boolean;
begin
parse_tag_recursively:=(tag='TRN') or (tag='PROPERTY') or (tag='IF') or
(tag='ELSE') or (tag='MULTIPLE');
end;
function TADPParser.do_if_tag(const params,content : Unicodestring):Unicodestring;
var pos:longint;
procedure do_skip_whitespace;
begin
while (pos<=length(params)) and (params[pos] in whitespace) do
inc(pos);
end;
function read_word:Unicodestring;
var oldpos:longint;
begin
oldpos:=pos;
while (pos<=length(params)) and (params[pos]<>' ') do
inc(pos);
read_word:=copy(params,oldpos,pos-oldpos);
end;
function parse_l3_expr:boolean;
var left,right,op:Unicodestring;
save_pos:longint;
begin
parse_l3_expr:=false;
do_skip_whitespace;
if pos>length(params) then
exit;
save_pos:=pos;
left:=read_word;
op:=upcase(left);
if (op='NIL') or (op='NOT') or
(op='EQ') or (op='NE') or
(op='LT') or (op='LTE') or
(op='GT') or (op='GTE') then
begin
left:='';
end
else
begin
left:=Fproperties.ReplaceInstring(left);
if pos>length(params) then
exit;
save_pos:=pos;
do_skip_whitespace;
op:=upcase(read_word);
end;
if op='NIL' then
begin
parse_l3_expr:=left='';
end
else if op='NOT' then
begin
if pos>length(params) then
exit;
do_skip_whitespace;
right:=read_word;
if upcase(right)='NIL' then
parse_l3_expr:=left<>'';
end
else if op='EQ' then
begin
if pos>length(params) then
exit;
do_skip_whitespace;
right:=FProperties.ReplaceInString(read_word);
parse_l3_expr:=left=right;
end
else if op='NE' then
begin
if pos>length(params) then
exit;
do_skip_whitespace;
right:=FProperties.ReplaceInString(read_word);
parse_l3_expr:=left<>right;
end
else if op='LT' then
begin
if pos>length(params) then
exit;
do_skip_whitespace;
right:=FProperties.ReplaceInString(read_word);
parse_l3_expr:=left<right;
end
else if op='LTE' then
begin
if pos>length(params) then
exit;
do_skip_whitespace;
right:=FProperties.ReplaceInString(read_word);
parse_l3_expr:=left<=right;
end
else if op='GTE' then
begin
if pos>length(params) then
exit;
do_skip_whitespace;
right:=FProperties.ReplaceInString(read_word);
parse_l3_expr:=left>=right;
end
else if op='GT' then
begin
if pos>length(params) then
exit;
do_skip_whitespace;
right:=FProperties.ReplaceInString(read_word);
parse_l3_expr:=left>right;
end
else
pos:=save_pos;
end;
function parse_l2_expr:boolean;
var left,right:boolean;
op:Unicodestring;
begin
parse_l2_expr:=false;
do_skip_whitespace;
if pos>length(params) then
exit;
left:=parse_l3_expr;
if pos>length(params) then
begin
parse_l2_expr:=left;
exit;
end;
op:=read_word;
if upcase(op)='AND' then
begin
if pos>length(params) then
exit;
right:=parse_l2_expr;
parse_l2_expr:=left and right;
end;
end;
function parse_l1_expr:boolean;
var left,right:boolean;
op:Unicodestring;
begin
parse_l1_expr:=false;
do_skip_whitespace;
if pos>length(params) then
exit;
left:=parse_l2_expr;
if pos>length(params) then
begin
parse_l1_expr:=left;
exit;
end;
op:=read_word;
if upcase(op)='OR' then
begin
if pos>length(params) then
exit;
right:=parse_l1_expr;
parse_l1_expr:=left and right;
end;
end;
begin
pos:=1;
last_expr_result:=parse_l1_expr;
if last_expr_result then
do_if_tag:=content
else
do_if_tag:='';
end;
function TADPParser.do_else_tag(const content:Unicodestring):Unicodestring;
begin
if not last_expr_result then
do_else_tag:=content
else
do_else_tag:='';
end;
procedure TADPParser.parse_param(var p: Unicodestring; out key, value: Unicodestring);
var pos,pl,pf:longint;
procedure p_skip_whitespace(var pos:longint);
begin
while (pos<=length(p)) and (p[pos] in whitespace) do
inc(pos);
end;
Var
S : UnicodeString;
begin
pos:=1;
p_skip_whitespace(pos);
pf:=pos;
pl:=0;
while (pos<=length(p)) and (p[pos] in ['A'..'Z','a'..'z']) do
begin
inc(pl);
inc(pos);
end;
key:=Copy(P,Pf,PL);
p_skip_whitespace(pos);
if (pos>length(p)) or (p[pos]<>'=') then
begin
p:='';
exit;
end;
inc(pos);
S:='';
PL:=0;
SetLength(S,Length(P));
pf:=Pos;
case p[pos] of
'''':
begin
inc(pos);
while (pos<=length(p)) and (p[pos]<>'''') do
begin
inc(PL);
S[PL]:=p[pos];
inc(pos);
end;
inc(pos);
end;
'"':
begin
inc(pos);
while (pos<=length(p)) and (p[pos]<>'"') do
begin
Inc(Pl);
S[PL]:=p[pos];
inc(pos);
end;
inc(pos);
end;
else
while p[pos] in ['A'..'Z','a'..'z'] do
begin
Inc(Pl);
S[PL]:=p[pos];
inc(pos);
end;
end;
SetLength(S,PL);
delete(p,1,pos-1);
Value:=S;
end;
function TADPParser.do_trn_tag(const params,content:Unicodestring):Unicodestring;
var p,key,value:Unicodestring;
message_key,message_locale:Unicodestring;
begin
message_key:='';
do_trn_tag:=content;
// Writeln('>',Content,'<');
message_locale:='';
p:=params;
while p<>'' do
begin
parse_param(p,key,value);
if key='key' then
message_key:=value
else if key='locale' then
message_locale:=value;
end;
if mode=process_catalog then
begin
if (message_locale<>'') and (message_key<>'') then
begin
// Writeln(message_locale,',',message_key,',',content);
fmessages.Write(message_locale,message_key,content);
end
end
else
if message_locale<>options.ulocale then
begin
value:='';
if (options.locale<>'') and (message_key<>'') then
value:=fmessages.Read(options.Ulocale,message_key);
if (Options.fallback_locale<>'') and (message_key<>'') then
value:=fmessages.Read(UTF8Decode(Options.fallback_locale),message_key);
if value<>'' then
do_trn_tag:=value;
end;
end;
function TADPParser.do_master_tag(const params:Unicodestring):Unicodestring;
var p,key,value:Unicodestring;
begin
master_template:=options.default_master;
p:=params;
while p<>'' do
begin
parse_param(p,key,value);
if key='src' then
master_template:=UTF8Encode(FProperties.ReplaceInString(value));
end;
do_master_tag:='';
end;
function TADPParser.do_slave_tag:Unicodestring;
begin
do_slave_tag:=slave_html;
end;
function TADPParser.do_multiple_tag(const params,content : Unicodestring):Unicodestring;
var l,p,key,value:Unicodestring;
datasource:ansistring;
i,n:longint;
lst : TStringListList;
hdr,row : TStringlist;
begin
Result:='';
p:=params;
while p<>'' do
begin
parse_param(p,key,value);
if key='name' then
datasource:=UTF8Encode(value);
end;
lst:=nil;
Data.Loaddatasource(datasource,lst);
if Not Assigned(Lst) then
Exit;
hdr:=tstringlist(lst.strs[0]);
for i:=1 to lst.strs.count-1 do
begin
row:=tstringlist(lst.strs[i]);
for n:=0 to hdr.count-1 do
FProperties.Write(UTF8Decode(datasource+'.'+hdr[n]),UTF8Decode(row[n]));
l:=FProperties.ReplaceInString(content);
Result:=Result+l;
end;
end;
function TADPParser.do_property_tag(const params,content :Unicodestring):Unicodestring;
var p,name,key,value:Unicodestring;
begin
name:='';
p:=params;
while p<>'' do
begin
parse_param(p,key,value);
if key='name' then
name:=value;
end;
if name<>'' then
fproperties.Write(name,content);
do_property_tag:='';
end;
function TADPParser.do_tag(const tag,params,content,closetag:Unicodestring):Unicodestring;
var utag:Unicodestring;
begin
utag:=upcase(tag);
if utag='TRN' then
do_tag:=do_trn_tag(params,content)
else if utag='IF' then
do_tag:=do_if_tag(params,content)
else if utag='ELSE' then
do_tag:=do_else_tag(content)
else if utag='MASTER' then
do_tag:=do_master_tag(params)
else if utag='SLAVE' then
do_tag:=do_slave_tag
else if utag='MULTIPLE' then
do_tag:=do_multiple_tag(params,content)
else if recognize_properties and (utag='PROPERTY') then
do_tag:=do_property_tag(params,content)
else
do_tag:='<'+tag+params+'>'+content+closetag;
end;
function TADPParser.parse_tag(var pos:longint):Unicodestring;
var tag,utag,
params,content,
closetag:Unicodestring;
begin
tag:='';
params:='';
inc(pos);
while (pos<=length(adp)) and (adp[pos] in ['A'..'Z','a'..'z']) do
begin
{ tag:=tag+adp[pos];}
setlength(tag,length(tag)+1);
tag[length(tag)]:=adp[pos];
inc(pos);
end;
while (pos<=length(adp)) and (adp[pos]<>'>') do
begin
{ params:=params+adp[pos];}
setlength(params,length(params)+1);
params[length(params)]:=adp[pos];
inc(pos);
end;
if pos>length(adp) then
begin
parse_tag:='';
exit;
end;
inc(pos);
utag:=upcase(tag);
if parse_tag_recursively(utag) then
begin
//writeln('Start of tag ',tag);
content:=parse_at(pos,upcase(tag));
// writeln('Stop of tag ',tag,'content = ',content,'----');
closetag:='';
if (pos+length(tag)+3<=length(adp)) and
(upcase(copy(adp,pos,length(tag)+3))='</'+utag+'>') then
begin
closetag:=copy(adp,pos,length(tag)+3);
pos:=pos+length(tag)+3;
end;
{Default behaviour.}
parse_tag:=do_tag(tag,params,content,closetag);
end
else
parse_tag:=do_tag(tag,params,'','');
end;
function TADPParser.parse_at(var pos:longint;stoptag:Unicodestring):Unicodestring;
var
i:longint;
res,tag:Unicodestring;
begin
Res:='';
repeat
while (adp[pos]<>'<') and (pos<=length(adp)) do
begin
setlength(res,length(res)+1);
res[length(res)]:=adp[pos];
inc(pos);
end;
if adp[pos]='<' then
begin
if (pos+1<length(adp)) and (adp[pos+1]='/') then
begin
tag:='';
i:=pos+2;
while adp[i] in ['A'..'Z','a'..'z'] do
begin
tag:=tag+adp[i];
inc(i);
end;
if upcase(tag)=stoptag then
begin
Result:=Res;
exit;
end;
setlength(res,length(res)+1);
res[length(res)]:='<';
inc(pos);
end
else
res:=res+parse_tag(pos);
end;
until pos>length(adp);
Result:=FProperties.ReplaceInString(res);
end;
function TADPParser.Parse(aSource: Unicodestring): Unicodestring;
var
pos:longint;
begin
adp:=aSource;
pos:=1;
Result:=parse_at(pos,'');
end;
{ TADPParser }
constructor TADPParser.create(aOptions: TOptions; aMessages: TMessages; aProperties: TProperties; aData: TDatasources);
begin
FOptions:=aoptions;
FMessages:=aMessages;
FProperties:=aProperties;
FDataSources:=AData;
end;
end.