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
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Dom.Element;
interface
uses
Collection.List,
Dom.Attribute,
Dom.Node,
Dom.Text,
Exception,
Iterator,
Text,
Text.Document.Position;
type
EDomElement = class(EException);
TDomElement = class(TDomNode)
private
FName: TText;
FAttributes: TDomAttributeMap;
FNodes: TList<TDomNode>;
FPosition: TSourcePosition;
public
constructor Create(const Name: TText); virtual;
destructor Destroy(); override;
function ElementWithName(Name: TText): TDomElement;
function ElementsWithName(Name: TText): TList<TDomElement>;
function ElementWithNameAndAttributeValue(const Name: TText; const AttributeName: TText; const AttributeValue: TText): TDomElement;
function ElementWithAttributeValue(const Name: TText; const Value: TText): TDomElement;
function ElementForPath(const NamePath: array of TText): TDomElement;
function GetFirstElement(): TDomElement; deprecated 'Please be exact in locating elements';
function GetText(): TText;
function GetTextWithDefault(const Default: TText): TText;
function GetOrCreateText(): TDomText;
function CreateElementIterator(): TIterator<TDomElement>;
function CreateElementWithNameIterator(const Name: TText): TIterator<TDomElement>;
property Name: TText read FName write FName;
property Attributes: TDomAttributeMap read FAttributes;
property Nodes: TList<TDomNode> read FNodes;
property Position: TSourcePosition read FPosition write FPosition;
public
class function CreateWithTextContent(Name: TText; TextContent: TText): TDomElement;
end;
implementation
uses
Dom.Element.Iterator,
Integer._32,
Object_.List,
Reference.User;
{ TDomElement }
constructor TDomElement.Create(const Name: TText);
begin
inherited Create();
FName := Name;
FAttributes := TDomAttributeMap.Create();
FNodes := TObjectList<TDomNode>.Create();
end;
function TDomElement.CreateElementIterator(): TIterator<TDomElement>;
begin
Result := TDomNodeListElementIterator.Create(Nodes);
end;
function TDomElement.CreateElementWithNameIterator(const Name: TText): TIterator<TDomElement>;
begin
Result := TDomNodeListElementWithNameIterator.Create(Nodes, Name);
end;
class function TDomElement.CreateWithTextContent(Name: TText; TextContent: TText): TDomElement;
var
ContentNode: TDomText;
begin
ContentNode := TDomText.Create();
ContentNode.Text := TextContent;
Result := TDomElement.Create(Name);
Result.Nodes.Add(ContentNode);
end;
destructor TDomElement.Destroy();
begin
Attributes.Free();
Nodes.Free();
inherited Destroy();
end;
function TDomElement.ElementForPath(const NamePath: array of TText): TDomElement;
var
i: TInteger32;
begin
Result := Self;
i := 0;
while (Result <> nil) and (i < Length(NamePath)) do
begin
Result := Result.ElementWithName(NamePath[i]);
Inc(i);
end;
end;
function TDomElement.GetText(): TText;
begin
case Nodes.Count of
0: Result := '';
1:
if Nodes.First is TDomText then
Result := (Nodes.First as TDomText).Text
else
raise EDomElement.Create('Text node expected');
else
raise EDomElement.Create('Element contains markup');
end;
end;
function TDomElement.GetTextWithDefault(const Default: TText): TText;
begin
if Self <> nil then
Result := GetText()
else
Result := Default;
end;
function TDomElement.ElementWithAttributeValue(const Name: TText; const Value: TText): TDomElement;
var
Iterator: TIterator<TDomElement>;
Attribute: TDomAttribute;
begin
Result := nil;
Iterator := CreateElementIterator();
while (Result = nil) and Iterator.IsValid do
begin
Attribute := Iterator.Current.Attributes.GetOptional(Name);
if (Attribute <> nil) and (Attribute.AsText = Value) then
Result := Iterator.Current
else
Iterator.GotoNext();
end;
Iterator.Free();
end;
function TDomElement.ElementWithName(Name: TText): TDomElement;
var
Iterator: TIterator<TDomElement>;
begin
Result := nil;
Iterator := CreateElementIterator();
while (Result = nil) and Iterator.IsValid do
begin
if Iterator.Current.Name = Name then
Result := Iterator.Current
else
Iterator.GotoNext();
end;
Iterator.Free();
end;
function TDomElement.ElementWithNameAndAttributeValue(const Name: TText; const AttributeName: TText; const AttributeValue: TText): TDomElement;
var
Iterator: TIterator<TDomElement>;
Attribute: TDomAttribute;
begin
Result := nil;
Iterator := CreateElementWithNameIterator(Name);
while (Result = nil) and Iterator.IsValid do
begin
Attribute := Iterator.Current.Attributes.GetOptional(AttributeName);
if (Attribute <> nil) and (Attribute.AsText = AttributeValue) then
Result := Iterator.Current
else
Iterator.GotoNext();
end;
Iterator.Free();
end;
function TDomElement.ElementsWithName(Name: TText): TList<TDomElement>;
var
i: TInteger32;
Element: TDomElement;
begin
Result := TObjectList<TDomElement>.Create();
Result.ElementManager := TReferenceUser<TDomElement>.Create();
for i := 0 to Nodes.Count - 1 do
begin
if Nodes[i] is TDomElement then
begin
Element := Nodes[i] as TDomElement;
if Element.Name = Name then
Result.Add(Element);
end;
end;
end;
function TDomElement.GetFirstElement(): TDomElement;
var
i: TInteger32;
begin
Result := nil;
i := 0;
while (Result = nil) and (i < Nodes.Count) do
begin
if Nodes[i] is TDomElement then
Result := Nodes[i] as TDomElement
else
Inc(i);
end;
end;
function TDomElement.GetOrCreateText(): TDomText;
begin
if (Nodes.Count > 0) and (Nodes.Last is TDomText) then
Result := Nodes.Last as TDomText
else
begin
Result := TDomText.Create();
Nodes.Add(Result);
end;
end;
end.
|