forked from jrsoftware/issrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLParse.pas
294 lines (259 loc) · 8.48 KB
/
XMLParse.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
285
286
287
288
289
290
291
292
293
294
unit XMLParse;
{ $jrsoftware: ishelp/ISHelpGen/XMLParse.pas,v 1.5 2009/07/29 09:50:24 mlaan Exp $ }
{ XML parser. Currently just calls MSXML 4.0 to do the real work. }
interface
{$IFNDEF VER80} { if it's not Delphi 1.0 }
{$IFNDEF VER90} { if it's not Delphi 2.0 }
{$IFNDEF VER93} { and it's not C++Builder 1.0 }
{$IFNDEF VER100} { if it's not Delphi 3.0 }
{$IFNDEF VER110} { and it's not C++Builder 3.0 }
{$IFNDEF VER120} {$IFNDEF VER125} { if it's not Delphi 4 or C++Builder 4 }
{$IFNDEF VER130} { if it's not Delphi 5 or C++Builder 5 }
{$DEFINE IS_D6} { then it must be at least Delphi 6 or C++Builder 6 }
{$ENDIF}
{$ENDIF} {$ENDIF}
{$ENDIF}
{$ENDIF}
{$ENDIF}
{$ENDIF}
{$ENDIF}
uses
Windows, SysUtils{$IFDEF IS_D6}, Variants{$ENDIF};
type
IXMLNode = interface
function GetAttribute(const AName: String): String;
function GetOptionalAttribute(const AName: String): String;
function GetFirstChild: IXMLNode;
function GetNodeName: String;
function GetNextSibling: IXMLNode;
function GetNodeType: Integer;
function GetParentNode: IXMLNode;
function GetPreviousSibling: IXMLNode;
function GetRealMSXMLNode: OleVariant;
function GetText: String;
function HasAttribute(const AName: String): Boolean;
function TransformNode(const Stylesheet: IXMLNode): String;
property Attributes[const AName: String]: String read GetAttribute;
property OptionalAttributes[const AName: String]: String read GetOptionalAttribute;
property FirstChild: IXMLNode read GetFirstChild;
property NextSibling: IXMLNode read GetNextSibling;
property NodeName: String read GetNodeName;
property NodeType: Integer read GetNodeType;
property ParentNode: IXMLNode read GetParentNode;
property PreviousSibling: IXMLNode read GetPreviousSibling;
property Text: String read GetText;
end;
TXMLDocument = class
private
FDoc: OleVariant;
function GetRoot: IXMLNode;
public
constructor Create;
procedure LoadFromFile(const AFilename: String);
procedure StripComments;
property Root: IXMLNode read GetRoot;
end;
const
{ Values for the NodeType property }
NODE_INVALID = 0;
NODE_ELEMENT = 1;
NODE_ATTRIBUTE = 2;
NODE_TEXT = 3;
NODE_CDATA_SECTION = 4;
NODE_ENTITY_REFERENCE = 5;
NODE_ENTITY = 6;
NODE_PROCESSING_INSTRUCTION = 7;
NODE_COMMENT = 8;
NODE_DOCUMENT = 9;
NODE_DOCUMENT_TYPE = 10;
NODE_DOCUMENT_FRAGMENT = 11;
NODE_NOTATION = 12;
implementation
uses
ActiveX, ComObj;
type
TXMLNode = class(TInterfacedObject, IXMLNode)
private
FRealNode: OleVariant;
function GetFirstChild: IXMLNode;
function GetAttribute(const AName: String): String;
function GetOptionalAttribute(const AName: String): String;
function GetNextSibling: IXMLNode;
function GetNodeName: String;
function GetNodeType: Integer;
function GetParentNode: IXMLNode;
function GetPreviousSibling: IXMLNode;
function GetRealMSXMLNode: OleVariant;
function GetText: String;
function HasAttribute(const AName: String): Boolean;
function TransformNode(const Stylesheet: IXMLNode): String;
public
constructor Create(const ARealNode: OleVariant);
end;
function IsVarAssigned(const AVariant: OleVariant): Boolean;
begin
case VarType(AVariant) of
varEmpty: Result := False;
varDispatch: Result := Assigned(TVarData(AVariant).VDispatch);
else
raise Exception.Create('IsVarAssigned: Unexpected variant type');
end;
end;
function MakeNode(const ARealNode: OleVariant): IXMLNode;
begin
if IsVarAssigned(ARealNode) then
Result := TXMLNode.Create(ARealNode)
else
Result := nil;
end;
function WideCharToUTF8String(const Src: PWideChar; const SrcLen: Integer): AnsiString;
var
DestLen, I: Integer;
DestBuf: AnsiString;
DestPtr: PAnsiChar;
C: Word;
begin
if (SrcLen < 0) or (SrcLen > High(Integer) div 3) then
raise Exception.Create('WideCharToUTF8String: SrcLen out of range');
SetString(DestBuf, nil, SrcLen * 3);
DestLen := 0;
DestPtr := PAnsiChar(DestBuf);
for I := 0 to SrcLen-1 do begin
C := Ord(Src[I]);
if C <= $7F then begin
DestPtr[DestLen] := AnsiChar(C);
Inc(DestLen);
end
else if C <= $7FF then begin
DestPtr[DestLen] := AnsiChar($C0 or (C shr 6));
DestPtr[DestLen+1] := AnsiChar($80 or (C and $3F));
Inc(DestLen, 2);
end
else begin
if (C >= $D800) and (C <= $DFFF) then
raise Exception.Create('WideCharToUTF8String: Surrogate pairs are not supported');
DestPtr[DestLen] := AnsiChar($E0 or (C shr 12));
DestPtr[DestLen+1] := AnsiChar($80 or ((C shr 6) and $3F));
DestPtr[DestLen+2] := AnsiChar($80 or (C and $3F));
Inc(DestLen, 3);
end;
end;
SetLength(DestBuf, DestLen);
Result := DestBuf;
{ Following is an alternate version which uses WideCharToMultiByte.
Consistency across different Windows platforms is uncertain, so I prefer
my version. }
(*
if SrcLen = 0 then
Result := ''
else begin
DestLen := WideCharToMultiByte(CP_UTF8, 0, Src, SrcLen, nil, 0, nil, nil);
if DestLen = 0 then
RaiseLastWin32Error;
SetString(DestBuf, nil, DestLen);
if WideCharToMultiByte(CP_UTF8, 0, Src, SrcLen, @DestBuf[1], DestLen, nil, nil) <> DestLen then
raise Exception.Create('VariantToUTF8String: Unexpected result from WideCharToMultiByte');
Result := DestBuf;
end;
*)
end;
function VariantToUTF8String(const V: OleVariant): String;
begin
if VarType(V) <> varOleStr then
raise Exception.Create('VariantToUTF8String: Expected varOleStr');
Result := WideCharToUTF8String(TVarData(V).VOleStr,
SysStringLen(TVarData(V).VOleStr));
end;
{ TXMLDocument }
constructor TXMLDocument.Create;
begin
inherited Create;
FDoc := CreateOleObject('MSXML2.DOMDocument.4.0');
FDoc.async := False;
FDoc.preserveWhitespace := True;
end;
function TXMLDocument.GetRoot: IXMLNode;
begin
Result := MakeNode(FDoc.documentElement);
end;
procedure TXMLDocument.LoadFromFile(const AFilename: String);
begin
if not FDoc.load(AFilename) then begin
if Integer(FDoc.parseError.line) <> 0 then
raise Exception.CreateFmt('XML parse error (line %d, column %d): %s',
[Integer(FDoc.parseError.line), Integer(FDoc.parseError.linepos),
FDoc.parseError.reason])
else
raise Exception.CreateFmt('XML parse error: %s', [FDoc.parseError.reason]);
end;
end;
procedure TXMLDocument.StripComments;
begin
FDoc.selectNodes('//comment()').removeAll;
end;
{ TXMLNode }
constructor TXMLNode.Create(const ARealNode: OleVariant);
begin
inherited Create;
FRealNode := ARealNode;
end;
function TXMLNode.GetAttribute(const AName: String): String;
var
N: OleVariant;
begin
N := FRealNode.attributes.getNamedItem(AName);
if not IsVarAssigned(N) then
raise Exception.CreateFmt('Attribute "%s" does not exist', [AName]);
Result := VariantToUTF8String(N.value);
end;
function TXMLNode.GetOptionalAttribute(const AName: String): String;
var
N: OleVariant;
begin
N := FRealNode.attributes.getNamedItem(AName);
if not IsVarAssigned(N) then
Result := ''
else
Result := VariantToUTF8String(N.value);
end;
function TXMLNode.GetFirstChild: IXMLNode;
begin
Result := MakeNode(FRealNode.firstChild);
end;
function TXMLNode.GetNodeName: String;
begin
Result := VariantToUTF8String(FRealNode.nodeName);
end;
function TXMLNode.GetNextSibling: IXMLNode;
begin
Result := MakeNode(FRealNode.nextSibling);
end;
function TXMLNode.GetNodeType: Integer;
begin
Result := FRealNode.nodeType;
end;
function TXMLNode.GetParentNode: IXMLNode;
begin
Result := MakeNode(FRealNode.parentNode);
end;
function TXMLNode.GetPreviousSibling: IXMLNode;
begin
Result := MakeNode(FRealNode.previousSibling);
end;
function TXMLNode.GetRealMSXMLNode: OleVariant;
begin
Result := FRealNode;
end;
function TXMLNode.GetText: String;
begin
Result := VariantToUTF8String(FRealNode.text);
end;
function TXMLNode.HasAttribute(const AName: String): Boolean;
begin
Result := IsVarAssigned(FRealNode.attributes.getNamedItem(AName));
end;
function TXMLNode.TransformNode(const Stylesheet: IXMLNode): String;
begin
Result := VariantToUTF8String(FRealNode.transformNode(Stylesheet.GetRealMSXMLNode));
end;
end.