-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlconsolewithtemplates.pas
228 lines (212 loc) · 5.23 KB
/
xmlconsolewithtemplates.pas
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
Unit
XMLConsoleWithTemplates;
Interface
Uses
Classes,
SysUtils,
XMLConsole,
XMLScanner,
HTMLParser,
HTMLNodes;
Type
TXMLConsoleWithTemplates = Class(TXMLCustomConsole)
Public
Procedure PrintTemplate(Var Message); Message 'printtemplate';
Procedure ProcessTemplate(Var Message); Message 'processtemplate';
Procedure DoPrintTemplate(Const aInputFileName : String);
Procedure DoProcessTemplate(Const aInputFileName, aOutputFileName : String);
End;
TXMLScreenConsoleWithTemplates = Class(TXMLConsoleWithTemplates)
Public
Function ReadLine: String; Override;
Procedure Out(Const aKind : TXMLConsoleMessageKind; Const aLine : String); Override;
Procedure OutLn(Const aKind : TXMLConsoleMessageKind; Const aLine : String); Override;
End;
Implementation
{ TXMLConsoleWithTemplates }
Procedure TXMLConsoleWithTemplates.PrintTemplate(Var Message);
Begin
If AtLeast(1) Then
Begin
Stack.Enter(1);
DoPrintTemplate(Stack.Top.Peek(0));
Stack.Leave(0);
End
Else
Begin
OutLn(mkError, 'Not enough parameters.');
OutLn(mkInfo, 'Format :');
OutLn(mkInfo, ' PRINTTEMPLATE <templatefile>');
End;
End;
Procedure TXMLConsoleWithTemplates.ProcessTemplate(Var Message);
Begin
If AtLeast(2) Then
Begin
Stack.Enter(2);
Stack.Enter(2);
DoProcessTemplate(Stack.Top.Peek(0), Stack.Top.Peek(1));
Stack.Leave(0);
Stack.Leave(0);
End
Else
Begin
OutLn(mkError, 'Not enough parameters.');
OutLn(mkInfo, 'Format :');
OutLn(mkInfo, ' PRINTTEMPLATE <templatefile>, <outputfile>');
End;
End;
Procedure TXMLConsoleWithTemplates.DoPrintTemplate(Const aInputFileName : String);
Var
lTemplate : THTMLRootNode;
lFile : TFileStream;
lSource : TXMLSource;
lTokens : TMemoryStream;
lScanner : TXMLScanner;
lTokenIterator : TXMLTokenIterator;
lParser : THTMLParser;
Begin
lFile := Nil;
lSource := Nil;
lTokens := Nil;
lScanner := Nil;
lTokenIterator := Nil;
lParser := Nil;
Try
lTemplate := THTMLRootNode.Create(Nil);
lFile := TFileStream.Create(aInputFileName, fmOpenRead);
lSource := TXMLSource.Create(lFile, False);
lTokens := TMemoryStream.Create;
lScanner := TXMLScanner.Create(lSource, lTokens, False);
lScanner.Scan;
lTokenIterator := TXMLTokenIterator.Create(lTokens, False);
lParser := THTMLParser.Create(lTokenIterator, lTemplate, False);
lParser.Parse;
lTemplate.PropagateConsole(Self);
OutLn(mkResponse, '$' + lTemplate.AsString + #13#10);
Finally
FreeAndNil(lFile);
FreeAndNil(lSource);
FreeAndNil(lTokens);
FreeAndNil(lScanner);
FreeAndNil(lTokenIterator);
FreeAndNil(lParser);
FreeAndNil(lTemplate);
End;
End;
Procedure TXMLConsoleWithTemplates.DoProcessTemplate(Const aInputFileName, aOutputFileName : String);
Var
lTemplate : THTMLRootNode;
lFile : TFileStream;
lSource : TXMLSource;
lTokens : TMemoryStream;
lScanner : TXMLScanner;
lTokenIterator : TXMLTokenIterator;
lParser : THTMLParser;
lFileOutput : TStringList;
Begin
lFile := Nil;
lSource := Nil;
lTokens := Nil;
lScanner := Nil;
lTokenIterator := Nil;
lParser := Nil;
lFileOutput := Nil;
Try
lTemplate := THTMLRootNode.Create(Nil);
lFile := TFileStream.Create(aInputFileName, fmOpenRead);
lSource := TXMLSource.Create(lFile, False);
lTokens := TMemoryStream.Create;
lScanner := TXMLScanner.Create(lSource, lTokens, False);
lScanner.Scan;
lTokenIterator := TXMLTokenIterator.Create(lTokens, False);
lParser := THTMLParser.Create(lTokenIterator, lTemplate, False);
lParser.Parse;
lTemplate.PropagateConsole(Self);
lFileOutput := TStringList.Create;
lFileOutput.Duplicates := dupAccept;
lFileOutput.Sorted := False;
lFileOutput.Text := lTemplate.AsString;
lFileOutput.SaveToFile(aOutputFileName);
Finally
FreeAndNil(lFile);
FreeAndNil(lSource);
FreeAndNil(lTokens);
FreeAndNil(lScanner);
FreeAndNil(lTokenIterator);
FreeAndNil(lParser);
FreeAndNil(lTemplate);
End;
End;
{ TXMLScreenConsoleWithTemplates }
Function TXMLScreenConsoleWithTemplates.ReadLine: String;
Var
lTmp : String;
Begin
lTmp := Prompt;
lTmp := StringReplace(lTmp, '$F', Focused.IndexedName, [ rfReplaceAll ]);
lTmp := StringReplace(lTmp, '$T', TimeToStr(Now), [ rfReplaceAll ]);
lTmp := StringReplace(lTmp, '$D', DateToStr(Now), [ rfReplaceAll ]);
Write(lTmp);
ReadLn(lTmp);
Result := lTmp;
End;
Procedure TXMLScreenConsoleWithTemplates.Out(Const aKind : TXMLConsoleMessageKind; Const aLine : String);
Begin
Case aKind Of
mkDebug :
If Verbosity[mkDebug] Then
Begin
Write('[D] ');
Write(aLine);
End;
mkError :
If Verbosity[mkError] Then
Begin
Write('[E] ');
Write(aLine);
End;
mkResponse :
If Verbosity[mkResponse] Then
Begin
Write('[R] ');
Write(aLine);
End;
mkInfo:
If Verbosity[mkInfo] Then
Begin
Write('[I] ');
Write(aLine);
End;
End;
End;
Procedure TXMLScreenConsoleWithTemplates.OutLn(Const aKind : TXMLConsoleMessageKind; Const aLine : String);
Begin
Case aKind Of
mkDebug :
If Verbosity[mkDebug] Then
Begin
Write('[D] ');
WriteLn(aLine);
End;
mkError :
If Verbosity[mkError] Then
Begin
Write('[E] ');
WriteLn(aLine);
End;
mkResponse :
If Verbosity[mkResponse] Then
Begin
Write('[R] ');
WriteLn(aLine);
End;
mkInfo:
If Verbosity[mkInfo] Then
Begin
Write('[I] ');
WriteLn(aLine);
End;
End;
End;
End.