-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlparser.pas
217 lines (196 loc) · 5.85 KB
/
htmlparser.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
{
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
HTMLParser;
Interface
Uses
Classes,
SysUtils,
XMLScanner,
HTMLNodes;
Type
THTMLParser = Class
Private
fOwnsSource : Boolean;
fSource : TXMLTokenIterator;
fDestination : THTMLNode;
Procedure Mark(aNode : THTMLNode);
Procedure ParseTagList(Const aPrevious : THTMLNode);
Procedure ParseTag(Const aPrevious : THTMLNode);
Procedure ParsePropertyList(Const aPrevious : THTMLNode);
Procedure ParseProperty(Const aPrevious : THTMLNode);
Procedure ParseText(Const aPrevious : THTMLNode);
Procedure ParseSpecialTag1(Const aPrevious : THTMLNode);
Procedure ParseSpecialTag2(Const aPrevious : THTMLNode);
Public
Constructor Create(Const aSource : TXMLTokenIterator; aDestination : THTMLNode; Const aOwnsSource : Boolean = True); Virtual;
Destructor Destroy; Override;
Procedure Parse;
End;
THTMLRootNode = Class(THTMLNode)
Public
Procedure DoLoadFromFile(Const aFileName : String);
End;
Implementation
Procedure THTMLParser.Mark(aNode : THTMLNode);
Begin
aNode.Row := fSource.Row;
aNode.Col := fSource.Col;
End;
Procedure THTMLParser.ParseTagList(Const aPrevious : THTMLNode);
Begin
// Debug WriteLn('Parsing tag list.');
While Not(fSource.IsEOS Or (fSource.Kind = tkXMLEOF)) Do
If fSource.Match([tkXMLText]) Then
ParseText(aPrevious)
Else If fSource.Match([tkXMLOpenTag, tkXMLIdentifier]) Then
ParseTag(aPrevious)
Else If fSource.Match([tkXMLOpenTag, tkXMLQuestion]) Then
ParseSpecialTag1(aPrevious)
Else If fSource.Match([tkXMLOpenTag, tkXMLExclamation]) Then
ParseSpecialTag2(aPrevious)
Else If fSource.Match([tkXMLOpenTag, tkXMLSlash]) Then
Exit
Else
fSource.RaiseError('Expected tag or text.');
// Debug WriteLn('Done.');
End;
Procedure THTMLParser.ParseTag(Const aPrevious : THTMLNode);
Var
lTag : THTMLNode;
Begin
// Debug WriteLn('Parsing tag.');
fSource.Consume(tkXMLOpenTag);
lTag := HTMLClassFactory.Build(fSource.Literal, aPrevious);
Mark(lTag);
lTag.Name := fSource.Extract(tkXMLIdentifier);
ParsePropertyList(lTag);
If HTMLClassFactory.TagHasChilds(lTag.Name) Then
Begin
fSource.Consume(tkXMLCloseTag);
ParseTagList(lTag);
fSource.Consume(tkXMLOpenTag);
fSource.Consume(tkXMLSlash);
fSource.Consume(lTag.Name);
fSource.Consume(tkXMLCloseTag);
End
Else
fSource.Consume(tkXMLCloseTag);
// Debug WriteLn('Done.');
End;
Procedure THTMLParser.ParsePropertyList(Const aPrevious : THTMLNode);
Begin
// Debug WriteLn('Parsing property list.');
While Not(fSource.Expected([tkXMLExclamation, tkXMLQuestion, tkXMLSlash, tkXMLCloseTag])) Do
Begin
fSource.Consume(tkXMLWhite, False);
ParseProperty(aPrevious);
If fSource.Expected(tkXMLEOF) Or fSource.IsEOS Then
fSource.RaiseError('Unexpected end of file.');
End;
// Debug WriteLn('Done.');
End;
Procedure THTMLParser.ParseProperty(Const aPrevious : THTMLNode);
Var
lName,
lValue : String;
Begin
// Debug WriteLn('Parsing property.');
lName := fSource.Extract(tkXMLIdentifier);
fSource.Consume(tkXMLEqual);
lValue := fSource.Extract(tkXMLString);
aPrevious.Properties.SetValue(lName, lValue);
// Debug WriteLn('Done.');
End;
Procedure THTMLParser.ParseText(Const aPrevious : THTMLNode);
Var
lTag : THTMLTextNode;
Begin
// Debug WriteLn('Parsing text.');
lTag := THTMLTextNode.Create(aPrevious);
lTag.Name := '';
lTag.Content := fSource.Extract(tkXMLText);
// Debug WriteLn('Done.');
End;
Procedure THTMLParser.ParseSpecialTag1(Const aPrevious : THTMLNode);
Var
lTag : THTMLSpecialTag1Node;
Begin
// Debug WriteLn('Parsing special tag (<?something?>).');
fSource.Consume(tkXMLOpenTag);
fSource.Consume(tkXMLQuestion);
lTag := THTMLSpecialTag1Node.Create(aPrevious);
Mark(lTag);
lTag.Name := fSource.Extract(tkXMLIdentifier);
ParsePropertyList(lTag);
fSource.Consume(tkXMLQuestion);
fSource.Consume(tkXMLCloseTag);
// Debug WriteLn('Done.');
End;
Procedure THTMLParser.ParseSpecialTag2(Const aPrevious : THTMLNode);
Var
lTag : THTMLSpecialTag2Node;
Begin
// Debug WriteLn('Parsing special tag (<!something!>).');
fSource.Consume(tkXMLOpenTag);
fSource.Consume(tkXMLExclamation);
lTag := THTMLSpecialTag2Node.Create(aPrevious);
Mark(lTag);
lTag.Name := fSource.Extract(tkXMLIdentifier);
ParsePropertyList(lTag);
fSource.Consume(tkXMLExclamation);
fSource.Consume(tkXMLCloseTag);
// Debug WriteLn('Done.');
End;
Constructor THTMLParser.Create(Const aSource : TXMLTokenIterator; aDestination : THTMLNode; Const aOwnsSource : Boolean = True);
Begin
Inherited Create;
fOwnsSource := aOwnsSource;
fSource := aSource;
fDestination := aDestination;
End;
Destructor THTMLParser.Destroy;
Begin
If fOwnsSource Then
fSource.Free;
Inherited Destroy;
End;
Procedure THTMLParser.Parse;
Begin
fSource.Start;
// Debug WriteLn('Parsing file.');
ParseTagList(fDestination);
// Debug WriteLn('Done.');
fSource.Consume(tkXMLEOF);
End;
Procedure THTMLRootNode.DoLoadFromFile(Const aFileName : String);
Var
lTokens : TMemoryStream;
Begin
lTokens := TMemoryStream.Create;
With TXMLScanner.Create(TXMLSource.Create(TFileStream.Create(aFileName, fmOpenRead)), lTokens) Do
Begin
Try
Scan;
Finally
Free;
End;
End;
With THTMLParser.Create(TXMLTokenIterator.Create(lTokens), Self) Do
Begin
Try
Parse;
Finally
Free;
End;
End;
End;
End.