-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlnodes.pas
284 lines (232 loc) · 6.99 KB
/
xmlnodes.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
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
{
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
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 General Public License for more details.
}
// Copyright (c) 2010 - J. Aldo G. de Freitas Junior
Unit
XMLNodes;
Interface
Uses
Classes,
SysUtils,
Tree,
StrUtils;
Type
EXMLNode = Class(Exception);
TXMLTextNode = Class;
TXMLNode = Class;
TXMLNodeClass = Class Of TXMLNode;
TXMLNode = Class(TTreeNodeWithProperties)
Public
Function IndexedName: String; Override;
Function GetTextChild: String;
Procedure SetTextChild(Const aContent : String);
Function AsXML: String; Virtual;
Function AsClosingXML: String; Virtual;
Property Text: String Read GetTextChild Write SetTextChild;
End;
TXMLSpecialTag1Node = Class(TXMLNode)
Public
Function AsXML: String; Override;
Function AsClosingXML: String; Override;
End;
TXMLSpecialTag2Node = Class(TXMLNode)
Public
Function AsXML: String; Override;
Function AsClosingXML: String; Override;
End;
TXMLTextNode = Class(TXMLNode)
Private
fContent : String;
Public
Function AsXML: String; Override;
Function AsClosingXML: String; Override;
Property Content : String Read fContent Write fContent;
End;
TXMLRootNode = Class(TXMLNode)
Public
Function AsXML: String; Override;
Function AsClosingXML: String; Override;
End;
TXMLAsTextIterator = Class(TTreeNodeIterator)
Private
fDepth : Integer;
fOutput : String;
Public
Constructor Create;
Procedure Process(Const aTarget : TTreeNode); Override;
Procedure OnNoChild(Const aTarget : TTreeNode); Override;
Procedure OnBeforeSingleChild(Const aTarget : TTreeNode); Override;
Procedure OnAfterSingleChild(Const aTarget : TTreeNode); Override;
Procedure OnBeforeAllChilds(Const aTarget : TTreeNode); Override;
Procedure OnAfterAllChilds(Const aTarget : TTreeNode); Override;
Procedure OnBeforeChild(Const aTarget : TTreeNode); Override;
Procedure OnAfterChild(Const aTarget : TTreeNode); Override;
Property Output : String Read fOutput Write fOutput;
End;
TXMLClassFactory = Class(TTreeClassFactory)
Public
Constructor Create;
Function Build(Const aClassName : String; Const aOwner : TXMLNode): TXMLNode; Overload;
End;
Var
XMLClassFactory : TXMLClassFactory;
Implementation
{ TXMLNode }
Function TXMLNode.IndexedName: String;
Begin
If Self Is TXMLTextNode Then
Result := Inherited IndexedName + 'TEXT'
Else
Result := Inherited IndexedName;
End;
Function TXMLNode.GetTextChild: String;
Begin
If Assigned(GetFirst) And (GetFirst Is TXMLTextNode) Then
Result := (GetFirst As TXMLTextNode).Content
Else
Raise EXMLNode.Create('No text attached to node');
End;
Procedure TXMLNode.SetTextChild(Const aContent : String);
Begin
If Assigned(GetFirst) And (GetFirst Is TXMLTextNode) Then
(GetFirst As TXMLTextNode).Content := aContent
Else
(AddChild(TXMLTextNode.Create(Self)) As TXMLTextNode).Content := aContent;
End;
Function TXMLNode.AsXML: String;
Begin
If Length(Properties.Pairs) > 0 Then
If Length(Childs) > 0 Then
Result := '<' + Name + Properties.Formatted + '>'
Else
Result := '<' + Name + Properties.Formatted + '/>'
Else
If Length(Childs) > 0 Then
Result := '<' + Name + '>'
Else
Result := '<' + Name + '/>';
End;
Function TXMLNode.AsClosingXML: String;
Begin
Result := '</' + Name + '>';
End;
{ TXMLSpecialTag1Node }
Function TXMLSpecialTag1Node.AsXML: String;
Begin
If Length(Properties.Pairs) > 0 Then
Result := '<?' + Name + ' ' + Properties.Formatted + '?>'
Else
Result := '<?' + Name + '?>';
End;
Function TXMLSpecialTag1Node.AsClosingXML: String;
Begin
Result := '';
End;
{ TXMLSpecialTag2Node }
Function TXMLSpecialTag2Node.AsXML: String;
Begin
If Length(Properties.Pairs) > 0 Then
Result := '<!' + Name + ' ' + Properties.Formatted + '!>'
Else
Result := '<!' + Name + '!>';
End;
Function TXMLSpecialTag2Node.AsClosingXML: String;
Begin
Result := '';
End;
{ TXMLTextNode }
Function TXMLTextNode.AsXML: String;
Begin
Result := fContent;
End;
Function TXMLTextNode.AsClosingXML: String;
Begin
Result := '';
End;
{ TXMLRootNode }
Function TXMLRootNode.AsXML: String;
Begin
Result := '';
End;
Function TXMLRootNode.AsClosingXML: String;
Begin
Result := '';
End;
{ TXMLAsTextIterator }
Constructor TXMLAsTextIterator.Create;
Begin
Inherited Create;
fDepth := 0;
fOutput := '';
End;
Procedure TXMLAsTextIterator.Process(Const aTarget : TTreeNode);
Begin
End;
Procedure TXMLAsTextIterator.OnNoChild(Const aTarget : TTreeNode);
Begin
If aTarget Is TXMLTextNode Then
fOutput := fOutput + (aTarget As TXMLNode).AsXML
Else
If Assigned(aTarget.Owner) And (Length(aTarget.Owner.Childs) = 1) Then
fOutput := fOutput + (aTarget As TXMLNode).AsXML
Else
fOutput := fOutput + DupeString(' ', fDepth) + (aTarget As TXMLNode).AsXML;
End;
Procedure TXMLAsTextIterator.OnBeforeSingleChild(Const aTarget : TTreeNode);
Begin
If Assigned(aTarget.Owner) And (Length(aTarget.Owner.Childs) = 1) Then
fOutput := fOutput + (aTarget As TXMLNode).AsXML
Else
If aTarget.GetFirst Is TXMLTextNode Then
fOutput := fOutput + DupeString(' ', fDepth) + (aTarget As TXMLNode).AsXML
Else
fOutput := fOutput + DupeString(' ', fDepth) + (aTarget As TXMLNode).AsXML + #13#10;
Inc(fDepth);
End;
Procedure TXMLAsTextIterator.OnAfterSingleChild(Const aTarget : TTreeNode);
Begin
Dec(fDepth);
If aTarget.GetFirst Is TXMLTextNode Then
fOutput := fOutput + (aTarget As TXMLNode).AsClosingXML
Else
fOutput := fOutput + #13#10 + DupeString(' ', fDepth) + (aTarget As TXMLNode).AsClosingXML;
End;
Procedure TXMLAsTextIterator.OnBeforeAllChilds(Const aTarget : TTreeNode);
Begin
fOutput := fOutput + DupeString(' ', fDepth) + (aTarget As TXMLNode).AsXML + #13#10;
Inc(fDepth);
End;
Procedure TXMLAsTextIterator.OnAfterAllChilds(Const aTarget : TTreeNode);
Begin
Dec(fDepth);
fOutput := fOutput + DupeString(' ', fDepth) + (aTarget As TXMLNode).AsClosingXML;
End;
Procedure TXMLAsTextIterator.OnBeforeChild(Const aTarget : TTreeNode);
Begin
End;
Procedure TXMLAsTextIterator.OnAfterChild(Const aTarget : TTreeNode);
Begin
fOutput := fOutput + #13#10;
End;
{ TXMLClassFactory }
Constructor TXMLClassFactory.Create;
Begin
Inherited Create;
DefaultClass := TXMLNode;
End;
Function TXMLClassFactory.Build(Const aClassName : String; Const aOwner : TXMLNode): TXMLNode; Overload;
Begin
Result := (Build(aClassName, aOwner As TTreeNode) As TXMLNode);
End;
Initialization
XMLClassFactory := TXMLClassFactory.Create;
XMLClassFactory.DefaultClass := TXMLNode;
Finalization
XMLClassFactory.Free;
End.