-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththinBasic_MSXML2_DOMDocument.Inc
More file actions
547 lines (477 loc) · 21.4 KB
/
thinBasic_MSXML2_DOMDocument.Inc
File metadata and controls
547 lines (477 loc) · 21.4 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
$DOMDOC_LoadXML = "LOADXML"
$DOMDOC_Load = "LOAD"
$DOMDOC_TEXT = "TEXT"
$DOMDOC_XML = "XML"
$DOMDOC_COUNT = "COUNT"
$DOMDOC_SetProperty = "SETPROPERTY"
$DOMDOC_GetProperty = "GETPROPERTY"
$DOMDOC_ParseError = "PARSEERROR"
$DOMDOC_ParseError_ErrorCode = "ERRORCODE"
$DOMDOC_ParseError_Reason = "REASON"
$DOMDOC_ParseError_SrcText = "SRCTEXT"
$DOMDOC_ParseError_Line = "LINE"
$DOMDOC_ParseError_LinePos = "LINEPOS"
$DOMDOC_ParseError_FilePos = "FILEPOS"
$DOMDOC_ParseError_Url = "URL"
$DOMDOC_GetElementsByTagName = "GETELEMENTSBYTAGNAME"
$DOMDOC_SelectSingleNode = "SELECTSINGLENODE"
$DOMDOC_SelectNodes = "SELECTNODES"
$DOMDOC_ChildNodes = "CHILDNODES"
'----------------------------------------------------------------------------------------------------------
'----------------------------------------------------------------------------
' Class
'----------------------------------------------------------------------------
Class cMsxml2_DOMDocument
'----------------------------------------------------------------------------
'---Class Private variables
'----------------------------------------------------------------------------
Instance oDOMDoc As IXMLDOMDocument3'Dispatch
Instance vResult As Variant
Instance lResult As Long
'----------------------------------------------------------------------------
'---Define here any Class Method (if needed)
'---Class methods are visible only from inside the class
'---so they can be called only from inside other methods.
'----------------------------------------------------------------------------
Class Method Create()
Set oDOMDoc = newcom $PROGID_MSXML2_DOMDocument600'"Msxml2.DOMDocument.6.0"
If Not IsObject(oDOMDoc) Then
thinBasic_RunTimeError(%ERR__MODULE_SPECIFIC, "It was not possible to create a DOMDocument object (" & $PROGID_MSXML2_DOMDocument600 & ")")
End If
End Method
Class Method Destroy()
oDOMDoc = Nothing
End Method
'----------------------------------------------------------------------------
' We need to define an interface that will contain public methods
'----------------------------------------------------------------------------
Interface iMsxml2_DOMDocument
Inherit IUnknown
'----------------------------------------------------------------------------
' GetClassObject
'----------------------------------------------------------------------------
Method Msxml2_DOMDocument_GetClassObject() As Dword
Method = VarPtr(oDOMDoc)
End Method
'----------------------------------------------------------------------------
' Creator Direct
'----------------------------------------------------------------------------
Method Msxml2_DOMDocument_Create_Direct(ByVal ptrToDirectRecordSet As Dword) As Dword
'---Here is the magic: we store at the memory address of pRecordSet (that is a ADORecordset COM pointer)
'---The address passed by the script. The address is usually returned by another Class method or property
'---Instructed to return an OBJPTR to an ADORecordset
Poke Dword, VarPtr(oDOMDoc), ptrToDirectRecordSet
End Method
' '----------------------------------------------------------------------------
' '
' '----------------------------------------------------------------------------
' Method LoadXML(ByVal sXml As String) As Long
' Local vXml As variant
'
' vXml = sXml
' Object Call oDOMDoc.LoadXml(vXML) To vResult
'
' End Method
'
' '----------------------------------------------------------------------------
' '
' '----------------------------------------------------------------------------
' Method Text() As String
'
' Object Call oDOMDoc.Text To vResult
'
' Method = Variant$(vResult)
' End Method
'----------------------------------------------------------------------------
'
'----------------------------------------------------------------------------
' Method GetElementsByTagName(ByVal sTag As String) As String
' register nItem as long
'
' Local vTag As variant
' Local ofields As Dispatch
' Local sResult As String
' Local vResult As Variant
' Local nItems As Long
' Local v1 As variant
'
' Set vTag = sTag
' Object Call oDOMDoc.GetElementsByTagName(vTag) To vResult
'
' Set oFields = vResult
' sResult = Variant$$(vResult)
'
''MsgBox FuncName$ + ":" + sResult
'
' Object Get oFields.length To vResult
' nItems = Variant#(vresult)
'
' For nItem = 0 To nItems -1
' v1 = nItem
' Object Get oFields.Item(v1).Text To vResult
' If VariantVT(vResult) <> 0 Then
' If nItem = 0 Then
' sResult += Variant$(vResult)
' Else
' sResult += $CrLf & Variant$(vResult)
' End If
' End If
' Next
' oFields = Nothing
'
' Method = sResult
' End Method
End Interface
End Class
'--------------------------------------------------------------------------------------
' We need to create a constructor wrapper function
' Constructor function will be executed by thinBasic when the class will be instantiated
' in script using the NEW keyword
'--------------------------------------------------------------------------------------
' IMPORTANT:
' ALL wrapper functions MUST HAVE a "BYVAL pObject AS LONG" parameter.
' pObject will contain a pointer to the object of our class
' This pointer is initialized by thinBasic engine to hold object reference
'--------------------------------------------------------------------------------------
Sub Msxml2_DOMDocument_Create( ByVal pObject As Long )
Local pp As Byte
pp = thinBasic_CheckOpenParens_Optional
If pp Then thinBasic_CheckCloseParens_Mandatory
If thinBasic_ErrorFree Then
'Here is the magic !!!!!!!!!!!!
'Use the magic REDIM ... AT and the trick is done
'We are defining a new class interface variable at the pointer location allocated by thinBasic and passed to all methods
ReDim oObject(1& To 1&) As iMsxml2_DOMDocument At pObject
oObject(1&) = Class "cMsxml2_DOMDocument"
End If
End Sub
'----------------------------------------------------------------------------
Sub Msxml2_DOMDocument_Create_Direct(ByVal pObject As Dword, ByVal pDirectClass As Dword)
'----------------------------------------------------------------------------
Local pp As Byte
Local pRecordSet As Ext
'---If all went fine ...
If thinBasic_ErrorFree Then
'Here is the magic !!!!!!!!!!!!
'Use the magic REDIM ... AT and the trick is done
'We are defining a new class interface variable at the pointer location allocated by thinBasic and passed to all methods
ReDim oObject(1& To 1&) As iMsxml2_DOMDocument At pObject
'---We instantiate a new class
oObject(1&) = Class "cMsxml2_DOMDocument"
'---We call sSet in order to assign initial value
'oObject(1&).cExcel_Workbook_Create(pRecordSet)
oObject(1&).Msxml2_DOMDocument_Create_Direct(pDirectClass)
'else
'---emit some error
End If
End Sub
'--------------------------------------------------------------------------------------
Sub Msxml2_DOMDocument_Destroy( ByVal pObject As Long )
'--------------------------------------------------------------------------------------
If pObject Then
ReDim oObject(1& To 1&) As iMsxml2_DOMDocument At pObject
Set oObject(1&) = Nothing
End If
End Sub
'----------------------------------------------------------------------------
Function Msxml2_DOMDocument_GetClassObject(ByVal pObject As Long) As Dword
'----------------------------------------------------------------------------
If thinBasic_ErrorFree Then
ReDim oObject(1& To 1&) As iMsxml2_DOMDocument At pObject
Function = oObject(1&).Msxml2_DOMDocument_GetClassObject()
End If
End Function
'----------------------------------------------------------------------------
'----------------------------------------------------------------------------
'--------------------------------------------------------------------------------------
Function Msxml2_DOMDocument_ClassHandling ( _
ByVal pObject As Long , _
ByVal lAction As Long , _
ByVal sPreviousName As String , _
Optional ByVal RetValNum As Ext Ptr , _
ByVal RetValStr As String Ptr , _
ByVal lRecursive As Long , _
ByVal sParam1 As String , _
ByVal sParam2 As String , _
ByVal eParam1 As Ext , _
ByVal eParam2 As Ext _
) As Long
'--------------------------------------------------------------------------------------
' IMPORTANT: function must return main return type of called method/property
'--------------------------------------------------------------------------------------
Local pp As Long
Local sName As String
Local sName2 As String
Local sName3 As String
Local sXml As String
Local sTagName As String
Local vResult As Variant
Local Counter As Long
Local sBuffer As String
Local lIdx As Long
Local sPropName As String
Local sPropValue As String
Static pNode As IXMLDOMNode
Static pNodeList As IXMLDOMNodeList
'---As usual we map our instance (whatever it will be) to a pObject pointer
'ReDim oObject(1& To 1&) As iMsxml2_DOMDocument At pObject
ReDim oObject(1& To 1&) As iMsxml2_DOMDocument At pObject
ReDim pDomDocument(1& To 1&) As IXMLDOMDocument3 At oObject(1&).Msxml2_DOMDocument_GetClassObject
'---We need to parse next token and we need to use special fucntion: thinBasic_GetUnknownToken
'---This function is special because it instruct parser (thinCore) to not check for token validity
'---but just parse something and return whetever it will find in terms of token string
' ATTENTION: thinBasic_GetUnknownToken always return UCASE tokens in order to simplify SELECT/CASE
sName = thinBasic_GetUnknownToken
'---Now we have to check parsed token using token content
Select Case Const$ sName '<<<---IMPORTANT: use CONST$ to speed up Select Case operations
'-------------------------------------------------------
'---Methods
'---------------
Case $DOMDOC_LoadXML
'---We need to tell thinCore what type of info we are returning assigning to return function value the type
'---Than assign to @RetValNum or @RetValStr the return value
Function = %thinBasic_ReturnNumber
'---parse Params
pp = thinBasic_CheckOpenParens_Optional
thinBasic_ParseString sXML
If pp Then thinBasic_CheckCloseParens_Mandatory
'---If all went fine ...
If thinBasic_ErrorFree Then
If lRecursive Then
'@RetValNum = Left$(@RetValStr, nChars)
Else
@RetValNum = pDomDocument(1&).LoadXml(sXml)
End If
End If
Case $DOMDOC_Load
'---We need to tell thinCore what type of info we are returning assigning to return function value the type
'---Than assign to @RetValNum or @RetValStr the return value
Function = %thinBasic_ReturnNumber
'---parse Params
pp = thinBasic_CheckOpenParens_Optional
thinBasic_ParseString sXML
If pp Then thinBasic_CheckCloseParens_Mandatory
'---If all went fine ...
If thinBasic_ErrorFree Then
If lRecursive Then
'@RetValNum = Left$(@RetValStr, nChars)
Else
@RetValNum = pDomDocument(1&).Load(sXml)
End If
End If
Case $DOMDOC_TEXT
Function = %thinBasic_ReturnString
@RetValStr = pDomDocument(1&).Text
Case $DOMDOC_XML
Function = %thinBasic_ReturnString
@RetValStr = pDomDocument(1&).XML
Case $DOMDOC_ParseError
'---Do net iteration
Case $DOMDOC_ParseError_ErrorCode
Select Case Const$ sPreviousName
Case $DOMDOC_ParseError
Function = %thinBasic_ReturnNumber
@RetValNum = pDomDocument(1&).ParseError.errorCode
End Select
Case $DOMDOC_ParseError_Reason
Select Case Const$ sPreviousName
Case $DOMDOC_ParseError
Function = %thinBasic_ReturnString
@RetValStr = pDomDocument(1&).ParseError.Reason
End Select
Case $DOMDOC_ParseError_SrcText
Select Case Const$ sPreviousName
Case $DOMDOC_ParseError
Function = %thinBasic_ReturnString
@RetValStr = pDomDocument(1&).ParseError.srcText
End Select
Case $DOMDOC_ParseError_Line
Select Case Const$ sPreviousName
Case $DOMDOC_ParseError
Function = %thinBasic_ReturnNumber
@RetValNum = pDomDocument(1&).ParseError.Line
End Select
Case $DOMDOC_ParseError_LinePos
Select Case Const$ sPreviousName
Case $DOMDOC_ParseError
Function = %thinBasic_ReturnNumber
@RetValNum = pDomDocument(1&).ParseError.LinePos
End Select
Case $DOMDOC_ParseError_FilePos
Select Case Const$ sPreviousName
Case $DOMDOC_ParseError
Function = %thinBasic_ReturnNumber
@RetValNum = pDomDocument(1&).ParseError.FilePos
End Select
Case $DOMDOC_ParseError_Url
Select Case Const$ sPreviousName
Case $DOMDOC_ParseError
Function = %thinBasic_ReturnString
@RetValStr = pDomDocument(1&).ParseError.Url
End Select
Case $DOMDOC_SetProperty
Function = %thinBasic_ReturnNumber
pp = thinBasic_CheckOpenParens_Optional
thinBasic_ParseString sPropName
if thinBasic_CheckComma_Mandatory then
thinBasic_ParseString sPropValue
End If
If pp Then thinBasic_CheckCloseParens_Mandatory
If thinBasic_ErrorFree Then
If lRecursive Then
Else
pDomDocument(1&).SetProperty(sPropName, sPropValue)
End If
End If
Case $DOMDOC_GetProperty
Function = %thinBasic_ReturnString
pp = thinBasic_CheckOpenParens_Optional
thinBasic_ParseString sPropName
If pp Then thinBasic_CheckCloseParens_Mandatory
If thinBasic_ErrorFree Then
If lRecursive Then
Else
sBuffer = Variant$$(pDomDocument(1&).GetProperty(sPropName))
End If
@RetValStr = sBuffer
End If
' Case "LEN"
' Function = %thinBasic_ReturnNumber
' @RetValNum = Len(@RetValStr)
Case $DOMDOC_GetElementsByTagName
'---We need to tell thinCore what type of info we are returning assigning to return function value the type
'---Than assign to @RetValNum or @RetValStr the return value
pp = thinBasic_CheckOpenParens_Optional
thinBasic_ParseString sTagName
If pp Then thinBasic_CheckCloseParens_Mandatory
If thinBasic_CheckOpenParens_Optional Then
thinBasic_ParseLong lIdx
thinBasic_CheckCloseParens_Mandatory
End If
If thinBasic_CheckPoint_Optional Then
sName2 = thinBasic_GetUnknownToken
End If
'---If all went fine ...
If thinBasic_ErrorFree Then
If lRecursive Then
'@RetValStr = Mid$(@RetValStr, eStart, eLen)
Else
'MsgBox "OK"
'@RetValNum = pDomDocument(1&).GetElementsByTagName(sTagName).Length
'MsgBox "OK1:"
pNodeList = pDomDocument(1&).GetElementsByTagName(sTagName)
'For Counter = 0 To pNodeList.length - 1
' sBuffer += pNodeList.Item(Counter).nodeName + ":" & pNodeList.Item(Counter).Text & $CrLf
'Next
'MsgBox "OK2:"
If IsObject(pNodeList) Then
If pNodeList.Length > 0 Then
'MsgBox "OK3:"
lIdx = lIdx - 1
If lIdx <= 0 Or lIdx > pNodeList.Length + 1 Then lIdx = 0
Select Case Const$ sName2
Case $DOMDOC_XML
Function = %thinBasic_ReturnString
sBuffer = pNodeList.Item(lIdx).xml
@RetValStr = sBuffer
Case $DOMDOC_TEXT
Function = %thinBasic_ReturnString
sBuffer = pNodeList.Item(lIdx).Text
@RetValStr = sBuffer
Case $DOMDOC_COUNT
Function = %thinBasic_ReturnNumber
@RetValNum = pNodeList.Length
Case Else
Function = %thinBasic_ReturnNumber
@RetValNum = VarPtr(pNodeList)
End Select
End If
'MsgBox "OK4:"
End If
End If
End If
Case $DOMDOC_SelectSingleNode
'---We need to tell thinCore what type of info we are returning assigning to return function value the type
'---Than assign to @RetValNum or @RetValStr the return value
Function = %thinBasic_ReturnString
pp = thinBasic_CheckOpenParens_Optional
thinBasic_ParseString sTagName
If pp Then thinBasic_CheckCloseParens_Mandatory
If thinBasic_CheckPoint_Optional Then
sName2 = thinBasic_GetUnknownToken
End If
'---If all went fine ...
If thinBasic_ErrorFree Then
If lRecursive Then
'@RetValStr = Mid$(@RetValStr, eStart, eLen)
Else
'MsgBox "OK1:" & sTagName
'@RetValNum = pDomDocument(1&).GetElementsByTagName(sTagName).Length
pNode = pDomDocument(1&).SelectSingleNode(sTagName)
'For Counter = 0 To pNodeList.length - 1
' sBuffer += pNodeList.Item(Counter).nodeName + ":" & pNodeList.Item(Counter).Text & $CrLf
'Next
If IsObject(pNode) Then
'MsgBox "OK2"
Select Case Const$ sName2
Case $DOMDOC_XML
sBuffer = pNode.xml
Case $DOMDOC_TEXT
sBuffer = pNode.Text
Case Else
Function = %thinBasic_ReturnNumber
@RetValNum = VarPtr(pNode)
End Select
End If
@RetValStr = sBuffer
End If
End If
Case $DOMDOC_ChildNodes
Function = %thinBasic_ReturnNumber
pp = thinBasic_CheckOpenParens_Optional
If pp Then thinBasic_CheckCloseParens_Mandatory
If thinBasic_ErrorFree Then
If lRecursive Then
'@RetValStr = Mid$(@RetValStr, eStart, eLen)
Else
'MsgBox "OK1:" & sTagName
'@RetValNum = pDomDocument(1&).GetElementsByTagName(sTagName).Length
pNodeList = pDomDocument(1&).ChildNodes
@RetValNum = VarPtr(pNodeList)
End If
End If
Case $DOMDOC_SelectNodes
Function = %thinBasic_ReturnNumber
pp = thinBasic_CheckOpenParens_Optional
thinBasic_ParseString sTagName
If pp Then thinBasic_CheckCloseParens_Mandatory
If thinBasic_ErrorFree Then
If lRecursive Then
'@RetValStr = Mid$(@RetValStr, eStart, eLen)
Else
'MsgBox "OK1:" & sTagName
'@RetValNum = pDomDocument(1&).GetElementsByTagName(sTagName).Length
pNodeList = pDomDocument(1&).SelectNodes(sTagName)
@RetValNum = VarPtr(pNodeList)
End If
End If
Case Else
thinBasic_RunTimeError(%ERR__CLASS_METHODPROPERTY_NOTFOUND)
End Select
'-------------------------------------------------------
'---Compound methods
'---------------
' In case we have compound methods (defined as point after a method or property
' call this function recursively.
'---------------
If thinBasic_CheckPoint_Optional Then
Function = Msxml2_DOMDocument_ClassHandling ( _
pObject , _
lAction , _
sName , _
RetValNum , _
RetValStr , _
%TRUE _
)
End If
End Function