Repository navigation
Expand file tree
/
Copy pathxml-serializer.prg
More file actions
1400 lines (1080 loc) · 45.8 KB
/
Copy pathxml-serializer.prg
File metadata and controls
1400 lines (1080 loc) · 45.8 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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
*
* XMLSerializer
*
* A class to serialize an XML document into a VFP object, back and forth. It may also be used to serialize
* an arbitrary VFP object (or an object fragment) into an XML document.
*
* Methods:
* m.VFP = m.XMLSerializer.XMLtoVFP (Source AS String)
* Retrieves an XML document (from a string, an URL or a file) and returns a VFP object matching the tree of the Source.
* xml* members of the returned object control different aspects of the XML nodes (name, namespaces, ...).
* Nodes that repeat their names (either because they occur more than once, in the source document, either because
* their VFP name is repeated) are treated as arrays.
* *************************************************************************************************************
* Important note:
* Based on the original code and ideas of Marco Plaza, and on his nfxmlread program (see nfXML poject in VFPX).
* *************************************************************************************************************
* Example of a serialization
*
* <?xml version="1.0"?>
* <doc>
* <child order="a">Some value</child>
* <child order="b">Other value</child>
* </doc>
*
* results in
*
* doc
* - child[1]
* - xmlattributes
* - order
* - xmlname
* - xmlns
* - xmlposition
* - xmltext
* - xmlname
* - xmlns
* - xmlcount
* - xmlposition
* - xmltext
* - child[2]
* - ...
* - xmlname
* - xmlns
* - xmlposition
*
* m.VFP = m.XMLSerializer.GetSimpleCopy(VFPObject AS Object[, Options AS Integer])
* Produces a simplified copy of the serialized object, from which the xml* information is stripped.
* The copy may be configured to produce added _value_ properties for mixed elements (with text and subtree), to
* hold text values in simplified xmltext properties, or to follow nfxml serializations.
* For instance, as for the above ezample:
*
* simplified result (Options = 1)
*
* doc
* - child[1]
* - xmlattributes
* - order
* - xmltext
* - child[2]
* - ...
*
* m.XML = m.XMLSerializer.VFPtoXML (VFPObject AS Object[, Root AS String])
* Creates an XML DOM object from a VFP object (either serialized from an XML document, or originally a VFP object).
* If xml* members are present in the VFP object, they are used to (re)construct the XML DOM.
* The name of a root may be passed, to encapsulate a rootless VFP object (or to add a new layer to another).
*
* m.Text = m.XMLSerializer.GetText (VFPObject.property)
* Retrieves the text associated to a serialized property.
* For instance, as for the above example,
* ? m.Serializer.GetText(m.VFP.doc.child[1])
* would return
* Some value
* or
* ? m.Serializer.GetText(m.VFP.doc.child[2].xmlattributes.order)
* would return
* b
* (note that order may become _order, if an array, because order is not allowed as a VFP array property name)
*
* m.Count = m.XMLSerializer.GetArrayLength (VFPObject.property)
* Retrieves the number of elements in a serialized array, or 0 if the property does not hold an array.
* For instance, as for the the above example
* ? m.Serializer.GetArrayLength(m.VFP.doc.child)
* would return
* 2
*
*
* Dependencies:
* VFP9
* MSXML6
* NAMER.PRG procedure file
*
* Load:
* SET PROCEDURE TO xml-serializer.prg ADDITIVE
* or
* DO xml-serializer.prg
*
* Use:
* m.XMLSerializer = CREATEOBJECT("XMLSerializer")
*
* dependency on Namer class
IF _VFP.StartMode = 0
DO LOCFILE("namer.prg")
ELSE
DO namer.prg
ENDIF
* install itself
IF !SYS(16) $ SET("Procedure")
SET PROCEDURE TO (SYS(16)) ADDITIVE
ENDIF
#INCLUDE "xml-serializer-class.h"
#DEFINE SAFETHIS ASSERT !USED("This") AND TYPE("This") == "O"
DEFINE CLASS XMLSerializer AS Custom
ADD OBJECT DomainNamer AS Namer
_memberdata = '<VFPData>' + ;
'<memberdata name="xmltovfp" type="method" display="XMLtoVFP"/>' + ;
'<memberdata name="vfptoxml" type="method" display="VFPtoXML"/>' + ;
'<memberdata name="gettext" type="method" display="GetText"/>' + ;
'<memberdata name="getsimplecopy" type="method" display="GetSimpleCopy"/>' + ;
'<memberdata name="getarraylength" type="method" display="GetArrayLength"/>' + ;
'<memberdata name="setoption" type="method" display="SetOption"/>' + ;
'<memberdata name="getoption" type="method" display="GetOption"/>' + ;
'<memberdata name="domainnamer" type="property" display="DomainNamer"/>' + ;
'<memberdata name="options" type="property" display="Options"/>' + ;
'<memberdata name="xmlerror" type="property" display="XMLError"/>' + ;
'<memberdata name="xmlline" type="property" display="XMLLine"/>' + ;
'</VFPData>'
XMLError = ""
XMLLine = 0
Parser = .NULL.
DIMENSION DataTypes[6]
DataTypes[1] = XMLT_TEXT
DataTypes[2] = XMLT_CDATA
DataTypes[3] = XMLT_ELEMENT
DataTypes[4] = XMLT_PI
DataTypes[5] = XMLT_COMMENT
DataTypes[6] = XMLT_DTD
DIMENSION XMLProperties[9]
XMLProperties[1] = XMLP_NAME
XMLProperties[2] = XMLP_TEXT
XMLProperties[3] = XMLP_NS
XMLProperties[4] = XMLP_PREFIXES
XMLProperties[5] = XMLP_POSITION
XMLProperties[6] = XMLP_COUNT
XMLProperties[7] = XMLP_ATTRIBUTES
XMLProperties[8] = XMLP_QNAME
XMLProperties[9] = XMLP_SOURCE
HIDDEN _Op_WhiteSpace
HIDDEN _Op_ProcessingInstruction
HIDDEN _Op_Comment
HIDDEN _Op_DTD
_Op_WhiteSpace =.F.
_Op_ProcessingInstruction = .F.
_Op_Comment = .F.
_Op_DTD = .F.
FUNCTION Init
SAFETHIS
* set the name translators for VFP and XML
This.DomainNamer.AttachProcessor("VFPNamer", LOCFILE("vfp-names.prg"))
This.DomainNamer.AttachProcessor("XMLNamer", LOCFILE("xml-names.prg"))
This.DomainNamer.SetProperty("VFPNamer", "AllowReserved", .F.)
* this is the XML parser (serialized XML DOM objects are created when needed)
This.Parser = CREATEOBJECT("MSXML2.DOMDocument.6.0")
This.Parser.Async = .F.
RETURN .T.
ENDFUNC
***************************************************************************************************
* SetOption
* Sets an option
***************************************************************************************************
FUNCTION SetOption (Option AS String, OptionValue AS AnyType)
SAFETHIS
DO CASE
CASE m.Option == XMLSERIAL_WHITESPACE
This._Op_WhiteSpace = m.OptionValue
CASE m.Option == XMLSERIAL_PROCESSINGINSTRUCTIONS
This._Op_ProcessingInstruction = m.OptionValue
CASE m.Option == XMLSERIAL_COMMENTS
This._Op_Comment = m.OptionValue
CASE m.Option == XMLSERIAL_DTD
This._Op_DTD = m.OptionValue
ENDCASE
ENDFUNC
***************************************************************************************************
* GetOption
* Returns an option value
***************************************************************************************************
FUNCTION GetOption (Option AS String)
SAFETHIS
DO CASE
CASE m.Option == XMLSERIAL_WHITESPACE
RETURN This._Op_WhiteSpace
CASE m.Option == XMLSERIAL_PROCESSINGINSTRUCTIONS
RETURN This._Op_ProcessingInstruction
CASE m.Option == XMLSERIAL_COMMENTS
RETURN This._Op_Comment
CASE m.Option == XMLSERIAL_DTD
RETURN This._Op_DTD
ENDCASE
RETURN .NULL.
ENDFUNC
***************************************************************************************************
* XMLtoVFP
* Source
* - a URL, or file name, or string, with the XML to process
* XPath
* - an XPath expression that resolves to a node list
* Namespaces
* - a string or a collection of keyed namespaces to resolve prefix references in XPath
* Returns a VFP Empty-based object, or .NULL., if errors during loading of the document
***************************************************************************************************
FUNCTION XMLtoVFP AS Empty
LPARAMETERS Source AS StringURLorDOM, XPath AS String, Namespaces AS CollectionOrString
SAFETHIS
ASSERT VARTYPE(m.Source) $ "CO" MESSAGE "Source must be a string or an object."
ASSERT PCOUNT() < 2 OR VARTYPE(m.XPath) == "C" MESSAGE "XPath must be a string expression."
ASSERT PCOUNT() < 3 OR VARTYPE(m.Namespaces) $ "CO" MESSAGE "Namespaces must be a string or an object."
LOCAL VFPObject AS Empty
LOCAL SourceObject AS MSXML2.DOMDocument60
LOCAL SourceTree AS MSXML2.IXMLDOMNodeList
LOCAL XPathError AS Exception
LOCAL CurrentNS AS String
LOCAL SelectionNS AS String
LOCAL LoopIndex AS Integer
This.XMLError = ""
This.XMLLine = 0
* if an XMLDOM object was passed, use it as the source
IF VARTYPE(m.Source) == "O"
m.SourceObject = m.Source
ELSE
* follow white space setting
This.Parser.preserveWhiteSpace = This._Op_WhiteSpace
* and DTD also
This.Parser.setProperty("ProhibitDTD", !This._Op_DTD)
This.Parser.validateOnParse = !This._Op_DTD
* otherwise, a source document has to be loaded
* try to load as an URL / file
IF !This.Parser.Load(m.Source)
* if not, as a string
This.Parser.LoadXML(m.Source)
ENDIF
This.XMLError = This.Parser.parseError.reason
This.XMLLine = This.Parser.parseError.line
m.SourceObject = This.Parser
ENDIF
IF EMPTY(This.XMLError)
* if an XPath parameter was passed, use it to set the root of the serialization
IF !EMPTY(m.XPath)
TRY
* the namespaces used in the XPath expression may be set as a string or as a collection
IF PCOUNT() = 3
m.CurrentNS = m.SourceObject.getProperty("SelectionNamespaces")
IF VARTYPE(m.Namespaces) == "C"
m.SelectionNS = m.Namespaces
ELSE
m.SelectionNS = ""
FOR m.LoopIndex = 1 TO m.Namespaces.Count
m.SelectionNS = m.SelectionNS + TEXTMERGE("xmlns:<<m.Namespaces.GetKey(m.LoopIndex)>>='<<m.Namespaces.Item(m.LoopIndex)>>' ")
ENDFOR
ENDIF
m.SourceObject.setProperty("SelectionNamespaces", m.SelectionNS)
ENDIF
* get the node list to serialize
m.SourceTree = m.SourceObject.selectNodes(m.XPath)
* restore the namespaces in use for selection
IF PCOUNT() = 3
m.SourceObject.setProperty("SelectionNamespaces", m.CurrentNS)
ENDIF
CATCH TO m.XPathError
This.XMLError = m.XPathError.Message
This.XMLLine = -1
ENDTRY
ELSE
* serialize nodes starting at the root, if a DOMDocument
IF ISNULL(m.SourceObject.ownerDocument)
m.SourceTree = m.SourceObject.childNodes()
* otherwise, serialize the XML fragment at the context point of the tree
ELSE
m.SourceTree = m.SourceObject.selectNodes(".")
ENDIF
ENDIF
ENDIF
* if the document was parsed
IF EMPTY(This.XMLError)
m.VFPObject = CREATEOBJECT("Empty")
* read the tree and put it in a VFP object
This.ReadXMLTree(m.SourceTree, m.VFPObject, .T.)
ELSE
* report failure
m.VFPObject = .NULL.
ENDIF
RETURN m.VFPObject
ENDFUNC
***************************************************************************************************
* VFPtoXML
* Source
* - a VFP object reference
* Root
* - [optional] the name of the root element
* Returns an XML DOM Document
***************************************************************************************************
FUNCTION VFPtoXML AS MSXML2.DOMDocument60
LPARAMETERS Source AS anyVFPObject, Root AS String
SAFETHIS
ASSERT VARTYPE(m.Source) = "O" MESSAGE "Source must be an object."
ASSERT PCOUNT() = 1 OR VARTYPE(m.Root) = "C" MESSAGE "Root name must be a string."
LOCAL XMLObject AS MSXML2.DOMDocument60
LOCAL Namespaces AS Collection
LOCAL XMLDeclaration AS MSXML2.IXMLDOMProcessingInstruction
LOCAL NsDeclaration AS MSXML2.IXMLDOMAttribute
LOCAL LoopIndex AS String
LOCAL ARRAY Properties[1]
LOCAL RootProperty AS Integer
LOCAL PropertyCount AS Integer
LOCAL PropertiesIndex AS Integer
LOCAL DTDProperty AS Integer
LOCAL ExactSetting AS String
DO CASE
* source must be an active VFP object
CASE VARTYPE(m.Source) != "O" OR ISNULL(m.Source)
RETURN .NULL.
CASE PCOUNT() = 1
m.ExactSetting = SET("Exact")
SET EXACT ON
* there must be a single root at the top of the object property tree
m.RootProperty = 0
* no DTD, yet
m.DTDProperty = 0
* look for the root (ignoring sibling processing instructions, and comments)
PropertyCount = AMEMBERS(m.Properties, m.Source, 0, "U")
FOR m.PropertiesIndex = 1 TO m.PropertyCount
IF !INLIST(LOWER(m.Properties[m.PropertiesIndex]), XML_DTD, XML_PI, XML_COMMENT, XML_ORPHANTEXT)
* give up if there is more than one
IF m.RootProperty != 0
m.RootProperty = 0
EXIT
ENDIF
m.RootProperty = m.PropertiesIndex
ENDIF
ENDFOR
IF m.ExactSetting == "OFF"
SET EXACT OFF
ENDIF
* give up if a root was not found, or it is an array, or it is undefined
IF m.RootProperty = 0 ;
OR TYPE("m.Source." + m.Properties[m.RootProperty], 1) == "A" ;
OR TYPE("m.Source." + m.Properties[m.RootProperty]) == "U"
RETURN .NULL.
ENDIF
OTHERWISE
m.PropertyCount = 1
ENDCASE
* the object that will be built and then returned
m.XMLObject = CREATEOBJECT("MSXML2.DOMDocument.6.0")
m.XMLObject.preserveWhiteSpace = This._Op_WhiteSpace
m.XMLDeclaration = m.XMLObject.createProcessingInstruction("xml", 'version="1.0" encoding="utf-8"')
m.XMLObject.appendChild(m.XMLDeclaration)
* the collection of the namespaces in use
m.Namespaces = CREATEOBJECT("Collection")
* process the VFP object tree, starting from top property or from the object itself
DO CASE
CASE PCOUNT() = 1 AND m.PropertyCount = 1
* a simple case where there is a single property at the top
This.ReadVFPTree(m.Properties[1], EVALUATE("m.Source." + m.Properties[1]), m.XMLObject, m.XMLObject, VFP_ELEMENT, "", m.Namespaces)
CASE m.PropertyCount > 1
* when there are several properties, with one identified root, process from the document level
This.ReadVFPTree("", m.Source, m.XMLObject, m.XMLObject, VFP_DOCUMENT, "", m.Namespaces)
OTHERWISE
* the root is named, and identified, process from there
This.ReadVFPTree(m.Root, m.Source, m.XMLObject, m.XMLObject, VFP_ELEMENT, "", m.Namespaces)
ENDCASE
* after the XML object is built, move the namespaces declarations to the root element to reduce verbosity
FOR m.LoopIndex = 1 TO m.Namespaces.Count
m.NsDeclaration = m.XMLObject.createAttribute("xmlns:" + m.Namespaces.Item(m.LoopIndex))
m.NsDeclaration.text = m.Namespaces.GetKey(m.LoopIndex)
m.XMLObject.documentElement.attributes.setNamedItem(m.NsDeclaration)
ENDFOR
* done
RETURN m.XMLObject
ENDFUNC
***************************************************************************************************
* GetText
* Element
* - a point in a serialized VFP object
* Returns the textual value of the element, or .NULL. if the element does not exist
***************************************************************************************************
FUNCTION GetText
LPARAMETERS Element
ASSERT VARTYPE(m.Element) == "O" MESSAGE "Element must be an object."
LOCAL ElementText AS String
LOCAL LoopIndex AS Integer
IF TYPE("m.Element.xmltext") == "O"
m.ElementText = ""
IF !ISNULL(m.Element.xmltext)
FOR m.LoopIndex = 1 TO m.Element.xmltext.Count
m.ElementText = m.ElementText + NVL(m.Element.xmltext(m.LoopIndex), "")
ENDFOR
ENDIF
ELSE
m.ElementText = .NULL.
ENDIF
RETURN m.ElementText
ENDFUNC
***************************************************************************************************
* GetArrayLength
* Element
* - a point in a serialized VFP object
* Returns the numbers of elements of an array, 0 for a regular element, or .NULL. if it is .NULL.
***************************************************************************************************
FUNCTION GetArrayLength
LPARAMETERS Element
ASSERT VARTYPE(m.Element) == "O" MESSAGE "Element must be an object."
LOCAL ArrayLength AS Integer
DO CASE
CASE TYPE("m.Element.xmlcount") == "N"
m.ArrayLength = m.Element.xmlcount
CASE !ISNULL(m.Element)
m.ArrayLength = 0
OTHERWISE
m.ArrayLength = .NULL.
ENDCASE
RETURN m.ArrayLength
ENDFUNC
***************************************************************************************************
* GetSimpleCopy
* Element
* - a point in a serialized VFP object
* Options
* - 0 (or .F., default) produce added _value_ properties for mixed elements (with text and subtree)
* - 1 (or .T.) text from mixed elements is simplified as an xmltext character property
* - 2 create nfXML compatible structure
* Returns a simplified copy of the serialized VFP object, from which xml* data is removed
***************************************************************************************************
FUNCTION GetSimpleCopy
LPARAMETERS Element, Options AS Integer
SAFETHIS
ASSERT VARTYPE(m.Element) == "O" MESSAGE "Element must be an object."
ASSERT VARTYPE(m.Options) $ "LN" MESSAGE "Options must be logical or a number."
* the segment of the simple copy created at this level
LOCAL SimpleCopy AS Empty
* and what follows downtree
LOCAL DownTree AS Empty
* properties found at this level, and immediately downtree
LOCAL ARRAY Properties[1], DownProperties[1]
* to control a property that is being processed (regular or array)
LOCAL Property AS String
LOCAL ArrayProperty AS String
LOCAL ArrayLength AS Integer
LOCAL ArrayNoValues AS Boolean
LOCAL TextValue AS String
* loop indexers
LOCAL LoopIndex AS Integer
LOCAL LoopArrayIndex AS Integer
* make sure that Options is 0, 1 or 2
IF VARTYPE(m.Options) == "L"
m.Options = IIF(m.Options, 1, 0)
ELSE
IF !INLIST(VAL(TRANSFORM(m.Options)), 1, 2)
m.Options = 0
ENDIF
ENDIF
* create the segment that will be returned
m.SimpleCopy = CREATEOBJECT("Empty")
* go through all properties at this level (from root downtree)
FOR m.LoopIndex = 1 TO AMEMBERS(m.Properties, m.Element, 0)
m.Property = UPPER(m.Properties[m.LoopIndex])
* ignore all xml* properties, except attributes
IF LEFT(m.Property, 3) != "XML" OR m.Property == XML_SIMPLEATTR
m.ArrayLength = This.GetArrayLength(m.Element.&Property.)
* cases where properties were arrayed
IF m.ArrayLength != 0
* create the full array (we have the number of elements, beforehand)
ADDPROPERTY(m.SimpleCopy, m.Property + "[" + LTRIM(STR(m.ArrayLength)) + "]")
IF m.Options = 0
* and a sibling <name>_value_[], to hold values if needed
ADDPROPERTY(m.SimpleCopy, m.Property + XML_SIMPLETEXT + "[" + LTRIM(STR(m.ArrayLength)) + "]")
* for now, there are no values assigned to this new array
m.ArrayNoValues = .T.
ENDIF
* go through each member of the array
FOR m.LoopArrayIndex = 1 TO m.ArrayLength
* create a reference to it
m.ArrayProperty = m.Property + "[" + LTRIM(STR(m.LoopArrayIndex)) + "]"
* fetch the simplified text value
m.TextValue = EVL(This.GetText(m.Element.&ArrayProperty.), "")
* and create the simplified version of the tree under it (if there is one)
m.DownTree = This.GetSimpleCopy(m.Element.&ArrayProperty., m.Options)
DO CASE
* if there is no tree
CASE AMEMBERS(m.DownProperties, m.DownTree) = 0
* just use the text value
m.SimpleCopy.&ArrayProperty. = NVL(m.TextValue, "")
* is there text, and we are using _value_ properties?
CASE m.Options = 0 AND !EMPTY(NVL(m.TextValue, ""))
* store the downtree in the property
m.SimpleCopy.&ArrayProperty. = m.DownTree
* and the text value in the sibling array
m.ArrayProperty = m.Property + XML_SIMPLETEXT +"[" + LTRIM(STR(m.LoopArrayIndex)) + "]"
m.SimpleCopy.&ArrayProperty. = m.TextValue
* signal that we used the sibling array
m.ArrayNoValues = .F.
* is there text, and we are running in nf compatible mode?
CASE m.Options = 2 AND !EMPTY(NVL(m.TextValue, ""))
* store the downtree in the property
m.SimpleCopy.&ArrayProperty. = m.DownTree
* and add a special property for the text
ADDPROPERTY(m.SimpleCopy.&ArrayProperty., XML_NFTEXT, m.TextValue)
OTHERWISE
* in other cases, store the downtree in the property
m.SimpleCopy.&ArrayProperty. = m.DownTree
* and add the text value to a character property, if set by option
IF m.Options = 1 AND !EMPTY(NVL(m.TextValue, ""))
ADDPROPERTY(m.SimpleCopy.&ArrayProperty., XML_TEXT, NVL(m.TextValue, ""))
ENDIF
ENDCASE
ENDFOR
* if the sibling array was not needed, it may be removed, now
IF m.Options = 0 AND m.ArrayNoValues
REMOVEPROPERTY(m.SimpleCopy, m.Property + XML_SIMPLETEXT)
ENDIF
* cases where property is not arrayed
ELSE
* get its simplified text
m.TextValue = This.GetText(m.Element.&Property.)
* and its downtree
m.DownTree = This.GetSimpleCopy(m.Element.&Property., m.Options)
DO CASE
* if there is no tree
CASE AMEMBERS(m.DownProperties, m.DownTree) = 0
* just use the text value
ADDPROPERTY(m.SimpleCopy, m.Property, NVL(m.TextValue, ""))
* is there text, and we are using _value_ properties?
CASE m.Options = 0 AND !EMPTY(NVL(m.TextValue, ""))
* store the downtree in the property
ADDPROPERTY(m.SimpleCopy, m.Property, m.DownTree)
* and the text value in the sibling property
ADDPROPERTY(m.SimpleCopy, m.Property + XML_SIMPLETEXT, m.TextValue)
* is there text, and we are running in nf compatible mode?
CASE m.Options = 2 AND !EMPTY(NVL(m.TextValue, ""))
* store the downtree in the property
ADDPROPERTY(m.SimpleCopy, m.Property, m.DownTree)
* and add a special property for the text
ADDPROPERTY(m.SimpleCopy.&Property., XML_NFTEXT, m.TextValue)
* there is no text, we are running in nf compatible mode and building the attributes structure?
CASE m.Options = 2 AND m.Property == XML_SIMPLEATTR
* store the attribute downtree in the property, with the nf name
ADDPROPERTY(m.SimpleCopy, XML_NFATTR, m.DownTree)
OTHERWISE
* in other cases, store the downtree in the property
ADDPROPERTY(m.SimpleCopy, m.Property, m.DownTree)
* and add the text value to a character property, if set by option
IF m.Options = 1 AND !EMPTY(NVL(m.TextValue, ""))
ADDPROPERTY(m.SimpleCopy.&Property., XML_TEXT, NVL(m.TextValue, ""))
ENDIF
ENDCASE
ENDIF
ENDIF
ENDFOR
* return the tree segment produced at this level
RETURN m.SimpleCopy
ENDFUNC
* ReadXMLTree - process an XML object / property and its children
* XMLNodes - the XML point in the tree
* VFPObject - the VFP object that is being built
HIDDEN FUNCTION ReadXMLTree
LPARAMETERS XMLNodes AS MSXML2.IXMLDOMNodeList, VFPObject AS Object, IsDocument AS Boolean
SAFETHIS
* to traverse the XML document tree
LOCAL Node AS MSXML2.IXMLDOMNode
LOCAL NodeIndex AS Integer
LOCAL NameSpace AS String
LOCAL Prefix AS String
LOCAL Prefixes AS Collection
LOCAL NodeVFPRoot AS Empty
* to identify and traverse the attributes of an element
LOCAL Attributes AS MSXML2.IXMLDOMNodeList
LOCAL HasAttributes AS Boolean
LOCAL AttributesVFPRoot AS Empty
* to store the text (regular and CDATA) of an element/attribute
LOCAL TextNode AS MSXML2.IXMLDOMNode
LOCAL TextNodeIndex AS Integer
LOCAL Texts AS Collection
LOCAL Source AS Collection
* name, value and xml representation of a point in the tree
LOCAL NewName AS String
LOCAL NewValue AS Empty
LOCAL NewNode AS Boolean
* to manipulate arrays of elements
LOCAL CheckArray AS String
LOCAL NewArrayName AS String
LOCAL ToArray AS Boolean
LOCAL TempValue AS Empty
* read DTD?
LOCAL ReadDTDs AS Boolean
LOCAL DTDs AS Integer
* read processing instructions?
LOCAL ReadProcessingInstructions AS Boolean
LOCAL ProcessingInstructions AS Integer
* read comments?
LOCAL ReadComments AS Boolean
LOCAL Comments AS Integer
* read orphan texts?
LOCAL ReadOrphanTexts AS Boolean
LOCAL OrphanTexts AS Integer
m.NodeIndex = 0
m.ReadDTDs = m.IsDocument AND This._Op_DTD
m.ReadOrphanTexts = m.IsDocument AND This._Op_WhiteSpace
m.ReadProcessingInstructions = This._Op_ProcessingInstruction
m.ReadComments = This._Op_Comment
STORE 0 TO m.ProcessingInstructions, m.Comments, m.OrphanTexts, m.DTDs
* traverse every node in the list
FOR EACH m.Node IN m.XMLNodes
* mark its relative position
m.NodeIndex = m.NodeIndex + 1
WITH m.Node
* process a node
DO CASE
CASE .nodeType = NODE_ELEMENT && found an element
* fetch all namespace prefixes
m.Prefixes = .NULL.
FOR EACH m.NameSpaceDeclaration IN m.Node.Attributes
* found a namespace declaration
IF m.NameSpaceDeclaration.namespaceURI == "http://www.w3.org/2000/xmlns/"
* prepare the collection of prefixes, if not done already
IF ISNULL(m.Prefixes)
m.Prefixes = CREATEOBJECT("Collection")
ENDIF
* if not prefixed, use a default prefix key for the entry
IF m.NameSpaceDeclaration.baseName == m.NameSpaceDeclaration.nodeName
m.Prefix = ":"
ELSE
m.Prefix = m.NameSpaceDeclaration.baseName
ENDIF
* add the (prefixed?) namespace to the collection
m.Prefixes.Add(m.NameSpaceDeclaration.text, m.Prefix)
ENDIF
ENDFOR
* check if there are any attributes that are not namespaces
m.Attributes = .selectNodes("@*[namespace-uri(.) != 'http://www.w3.org/2000/xmlns/']")
m.HasAttributes = m.Attributes.length > 0
m.TextNodeIndex = 0
m.Texts = .NULL.
m.Source = .NULL.
* traverse all children, looking for text nodes
FOR EACH m.TextNode IN .childNodes
m.TextNodeIndex = m.TextNodeIndex + 1
* if a text or CDATA child was found
IF INLIST(m.TextNode.nodeType, NODE_TEXT, NODE_CDATA)
* insert it in the texts collection, with its position marked
IF ISNULL(m.Texts)
m.Texts = CREATEOBJECT("Collection")
m.Source = CREATEOBJECT("Collection")
ENDIF
m.Texts.Add(m.TextNode.text, IIF(m.TextNode.nodeType = NODE_TEXT, XML_ISTEXT, XML_ISCDATA) + LTRIM(STR(m.TextNodeIndex)))
m.Source.Add(m.TextNode.xml, IIF(m.TextNode.nodeType = NODE_TEXT, XML_ISTEXT, XML_ISCDATA) + LTRIM(STR(m.TextNodeIndex)))
ENDIF
ENDFOR
* fetch the namespace for the current node
m.NameSpace = .namespaceURI
CASE .nodeType = NODE_ATTRIBUTE && found an attribute
* attributes do not have attributes
m.HasAttributes = .F.
* just text, that is added to its collection
m.Texts = CREATEOBJECT("Collection")
m.Texts.Add(.text, XML_ISTEXT + "1")
m.Source = CREATEOBJECT("Collection")
m.Source.Add(.xml, XML_ISTEXT + "1")
* attributes may have a namespace
m.NameSpace = EVL(.namespaceURI, .NULL.)
CASE .nodeType = NODE_DTD AND m.ReadDTDs
m.DTDs = This._newDTD(m.VFPObject, .parentNode.doctype.xml, m.DTDs, m.NodeIndex)
LOOP
CASE .nodeType = NODE_PROCINSTRUCTION AND m.ReadProcessingInstructions
m.ProcessingInstructions = This._newProcessingInstruction(m.VFPObject, .nodeName, .nodeValue, m.ProcessingInstructions, m.NodeIndex)
LOOP
CASE .nodeType = NODE_COMMENT AND m.ReadComments
m.Comments = This._newComment(m.VFPObject, .nodeValue, m.Comments, m.NodeIndex)
LOOP
* orphan text nodes are located at the document level
CASE .nodeType = NODE_TEXT AND m.ReadOrphanTexts
m.OrphanTexts = This._newOrphanText(m.VFPObject, .nodeValue, .xml, m.OrphanTexts, m.NodeIndex)
LOOP
OTHERWISE && for all other cases ignore the XML source
LOOP
ENDCASE
* try to treat the node as a single property
m.ToArray = .F.
* get an allowed VFP name, corresponding to the XML name
This.DomainNamer.SetOriginalName(.baseName)
This.DomainNamer.SetProperty("VFPNamer", "SafePropertyName", .T.)
This.DomainNamer.SetProperty("VFPNamer", "SafeArrayPropertyName", .F.)
m.NewName = This.DomainNamer.GetName("VFPNamer")
* is it a new node?
m.NewNode = TYPE("m.VFPObject." + m.NewName) == "U"
* this is the information related to a single node:
* - the original names
* - the text section(s) of the node
* - the namespace
* - the prefixes
* - and its position, in the tree
m.NewValue = This._newVFPNode(.nodeName, m.Texts, m.Source, m.NameSpace, m.Prefixes, m.NodeIndex)
* an element or attribute with this name did not exist
IF m.NewNode
* check if there are siblings (elements or attributes) with the same normalized name,
* which will generate an array
m.CheckArray = "../" + IIF(.nodeType = 2,"@", "") + "*" + ;
"[translate(local-name(), " + ;
"'-abcdefghijklmnopqrstuvwxyzàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'," + ;
"'_ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ') = '" + ;
UPPER(m.NewName) + "']"
* if there are none,
IF .specified AND .selectNodes(m.CheckArray).length <= 1
* add a regular property to this level of the current object
ADDPROPERTY(m.VFPObject, m.NewName, m.NewValue)
* that will be used as a root for further processing
m.NodeVFPRoot = EVALUATE("m.VFPObject." + m.NewName)
ELSE
* get a name, again, now safe for array names
This.DomainNamer.SetProperty("VFPNamer", "SafePropertyName", .F.)
This.DomainNamer.SetProperty("VFPNamer", "SafePropertyArrayName", .T.)
m.NewName = This.DomainNamer.GetName("VFPNamer")
* if the name was not already in use, use it now for the property
IF TYPE("m.VFPObject." + m.NewName) == "U"
ADDPROPERTY(m.VFPObject, m.NewName + "[1]")
* set a counter
ADDPROPERTY(m.NewValue, XML_COUNT, 1)
* and the value
m.VFPObject.&NewName.[1] = m.NewValue
* use it as a root for further processing
m.NodeVFPRoot = EVALUATE("m.VFPObject." + m.NewName + "[1]")
ELSE
* similar node names were found at the same level, so the property will be an array already set
m.ToArray = .T.
ENDIF
ENDIF
ELSE
* if a property already exists, then it must be assigned to an array
m.ToArray = .T.
* a few cases must require a change from a regular property to an array property,
* when the translated VFP name can not be trapped by the selectNodes query
* (for instance, when the XML name clashes with a VFP reserved word)
IF TYPE("m.VFPObject." + m.NewName, 1) != "A"
* get a name, again, now safe for array names
This.DomainNamer.SetProperty("VFPNamer", "SafePropertyName", .F.)
This.DomainNamer.SetProperty("VFPNamer", "SafePropertyArrayName", .T.)
m.NewArrayName = This.DomainNamer.GetName("VFPNamer")
* if an array has not already been set with this adjusted name
IF TYPE("m.VFPObject." + m.NewArrayName, 1) != "A"
* hold the value of the regular property
m.TempValue = EVALUATE("m.VFPObject." + m.NewName)
* remove it
REMOVEPROPERTY(m.VFPObject, m.NewName)
* set the name to the array name (they may be different)
m.NewName = m.NewArrayName
* insert it back again as an array
ADDPROPERTY(m.VFPObject, m.NewName + "[1]")
* set a counter
ADDPROPERTY(m.TempValue, XML_COUNT, 1)
* and restore the value
m.VFPObject.&NewName.[1] = m.TempValue
ENDIF
ENDIF
ENDIF
* the property is an array element
IF m.ToArray
* set the counter for the array
m.VFPObject.&NewName.[1].xmlcount = m.VFPObject.&NewName.[1].xmlcount + 1
* create a new element at the end of the array
m.NewName = m.NewName + "[" + LTRIM(STR(ALEN(m.VFPObject.&NewName.) + 1)) + "]"
DIMENSION m.VFPObject.&NewName.
* set its value
m.VFPObject.&NewName. = m.NewValue
* and proceed, later on, to its subtree
m.NodeVFPRoot = EVALUATE("m.VFPObject." + m.NewName)
ENDIF
* if the current node has attributes
IF m.HasAttributes
* add to the VFP object
ADDPROPERTY(m.NodeVFPRoot, XML_ATTRIBUTE, CREATEOBJECT("Empty"))