-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparsexml.rex
1788 lines (1655 loc) · 72.1 KB
/
parsexml.rex
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
/*REXX 2.0.0
Copyright (c) 2009-2020, Andrew J. Armstrong
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**********************************************************************
** **
** ALL CODE BELOW THIS POINT BELONGS TO THE XML PARSER. YOU MUST **
** APPEND IT TO ANY REXX SOURCE FILE THAT REQUIRES AN XML PARSING **
** CAPABILITY. SINCE REXX HAS NO 'LIBRARY' FUNCTIONALITY, A WAY TO **
** AVOID HAVING DIFFERENT VERSIONS OF THE PARSER IN EACH OF YOUR **
** REXX PROCS IS TO DYNAMICALLY APPEND A CENTRAL VERSION TO EACH OF **
** YOUR REXX PROCS BEFORE EXECUTION. **
** **
** THE EXACT PROCEDURE TO FOLLOW DEPENDS ON YOUR PLATFORM, BUT... **
** TO HELP YOU DO THIS, I HAVE INCLUDED A REXX PRE-PROCESSOR CALLED **
** REXXPP THAT CAN BE USED TO SEARCH FOR 'INCLUDE' DIRECTIVES AND **
** REPLACE THEM WITH THE SPECIFIED FILE CONTENTS. IT HAS BEEN TESTED **
** ON TSO, AND ON WIN32 USING REGINA REXX VERSION 3.3. **
** **
**********************************************************************/
/*REXX*****************************************************************
** **
** NAME - PARSEXML **
** **
** FUNCTION - A Rexx XML parser. It is non-validating, so DTDs and **
** XML schemas are ignored. Ok, DTD entities are processed**
** but that's all. **
** **
** USAGE - 1. Initialize the parser by: **
** **
** call initParser [options...] **
** **
** 2. Parse the XML file to build an in-memory model **
** **
** returncode = parseFile('filename') **
** ...or... **
** returncode = parseString('xml in a string') **
** **
** 3. Navigate the in-memory model with the DOM API. For **
** example: **
** **
** say 'The document element is called', **
** getName(getDocumentElement()) **
** say 'Children of the document element are:' **
** node = getFirstChild(getDocumentElement()) **
** do while node <> '' **
** if isElementNode(node) **
** then say 'Element node:' getName(node) **
** else say ' Text node:' getText(node) **
** node = getNextSibling(node) **
** end **
** **
** 4. Optionally, destroy the in-memory model: **
** **
** call destroyParser **
** **
** INPUT - An XML file containing: **
** 1. An optional XML prolog: **
** - 0 or 1 XML declaration: **
** <?xml version="1.0" encoding="..." ...?> **
** - 0 or more comments, PIs, and whitespace: **
** <!-- a comment --> **
** <?target string?> **
** - 0 or 1 document type declaration. Formats: **
** <!DOCTYPE root SYSTEM "sysid"> **
** <!DOCTYPE root PUBLIC "pubid" SYSTEM "sysid"> **
** <!DOCTYPE root [internal dtd]> **
** 2. An XML body: **
** - 1 Document element containing 0 or more child **
** elements. For example: **
** <doc attr1="value1" attr2="value2"...> **
** Text of doc element **
** <child1 attr1="value1"> **
** Text of child1 element **
** </child1> **
** More text of doc element **
** <!-- an empty child element follows --> **
** <child2/> **
** Even more text of doc element **
** </doc> **
** - Elements may contain: **
** Unparsed character data: **
** <![CDATA[...unparsed data...]]> **
** Entity references: **
** &name; **
** Character references: **
** &#nnnnn; **
** &#xXXXX; **
** 3. An XML epilog (which is ignored): **
** - 0 or more comments, PIs, and whitespace. **
** **
** API - The basic setup/teardown API calls are: **
** **
** initParser [options] **
** Initialises the parser's global variables and **
** remembers any runtime options you specify. The **
** options recognized are: **
** NOBLANKS - Suppress whitespace-only nodes **
** DEBUG - Display some debugging info **
** DUMP - Display the parse tree **
** **
** parseFile(filename) **
** Parses the XML data in the specified filename and **
** builds an in-memory model that can be accessed via **
** the DOM API (see below). **
** **
** parseString(text) **
** Parses the XML data in the specified string. **
** **
** destroyParser **
** Destroys the in-memory model and miscellaneous **
** global variables. **
** **
** - In addition, the following utility API calls can be **
** used: **
** **
** removeWhitespace(text) **
** Returns the supplied text string but with all **
** whitespace characters removed, multiple spaces **
** replaced with single spaces, and leading and **
** trailing spaces removed. **
** **
** removeQuotes(text) **
** Returns the supplied text string but with any **
** enclosing apostrophes or double-quotes removed. **
** **
** escapeText(text) **
** Returns the supplied text string but with special **
** characters encoded (for example, '<' becomes <) **
** **
** toString(node) **
** Walks the document tree (beginning at the specified**
** node) and returns a string in XML format. **
** **
** DOM API - The DOM (ok, DOM-like) calls that you can use are **
** listed below: **
** **
** Document query/navigation API calls **
** ----------------------------------- **
** **
** getRoot() **
** Returns the node number of the root node. This **
** can be used in calls requiring a 'node' argument. **
** In this implementation, getDocumentElement() and **
** getRoot() are (incorrectly) synonymous - this may **
** change, so you should use getDocumentElement() **
** in preference to getRoot(). **
** **
** getDocumentElement() **
** Returns the node number of the document element. **
** The document element is the topmost element node. **
** You should use this in preference to getRoot() **
** (see above). **
** **
** getName(node) **
** Returns the name of the specified node. **
** **
** getNodeValue(node) **
** getText(node) **
** Returns the text content of an unnamed node. A **
** node without a name can only contain text. It **
** cannot have attributes or children. **
** **
** getAttributeCount(node) **
** Returns the number of attributes present on the **
** specified node. **
** **
** getAttributeMap(node) **
** Builds a map of the attributes of the specified **
** node. The map can be accessed via the following **
** variables: **
** g.0ATTRIBUTE.0 = The number of attributes mapped.**
** g.0ATTRIBUTE.n = The name of attribute 'n' (in **
** order of appearance). n > 0. **
** g.0ATTRIBUTE.name = The value of the attribute **
** called 'name'. **
** **
** getAttributeName(node,n) **
** Returns the name of the nth attribute of the **
** specified node (1 is first, 2 is second, etc). **
** **
** getAttributeNames(node) **
** Returns a space-delimited list of the names of the **
** attributes of the specified node. **
** **
** getAttribute(node,name) **
** Returns the value of the attribute called 'name' of**
** the specified node. **
** **
** getAttribute(node,n) **
** Returns the value of the nth attribute of the **
** specified node (1 is first, 2 is second, etc). **
** **
** setAttribute(node,name,value) **
** Updates the value of the attribute called 'name' **
** of the specified node. If no attribute exists with **
** that name, then one is created. **
** **
** setAttributes(node,name1,value1,name2,value2,...) **
** Updates the attributes of the specified node. Zero **
** or more name/value pairs are be specified as the **
** arguments. **
** **
** hasAttribute(node,name) **
** Returns 1 if the specified node has an attribute **
** with the specified name, else 0. **
** **
** getParentNode(node) **
** getParent(node) **
** Returns the node number of the specified node's **
** parent. If the node number returned is 0, then the **
** specified node is the root node. **
** All nodes have a parent (except the root node). **
** **
** getFirstChild(node) **
** Returns the node number of the specified node's **
** first child node. **
** **
** getLastChild(node) **
** Returns the node number of the specified node's **
** last child node. **
** **
** getChildNodes(node) **
** getChildren(node) **
** Returns a space-delimited list of node numbers of **
** the children of the specified node. You can use **
** this list to step through the children as follows: **
** children = getChildren(node) **
** say 'Node' node 'has' words(children) 'children' **
** do i = 1 to words(children) **
** child = word(children,i) **
** say 'Node' child 'is' getName(child) **
** end **
** **
** getChildrenByName(node,name) **
** Returns a space-delimited list of node numbers of **
** the immediate children of the specified node which **
** are called 'name'. Names are case-sensitive. **
** **
** getElementsByTagName(node,name) **
** Returns a space-delimited list of node numbers of **
** the descendants of the specified node which are **
** called 'name'. Names are case-sensitive. **
** **
** getNextSibling(node) **
** Returns the node number of the specified node's **
** next sibling node. That is, the next node sharing **
** the same parent. **
** **
** getPreviousSibling(node) **
** Returns the node number of the specified node's **
** previous sibling node. That is, the previous node **
** sharing the same parent. **
** **
** getProcessingInstruction(name) **
** Returns the value of the PI with the specified **
** target name. **
** **
** getProcessingInstructionList() **
** Returns a space-delimited list of the names of all **
** PI target names. **
** **
** getNodeType(node) **
** Returns a number representing the specified node's **
** type. The possible values can be compared to the **
** following global variables: **
** g.0ELEMENT_NODE = 1 **
** g.0ATTRIBUTE_NODE = 2 **
** g.0TEXT_NODE = 3 **
** g.0CDATA_SECTION_NODE = 4 **
** g.0ENTITY_REFERENCE_NODE = 5 **
** g.0ENTITY_NODE = 6 **
** g.0PROCESSING_INSTRUCTION_NODE = 7 **
** g.0COMMENT_NODE = 8 **
** g.0DOCUMENT_NODE = 9 **
** g.0DOCUMENT_TYPE_NODE = 10 **
** g.0DOCUMENT_FRAGMENT_NODE = 11 **
** g.0NOTATION_NODE = 12 **
** Note: as this exposes internal implementation **
** details, it is best not to use this routine. **
** Consider using isTextNode() etc instead. **
** **
** isCDATA(node) **
** Returns 1 if the specified node is an unparsed **
** character data (CDATA) node, else 0. CDATA nodes **
** are used to contain content that you do not want **
** to be treated as XML data. For example, HTML data. **
** **
** isElementNode(node) **
** Returns 1 if the specified node is an element node,**
** else 0. **
** **
** isTextNode(node) **
** Returns 1 if the specified node is a text node, **
** else 0. **
** **
** isCommentNode(node) **
** Returns 1 if the specified node is a comment node, **
** else 0. Note: when a document is parsed, comment **
** nodes are ignored. This routine returns 1 iff a **
** comment node has been inserted into the in-memory **
** document tree by using createComment(). **
** **
** hasChildren(node) **
** Returns 1 if the specified node has one or more **
** child nodes, else 0. **
** **
** getDocType(doctype) **
** Gets the text of the <!DOCTYPE> prolog node. **
** **
** Document creation/mutation API calls **
** ------------------------------------ **
** **
** createDocument(name) **
** Returns the node number of a new document node **
** with the specified name. **
** **
** createDocumentFragment(name) **
** Returns the node number of a new document fragment **
** node with the specified name. **
** **
** createElement(name) **
** Returns the node number of a new empty element **
** node with the specified name. An element node can **
** have child nodes. **
** **
** createTextNode(data) **
** Returns the node number of a new text node. A text **
** node can *not* have child nodes. **
** **
** createCDATASection(data) **
** Returns the node number of a new Character Data **
** (CDATA) node. A CDATA node can *not* have child **
** nodes. CDATA nodes are used to contain content **
** that you do not want to be treated as XML data. **
** For example, HTML data. **
** **
** createComment(data) **
** Returns the node number of a new commend node. **
** A command node can *not* have child nodes. **
** **
** appendChild(node,parent) **
** Appends the specified node to the end of the list **
** of children of the specified parent node. **
** **
** insertBefore(node,refnode) **
** Inserts node 'node' before the reference node **
** 'refnode'. **
** **
** removeChild(node) **
** Removes the specified node from its parent and **
** returns its node number. The removed child is now **
** an orphan. **
** **
** replaceChild(newnode,oldnode) **
** Replaces the old child 'oldnode' with the new **
** child 'newnode' and returns the old child's node **
** number. The old child is now an orphan. **
** **
** setAttribute(node,attrname,attrvalue) **
** Adds or replaces the attribute called 'attrname' **
** on the specified node. **
** **
** removeAttribute(node,attrname) **
** Removes the attribute called 'attrname' from the **
** specified node. **
** **
** setDocType(doctype) **
** Sets the text of the <!DOCTYPE> prolog node. **
** **
** cloneNode(node,[deep]) **
** Creates a copy (a clone) of the specified node **
** and returns its node number. If deep = 1 then **
** all descendants of the specified node are also **
** cloned, else only the specified node and its **
** attributes are cloned. **
** **
** NOTES - 1. This parser creates global variables and so its **
** operation may be severely jiggered if you update **
** any of them accidentally (or on purpose). The **
** variables you should avoid updating yourself are: **
** **
** g.0ATTRIBUTE.n **
** g.0ATTRIBUTE.name **
** g.0ATTRSOK **
** g.0DTD **
** g.0ENDOFDOC **
** g.0ENTITIES **
** g.0ENTITY.name **
** g.0FIRST.n **
** g.0LAST.n **
** g.0NAME.n **
** g.0NEXT.n **
** g.0NEXTID **
** g.0OPTION.name **
** g.0OPTIONS **
** g.0PARENT.n **
** g.0PI **
** g.0PI.name **
** g.0PREV.n **
** g.0PUBLIC **
** g.0ROOT **
** g.0STACK **
** g.0SYSTEM **
** g.0TEXT.n **
** g.0TYPE.n **
** g.0WHITESPACE **
** g.0XML **
** g.?XML **
** g.?XML.VERSION **
** g.?XML.ENCODING **
** g.?XML.STANDALONE **
** **
** 2. To reduce the incidence of name clashes, procedure **
** names that are not meant to be part of the public **
** API have been prefixed with '_'. **
** **
** **
** AUTHOR - Andrew J. Armstrong <androidarmstrong+sf@gmail.com> **
** **
** CONTRIBUTORS - **
** Alessandro Battilani **
** <alessandro.battilani@bancaintesa.it> **
** **
** **
** HISTORY - Date By Reason (most recent at the top pls) **
** -------- --------------------------------------------- **
** 20090822 AJA Changed from GPL to BSD license. **
** Ignore whitespace to fix parse error. **
** 20070325 AJA Whitespace defaults to '090a0d'x. **
** 20070323 AJA Added createDocumentFragment(). **
** Added isDocumentFragmentNode(). **
** Added isDocumentNode(). **
** 20060915 AJA Added cloneNode(). **
** Added deepClone(). **
** Changed removeChild() to return the **
** node number of the child instead of **
** clearing it. **
** Changed replaceChild() to return the **
** node number of the old child instead **
** of clearing it. **
** 20060913 AJA Fixed bug in _resolveEntities(). **
** 20060808 AB Added support for reading from a DD **
** name when running IRXJCL on MVS. **
** This change was contributed by **
** Alessandro Battilani from Banca **
** Intesa, Italy. **
** 20060803 AJA Fixed loop in getAttributeMap(). **
** 20051025 AJA Now checks parentage before adding a **
** child node: **
** Fixed appendChild(id,parent) **
** Fixed insertBefore(id,ref) **
** 20051014 AJA Added alias routine names to more **
** closely match the DOM specification. **
** Specifically: **
** Added getNodeName() **
** Added getNodeValue() **
** Added getParentNode() **
** Added getChildNodes() **
** Added hasChildNodes() **
** Added getElementsByTagName() . **
** 20050919 AJA Added setAttributes helper routine. **
** 20050914 AJA Added createComment and isComment. **
** 20050913 AJA Added get/setDocType routines. **
** 20050907 AJA Added _setDefaultEntities routine. **
** 20050601 AJA Added '250d'x to whitespace for TSO. **
** 20050514 AJA Removed getAttributes API call and **
** reworked attribute processing. **
** Added toString API call. **
** 20040706 AJA Added creation/modification support. **
** 20031216 AJA Bugfix: _parseElement with no attrs **
** causes crash. **
** 20031031 AJA Correctly parse '/' in attributes. **
** Fixed entity resolution. **
** 20030912 AJA Bugfix: Initialize sXmlData first. **
** Bugfix: Correctly parse a naked '>' **
** present in an attribute value. **
** Enhancement: DUMP option now displays **
** first part of each text node. **
** 20030901 AJA Initial version. **
** **
**********************************************************************/
parse source . . sSourceFile .
parse value sourceline(1) with . sVersion
say 'Rexx XML Parser' sVersion
say 'You cannot invoke this rexx by itself!'
say
say 'This rexx is a collection of subroutines to be called'
say 'from your own rexx procedures. You should either:'
say ' - Append this procedure to your own rexx procedure,'
say ' or,'
say ' - Append the following line to your rexx:'
say ' /* INCLUDE' sSourceFile '*/'
say ' ...and run the rexx preprocessor:'
say ' rexxpp myrexx myrexxpp'
say ' This will create myrexxpp by appending this file to myrexx'
exit
/*-------------------------------------------------------------------*
* Set up global variables for the parser
*-------------------------------------------------------------------*/
initParser: procedure expose g.
parse arg sOptions
g. = '' /* Note: stuffs up caller who may have set g. variables */
g.0OPTIONS = translate(sOptions)
sOptions = 'DEBUG DUMP NOBLANKS'
do i = 1 to words(sOptions)
sOption = word(sOptions,i)
g.0OPTION.sOption = wordpos(sOption,g.0OPTIONS) > 0
end
parse source sSystem sInvocation sSourceFile
select
when sSystem = 'WIN32' then g.0WHITESPACE = '090a0d'x
when sSystem = 'TSO' then g.0WHITESPACE = '05250d'x
otherwise g.0WHITESPACE = '090a0d'x /*20070325*/
end
g.0LEADERS = '_:ABCDEFGHIJKLMNOPQRSTUVWXYZ' ||,
'abcdefghijklmnopqrstuvwxyz'
g.0OTHERS = g.0LEADERS'.-0123456789'
call _setDefaultEntities
/* Not all of the following node types are used... */
g.0ELEMENT_NODE = 1; g.0NODETYPE.1 = 'Element'
g.0ATTRIBUTE_NODE = 2; g.0NODETYPE.2 = 'Attribute'
g.0TEXT_NODE = 3; g.0NODETYPE.3 = 'Text'
g.0CDATA_SECTION_NODE = 4; g.0NODETYPE.4 = 'CDATA Section'
g.0ENTITY_REFERENCE_NODE = 5 /* NOT USED */
g.0ENTITY_NODE = 6 /* NOT USED */
g.0PROCESSING_INSTRUCTION_NODE = 7 /* NOT USED */
g.0COMMENT_NODE = 8; g.0NODETYPE.8 = 'Comment'
g.0DOCUMENT_NODE = 9; g.0NODETYPE.9 = 'Document'
g.0DOCUMENT_TYPE_NODE = 10 /* NOT USED */
g.0DOCUMENT_FRAGMENT_NODE = 11; g.0NODETYPE.11 = 'Document Fragment'
g.0NOTATION_NODE = 12 /* NOT USED */
g.0ENDOFDOC = 0
return
/*-------------------------------------------------------------------*
* Clean up parser
*-------------------------------------------------------------------*/
destroyParser: procedure expose g.
/* Note: it would be easy to just "drop g.", but this could
possibly stuff up the caller who may be using other
"g." variables...
todo: revisit this one (parser may have to 'own' g. names)
*/
drop g.?XML g.0ROOT g.0SYSTEM g.0PUBLIC g.0DTD
do i = 1 to words(g.0PI)
sName = word(g.0PI,i)
drop g.0PI.sName
end
drop g.0PI
do i = 1 to words(g.0ENTITIES)
sName = word(g.0ENTITIES,i)
drop g.0ENTITY.sName
end
drop g.0ENTITIES
call _setDefaultEntities
if datatype(g.0NEXTID,'WHOLE')
then do
do i = 1 to g.0NEXTID
drop g.0PARENT.i g.0FIRST.i g.0LAST.i g.0PREV.i,
g.0NEXT.i g.0NAME.i g.0TEXT.i
end
end
drop g.0NEXTID g.0STACK g.0ENDOFDOC
return
/*-------------------------------------------------------------------*
* Read a file into a string
*-------------------------------------------------------------------*/
parseFile: procedure expose g.
parse arg sFile
parse source sSystem sInvocation sSourceFile . . . sInitEnv .
sXmlData = ''
select
when sSystem = 'TSO' & sInitEnv = 'TSO' then do
/* sFile is a dataset name */
address TSO
junk = OUTTRAP('junk.') /* Trap and discard messages */
'ALLOCATE DD(INPUT) DSN('sFile')'
'EXECIO * DISKR INPUT (FINIS'
'FREE DD(INPUT)'
address
do queued()
parse pull sLine
sXmlData = sXmlData || sLine
end
junk = OUTTRAP('OFF')
end
when sSystem = 'TSO' & sInitEnv = 'MVS' then do
/* sFile is a DD name */
address MVS 'EXECIO * DISKR' sFile '(FINIS'
do queued()
parse pull sLine
sXmlData = sXmlData || sLine
end
end
otherwise do
sXmlData = charin(sFile,,chars(sFile))
end
end
return parseString(sXmlData)
/*-------------------------------------------------------------------*
* Parse a string containing XML
*-------------------------------------------------------------------*/
parseString: procedure expose g.
parse arg g.0XML
call _parseXmlDecl
do while pos('<',g.0XML) > 0
parse var g.0XML sLeft'<'sData
select
when left(sData,1) = '?' then call _parsePI sData
when left(sData,9) = '!DOCTYPE ' then call _parseDocType sData
when left(sData,3) = '!--' then call _parseComment sData
otherwise call _parseElement sData
end
end
return 0
/*-------------------------------------------------------------------*
* <?xml version="1.0" encoding="..." ...?>
*-------------------------------------------------------------------*/
_parseXmlDecl: procedure expose g.
if left(g.0XML,6) = '<?xml '
then do
parse var g.0XML '<?xml 'sXMLDecl'?>'g.0XML
g.?xml = space(sXMLDecl)
sTemp = _getNormalizedAttributes(g.?xml)
parse var sTemp 'version='g.?xml.version'ff'x
parse var sTemp 'encoding='g.?xml.encoding'ff'x
parse var sTemp 'standalone='g.?xml.standalone'ff'x
end
return
/*-------------------------------------------------------------------*
* <?target string?>
*-------------------------------------------------------------------*/
_parsePI: procedure expose g.
parse arg '?'sProcessingInstruction'?>'g.0XML
call _setProcessingInstruction sProcessingInstruction
return
/*-------------------------------------------------------------------*
* <!DOCTYPE root SYSTEM "sysid">
* <!DOCTYPE root SYSTEM "sysid" [internal dtd]>
* <!DOCTYPE root PUBLIC "pubid" "sysid">
* <!DOCTYPE root PUBLIC "pubid" "sysid" [internal dtd]>
* <!DOCTYPE root [internal dtd]>
*-------------------------------------------------------------------*/
_parseDocType: procedure expose g.
parse arg '!DOCTYPE' sDocType'>'
if g.0ROOT <> ''
then call _abort 'XML002E Multiple "<!DOCTYPE" declarations'
if pos('[',sDocType) > 0
then do
parse arg '!DOCTYPE' sDocType'['g.0DTD']>'g.0XML
parse var sDocType g.0ROOT sExternalId
if sExternalId <> '' then call _parseExternalId sExternalId
g.0DTD = strip(g.0DTD)
call _parseDTD g.0DTD
end
else do
parse arg '!DOCTYPE' g.0ROOT sExternalId'>'g.0XML
if sExternalId <> '' then call _parseExternalId sExternalId
end
g.0ROOT = strip(g.0ROOT)
return
/*-------------------------------------------------------------------*
* SYSTEM "sysid"
* PUBLIC "pubid" "sysid"
*-------------------------------------------------------------------*/
_parseExternalId: procedure expose g.
parse arg sExternalIdType .
select
when sExternalIdType = 'SYSTEM' then do
parse arg . g.0SYSTEM
g.0SYSTEM = removeQuotes(g.0SYSTEM)
end
when sExternalIdType = 'PUBLIC' then do
parse arg . g.0PUBLIC g.0SYSTEM
g.0PUBLIC = removeQuotes(g.0PUBLIC)
g.0SYSTEM = removeQuotes(g.0SYSTEM)
end
otherwise do
parse arg sExternalEntityDecl
call _abort 'XML003E Invalid external entity declaration:',
sExternalEntityDecl
end
end
return
/*-------------------------------------------------------------------*
* <!ENTITY name "value">
* <!ENTITY name SYSTEM "sysid">
* <!ENTITY name PUBLIC "pubid" "sysid">
* <!ENTITY % name pedef>
* <!ELEMENT elementname contentspec>
* <!ATTLIST elementname attrname attType DefaultDecl ...>
* <!NOTATION name notationdef>
*-------------------------------------------------------------------*/
_parseDTD: procedure expose g.
parse arg sDTD
do while pos('<!',sDTD) > 0
parse var sDTD '<!'sDecl sName sValue'>'sDTD
select
when sDecl = 'ENTITY' then do
parse var sValue sWord1 .
select
when sName = '%' then nop
when sWord1 = 'SYSTEM' then nop
when sWord1 = 'PUBLIC' then nop
otherwise do
sValue = _resolveEntities(removeQuotes(sValue))
call _setEntity sName,sValue
end
end
end
otherwise nop /* silently ignore other possibilities for now */
end
end
return
/*-------------------------------------------------------------------*
* <!-- comment -->
*-------------------------------------------------------------------*/
_parseComment: procedure expose g.
parse arg sComment'-->'g.0XML
/* silently ignore comments */
return
/*-------------------------------------------------------------------*
* <tag attr1="value1" attr2="value2" ...>...</tag>
* <tag attr1="value1" attr2="value2" .../>
*-------------------------------------------------------------------*/
_parseElement: procedure expose g.
parse arg sXML
if g.0ENDOFDOC
then call _abort 'XML004E Only one top level element is allowed.',
'Found:' subword(g.0XML,1,3)
call _startDocument
g.0XML = '<'sXML
do while pos('<',g.0XML) > 0 & \g.0ENDOFDOC
parse var g.0XML sLeft'<'sBetween'>'g.0XML
if length(sLeft) > 0
then call _characters sLeft
if g.0OPTION.DEBUG
then say g.0STACK sBetween
if left(sBetween,8) = '![CDATA['
then do
g.0XML = sBetween'>'g.0XML /* ..back it out! */
parse var g.0XML '![CDATA['sBetween']]>'g.0XML
call _characterData sBetween
end
else do
sBetween = removeWhiteSpace(sBetween) /*20090822*/
select
when left(sBetween,3) = '!--' then do /* <!-- comment --> */
if right(sBetween,2) <> '--'
then do /* backup a bit and look for end-of-comment */
g.0XML = sBetween'>'g.0XML
if pos('-->',g.0XML) = 0
then call _abort 'XML005E End of comment missing after:',
'<'g.0XML
parse var g.0XML sComment'-->'g.0XML
end
end
when left(sBetween,1) = '?' then do /* <?target string?> */
parse var sBetween '?'sProcessingInstruction'?'
call _setProcessingInstruction sProcessingInstruction
end
when left(sBetween,1) = '/' then do /* </tag> */
call _endElement substr(sBetween,2) /* tag */
end
when right(sBetween,1) = '/' /* <tag ...attrs.../> */
then do
parse var sBetween sTagName sAttrs
if length(sAttrs) > 0 /*20031216*/
then sAttrs = substr(sAttrs,1,length(sAttrs)-1) /*20031216*/
else parse var sTagName sTagName'/' /* <tag/> 20031216*/
sAttrs = _getNormalizedAttributes(sAttrs)
call _startElement sTagName sAttrs
call _endElement sTagName
end
otherwise do /* <tag ...attrs ...> ... </tag> */
parse var sBetween sTagName sAttrs
sAttrs = _getNormalizedAttributes(sAttrs)
if g.0ATTRSOK
then do
call _startElement sTagName sAttrs
end
else do /* back up a bit and look for the real end of tag */
g.0XML = '<'sBetween'>'g.0XML
if pos('>',g.0XML) = 0
then call _abort 'XML006E Missing end tag for:' sTagName
/* reparse on next cycle avoiding premature '>'...*/
end
end
end
end
end
call _endDocument
return
_startDocument: procedure expose g.
g.0NEXTID = 0
g.0STACK = 0
return
_startElement: procedure expose g.
parse arg sTagName sAttrs
id = _getNextId()
call _updateLinkage id
g.0NAME.id = sTagName
g.0TYPE.id = g.0ELEMENT_NODE
call _addAttributes id,sAttrs
cid = _pushElement(id)
return
_updateLinkage: procedure expose g.
parse arg id
parent = _peekElement()
g.0PARENT.id = parent
parentsLastChild = g.0LAST.parent
g.0NEXT.parentsLastChild = id
g.0PREV.id = parentsLastChild
g.0LAST.parent = id
if g.0FIRST.parent = ''
then g.0FIRST.parent = id
return
_characterData: procedure expose g.
parse arg sChars
id = _getNextId()
call _updateLinkage id
g.0TEXT.id = sChars
g.0TYPE.id = g.0CDATA_SECTION_NODE
return
_characters: procedure expose g.
parse arg sChars
sText = _resolveEntities(sChars)
if g.0OPTION.NOBLANKS & removeWhitespace(sText) = ''
then return
id = _getNextId()
call _updateLinkage id
g.0TEXT.id = sText
g.0TYPE.id = g.0TEXT_NODE
return
_endElement: procedure expose g.
parse arg sTagName
id = _popElement()
g.0ENDOFDOC = id = 1
if sTagName == g.0NAME.id
then nop
else call _abort,
'XML007E Expecting </'g.0NAME.id'> but found </'sTagName'>'
return
_endDocument: procedure expose g.
id = _peekElement()
if id <> 0
then call _abort 'XML008E End of document tag missing: 'id getName(id)
if g.0ROOT <> '' & g.0ROOT <> getName(getRoot())
then call _abort 'XML009E Root element name "'getName(getRoot())'"',
'does not match DTD root "'g.0ROOT'"'
if g.0OPTION.DUMP
then call _displayTree
return
_displayTree: procedure expose g.
say right('',4),
right('',4),
left('',12),
right('',6),
'--child--',
'-sibling-',
'attribute'
say right('id',4),
right('type',4),
left('name',12),
right('parent',6),
right('1st',4),
right('last',4),
right('prev',4),
right('next',4),
right('1st',4),
right('last',4)
do id = 1 to g.0NEXTID
if g.0PARENT.id <> '' | id = 1 /* skip orphans */
then do
select
when g.0TYPE.id = g.0CDATA_SECTION_NODE then sName = '#CDATA'
when g.0TYPE.id = g.0TEXT_NODE then sName = '#TEXT'
otherwise sName = g.0NAME.id
end
say right(id,4),
right(g.0TYPE.id,4),
left(sName,12),
right(g.0PARENT.id,6),
right(g.0FIRST.id,4),
right(g.0LAST.id,4),
right(g.0PREV.id,4),
right(g.0NEXT.id,4),
right(g.0FIRSTATTR.id,4),
right(g.0LASTATTR.id,4),
left(removeWhitespace(g.0TEXT.id),19)
end
end
return
_pushElement: procedure expose g.
parse arg id
g.0STACK = g.0STACK + 1
nStackDepth = g.0STACK
g.0STACK.nStackDepth = id
return id
_popElement: procedure expose g.
n = g.0STACK
if n = 0
then id = 0
else do
id = g.0STACK.n
g.0STACK = g.0STACK - 1
end
return id
_peekElement: procedure expose g.
n = g.0STACK
if n = 0
then id = 0
else id = g.0STACK.n
return id
_getNextId: procedure expose g.
g.0NEXTID = g.0NEXTID + 1
return g.0NEXTID
_addAttributes: procedure expose g.
parse arg id,sAttrs
do while pos('ff'x,sAttrs) > 0
parse var sAttrs sAttrName'='sAttrValue 'ff'x sAttrs