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
  
1001
  
1002
  
1003
  
1004
  
1005
  
1006
  
1007
  
1008
  
1009
  
1010
  
1011
  
1012
  
1013
  
1014
  
1015
  
1016
  
1017
  
1018
  
1019
  
1020
  
1021
  
1022
  
1023
  
1024
  
1025
  
1026
  
1027
  
1028
  
1029
  
1030
  
1031
  
1032
  
1033
  
1034
  
1035
  
1036
  
1037
  
1038
  
1039
  
1040
  
1041
  
1042
  
1043
  
1044
  
1045
  
1046
  
1047
  
1048
  
1049
  
1050
  
1051
  
1052
  
1053
  
1054
  
1055
  
1056
  
1057
  
1058
  
1059
  
1060
  
1061
  
1062
  
1063
  
1064
  
1065
  
1066
  
1067
  
1068
  
1069
  
1070
  
1071
  
1072
  
1073
  
1074
  
1075
  
1076
  
1077
  
1078
  
1079
  
1080
  
1081
  
1082
  
1083
  
1084
  
1085
  
1086
  
1087
  
1088
  
1089
  
1090
  
1091
  
1092
  
1093
  
1094
  
1095
  
1096
  
1097
  
1098
  
1099
  
1100
  
1101
  
1102
  
1103
  
1104
  
1105
  
1106
  
1107
  
1108
  
1109
  
1110
  
1111
  
1112
  
1113
  
1114
  
1115
  
1116
  
1117
  
1118
  
1119
  
1120
  
1121
  
1122
  
1123
  
1124
  
1125
  
1126
  
1127
  
1128
  
1129
  
1130
  
1131
  
1132
  
1133
  
1134
  
1135
  
1136
  
1137
  
1138
  
1139
  
1140
  
1141
  
1142
  
1143
  
1144
  
1145
  
1146
  
1147
  
1148
  
1149
  
1150
  
1151
  
1152
  
1153
  
1154
  
1155
  
1156
  
1157
  
1158
  
1159
  
1160
  
1161
  
1162
  
1163
  
1164
  
1165
  
1166
  
1167
  
1168
  
1169
  
1170
  
1171
  
1172
  
1173
  
1174
  
1175
  
1176
  
1177
  
1178
  
1179
  
1180
  
1181
  
1182
  
1183
  
1184
  
1185
  
1186
  
1187
  
1188
  
1189
  
1190
  
1191
  
1192
  
1193
  
1194
  
1195
  
1196
  
1197
  
1198
  
1199
  
1200
  
1201
  
1202
  
1203
  
1204
  
1205
  
1206
  
1207
  
1208
  
1209
  
1210
  
1211
  
1212
  
1213
  
1214
  
1215
  
1216
  
1217
  
1218
  
1219
  
1220
  
1221
  
1222
  
1223
  
1224
  
1225
  
1226
  
1227
  
1228
  
1229
  
1230
  
1231
  
1232
  
1233
  
1234
  
1235
  
1236
  
1237
  
1238
  
1239
  
1240
  
1241
  
1242
  
1243
  
1244
  
1245
  
1246
  
1247
  
1248
  
1249
  
1250
  
1251
  
1252
  
1253
  
1254
  
1255
  
1256
  
1257
  
1258
  
1259
  
1260
  
1261
  
1262
  
1263
  
1264
  
1265
  
1266
  
1267
  
1268
  
1269
  
1270
  
1271
  
1272
  
1273
  
1274
  
1275
  
1276
  
1277
  
1278
  
1279
  
1280
  
1281
  
1282
  
1283
  
1284
  
1285
  
1286
  
1287
  
1288
  
1289
  
1290
  
1291
  
1292
  
1293
  
1294
  
1295
  
1296
  
1297
  
1298
  
1299
  
1300
  
1301
  
1302
  
1303
  
1304
  
1305
  
1306
  
1307
  
1308
  
1309
  
1310
  
1311
  
1312
  
1313
  
1314
  
1315
  
1316
  
1317
  
1318
  
1319
  
1320
  
1321
  
1322
  
1323
  
1324
  
1325
  
1326
  
1327
  
1328
  
1329
  
1330
  
1331
  
1332
  
1333
  
1334
  
1335
  
1336
  
1337
  
1338
  
1339
  
1340
  
1341
  
1342
  
1343
  
1344
  
1345
  
1346
  
1347
  
1348
  
1349
  
1350
  
1351
  
1352
  
1353
  
1354
  
1355
  
1356
  
1357
  
1358
  
1359
  
1360
  
1361
  
1362
  
1363
  
1364
  
1365
  
1366
  
1367
  
1368
  
1369
  
1370
  
1371
  
1372
  
1373
  
1374
  
1375
  
1376
  
1377
  
1378
  
1379
  
1380
  
1381
  
1382
  
1383
  
1384
  
1385
  
1386
  
1387
  
1388
  
1389
  
1390
  
1391
  
1392
  
1393
  
1394
  
1395
  
1396
  
1397
  
1398
  
1399
  
1400
  
1401
  
1402
  
1403
  
1404
  
1405
  
1406
  
1407
  
1408
  
1409
  
1410
  
1411
  
1412
  
1413
  
1414
  
1415
  
1416
  
1417
  
1418
  
1419
  
1420
  
1421
  
1422
  
1423
  
1424
  
1425
  
1426
  
1427
  
1428
  
1429
  
1430
  
1431
  
1432
  
1433
  
1434
  
1435
  
1436
  
1437
  
1438
  
1439
  
1440
  
1441
  
1442
  
1443
  
1444
  
1445
  
1446
  
1447
  
1448
  
1449
  
1450
  
1451
  
1452
  
1453
  
1454
  
1455
  
1456
  
1457
  
1458
  
1459
  
1460
  
1461
  
1462
  
1463
  
1464
  
1465
  
1466
  
1467
  
1468
  
1469
  
1470
  
1471
  
1472
  
1473
  
1474
  
1475
  
1476
  
1477
  
1478
  
1479
  
1480
  
1481
  
1482
  
1483
  
1484
  
1485
  
1486
  
1487
  
1488
  
1489
  
1490
  
1491
  
1492
  
1493
  
1494
  
1495
  
1496
  
1497
  
1498
  
1499
  
1500
  
1501
  
1502
  
1503
  
1504
  
1505
  
1506
  
1507
  
1508
  
1509
  
1510
  
1511
  
1512
  
1513
  
1514
  
1515
  
1516
  
1517
  
1518
  
1519
  
1520
  
1521
  
1522
  
1523
  
1524
  
1525
  
1526
  
1527
  
1528
  
1529
  
1530
  
1531
  
1532
  
1533
  
1534
  
1535
  
1536
  
1537
  
1538
  
1539
  
1540
  
1541
  
1542
  
1543
  
1544
  
1545
  
1546
  
1547
  
1548
  
1549
  
1550
  
1551
  
1552
  
1553
  
1554
  
1555
  
1556
  
1557
  
1558
  
1559
  
1560
  
1561
  
1562
  
1563
  
1564
  
1565
  
1566
  
1567
  
1568
  
1569
  
1570
  
1571
  
1572
  
1573
  
1574
  
1575
  
1576
  
1577
  
1578
  
1579
  
1580
  
1581
  
1582
  
1583
  
1584
  
1585
  
1586
  
1587
  
1588
  
1589
  
1590
  
1591
  
1592
  
1593
  
1594
  
1595
  
1596
  
1597
  
1598
  
1599
  
1600
  
1601
  
1602
  
1603
  
1604
  
1605
  
1606
  
1607
  
1608
  
1609
  
1610
  
1611
  
1612
  
1613
  
1614
  
1615
  
1616
  
1617
  
1618
  
1619
  
1620
  
1621
  
1622
  
1623
  
1624
  
1625
  
1626
  
1627
  
1628
  
1629
  
1630
  
1631
  
1632
  
1633
  
1634
  
1635
  
1636
  
1637
  
1638
  
1639
  
1640
  
1641
  
1642
  
1643
  
1644
  
1645
  
1646
  
1647
  
1648
  
1649
  
1650
  
1651
  
1652
  
1653
  
1654
  
1655
  
1656
  
1657
  
1658
  
1659
  
1660
  
1661
  
1662
  
1663
  
1664
  
1665
  
1666
  
1667
  
1668
  
1669
  
1670
  
1671
  
1672
  
1673
  
1674
  
1675
  
1676
  
1677
  
1678
  
1679
  
1680
  
1681
  
1682
  
1683
  
1684
  
1685
  
1686
  
1687
  
1688
  
1689
  
1690
  
1691
  
1692
  
1693
  
1694
  
1695
  
1696
  
1697
  
1698
  
1699
  
1700
  
1701
  
1702
  
1703
  
1704
  
1705
  
1706
  
1707
  
1708
  
1709
  
1710
  
1711
  
1712
  
1713
  
1714
  
1715
  
1716
  
1717
  
1718
  
1719
  
1720
  
1721
  
1722
  
1723
  
1724
  
1725
  
1726
  
1727
  
1728
  
1729
  
1730
  
1731
  
1732
  
1733
  
1734
  
1735
  
1736
  
1737
  
1738
  
1739
  
1740
  
1741
  
1742
  
1743
  
1744
  
1745
  
1746
  
1747
  
1748
  
1749
  
1750
  
1751
  
1752
  
1753
  
1754
  
1755
  
1756
  
1757
  
1758
  
1759
  
1760
  
1761
  
1762
  
1763
  
1764
  
1765
  
1766
  
1767
  
1768
  
1769
  
1770
  
1771
  
1772
  
1773
  
1774
  
1775
  
1776
  
1777
  
1778
  
1779
  
1780
  
1781
  
1782
  
1783
  
1784
  
1785
  
1786
  
1787
  
1788
  
1789
  
1790
  
1791
  
1792
  
1793
  
1794
  
1795
  
1796
  
1797
  
1798
  
1799
  
1800
  
1801
  
1802
  
1803
  
1804
  
1805
  
1806
  
1807
  
1808
  
1809
  
1810
  
1811
  
1812
  
1813
  
1814
  
1815
  
1816
  
1817
  
1818
  
1819
  
1820
  
1821
  
1822
  
1823
  
1824
  
1825
  
1826
  
1827
  
1828
  
1829
  
1830
  
1831
  
1832
  
1833
  
1834
  
1835
  
1836
  
1837
  
1838
  
1839
  
1840
  
1841
  
1842
  
1843
  
1844
  
1845
  
1846
  
1847
  
1848
  
1849
  
1850
  
1851
  
1852
  
1853
  
1854
  
1855
  
1856
  
1857
  
1858
  
1859
  
1860
  
1861
  
1862
  
1863
  
1864
  
1865
  
1866
  
1867
  
1868
  
1869
  
1870
  
1871
  
1872
  
1873
  
1874
  
1875
  
1876
  
1877
  
1878
  
1879
  
1880
  
1881
  
1882
  
1883
  
1884
  
1885
  
1886
  
1887
  
1888
  
1889
  
1890
  
1891
  
1892
  
1893
  
1894
  
1895
  
1896
  
1897
  
1898
  
1899
  
1900
  
1901
  
1902
  
1903
  
1904
  
1905
  
1906
  
1907
  
1908
  
1909
  
1910
  
1911
  
1912
  
1913
  
1914
  
1915
  
1916
  
1917
  
1918
  
1919
  
1920
  
1921
  
1922
  
1923
  
1924
  
1925
  
1926
  
1927
  
1928
  
1929
  
1930
  
1931
  
1932
  
1933
  
1934
  
1935
  
1936
  
1937
  
1938
  
1939
  
1940
  
1941
  
1942
  
1943
  
1944
  
1945
  
1946
  
1947
  
1948
  
1949
  
1950
  
1951
  
1952
  
1953
  
1954
  
1955
  
1956
  
1957
  
1958
  
1959
  
1960
  
1961
  
1962
  
1963
  
1964
  
1965
  
1966
  
1967
  
1968
  
1969
  
1970
  
1971
  
1972
  
1973
  
1974
  
1975
  
1976
  
1977
  
1978
  
1979
  
1980
  
1981
  
1982
  
1983
  
1984
  
1985
  
1986
  
1987
  
1988
  
1989
  
1990
  
1991
  
1992
  
1993
  
1994
  
1995
  
1996
  
1997
  
1998
  
1999
  
2000
  
2001
  
2002
  
2003
  
2004
  
2005
  
2006
  
2007
  
2008
  
2009
  
2010
  
2011
  
2012
  
2013
  
2014
  
2015
  
2016
  
2017
  
2018
  
2019
  
2020
  
2021
  
2022
  
2023
  
2024
  
2025
  
2026
  
2027
  
2028
  
2029
  
2030
  
2031
  
2032
  
2033
  
2034
  
2035
  
2036
  
2037
  
2038
  
2039
  
2040
  
2041
  
2042
  
2043
  
2044
  
2045
  
2046
  
2047
  
2048
  
2049
  
2050
  
2051
  
2052
  
2053
  
2054
  
2055
  
2056
  
2057
  
2058
  
2059
  
2060
  
2061
  
2062
  
2063
  
2064
  
2065
  
2066
  
2067
  
2068
  
2069
  
2070
  
2071
  
2072
  
2073
  
2074
  
2075
  
2076
  
2077
  
2078
  
2079
  
2080
  
2081
  
2082
  
2083
  
2084
  
2085
  
2086
  
2087
  
2088
  
2089
  
2090
  
2091
  
2092
  
2093
  
2094
  
2095
  
2096
  
2097
  
2098
  
2099
  
2100
  
2101
  
2102
  
2103
  
2104
  
2105
  
2106
  
2107
  
2108
  
2109
  
2110
  
2111
  
2112
  
2113
  
2114
  
2115
  
2116
  
2117
  
2118
  
2119
  
2120
  
2121
  
2122
  
2123
  
2124
  
2125
  
2126
  
2127
  
2128
  
2129
  
2130
  
2131
  
2132
  
2133
  
2134
  
2135
  
2136
  
2137
  
2138
  
2139
  
2140
  
2141
  
2142
  
2143
  
2144
  
2145
  
2146
  
2147
  
2148
  
2149
  
2150
  
2151
  
2152
  
2153
  
2154
  
2155
  
2156
  
2157
  
2158
  
2159
  
2160
  
2161
  
2162
  
2163
  
2164
  
2165
  
2166
  
2167
  
2168
  
2169
  
2170
  
2171
  
2172
  
2173
  
2174
  
2175
  
2176
  
2177
  
2178
  
2179
  
2180
  
2181
  
2182
  
2183
  
2184
  
2185
  
2186
  
2187
  
2188
  
2189
  
2190
  
2191
  
2192
  
2193
  
2194
  
2195
  
2196
  
2197
  
2198
  
2199
  
2200
  
2201
  
2202
  
2203
  
2204
  
2205
  
2206
  
2207
  
2208
  
2209
  
2210
  
2211
  
2212
  
2213
  
2214
  
2215
  
2216
  
2217
  
2218
  
2219
  
2220
  
2221
  
2222
  
2223
  
2224
  
2225
  
2226
  
2227
  
2228
  
2229
  
2230
  
2231
  
2232
  
2233
  
2234
  
2235
  
2236
  
2237
  
2238
  
2239
  
2240
  
2241
  
2242
  
2243
  
2244
  
2245
  
2246
  
2247
  
2248
  
2249
  
2250
  
2251
  
2252
  
2253
  
2254
  
2255
  
2256
  
2257
  
2258
  
2259
  
2260
  
2261
  
2262
  
2263
  
2264
  
2265
  
2266
  
2267
  
2268
  
2269
  
2270
  
2271
  
2272
  
2273
  
2274
  
2275
  
2276
  
2277
  
2278
  
2279
  
2280
  
2281
  
2282
  
2283
  
2284
  
2285
  
2286
  
2287
  
2288
  
2289
  
2290
  
2291
  
2292
  
2293
  
2294
  
2295
  
2296
  
2297
  
2298
  
2299
  
2300
  
2301
  
2302
  
2303
  
2304
  
2305
  
2306
  
2307
  
2308
  
2309
  
2310
  
2311
  
2312
  
2313
  
2314
  
2315
  
2316
  
2317
  
2318
  
2319
  
2320
  
2321
  
2322
  
2323
  
2324
  
2325
  
2326
  
2327
  
2328
  
2329
  
2330
  
2331
  
2332
  
2333
  
2334
  
2335
  
2336
  
2337
  
2338
  
2339
  
2340
  
2341
  
2342
  
2343
  
2344
  
2345
  
2346
  
2347
  
2348
  
2349
  
2350
  
2351
  
2352
  
2353
  
2354
  
2355
  
2356
  
2357
  
2358
  
2359
  
2360
  
2361
  
2362
  
2363
  
2364
  
2365
  
2366
  
2367
  
2368
  
2369
  
2370
  
2371
  
2372
  
2373
  
2374
  
2375
  
2376
  
2377
  
2378
  
2379
  
2380
  
2381
  
2382
  
2383
  
2384
  
2385
  
2386
  
2387
  
2388
  
2389
  
2390
  
2391
  
2392
  
2393
  
2394
  
2395
  
2396
  
2397
  
2398
  
2399
  
2400
  
2401
  
2402
  
2403
  
2404
  
2405
  
2406
  
2407
  
2408
  
2409
  
2410
  
2411
  
2412
  
2413
  
2414
  
2415
  
2416
  
2417
  
2418
  
2419
  
2420
  
2421
  
2422
  
2423
  
2424
  
2425
  
2426
  
2427
  
2428
  
2429
  
2430
  
2431
  
2432
  
2433
  
2434
  
2435
  
2436
  
2437
  
2438
  
2439
  
2440
  
2441
  
2442
  
2443
  
2444
  
2445
  
2446
  
2447
  
2448
  
2449
  
2450
  
2451
  
2452
  
2453
  
2454
  
2455
  
2456
  
2457
  
2458
  
2459
  
2460
  
2461
  
2462
  
2463
  
2464
  
2465
  
2466
  
2467
  
2468
  
2469
  
2470
  
2471
  
2472
  
2473
  
2474
  
2475
  
2476
  
2477
  
2478
  
2479
  
2480
  
2481
  
2482
  
2483
  
2484
  
2485
  
2486
  
2487
  
2488
  
2489
  
2490
  
2491
  
2492
  
2493
  
2494
  
2495
  
2496
  
2497
  
2498
  
2499
  
2500
  
2501
  
2502
  
2503
  
2504
  
2505
  
2506
  
2507
  
2508
  
2509
  
2510
  
2511
  
2512
  
2513
  
2514
  
2515
  
2516
  
2517
  
2518
  
2519
  
2520
  
2521
  
2522
  
2523
  
2524
  
2525
  
2526
  
2527
  
2528
  
2529
  
2530
  
2531
  
2532
  
2533
  
2534
  
2535
  
2536
  
2537
  
2538
  
2539
  
2540
  
2541
  
2542
  
2543
  
2544
  
2545
  
2546
  
2547
  
2548
  
2549
  
2550
  
2551
  
2552
  
2553
  
2554
  
2555
  
2556
  
2557
  
2558
  
2559
  
2560
  
2561
  
2562
  
2563
  
2564
  
2565
  
2566
  
2567
  
2568
  
2569
  
2570
  
2571
  
2572
  
2573
  
2574
  
2575
  
2576
  
2577
  
2578
  
2579
  
2580
  
2581
  
#pike __REAL_VERSION__ 
 
/* 
 */ 
 
//! XML parser that generates node-trees. 
//! 
//! Has some support for XML namespaces 
//! @url{http://www.w3.org/TR/REC-xml-names/@} @rfc{2518:23.4@}. 
//! 
//! @note 
//!   This module defines two sets of node trees; 
//!   the @[SimpleNode]-based, and the @[Node]-based. 
//!   The main difference between the two, is that 
//!   the @[Node]-based trees have parent pointers, 
//!   which tend to generate circular data references 
//!   and thus garbage. 
//! 
//!   There are some more subtle differences between 
//!   the two. Please read the documentation carefully. 
 
//! 
constant STOP_WALK = -1; 
 
//! 
constant XML_ROOT     = 0x0001; 
 
//! 
constant XML_ELEMENT  = 0x0002; 
 
//! 
constant XML_TEXT     = 0x0004; 
 
//! 
constant XML_HEADER   = 0x0008; 
 
//! 
constant XML_PI       = 0x0010; 
 
//! 
constant XML_COMMENT  = 0x0020; 
 
//! 
constant XML_DOCTYPE  = 0x0040; 
 
//! Attribute nodes are created on demand 
constant XML_ATTR     = 0x0080;    //  Attribute nodes are created on demand 
 
//! 
constant DTD_ENTITY   = 0x0100; 
 
//! 
constant DTD_ELEMENT  = 0x0200; 
 
//! 
constant DTD_ATTLIST  = 0x0400; 
 
//! 
constant DTD_NOTATION = 0x0800; 
 
//! 
constant XML_NODE     = (XML_ROOT | XML_ELEMENT | XML_TEXT | 
                       XML_PI | XML_COMMENT | XML_ATTR); 
#define STOP_WALK  -1 
#define  XML_ROOT     0x0001 
#define  XML_ELEMENT  0x0002 
#define  XML_TEXT     0x0004 
#define  XML_HEADER   0x0008 
#define  XML_PI       0x0010 
#define  XML_COMMENT  0x0020 
#define  XML_DOCTYPE  0x0040 
#define  XML_ATTR     0x0080     //  Attribute nodes are created on demand 
#define  DTD_ENTITY   0x0100 
#define  DTD_ELEMENT  0x0200 
#define  DTD_ATTLIST  0x0400 
#define  DTD_NOTATION 0x0800 
#define  XML_NODE     (XML_ROOT | XML_ELEMENT | XML_TEXT |    \ 
                                           XML_PI | XML_COMMENT | XML_ATTR) 
 
constant type_names = ([ 
    XML_ROOT : "ROOT", 
    XML_ELEMENT : "ELEMENT", 
    XML_TEXT : "TEXT", 
    XML_HEADER : "HEADER", 
    XML_PI : "PI", 
    XML_COMMENT : "COMMENT", 
    XML_DOCTYPE : "DOCTYPE", 
    XML_ATTR : "ATTR", 
    DTD_ENTITY: "!ENTITY", 
    DTD_ELEMENT: "!ELEMENT", 
    DTD_ATTLIST:  "!ATTLIST", 
    DTD_NOTATION: "!NOTATION", 
]); 
 
string get_type_name(int type) 
{ 
    if( type_names[type] ) return type_names[type]; 
    return (string)type; 
} 
 
//! Flags used together with @[simple_parse_input()] and 
//! @[simple_parse_file()]. 
enum ParseFlags { 
  PARSE_WANT_ERROR_CONTEXT =            0x1, 
#define PARSE_WANT_ERROR_CONTEXT      0x1 
  PARSE_FORCE_LOWERCASE =             0x2, 
#define PARSE_FORCE_LOWERCASE         0x2 
  PARSE_ENABLE_NAMESPACES =           0x4, 
#define PARSE_ENABLE_NAMESPACES               0x4 
  // Negated flag for compatibility. 
  PARSE_DISALLOW_RXML_ENTITIES =        0x8, 
#define PARSE_DISALLOW_RXML_ENTITIES  0x8 
 
  PARSE_COMPAT_ALLOW_ERRORS_7_2 =       0x10, 
#define PARSE_COMPAT_ALLOW_ERRORS_7_2 0x10 
  PARSE_COMPAT_ALLOW_ERRORS_7_6 =     0x20, 
#define PARSE_COMPAT_ALLOW_ERRORS_7_6 0x20 
  // The following exists for compatibility only. 
  PARSE_CHECK_ALL_ERRORS =              0, 
} 
 
//! Quotes the string given in @[data] by escaping &, < and >. 
string text_quote(string data) 
{ 
  return replace(data, ([ "&":"&amp;", 
                          "<":"&lt;", 
                          ">":"&gt;" ]) ); 
} 
 
//! Quotes strings just like @[text_quote], but entities in the form 
//! @tt{&foo.bar;@} will not be quoted. 
string roxen_text_quote(string data) { 
  string out = ""; 
  int pos, opos; 
  while ((pos = search(data, "&", pos)) >= 0) { 
    if ((sscanf(data[pos..], "&%[^\n\r\t <>;&];%*s", string entity) == 2) && 
        search(entity, ".") >= 1 && 
        !has_suffix(entity, ".")) { 
      out += text_quote(data[opos..pos - 1]) + "&" + entity + ";"; 
      pos += sizeof(entity) + 2; 
    } else { 
      out += text_quote(data[opos..pos]); 
      pos++; 
    } 
    opos = pos; 
  } 
  return out + text_quote(data[opos..]); 
} 
 
//! Quotes the string given in @[data] by escaping &, <, >, ' and ". 
string attribute_quote(string data, void|string ignore) 
{ 
  switch(ignore) 
  { 
  case "\"": 
    return replace(data, ([ "'":"&apos;", 
                            "&":"&amp;", 
                            "\n":"&#a;", 
                            "\r":"&#d;", 
                            "<":"&lt;", 
                            ">":"&gt;" ]) ); 
  case "'": 
    return replace(data, ([ "\"":"&quot;", 
                            "&":"&amp;", 
                            "\n":"&#a;", 
                            "\r":"&#d;", 
                            "<":"&lt;", 
                            ">":"&gt;" ]) ); 
 
  default: 
    return replace(data, ([ "\"":"&quot;", 
                            "'":"&apos;", 
                            "&":"&amp;", 
                            "\n":"&#a;", 
                            "\r":"&#d;", 
                            "<":"&lt;", 
                            ">":"&gt;" ]) ); 
  } 
} 
 
//! Quotes strings just like @[attribute_quote], but entities in the 
//! form @tt{&foo.bar;@} will not be quoted. 
string roxen_attribute_quote(string data) 
{ 
  return replace(roxen_text_quote(data), 
                 ([ "\"":"&quot;", 
                    "'":"&apos;" ])); 
} 
 
void throw_error(string str, mixed ... args) 
{ 
  //  Put message in debug log and throw exception 
  str = "Parser.XML.Tree: " + str; 
  error(str, @args); 
} 
 
//! Namespace aware parser. 
class XMLNSParser { 
  ADT.Stack namespace_stack = ADT.Stack(); 
 
  void create() 
  { 
    // Sentinel and default namespaces. 
    namespace_stack->push(([ 
                            "xml":"http://www.w3.org/XML/1998/namespace", 
                            "xmlns":"http://www.w3.org/2000/xmlns/", 
                          ])); 
  } 
 
  //! Check @[attrs] for namespaces. 
  //! 
  //! @returns 
  //!   Returns the namespace expanded version of @[attrs]. 
  mapping(string:string) Enter(mapping(string:string) attrs) 
  { 
    mapping(string:string) namespaces = namespace_stack->top() + ([]); 
    foreach(attrs; string attr; string val) { 
      if (attr == "xmlns") { 
        if (val == "") error("Bad namespace specification (%O=\"\")\n", attr); 
        if ((val == "http://www.w3.org/2000/xmlns/") || 
            (val == "http://www.w3.org/XML/1998/namespace")) { 
          error("Invalid namespace declaration.\n"); 
        } 
        namespaces[0] = val; 
      } else if (has_prefix(attr, "xmlns:")) { 
        if (val == "") error("Bad namespace specification (%O=\"\")\n", attr); 
        if ((val == "http://www.w3.org/2000/xmlns/") || 
            (val == "http://www.w3.org/XML/1998/namespace") || 
            (attr == "xmlns:xml") || (attr == "xmlns:xmlns")) { 
          if ((attr != "xmlns:xml") || 
              (val != "http://www.w3.org/XML/1998/namespace")) { 
            error("Invalid namespace declaration.\n"); 
          } 
        } 
        namespaces[attr[6..]] = val; 
      } 
    } 
    namespace_stack->push(namespaces); 
    // Now that we know what the namespaces are, we 
    // can expand the namespaces in the other attributes. 
    mapping(string:string) result = ([]); 
    foreach(attrs; string attr; string val) { 
      int i; 
      if (!has_prefix(attr, "xmlns:") && (i = search(attr, ":")) >= 0) { 
        string key = attr[..i-1]; 
        attr = attr[i+1..]; 
        string prefix = namespaces[key]; 
        if (!prefix) { 
          error("Unknown namespace %O for attribute %O\n", 
                key, attr); 
        } 
        attr = prefix+attr; 
      } 
      result[attr] = val; 
    } 
    return result; 
  } 
 
  string Decode(string name) 
  { 
    int i = search(name, ":"); 
    string key; 
    if (i >= 0) { 
      key = name[..i-1]; 
      name = name[i+1..]; 
    } 
    if ((key || name) == "xmlns") { 
      if (key) name = key + ":" + name; 
      return name; 
    } 
    string prefix = namespace_stack->top()[key]; 
    if (!prefix) { 
      if (key) { 
        error("Unknown namespace %O for tag %O\n", 
              key, name); 
      } else { 
        error("No default namespace, and tag without namespace: %O\n", 
              name); 
      } 
    } 
    return prefix + name; 
  } 
 
  string Encode(string name) 
  { 
    string longest; 
    string best; 
    foreach(namespace_stack->top(); string ns; string prefix) { 
      if (has_prefix(name, prefix) && 
          (!longest || sizeof(prefix) > sizeof(longest))) { 
        longest = prefix; 
        best = ns; 
      } 
    } 
    if (!longest) { 
      error("No namespace containing tag %O found.\n", name); 
    } 
    name = name[sizeof(longest)..]; 
    if (best) return best + ":" + name; 
    return name; 
  } 
 
  void Leave() 
  { 
    namespace_stack->pop(); 
  } 
} 
 
//! Base class for nodes. 
class AbstractSimpleNode { 
  //  Private member variables 
  /* protected */ array(AbstractSimpleNode) mChildren = ({ }); 
 
  //! Returns all the nodes children. 
  array(AbstractSimpleNode) get_children() { return (mChildren); } 
 
  //! Returns the number of children of the node. 
  int count_children()           { return (sizeof(mChildren)); } 
 
  //! Returns an initialized copy of the node. 
  //! @note 
  //!   The returned node has no children. 
  optional AbstractSimpleNode low_clone() { 
    return AbstractSimpleNode(); 
  } 
 
  //! Returns a clone of the sub-tree rooted in the node. 
  AbstractSimpleNode clone() { 
    AbstractSimpleNode n = low_clone(); 
    foreach(mChildren, AbstractSimpleNode child) 
      n->add_child( child->clone() ); 
    return n; 
  } 
 
  //! Returns the last child node or zero. 
  AbstractSimpleNode get_last_child() 
  { 
    if (!sizeof(mChildren)) 
      return 0; 
    else 
      return (mChildren[-1]); 
  } 
 
  //! The [] operator indexes among the node children, so 
  //! @expr{node[0]@} returns the first node and @expr{node[-1]@} the last. 
  //! @note 
  //!   The [] operator will select a node from all the nodes children, 
  //!   not just its element children. 
  protected AbstractSimpleNode `[](int pos) 
  { 
    //  Treat pos as index into array 
    if ((pos < 0) || (pos > sizeof(mChildren) - 1)) 
      return 0; 
    return (mChildren[pos]); 
  } 
 
  //! Adds the given node to the list of children of this node. The 
  //! new node is added last in the list. 
  //! 
  //! @note 
  //!   The return value differs from the one returned 
  //!   by @[Node()->add_child()]. 
  //! 
  //! @returns 
  //!   The current node. 
  AbstractSimpleNode add_child(AbstractSimpleNode c) 
  { 
    mChildren += ({ c }); 
 
    //  Let caller get the node back for easy chaining of calls 
    return this; 
  } 
 
  //! Adds the node @[c] to the list of children of this node. The 
  //! node is added before the node @[old], which is assumed to be an 
  //! existing child of this node. The node is added last if @[old] is 
  //! zero. 
  //! 
  //! @returns 
  //!   The current node. 
  AbstractSimpleNode add_child_before (AbstractSimpleNode c, 
                                       AbstractSimpleNode old) 
  { 
    if (old) { 
      int index = search (mChildren, old); 
      mChildren = mChildren[..index - 1] + ({c}) + mChildren[index..]; 
    } 
    else 
      mChildren += ({c}); 
    return this; 
  } 
 
  //! Adds the node @[c] to the list of children of this node. The 
  //! node is added after the node @[old], which is assumed to be an 
  //! existing child of this node. The node is added first if @[old] 
  //! is zero. 
  //! 
  //! @returns 
  //!   The current node. 
  AbstractSimpleNode add_child_after (AbstractSimpleNode c, 
                                      AbstractSimpleNode old) 
  { 
    if (old) { 
      int index = search (mChildren, old); 
      mChildren = mChildren[..index] + ({c}) + mChildren[index + 1..]; 
    } 
    else 
      mChildren = ({c}) + mChildren; 
    return this; 
  } 
 
  //! Removes all occurrences of the provided node from the list of 
  //! children of this node. 
  void remove_child(AbstractSimpleNode c) 
  { 
    mChildren -= ({ c }); 
  } 
 
  //! Replaces the nodes children with the provided ones. 
  void replace_children(array(AbstractSimpleNode) children) { 
    mChildren = children; 
  } 
 
 
  //! Replaces the first occurrence of the old node child with 
  //! the new node child or children. 
  //! @note 
  //!   The return value differs from the one returned 
  //!   by @[Node()->replace_child()]. 
  //! @returns 
  //!   Returns the current node on success, and @expr{0@} (zero) 
  //!   if the node @[old] wasn't found. 
  AbstractSimpleNode replace_child(AbstractSimpleNode old, 
                                   AbstractSimpleNode|array(AbstractSimpleNode) new) 
  { 
    int index = search(mChildren, old); 
    if (index < 0) 
      return 0; 
    if( arrayp(new) ) 
      mChildren = mChildren[..index-1] + new + mChildren[index+1..]; 
    else 
      mChildren[index] = new; 
    return this; 
  } 
 
  //! Destruct the tree recursively. When the inheriting 
  //! @[AbstractNode] or @[Node] is used, which have parent pointers, 
  //! this function should be called for every tree that no longer is 
  //! in use to avoid frequent garbage collector runs. 
  void zap_tree() 
  { 
    if (mChildren) 
      // Avoid mChildren->zap_tree() since applying an array causes 
      // pike to recurse more heavily on the C stack than a normal 
      // function call. 
      foreach (mChildren, AbstractSimpleNode child) 
        child && child->zap_tree(); 
    destruct (this); 
  } 
 
  //! Traverse the node subtree in preorder, root node first, then 
  //! subtrees from left to right, calling the callback function 
  //! for every node. If the callback function returns @[STOP_WALK] 
  //! the traverse is promptly aborted and @[STOP_WALK] is returned. 
  int walk_preorder(function(AbstractSimpleNode, mixed ...:int|void) callback, 
                    mixed ... args) 
  { 
    if (callback(this, @args) == STOP_WALK) 
      return STOP_WALK; 
    foreach(mChildren, AbstractSimpleNode c) 
      if (c->walk_preorder(callback, @args) == STOP_WALK) 
        return STOP_WALK; 
  } 
 
  //! Traverse the node subtree in preorder, root node first, then 
  //! subtrees from left to right. For each node we call @[cb_1] 
  //! before iterating through children, and then @[cb_2] 
  //! (which always gets called even if the walk is aborted earlier). 
  //! If the callback function returns @[STOP_WALK] the traverse 
  //! decend is aborted and @[STOP_WALK] is returned once all waiting 
  //! @[cb_2] functions have been called. 
  int walk_preorder_2(function(AbstractSimpleNode, mixed ...:int|void) cb_1, 
                      function(AbstractSimpleNode, mixed ...:int|void) cb_2, 
                      mixed ... args) 
  { 
    int  res; 
 
    res = cb_1(this, @args); 
    if (!res) 
      foreach(mChildren, AbstractSimpleNode c) 
        res = res || c->walk_preorder_2(cb_1, cb_2, @args); 
    return (cb_2(this, @args) || res); 
  } 
 
  //! Traverse the node subtree in inorder, left subtree first, then 
  //! root node, and finally the remaining subtrees, calling the function 
  //! @[callback] for every node. If the function @[callback] returns 
  //! @[STOP_WALK] the traverse is promptly aborted and @[STOP_WALK] 
  //! is returned. 
  int walk_inorder(function(AbstractSimpleNode, mixed ...:int|void) callback, 
                   mixed ... args) 
  { 
    if (sizeof(mChildren) > 0) 
      if (mChildren[0]->walk_inorder(callback, @args) == STOP_WALK) 
        return STOP_WALK; 
    if (callback(this, @args) == STOP_WALK) 
      return STOP_WALK; 
    foreach(mChildren[1..], AbstractSimpleNode c) 
      if (c->walk_inorder(callback, @args) == STOP_WALK) 
        return STOP_WALK; 
  } 
 
  //! Traverse the node subtree in postorder, first subtrees from left 
  //! to right, then the root node, calling the function @[callback] 
  //! for every node. If the function @[callback] returns @[STOP_WALK] 
  //! the traverse is promptly aborted and @[STOP_WALK] is returned. 
  int walk_postorder(function(AbstractSimpleNode, 
                              mixed ...:int|void) callback, 
                     mixed ... args) 
  { 
    foreach(mChildren, AbstractSimpleNode c) 
      if (c->walk_postorder(callback, @args) == STOP_WALK) 
        return STOP_WALK; 
    if (callback(this, @args) == STOP_WALK) 
      return STOP_WALK; 
  } 
 
  //! Iterates over the nodes children from left to right, calling the 
  //! function @[callback] for every node. If the callback function 
  //! returns @[STOP_WALK] the iteration is promptly aborted and 
  //! @[STOP_WALK] is returned. 
  int iterate_children(function(AbstractSimpleNode, 
                                mixed ...:int|void) callback, 
                       mixed ... args) 
  { 
    foreach(mChildren, AbstractSimpleNode c) 
      if (callback(c, @args) == STOP_WALK) 
        return STOP_WALK; 
  } 
 
  //! Returns a list of all descendants in document order. Includes 
  //! this node if @[include_self] is set. 
  array(AbstractSimpleNode) get_descendants(int(0..1) include_self) 
  { 
    array   res; 
 
    //  Walk subtrees in document order and add to resulting list 
    res = include_self ? ({ this }) : ({ }); 
    foreach(mChildren, AbstractSimpleNode child) { 
      res += child->get_descendants(1); 
    } 
    return (res); 
  } 
 
  //! Optional factory for creating contained nodes. 
  //! 
  //! @param type 
  //!   Type of node to create. One of: 
  //!   @int 
  //!     @value XML_TEXT 
  //!       XML text. @[text] contains a string with the text. 
  //!     @value XML_COMMENT 
  //!       XML comment. @[text] contains a string with the comment text. 
  //!     @value XML_HEADER 
  //!       @tt{<?xml?>@}-header @[attr] contains a mapping with 
  //!       the attributes. 
  //!     @value XML_PI 
  //!       XML processing instruction. @[name] contains the name of the 
  //!       processing instruction and @[text] the remainder. 
  //!     @value XML_ELEMENT 
  //!       XML element tag. @[name] contains the name of the tag and 
  //!       @[attr] the attributes. 
  //!     @value XML_DOCTYPE 
  //!     @value DTD_ENTITY 
  //!     @value DTD_ELEMENT 
  //!     @value DTD_ATTLIST 
  //!     @value DTD_NOTATION 
  //!       DTD information. 
  //!   @endint 
  //! 
  //! @param name 
  //!   Name of the tag if applicable. 
  //! 
  //! @param attr 
  //!   Attributes for the tag if applicable. 
  //! 
  //! @param text 
  //!   Contained text of the tab if any. 
  //! 
  //! This function is called during parsning to create the various 
  //! XML nodes. 
  //! 
  //! Define this function to provide application-specific XML nodes. 
  //! 
  //! @returns 
  //!   Returns one of 
  //!   @mixed 
  //!     @type AbstractSimpleNode 
  //!       A node object representing the XML tag. 
  //!     @type int(0..0) 
  //!       @expr{0@} (zero) if the subtree rooted here should be cut. 
  //!     @type zero 
  //!       @expr{UNDEFINED@} to fall back to the next level of parser 
  //!       (ie behave as if this function does not exist). 
  //!   @endmixed 
  //! 
  //! @note 
  //!   This function is only relevant for @[XML_ELEMENT] nodes. 
  //! 
  //! @note 
  //!   This function is not available in Pike 7.6 and earlier. 
  //! 
  //! @note 
  //!   In Pike 8.0 and earlier this function was only called in 
  //!   root nodes. 
  optional this_program node_factory(int type, string name, 
                                     mapping attr, string text); 
} 
 
//! Base class for nodes with parent pointers. 
class AbstractNode { 
  inherit AbstractSimpleNode; 
  //  Private member variables 
  /* protected */ AbstractNode           mParent = 0; 
 
  // Instruct Pike.count_memory to search three steps: mChildren (in 
  // VirtualNode also mAttrNodes) -> array value -> mParent. 
  constant pike_cycle_depth = 3; 
 
  //  Public methods 
 
  //! Sets the parent node to @[parent]. 
  void set_parent(AbstractNode parent)    { mParent = parent; } 
 
  //! Returns the parent node. 
  AbstractNode get_parent()          { return (mParent); } 
 
#if 0 
  protected void create() 
  { 
    error("Creating a plain AbstractNode.\n"); 
  } 
#endif /* 0 */ 
 
  //! Returns an initialized copy of the node. 
  //! @note 
  //!   The returned node has no children, and no parent. 
  optional AbstractNode low_clone() 
  { 
    return AbstractNode(); 
  } 
 
  //! Clones the node, optionally connected to parts of the tree. 
  //! If direction is -1 the cloned nodes parent will be set, if 
  //! direction is 1 the clone nodes childen will be set. 
  AbstractNode clone(void|int(-1..1) direction) { 
    AbstractNode n = low_clone(); 
    if(mParent && direction!=1) 
      n->set_parent( mParent->clone(-1) ); 
    if(direction!=-1) 
      foreach(mChildren, AbstractNode child) 
        n->add_child( child->clone(1) ); 
    return n; 
  } 
 
  //! Follows all parent pointers and returns the root node. 
  AbstractNode get_root() 
  { 
    AbstractNode  parent, node; 
 
    parent = this; 
    while (node = parent->mParent) 
      parent = node; 
    return (parent); 
  } 
 
  //! Adds the node @[c] to the list of children of this node. The 
  //! node is added before the node @[old], which is assumed to be an 
  //! existing child of this node. The node is added first if @[old] 
  //! is zero. 
  //! 
  //! @note 
  //!   Returns the new child node, NOT the current node. 
  //! 
  //! @returns 
  //! The new child node is returned. 
  AbstractNode add_child(AbstractNode c) 
  { 
    c->mParent = ::add_child(c); 
 
    //  Let caller get the new node back for easy chaining of calls 
    return (c); 
  } 
 
  //! Adds the node @[c] to the list of children of this node. The 
  //! node is added before the node @[old], which is assumed to be an 
  //! existing child of this node. The node is added last if @[old] is 
  //! zero. 
  //! 
  //! @returns 
  //!   The current node. 
  AbstractNode add_child_before (AbstractNode c, AbstractNode old) 
  { 
    return c->mParent = ::add_child_before (c, old); 
  } 
 
  //! Adds the node @[c] to the list of children of this node. The 
  //! node is added after the node @[old], which is assumed to be an 
  //! existing child of this node. The node is added first if @[old] 
  //! is zero. 
  //! 
  //! @returns 
  //!   The current node. 
  AbstractNode add_child_after (AbstractNode c, AbstractNode old) 
  { 
    return c->mParent = ::add_child_after (c, old); 
  } 
 
  //! Variants of @[add_child], @[add_child_before] and 
  //! @[add_child_after] that doesn't set the parent pointer in the 
  //! newly added children. 
  //! 
  //! This is useful while building a node tree, to get efficient 
  //! refcount garbage collection if the build stops abruptly. 
  //! @[fix_tree] has to be called on the root node when the building 
  //! is done. 
  AbstractNode tmp_add_child(AbstractNode c) 
  { 
    ::add_child (c); return c; 
  } 
  AbstractNode tmp_add_child_before (AbstractNode c, AbstractNode old) 
  { 
    return ::add_child_before (c, old); 
  } 
  AbstractNode tmp_add_child_after (AbstractNode c, AbstractNode old) 
  { 
    return ::add_child_after (c, old); 
  } 
 
  //! Fix all parent pointers recursively in a tree that has been 
  //! built with @[tmp_add_child]. 
  void fix_tree() 
  { 
    foreach (mChildren, AbstractNode c) 
      if (c->mParent != this) { 
        c->mParent = this; 
        c->fix_tree(); 
      } 
  } 
 
  //! Removes all occurrences of the provided node from the called nodes 
  //! list of children. The removed nodes parent reference is set to null. 
  void remove_child(AbstractNode c) 
  { 
    ::remove_child(c); 
    c->mParent = 0; 
  } 
 
  //! Removes this node from its parent. The parent reference is set to null. 
  void remove_node() { 
    mParent->remove_child(this); 
  } 
 
  //! Replaces the nodes children with the provided ones. All parent 
  //! references are updated. 
  void replace_children(array(AbstractNode) children) { 
    foreach(mChildren, AbstractNode c) 
      c->mParent = 0; 
    ::replace_children(children); 
    foreach(mChildren, AbstractNode c) 
      c->mParent = this; 
  } 
 
 
  //! Replaces the first occurrence of the old node child with the new 
  //! node child or children. All parent references are updated. 
  //! @note 
  //!   The returned value is NOT the current node. 
  //! @returns 
  //!   Returns the new child node. 
  AbstractNode replace_child(AbstractNode old, 
                             AbstractNode|array(AbstractNode) new) 
  { 
    if (!::replace_child(old, new)) return 0; 
    new->mParent = this; 
    old->mParent = 0; 
    return new; 
  } 
 
  //! Replaces this node with the provided one. 
  //! @returns 
  //!   Returns the new node. 
  AbstractNode|array(AbstractNode) 
    replace_node(AbstractNode|array(AbstractNode) new) 
  { 
    mParent->replace_child(this, new); 
    return new; 
  } 
 
  //! Returns all preceding siblings, i.e. all siblings present before 
  //! this node in the parents children list. 
  array(AbstractNode) get_preceding_siblings() 
  { 
    array  siblings; 
    int    pos; 
 
    //  Get parent list of children and locate ourselves 
    if (!mParent) 
      return ({ }); 
    siblings = mParent->get_children(); 
    pos = search(siblings, this); 
 
    //  Return array in reverse order not including self 
    return (reverse(siblings[..(pos - 1)])); 
  } 
 
  //! Returns all following siblings, i.e. all siblings present after 
  //! this node in the parents children list. 
  array(AbstractNode) get_following_siblings() 
  { 
    array  siblings; 
    int    pos; 
 
    //  Get parent list of children and locate ourselves 
    if (!mParent) 
      return ({ }); 
    siblings = mParent->get_children(); 
    pos = search(siblings, this); 
 
    //  Select array range 
    return (siblings[(pos + 1)..]); 
  } 
 
  //! Returns all siblings, including this node. 
  array(AbstractNode) get_siblings() 
  { 
    //  If not found we return ourself only 
    if (!mParent) 
      return ({ this }); 
    return (mParent->get_children()); 
  } 
 
  //! Returns a list of all ancestors, with the top node last. 
  //! The list will start with this node if @[include_self] is set. 
  array(AbstractNode) get_ancestors(int(0..1) include_self) 
  { 
    array     res; 
    AbstractNode  node; 
 
    //  Repeat until we reach the top 
    res = include_self ? ({ this }) : ({ }); 
    node = this; 
    while (node = node->get_parent()) 
      res += ({ node }); 
    return (res); 
  } 
 
  //! Returns all preceding nodes, excluding this nodes ancestors. 
  array(AbstractNode) get_preceding() 
  { 
    AbstractNode   node, root, self; 
    array      res = ({ }); 
 
    //  Walk tree from root until we find ourselves and add all preceding 
    //  nodes. We should return the nodes in reverse document order. 
    self = this; 
    root = get_root(); 
    root->walk_preorder( 
                        lambda(AbstractNode n) { 
                          //  Have we reached our own node? 
                          if (n == self) 
                            return STOP_WALK; 
                          else 
                            res = ({ n }) + res; 
                        }); 
 
    //  Finally remove all of our ancestors 
    root = this; 
    while (node = root->get_parent()) { 
      root = node; 
      res -= ({ node }); 
    } 
    return (res); 
  } 
 
  //! Returns all the nodes that follows after the current one. 
  array(AbstractNode) get_following() 
  { 
    array      siblings; 
    AbstractNode   node; 
    array      res = ({ }); 
 
    //  Add subtrees from right-hand siblings and keep walking towards 
    //  the root of the tree. 
    node = this; 
    do { 
      siblings = node->get_following_siblings(); 
      foreach(siblings, AbstractNode n) { 
        n->walk_preorder( 
                         lambda(AbstractNode n) { 
                           //  Add to result 
                           res += ({ n }); 
                         }); 
      } 
 
      node = node->get_parent(); 
    } while (node); 
    return (res); 
  } 
} 
 
//!  Node in XML tree 
protected class VirtualNode { 
  //  Member variables for this node type 
  protected int            mNodeType; 
  protected string              mShortNamespace = "";       // Namespace prefix 
  protected string              mNamespace;     // Resolved namespace 
  protected string         mTagName; 
  protected mapping(string:string) mAttributes;         // Resolved attributes 
  protected mapping(string:string)      mShortAttributes;       // Shortened attributes 
  protected array(Node) mAttrNodes;   //  created on demand 
  protected string         mText; 
  protected int            mDocOrder; 
 
  // Functions implemented via multiple inheritance. 
  array(AbstractNode) get_children(); 
  int walk_preorder(function(AbstractSimpleNode, mixed ...:int|void) callback, 
                    mixed ... args); 
 
  protected VirtualNode low_clone() 
  { 
    return this_program(get_node_type(), get_full_name(), 
                        get_attributes(), get_text()); 
  } 
 
  //  This can be accessed directly by various methods to cache parsing 
  //  info for faster processing. Some use it for storing flags and others 
  //  use it to cache reference objects. 
  public mixed           mNodeData = 0; 
 
  //  Public methods 
  //! Returns this nodes attributes, which can be altered 
  //! destructivly to alter the nodes attributes. 
  //! 
  //! @seealso 
  //!   @[replace_attributes()] 
  mapping(string:string) get_attributes()   { return (mAttributes); } 
 
  //! Replace the entire set of attributes. 
  //! 
  //! @seealso 
  //!   @[get_attributes()] 
  void replace_attributes(mapping(string:string) attrs) 
  { 
    if (mShortAttributes && mShortAttributes == mAttributes) 
      mShortAttributes = attrs; 
    mAttributes = attrs; 
  } 
 
  //! Returns this nodes name-space adjusted attributes. 
  //! 
  //! @note 
  //!   @[set_short_namespaces()] or @[set_short_attributes()] must 
  //!   have been called before calling this function. 
  mapping get_short_attributes()   { return (mShortAttributes); } 
 
  //! Sets this nodes name-space adjusted attributes. 
  void set_short_attributes(mapping short_attrs) 
  { 
    mShortAttributes = short_attrs; 
  } 
 
  //! Returns the node type. See defined node type constants. 
  int get_node_type()        { return (mNodeType); } 
 
  //! Returns text content in node. 
  string get_text()          { return (mText); } 
 
  //! 
  int get_doc_order()        { return (mDocOrder); } 
 
  //! 
  void set_doc_order(int o)  { mDocOrder = o; } 
 
  //! Returns the name of the element node, or the nearest element above if 
  //! an attribute node. 
  string get_tag_name() 
  { 
    return mTagName; 
  } 
 
  //! Return name of tag or name of attribute node. 
  string get_any_name() 
  { 
    return (mTagName); 
  } 
 
  //! Change the tag name destructively. Can only be used on element and 
  //! processing-instruction nodes. 
  void set_tag_name(string name) 
  { 
    if (mNodeType & (XML_ELEMENT | XML_PI)) 
      mTagName = name; 
  } 
 
  //! Return the (resolved) namespace for this node. 
  string get_namespace() 
  { 
    return mNamespace; 
  } 
 
  //! Return fully qualified name of the element node. 
  string get_full_name() 
  { 
    return mNamespace + mTagName; 
  } 
 
  string get_short_name() 
  { 
    return mShortNamespace + mTagName; 
  } 
 
  //! 
  protected void create(int type, string name, mapping attr, string text) 
  { 
    if (name) { 
      if (has_value(name, ":") && sscanf (name, "%*[^/:]%*c") == 2) { 
        sscanf(reverse(name), "%[^/:]", mTagName); 
        mTagName=reverse(mTagName); 
        mShortNamespace = mNamespace = name[..<sizeof(mTagName)]; 
      } 
      else { 
        mTagName = name; 
        mNamespace = ""; 
      } 
    } 
    mNodeType = type; 
    mAttributes = attr; 
    mText = text; 
    mAttrNodes = 0; 
  } 
 
  //! If the node is an attribute node or a text node, its value is returned. 
  //! Otherwise the child text nodes are concatenated and returned. 
  string value_of_node() 
  { 
    string  str = ""; 
 
    switch (mNodeType) { 
    case XML_ATTR: 
    case XML_TEXT: 
      //  If attribute node we return attribute value. For text nodes we 
      //  return (you guessed it!) text. 
      return (mText); 
 
    default: 
      //  Concatenate text children 
      walk_preorder(lambda(Node n) { 
                      if (n->get_node_type() == XML_TEXT) 
                        str += n->get_text(); 
                    }); 
      return (str); 
    } 
  } 
 
  //! Returns the first element child to this node. 
  //! 
  //! @param name 
  //!   If provided, the first element child with that name is returned. 
  //! 
  //! @param full 
  //!   If specified, name matching will be done against the full name. 
  //! 
  //! @returns 
  //!   Returns the first matching node, and 0 if no such node was found. 
  AbstractNode get_first_element(string|void name, int(0..1)|void full) { 
    if (!name) { 
      foreach(get_children(), AbstractNode c) 
        if(c->get_node_type()==XML_ELEMENT) 
          return c; 
    } else if (!full) { 
      foreach(get_children(), AbstractNode c) 
        if(c->get_node_type()==XML_ELEMENT && 
           c->get_tag_name()==name) 
          return c; 
    } else { 
      foreach(get_children(), AbstractNode c) 
        if(c->get_node_type()==XML_ELEMENT && 
           c->get_full_name()==name) 
          return c; 
    } 
    return 0; 
  } 
 
  //! Returns all element children to this node. 
  //! 
  //! @param name 
  //!   If provided, only elements with that name is returned. 
  //! 
  //! @param full 
  //!   If specified, name matching will be done against the full name. 
  //! 
  //! @returns 
  //!   Returns an array with matching nodes. 
  array(AbstractNode) get_elements(string|void name, int(0..1)|void full) { 
    if (!name) { 
      return filter(get_children(), 
                    lambda(Node n) { 
                      return n->get_node_type()==XML_ELEMENT; 
                    } ); 
    } else if (!full) { 
      return filter(get_children(), 
                    lambda(Node n) { 
                      return n->get_node_type()==XML_ELEMENT && 
                        n->get_tag_name()==name; 
                    } ); 
    } else { 
      return filter(get_children(), 
                    lambda(Node n) { 
                      return n->get_node_type()==XML_ELEMENT && 
                        n->get_full_name()==name; 
                    } ); 
    } 
  } 
 
  // It doesn't produce html, and not of the node only. 
  // Note: Returns wide data! 
  string html_of_node(void|int(0..1) preserve_roxen_entities) 
  { 
    String.Buffer data = String.Buffer(); 
    set_short_namespaces(); 
    if(preserve_roxen_entities) 
      low_render_xml(data, this, roxen_text_quote, roxen_attribute_quote); 
    else 
      low_render_xml(data, this, text_quote, attribute_quote); 
    return (string)data; 
  } 
 
  //! It is possible to cast a node to a string, which will return 
  //! @[render_xml()] for that node. 
  protected mixed cast(string to) { 
    if(to=="string") return render_xml(); 
    return UNDEFINED; 
  } 
 
  // FIXME: Consider moving this to the corresponding base node classes? 
  protected void low_render_xml(String.Buffer data, Node n, 
                             function(string:string) textq, 
                             function(string,void|string:string) attrq, 
                             void|mapping(string:string) namespace_lookup) 
  { 
    string tagname; 
    switch(n->get_node_type()) { 
    case XML_TEXT: 
      data->add(textq(n->get_text())); 
      break; 
 
    case XML_ELEMENT: 
      if (!sizeof(tagname = n->get_short_name())) 
        break; 
 
      data->add("<", tagname); 
      if (mapping attr = n->get_short_attributes()) { 
        foreach(sort(indices(attr)), string a) { 
          if( has_value(attr[a], "'") ) 
            data->add(" ", a, "=\"", attrq(attr[a], "'"), "\""); 
          else 
            data->add(" ", a, "='", attrq(attr[a], "\""), "'"); 
        } 
      } 
      if (n->count_children()) 
        data->add(">"); 
      else 
        data->add("/>"); 
      break; 
 
    case XML_HEADER: 
      data->add("<?xml"); 
      if (mapping attr = n->get_attributes() + ([])) { 
        // version and encoding must come in the correct order. 
        // version must always be present. 
        if (attr->version) 
          data->add(" version='", attrq(attr->version), "'"); 
        else 
          data->add(" version='1.0'"); 
        m_delete(attr, "version"); 
        if (attr->encoding) 
          data->add(" encoding='", attrq(attr->encoding), "'"); 
        m_delete(attr, "encoding"); 
        foreach(sort(indices(attr)), string a) 
          data->add(" ", a, "='", attrq(attr[a]), "'"); 
      } 
      data->add("?>"); 
      break; 
 
    case XML_PI: 
      data->add("<?", n->get_short_name()); 
      string text = n->get_text(); 
      if (sizeof(text)) 
        data->add(" ", text); 
      data->add("?>"); 
      break; 
 
    case XML_COMMENT: 
      data->add("<!--", n->get_text(), "-->"); 
      break; 
 
    case XML_DOCTYPE: 
      data->add("<!DOCTYPE ", n->get_short_name()); 
      mapping attrs = n->get_attributes(); 
      if (attrs->PUBLIC) { 
        data->sprintf(" PUBLIC %O %O", 
                      attrs->PUBLIC, attrs->SYSTEM || ""); 
      } else if (attrs->SYSTEM) { 
        data->sprintf(" SYSTEM %O", attrs->SYSTEM); 
      } 
      if (n->count_children()) { 
        // Use the raw internal subset if available. 
        if (attrs->internal_subset) { 
          data->add(" [", attrs->internal_subset, "]>"); 
          return; 
        } else { 
          // We need to render the DTD by hand. 
          // NOTE: Any formatting (including PEReferences) of the 
          //       originating DTD will be lost. 
          data->add(" [\n"); 
        } 
      } else { 
        data->add(">"); 
      } 
      break; 
 
    case DTD_ENTITY: 
      tagname = n->get_short_name(); 
      if (tagname[0] == '%') { 
        data->add("  <!ENTITY % ", tagname[1..], " "); 
      } else { 
        data->add("  <!ENTITY ", tagname, " "); 
      } 
      data->add("\"", attribute_quote(n->get_text()), "\" >\n"); 
      break; 
 
    case DTD_ELEMENT: 
      data->add("  <!ELEMENT ", n->get_short_name(), " "); 
      n->render_expression(data); 
      data->add(" >\n"); 
      break; 
    } 
 
    // FIXME: The following code is probably only relevant to 
    //        XML_ELEMENT and XML_DOCTYPE nodes. Consider moving 
    //        it to the corresponding cases above. 
 
    array(Node) children = n->get_children(); 
    foreach(children, Node n) { 
      low_render_xml(data, n, textq, attrq, namespace_lookup); 
    } 
 
    if (n->get_node_type() == XML_ELEMENT) { 
      if (n->count_children()) 
        if (sizeof(tagname)) 
          data->add("</", tagname, ">"); 
    } else if ((n->get_node_type() == XML_DOCTYPE) && (n->count_children())) { 
      data->add(" ]>"); 
    } 
  } 
 
  // Get the encoding from the XML-header (if any). 
  // 
  // Create a new XML-header if there's none. 
  // 
  // Add an encoding attribute if there is none. 
  protected string get_encoding() 
  { 
    Node xml_header; 
    if (sizeof(get_children()) && 
        (xml_header = get_children()[0])->get_node_type()==XML_HEADER) { 
      string encoding; 
      if (encoding = xml_header->get_attributes()->encoding) { 
        return encoding; 
      } 
      xml_header->get_attributes()->encoding = "utf-8"; 
    } 
    return "utf-8"; 
  } 
 
  void set_short_namespaces(void|mapping(string:string) forward_lookup, 
                            void|mapping(string:string) backward_lookup) 
  { 
    if (!mTagName) return; 
    if (!forward_lookup) { 
      forward_lookup = ([ 
        "http://www.w3.org/XML/1998/namespace":"xml:", 
        "http://www.w3.org/2000/xmlns/":"xmlns:", 
      ]); 
      backward_lookup = ([ 
        "xml:":"http://www.w3.org/XML/1998/namespace", 
        "xmlns:":"http://www.w3.org/2000/xmlns/", 
      ]); 
    } else { 
      // Make sure changes aren't propagated backwards... 
      forward_lookup += ([]); 
      backward_lookup += ([]); 
    } 
    // First check if any namespaces are defined by this tag. 
    mapping attrs = get_attributes() || ([]); 
    mapping short_attrs = attrs + ([]); 
    foreach(indices(attrs), string attr_name) { 
      if (has_prefix(attr_name, "xmlns")) { 
        string short_prefix = ""; 
        if (has_prefix(attr_name, "xmlns:")) { 
          short_prefix = attr_name[6..] + ":"; 
        } 
        if (backward_lookup[short_prefix]) { 
          m_delete(forward_lookup, backward_lookup[short_prefix]); 
        } 
        backward_lookup[short_prefix] = attrs[attr_name]; 
        forward_lookup[attrs[attr_name]] = short_prefix; 
      } 
    } 
    if (!mShortAttributes) { 
      // Then set the short namespace for this tag. 
      mShortNamespace = ""; 
      if (sizeof(mNamespace)) { 
        if (!(mShortNamespace = forward_lookup[mNamespace])) { 
#if 0 
        werror("Forward_lookup: %O\n" 
                 "Backward_lookup: %O\n" 
                 "mNamespace:%O\n", 
                 forward_lookup, 
                 backward_lookup, 
                 mNamespace); 
#endif /* 0 */ 
 
          string found; 
          string full_name = get_full_name(); 
          // Check if there are any longer namespaces that might match. 
          foreach(forward_lookup; string long;) { 
            if (has_prefix(full_name, long) && 
                (!found || (sizeof(found) < sizeof(long)))) { 
              found = long; 
              break; 
            } 
          } 
 
          if (found) { 
#if 0 
          werror("Found: NS: %O <%s%s/>\n", 
                   found, forward_lookup[found], full_name[sizeof(found)..]); 
#endif /* 0 */ 
          mTagName = full_name[sizeof(found)..]; 
            mNamespace = found; 
            mShortNamespace = forward_lookup[found]; 
          } else { 
#if 0 
          werror("No suitable namespace found for %O.\n", 
                   full_name); 
#endif /* 0 */ 
          // We need to allocate a short namespace symbol. 
            // FIXME: This is O(n²). 
            int i; 
            while(backward_lookup[mShortNamespace = ("NS"+i+":")]) { 
              i++; 
            } 
            backward_lookup[mShortNamespace] = mNamespace; 
            forward_lookup[mNamespace] = mShortNamespace; 
            attrs["xmlns:NS"+i] = mNamespace; 
            short_attrs["xmlns:NS"+i] = mNamespace; 
          } 
        } 
      } 
#if 0 
      werror("attrs: %O\n" 
             "short attrs: %O\n", 
             attrs, short_attrs); 
#endif /* 0 */ 
      // Then set the short namespaces for any attributes. 
      foreach(indices(attrs), string attr_name) { 
        if (!has_prefix(attr_name, "xmlns:")) { 
          int i = -1; 
          int j; 
          while ((j = search(attr_name, ":", i + 1)) >= 0) { 
            i = j; 
          } 
          while ((j = search(attr_name, "/", i + 1)) >= 0) { 
            i = j; 
          } 
          if (i >= 0) { 
            string ns = attr_name[..i]; 
            string prefix; 
 
            // Check if we already have some namespace that is a longer 
            // prefix of this attribute than ns. This isn't only for 
            // looks; there are broken XML parsers that require the 
            // break between the namespace and the attribute name to be 
            // at a specific spot, e.g. the one used in the WebDAV 
            // client in MS XP Pro. 
            foreach (forward_lookup; string long;) 
              if (sizeof (long) > sizeof (ns) && 
                  has_prefix (attr_name, long)) { 
                ns = long; 
                i = sizeof (long) - 1; 
                break; 
              } 
 
            if (!(prefix = forward_lookup[ns])) { 
              // We need to allocate a short namespace symbol. 
#if 0 
            werror("Namespace %O not found in %O\n", 
                     ns, forward_lookup); 
#endif /* 0 */ 
            // FIXME: This is O(n²). 
              int i; 
              while(backward_lookup[prefix = ("NS"+i+":")]) { 
                i++; 
              } 
              backward_lookup[prefix] = ns; 
              forward_lookup[mNamespace] = prefix; 
              attrs["xmlns:NS"+i] = ns; 
              short_attrs["xmlns:NS"+i] = ns; 
#if 0 
            werror("New namespace: %O %O\n", prefix, ns); 
              werror("Forward_lookup: %O\n" 
                     "Backward_lookup: %O\n" 
                     "mNamespace:%O\n", 
                     forward_lookup, 
                     backward_lookup, 
                     mNamespace); 
#endif /* 0 */ 
          } 
            m_delete(short_attrs, attr_name); 
            short_attrs[prefix + attr_name[i+1..]] = attrs[attr_name]; 
          } 
        } 
      } 
      mShortAttributes = short_attrs; 
    } 
    // And then do it for all the children. 
    get_children()->set_short_namespaces(forward_lookup, backward_lookup); 
  } 
 
  protected Charset.Encoder get_encoder(string encoding) 
  { 
    return Charset.encoder(encoding)->set_replacement_callback(lambda(string c) 
      { 
        return sprintf("&#%x;", c[0]); 
      }); 
  } 
 
  //! Creates an XML representation of the node sub tree. If the 
  //! flag @[preserve_roxen_entities] is set, entities on the form 
  //! @tt{&foo.bar;@} will not be escaped. 
  //! 
  //! @param namespace_lookup 
  //!   Mapping from namespace prefix to namespace symbol prefix. 
  //! 
  //! @param encoding 
  //!   Force a specific output character encoding. By default the 
  //!   encoding set in the document XML processing instruction will 
  //!   be used, with UTF-8 as a fallback. Setting this value will 
  //!   change the XML processing instruction, if present. 
  string render_xml(void|int(0..1) preserve_roxen_entities, 
                    void|mapping(string:string) namespace_lookup, 
                    void|string encoding) 
  { 
    String.Buffer data = String.Buffer(); 
    if( encoding ) 
    { 
      Node xml_header; 
      if (sizeof(get_children()) && 
          (xml_header = get_children()[0])->get_node_type()==XML_HEADER) 
        xml_header->get_attributes()->encoding=encoding; 
    } 
    else 
      encoding = get_encoding(); 
 
    set_short_namespaces(); 
    if(preserve_roxen_entities) 
      low_render_xml(data, this, roxen_text_quote, 
                     roxen_attribute_quote, 
                     namespace_lookup); 
    else 
      low_render_xml(data, this, text_quote, attribute_quote, 
                     namespace_lookup); 
    return get_encoder(encoding)->feed((string)data)->drain(); 
  } 
 
  //! Creates an XML representation for the node sub tree and streams 
  //! the output to the file @[f]. If the flag @[preserve_roxen_entities] 
  //! is set, entities on the form @tt{&foo.bar;@} will not be escaped. 
  void render_to_file(Stdio.File f, 
                      void|int(0..1) preserve_roxen_entities) { 
    object data = class (Stdio.File f, object encoder) { 
      void add(string ... args) { 
        encoder->feed(args[*]); 
        f->write(encoder->drain()); 
      } 
    } (f, get_encoder(get_encoding())); 
    set_short_namespaces(); 
    if(preserve_roxen_entities) 
      low_render_xml(data, this, roxen_text_quote, 
                     roxen_attribute_quote); 
    else 
      low_render_xml(data, this, text_quote, attribute_quote); 
  } 
 
  /*protected*/ void _add_to_text (string str) 
  // Only to be used internally from the parse callback. 
  { 
    mText += str; 
  } 
 
  string _sprintf(int t) { 
    return t=='O' && sprintf("%O(#%d:%s,%O)", this_program, mDocOrder, 
                             get_type_name(get_node_type()), get_full_name()); 
  } 
} 
 
//! XML node without parent pointers and attribute nodes. 
class SimpleNode 
{ 
  inherit AbstractSimpleNode; 
  inherit VirtualNode; 
 
  // Needed for cross-overloading 
  optional SimpleNode low_clone() 
  { 
    return VirtualNode::low_clone(); 
  } 
} 
 
 
//! XML node with parent pointers. 
class Node 
{ 
  inherit AbstractNode; 
  inherit VirtualNode; 
 
  // Needed for cross-overloading 
  optional Node low_clone() 
  { 
    return VirtualNode::low_clone(); 
  } 
 
  //! Returns the name of the element node, or the nearest element above if 
  //! an attribute node. 
  string get_tag_name() 
  { 
    //  Fake ATTR nodes query their parent 
    return ((mNodeType == XML_ATTR) ? get_parent()->get_tag_name() : mTagName); 
  } 
 
  //! Returns the name of the attribute node. 
  string get_attr_name() 
  { 
    //  Only works for fake ATTR nodes 
    return ((mNodeType == XML_ATTR) ? mTagName : ""); 
  } 
 
  //! Creates and returns an array of new nodes; they will not be 
  //! added as proper children to the parent node, but the parent 
  //! link in the nodes are set so that upwards traversal is made 
  //! possible. 
  array(Node) get_attribute_nodes() 
  { 
    Node   node; 
    int       node_num; 
 
    //  Return if already computed 
    if (mAttrNodes) 
      return (mAttrNodes); 
 
    //  Only applicable for XML_ROOT and XML_ELEMENT 
    if ((mNodeType != XML_ROOT) && (mNodeType != XML_ELEMENT)) 
      return ({ }); 
 
    //  After creating these nodes we need to give them node numbers 
    //  which harmonize with the existing numbers. Fortunately we 
    //  inserted a gap in the series when first numbering the original 
    //  nodes. 
    mAttrNodes = ({ }); 
    node_num = get_doc_order() + 1; 
    foreach(indices(mAttributes), string attr) { 
      node = AttributeNode(attr, mAttributes[attr]); 
      node->set_parent(this); 
      node->set_doc_order(node_num++); 
      mAttrNodes += ({ node }); 
    } 
    return (mAttrNodes); 
  } 
 
  //  Override AbstractNode::`[] 
  protected Node `[](string|int pos) 
  { 
    //  If string indexing we find attributes which match the string 
    if (stringp(pos)) { 
      //  Make sure attribute node list is instantiated 
      array(Node) attr = get_attribute_nodes(); 
 
      //  Find attribute name 
      foreach(attr, Node n) 
        if (n->get_attr_name() == pos) 
          return (n); 
      return (0); 
    } else 
      //  Call inherited method 
      return (AbstractNode::`[](pos)); 
  } 
} 
 
string report_error_context(string data, int ofs) 
{ 
  string pre = reverse(data[..ofs - 1]); 
  string post = data[ofs..]; 
  sscanf(pre, "%s\n", pre); 
  pre = reverse(pre); 
  sscanf(post, "%s\n", post); 
  return "\nContext: " + pre + post + "\n"; 
} 
 
// Used for debugging... 
class WrappedSimple 
{ 
  inherit .Simple; 
 
  string lookup_entity(string entity) 
  { 
    string ret = ::lookup_entity(entity); 
    werror("lookup_entity(%O) ==> %O\n", entity, ret); 
    return ret; 
  } 
 
  void define_entity_raw(string entity, string raw) 
  { 
    werror("define_entity_raw(%O, %O)\n", entity, raw); 
    ::define_entity_raw(entity, raw); 
  } 
} 
 
//! Mixin for parsing XML. 
//! 
//! Uses @[Parser.XML.Simple] to perform 
//! the actual parsing. 
class XMLParser 
{ 
  this_program add_child(this_program); 
  void create(int, string, mapping, string); 
 
  this_program doctype_node; 
 
  protected ADT.Stack container_stack = ADT.Stack(); 
 
  void parse(string data, 
             void|mapping predefined_entities, 
             ParseFlags|void flags, 
             string|void default_namespace) 
  { 
    //.Simple xp = WrappedSimple(); 
    .Simple xp = .Simple(); 
 
    if (!(flags & PARSE_DISALLOW_RXML_ENTITIES)) 
      xp->allow_rxml_entities(1); 
 
    if (flags & PARSE_COMPAT_ALLOW_ERRORS_7_2) 
      xp->compat_allow_errors ("7.2"); 
    else if (flags & PARSE_COMPAT_ALLOW_ERRORS_7_6) 
      xp->compat_allow_errors ("7.6"); 
 
    //  Init parser with predefined entities 
    if (predefined_entities) 
      foreach(indices(predefined_entities), string entity) 
        xp->define_entity_raw(entity, predefined_entities[entity]); 
 
    // Construct tree from string 
    mixed err = catch 
    { 
      mapping(string:mixed) extras = ([]); 
      if (flags & PARSE_FORCE_LOWERCASE) { 
        extras->force_lc = 1; 
      } 
      if (flags & PARSE_ENABLE_NAMESPACES) { 
        extras->xmlns = XMLNSParser(); 
        if (default_namespace) { 
          // Set the default namespace. 
          extras->xmlns->namespace_stack->top()[0] = default_namespace; 
        } 
      } 
      catch( data=xp->autoconvert(data) ); 
      container_stack = ADT.Stack(); 
      foreach(xp->parse(data, parse_xml_callback, 
                        sizeof(extras) && extras), 
              this_program child) 
        add_child(child); 
    }; 
 
    if(err) 
    { 
      //  If string msg is found we propagate the error. If error message 
      //  contains " [Offset: 4711]" we add the input data to the string. 
      if (stringp(err) && (flags & PARSE_WANT_ERROR_CONTEXT)) 
      { 
        if (sscanf(err, "%s [Offset: %d]", err, int ofs) == 2) 
          err += report_error_context(data, ofs); 
      } 
      throw(err); 
    } 
  } 
 
  //! Factory for creating nodes. 
  //! 
  //! @param type 
  //!   Type of node to create. One of: 
  //!   @int 
  //!     @value XML_TEXT 
  //!       XML text. @[text] contains a string with the text. 
  //!     @value XML_COMMENT 
  //!       XML comment. @[text] contains a string with the comment text. 
  //!     @value XML_HEADER 
  //!       @tt{<?xml?>@}-header @[attr] contains a mapping with 
  //!       the attributes. 
  //!     @value XML_PI 
  //!       XML processing instruction. @[name] contains the name of the 
  //!       processing instruction and @[text] the remainder. 
  //!     @value XML_ELEMENT 
  //!       XML element tag. @[name] contains the name of the tag and 
  //!       @[attr] the attributes. 
  //!     @value XML_DOCTYPE 
  //!     @value DTD_ENTITY 
  //!     @value DTD_ELEMENT 
  //!     @value DTD_ATTLIST 
  //!     @value DTD_NOTATION 
  //!       DTD information. 
  //!   @endint 
  //! 
  //! @param name 
  //!   Name of the tag if applicable. 
  //! 
  //! @param attr 
  //!   Attributes for the tag if applicable. 
  //! 
  //! @param text 
  //!   Contained text of the tab if any. 
  //! 
  //! This function is called during parsning to create the various 
  //! XML nodes. 
  //! 
  //! Overload this function to provide application-specific XML nodes. 
  //! 
  //! @returns 
  //!   Returns a node object representing the XML tag, 
  //!   or @expr{0@} (zero) if the subtree rooted in the 
  //!   tag should be cut. 
  //! 
  //! @note 
  //!   This function is not available in Pike 7.6 and earlier. 
  //! 
  //! @seealso 
  //!   @[node_factory_dispatch()], @[AbstractSimpleNode()->node_factory()] 
  protected AbstractSimpleNode node_factory(int type, string name, 
                                            mapping attr, string text); 
 
  //! Dispatcher of @[node_factory()]. 
  //! 
  //! This function finds a suitable @[node_factory()] given the 
  //! current parser context to call with the same arguments. 
  protected AbstractSimpleNode node_factory_dispatch(int type, string name, 
                                                     mapping attr, string text) 
  { 
    foreach(reverse(values(container_stack)), AbstractNode n) { 
      if (!n || !n->node_factory) continue; 
      AbstractSimpleNode res = n->node_factory(type, name, attr, text); 
      if (!undefinedp(res)) return res; 
    } 
    return node_factory(type, name, attr, text); 
  } 
 
  protected AbstractSimpleNode|int(0..0) 
    parse_xml_callback(string type, string name, 
                       mapping attr, string|array contents, 
                       mixed location, mixed ...extra) 
  { 
    AbstractSimpleNode node; 
    mapping short_attr = attr; 
 
    switch (type) { 
    case "": 
    case "<![CDATA[": 
      //  Create text node 
      return node_factory_dispatch(XML_TEXT, "", 0, contents); 
 
    case "<!--": 
      //  Create comment node 
      return node_factory_dispatch(XML_COMMENT, "", 0, contents); 
 
    case "<?xml": 
      //  XML header tag 
      return node_factory_dispatch(XML_HEADER, "", attr, ""); 
 
    case "<!ENTITY": 
      return node_factory_dispatch(DTD_ENTITY, name, attr, contents); 
    case "<!ELEMENT": 
      return node_factory_dispatch(DTD_ELEMENT, name, 0, contents); 
    case "<!ATTLIST": 
      return node_factory_dispatch(DTD_ATTLIST, name, attr, contents); 
    case "<!NOTATION": 
      return node_factory_dispatch(DTD_NOTATION, name, attr, contents); 
    case "<!DOCTYPE": 
      return node_factory_dispatch(XML_DOCTYPE, name, attr, contents); 
 
    case "<?": 
      //  XML processing instruction 
      return node_factory_dispatch(XML_PI, name, attr, contents); 
 
    case "<>": 
      //  Create new tag node. 
      if (arrayp(extra) && sizeof(extra) && mappingp(extra[0])) { 
        //  Convert tag and attribute names to lowercase 
        //  if requested. 
        if (extra[0]->force_lc) { 
          name = lower_case(name); 
          attr = mkmapping(map(indices(attr), lower_case), 
                           values(attr)); 
          short_attr = attr; 
        } 
        //  Parse namespace information of available. 
        if (extra[0]->xmlns) { 
          XMLNSParser xmlns = extra[0]->xmlns; 
          attr = xmlns->Enter(attr); 
          name = xmlns->Decode(name); 
          xmlns->Leave(); 
          short_attr = UNDEFINED; 
        } 
      } 
      node = node_factory_dispatch(XML_ELEMENT, name, attr, ""); 
      if (node && short_attr) node->set_short_attributes(short_attr); 
      return node; 
 
    case ">": 
      //  Create tree node for this container 
      if (arrayp(extra) && sizeof(extra) && mappingp(extra[0])) { 
        //  Convert tag and attribute names to lowercase 
        //  if requested. 
        if (extra[0]->force_lc) { 
          name = lower_case(name); 
          attr = mkmapping(map(indices(attr), lower_case), values(attr)); 
          short_attr = attr; 
        } 
        //  Parse namespace information if available. 
        if (extra[0]->xmlns) { 
          XMLNSParser xmlns = extra[0]->xmlns; 
          name = xmlns->Decode(name); 
          attr = mkmapping(map(indices(attr), xmlns->Decode), values(attr)); 
          xmlns->Leave(); 
          short_attr = UNDEFINED; 
        } 
      } 
      node = container_stack->pop(); 
 
      if (node) { 
        // FIXME: Validate that the node has the expected content. 
 
        //  Add children to our tree node. We need to merge consecutive text 
        //  children since two text elements can't be neighbors according to 
        //  the W3 spec. This is necessary since CDATA sections are 
        //  converted to text nodes which might need to be concatenated 
        //  with neighboring text nodes. 
        Node text_node; 
        int(0..1) modified; 
 
        if (short_attr) node->set_short_attributes(short_attr); 
 
        foreach(contents; int i; Node child) { 
          if (child->get_node_type() == XML_TEXT) { 
            if (text_node) { 
              //  Add this text string to the previous text node. 
              text_node->_add_to_text (child->get_text()); 
              contents[i]=0; 
              modified=1; 
            } 
            else 
              text_node = child; 
          } else 
            text_node = 0; 
        } 
 
        if( modified ) 
          contents -= ({ 0 }); 
        node->replace_children( contents ); 
      } 
      return (node); 
 
    case "error": 
      //  Error message present in contents. If "location" is present in the 
      //  "extra" mapping we encode that value in the message string so the 
      //  handler for this throw() can display the proper error context. 
      if (name) { 
        // We append the name of the tag that caused the error to be triggered. 
        contents += sprintf(" [Tag: %O]", name); 
      } 
      if (location && mappingp(location)) 
        throw_error(contents + " [Offset: " + location->location + "]\n"); 
      else 
        throw_error(contents + "\n"); 
 
    case "<": 
      if (arrayp(extra) && sizeof(extra) && mappingp(extra[0]) && 
          extra[0]->xmlns) { 
        XMLNSParser xmlns = extra[0]->xmlns; 
        attr = xmlns->Enter(attr); 
        name = xmlns->Decode(name); 
      } 
      container_stack->push(node_factory_dispatch(XML_ELEMENT, name, attr, "")); 
      return 0; 
 
    default: 
      // werror("Unknown XML type: %O: %O, %O\n", type, attr, contents); 
      return 0; 
    } 
  } 
} 
 
// 
// --- Compatibility below this point 
// 
 
//! Takes an XML string and produces a @[SimpleNode] tree. 
SimpleRootNode simple_parse_input(string data, 
                                  void|mapping predefined_entities, 
                                  ParseFlags|void flags, 
                                  string|void default_namespace) 
{ 
  return SimpleRootNode(data, predefined_entities, flags, 
                        default_namespace); 
} 
 
//! Loads the XML file @[path], creates a @[SimpleNode] tree representation and 
//! returns the root node. 
SimpleRootNode simple_parse_file(string path, 
                                 void|mapping predefined_entities, 
                                 ParseFlags|void flags, 
                                 string|void default_namespace) 
{ 
  Stdio.File  file = Stdio.File(path, "r"); 
  string      data; 
 
  //  Try loading file and parse its contents 
  if(catch { 
    data = file->read(); 
    file->close(); 
  }) 
    throw_error("Could not read XML file %O.\n", path); 
  else 
    return simple_parse_input(data, predefined_entities, flags, 
                              default_namespace); 
} 
 
//! Takes an XML string and produces a node tree. 
//! 
//! @note 
//! @[flags] is not used for @[PARSE_WANT_ERROR_CONTEXT], 
//! @[PARSE_FORCE_LOWERCASE] or @[PARSE_ENABLE_NAMESPACES] since they 
//! are covered by the separate flag arguments. 
RootNode parse_input(string data, void|int(0..1) no_fallback, 
                     void|int(0..1) force_lowercase, 
                     void|mapping(string:string) predefined_entities, 
                     void|int(0..1) parse_namespaces, 
                     ParseFlags|void flags) 
{ 
    if(no_fallback) 
        flags |= PARSE_WANT_ERROR_CONTEXT; 
 
    if(force_lowercase) 
        flags |= PARSE_FORCE_LOWERCASE; 
 
    if(parse_namespaces) 
        flags |= PARSE_ENABLE_NAMESPACES; 
 
    return RootNode(data, predefined_entities, flags); 
} 
 
//! Loads the XML file @[path], creates a node tree representation and 
//! returns the root node. 
Node parse_file(string path, int(0..1)|void parse_namespaces) 
{ 
  Stdio.File  file = Stdio.File(path, "r"); 
  string      data; 
 
  //  Try loading file and parse its contents 
  if(catch { 
    data = file->read(); 
    file->close(); 
  }) 
    throw_error("Could not read XML file %O.\n", path); 
  else 
    return parse_input(data, 0, 0, 0, parse_namespaces); 
} 
 
protected class DTDElementHelper 
{ 
  array expression; 
  array get_expression() 
  { 
    return expression; 
  } 
 
  void low_render_expression(String.Buffer data, array|string expr) 
  { 
    if (stringp(expr)) { 
      data->add(expr); 
    } else if ((<"?", "*", "+">)[expr[0]]) { 
      // Postfix operator. 
      low_render_expression(data, expr[1]); 
      data->add(expr[0]); 
    } else if (expr[0] == "#PCDATA") { 
      // Special case... 
      if (sizeof(expr) == 1) { 
        data->add("(#PCDATA)"); 
      } else { 
        data->add("(#PCDATA"); 
        foreach(expr[1..], string e) { 
          data->add(" | ", e); 
        } 
        data->add(")*"); 
      } 
    } else { 
      // Infix operator. 
      data->add("("); 
      foreach(expr[1..]; int ind; array|string e) { 
        if (ind) data->add(" ", expr[0], " "); 
        low_render_expression(data, e); 
      } 
      data->add(")"); 
    } 
  } 
 
  void render_expression(String.Buffer data) 
  { 
    low_render_expression(data, expression); 
  } 
} 
 
// Convenience stuff for creation of @[SimpleNode]s. 
 
//! The root node of an XML-tree consisting of @[SimpleNode]s. 
class SimpleRootNode 
{ 
  inherit SimpleNode; 
  inherit XMLParser; 
 
  protected mapping(string:SimpleElementNode) node_ids; 
 
  //! Find the element with the specified id. 
  //! 
  //! @param id 
  //!   The XML id of the node to search for. 
  //! 
  //! @param force 
  //!   Force a regeneration of the id lookup cache. 
  //!   Needed the first time after the node tree has been 
  //!   modified by adding or removing element nodes, or 
  //!   by changing the id attribute of an element node. 
  //! 
  //! @returns 
  //!   Returns the element node with the specified id 
  //!   if any. Returns @[UNDEFINED] otherwise. 
  //! 
  //! @seealso 
  //!   @[flush_node_id_cache] 
  SimpleElementNode get_element_by_id(string id, int|void force) 
  { 
    if (!node_ids || force) { 
      mapping(string:SimpleNode) new_lookup = ([]); 
      walk_preorder(lambda(SimpleNode node, 
                           mapping(string:SimpleElementNode) new_lookup) { 
                      if (node->get_node_type() != XML_ELEMENT) return 0; 
                      if (string id = node->get_attributes()->id) { 
                        if (new_lookup[id]) { 
                          error("Multiple nodes with the same id " 
                                "(id: %O, nodes: %O, %O)\n", 
                                id, new_lookup[id], node); 
                        } 
                        new_lookup[id] = node; 
                      } 
                      return 0; 
                    }, new_lookup); 
      node_ids = new_lookup; 
    } 
    return node_ids[id]; 
  } 
 
  void flush_node_id_cache() 
  //! Clears the node id cache built and used by @[get_element_by_id]. 
  { 
    node_ids = 0; 
  } 
 
  protected SimpleNode low_clone() 
  { 
    return SimpleRootNode(); 
  } 
 
  protected SimpleNode node_factory(int type, string name, 
                                    mapping attr, string|array text) 
  { 
    switch(type) { 
    case XML_TEXT: return SimpleTextNode(text); 
    case XML_COMMENT: return SimpleCommentNode(text); 
    case XML_HEADER: return SimpleHeaderNode(attr); 
    case XML_PI: return SimplePINode(name, attr, text); 
    case XML_ELEMENT: return SimpleElementNode(name, attr); 
    case XML_DOCTYPE: return SimpleDoctypeNode(name, attr, text); 
    case DTD_ENTITY: return SimpleDTDEntityNode(name, attr, text); 
    case DTD_ELEMENT: return SimpleDTDElementNode(name, text); 
    case DTD_ATTLIST: return SimpleDTDAttlistNode(name, attr, text); 
    case DTD_NOTATION: return SimpleDTDNotationNode(name, attr, text); 
    default: return SimpleNode(type, name, attr, text); 
    } 
  } 
 
  //! 
  protected void create(string|void data, 
                     mapping|void predefined_entities, 
                     ParseFlags|void flags, 
                     string|void default_namespace) 
  { 
    ::create(XML_ROOT, "", 0, ""); 
    if (data) { 
      parse(data, predefined_entities, flags, default_namespace); 
    } 
  } 
} 
 
//! 
class SimpleTextNode 
{ 
  inherit SimpleNode; 
  protected SimpleNode low_clone() 
  { 
    return SimpleTextNode(get_text()); 
  } 
 
  //! 
  protected void create(string text) 
  { 
    ::create(XML_TEXT, "", 0, text); 
  } 
} 
 
//! 
class SimpleCommentNode 
{ 
  inherit SimpleNode; 
  protected SimpleNode low_clone() 
  { 
    return SimpleCommentNode(get_text()); 
  } 
 
  //! 
  protected void create(string comment) 
  { 
    ::create(XML_COMMENT, "", 0, comment); 
  } 
} 
 
//! 
class SimpleHeaderNode 
{ 
  inherit SimpleNode; 
  protected SimpleNode low_clone() 
  { 
    return SimpleHeaderNode(get_attributes()); 
  } 
 
  //! 
  protected void create(mapping(string:string) attrs) 
  { 
    ::create(XML_HEADER, "", attrs, ""); 
  } 
} 
 
//! 
class SimplePINode 
{ 
  inherit SimpleNode; 
  protected SimpleNode low_clone() 
  { 
    return SimplePINode(get_full_name(), get_attributes(), get_text()); 
  } 
 
  //! 
  protected void create(string name, mapping(string:string) attrs, 
                     string contents) 
  { 
    ::create(XML_PI, name, attrs, contents); 
  } 
} 
 
//! 
class SimpleDoctypeNode 
{ 
  inherit SimpleNode; 
  protected SimpleNode low_clone() 
  { 
    return SimpleDoctypeNode(get_full_name(), get_attributes(), 0); 
  } 
 
  //! 
  protected void create(string name, mapping(string:string) attrs, 
                     array contents) 
  { 
    ::create(XML_DOCTYPE, name, attrs, ""); 
    if (contents) { 
      replace_children(contents); 
    } 
  } 
} 
 
//! 
class SimpleDTDEntityNode 
{ 
  inherit SimpleNode; 
  protected SimpleNode low_clone() 
  { 
    return SimpleDTDEntityNode(get_full_name(), get_attributes(), get_text()); 
  } 
 
  //! 
  protected void create(string name, mapping(string:string) attrs, 
                     string contents) 
  { 
    ::create(DTD_ENTITY, name, attrs, contents); 
  } 
} 
 
//! 
class SimpleDTDElementNode 
{ 
  inherit SimpleNode; 
  inherit DTDElementHelper; 
 
  protected SimpleNode low_clone() 
  { 
    return SimpleDTDElementNode(get_full_name(), get_expression()); 
  } 
 
  //! 
  protected void create(string name, array expression) 
  { 
    this::expression = expression; 
    ::create(DTD_ELEMENT, name, 0, ""); 
  } 
} 
 
//! 
class SimpleDTDAttlistNode 
{ 
  inherit SimpleNode; 
  protected SimpleNode low_clone() 
  { 
    return SimpleDTDAttlistNode(get_full_name(), get_attributes(), get_text()); 
  } 
 
  //! 
  protected void create(string name, mapping(string:string) attrs, 
                     string contents) 
  { 
    ::create(DTD_ATTLIST, name, attrs, contents); 
  } 
} 
 
//! 
class SimpleDTDNotationNode 
{ 
  inherit SimpleNode; 
  protected SimpleNode low_clone() 
  { 
    return SimpleDTDNotationNode(get_full_name(), get_attributes(), get_text()); 
  } 
 
  //! 
  protected void create(string name, mapping(string:string) attrs, 
                     string contents) 
  { 
    ::create(DTD_NOTATION, name, attrs, contents); 
  } 
} 
 
//! 
class SimpleElementNode 
{ 
  inherit SimpleNode; 
  protected SimpleNode low_clone() 
  { 
    return SimpleElementNode(get_full_name(), get_attributes()); 
  } 
 
  //! 
  protected void create(string name, mapping(string:string) attrs) 
  { 
    ::create(XML_ELEMENT, name, attrs, ""); 
  } 
} 
 
// Convenience stuff for creation of @[Node]s. 
 
//! The root node of an XML-tree consisting of @[Node]s. 
class RootNode 
{ 
  inherit Node; 
  inherit XMLParser; 
 
  protected mapping(string:ElementNode) node_ids; 
 
  //! Find the element with the specified id. 
  //! 
  //! @param id 
  //!   The XML id of the node to search for. 
  //! 
  //! @param force 
  //!   Force a regeneration of the id lookup cache. 
  //!   Needed the first time after the node tree has been 
  //!   modified by adding or removing element nodes, or 
  //!   by changing the id attribute of an element node. 
  //! 
  //! @returns 
  //!   Returns the element node with the specified id 
  //!   if any. Returns @[UNDEFINED] otherwise. 
  //! 
  //! @seealso 
  //!   @[flush_node_id_cache] 
  ElementNode get_element_by_id(string id, int|void force) 
  { 
    if (!node_ids || force) { 
      mapping(string:ElementNode) new_lookup = ([]); 
      walk_preorder(lambda(SimpleNode node, 
                           mapping(string:ElementNode) new_lookup) { 
                      if (node->get_node_type() != XML_ELEMENT) return 0; 
                      if (string id = node->get_attributes()->id) { 
                        if (new_lookup[id]) { 
                          error("Multiple nodes with the same id " 
                                "(id: %O, nodes: %O, %O)\n", 
                                id, new_lookup[id], node); 
                        } 
                        new_lookup[id] = node; 
                      } 
                      return 0; 
                    }, new_lookup); 
      node_ids = new_lookup; 
    } 
    return node_ids[id]; 
  } 
 
  void flush_node_id_cache() 
  //! Clears the node id cache built and used by @[get_element_by_id]. 
  { 
    node_ids = 0; 
  } 
 
  protected Node low_clone() 
  { 
    return RootNode(); 
  } 
 
  protected Node node_factory(int type, string name, 
                              mapping attr, string|array text) 
  { 
    switch(type) { 
    case XML_TEXT: return TextNode(text); 
    case XML_COMMENT: return CommentNode(text); 
    case XML_HEADER: return HeaderNode(attr); 
    case XML_PI: return PINode(name, attr, text); 
    case XML_ELEMENT: return ElementNode(name, attr); 
    case XML_DOCTYPE: return DoctypeNode(name, attr, text); 
    case DTD_ENTITY: return DTDEntityNode(name, attr, text); 
    case DTD_ELEMENT: return DTDElementNode(name, text); 
    case DTD_ATTLIST: return DTDAttlistNode(name, attr, text); 
    case DTD_NOTATION: return DTDNotationNode(name, attr, text); 
    default: return Node(type, name, attr, text); 
    } 
  } 
 
  //! 
  protected void create(string|void data, 
                     mapping|void predefined_entities, 
                     ParseFlags|void flags) 
  { 
    ::create(XML_ROOT, "", 0, ""); 
    if (data) { 
      parse(data, predefined_entities, flags); 
    } 
  } 
} 
 
//! 
class TextNode 
{ 
  inherit Node; 
  protected Node low_clone() 
  { 
    return TextNode(get_text()); 
  } 
 
  //! 
  protected void create(string text) 
  { 
    ::create(XML_TEXT, "", 0, text); 
  } 
} 
 
//! 
class CommentNode 
{ 
  inherit Node; 
  protected Node low_clone() 
  { 
    return CommentNode(get_text()); 
  } 
 
  //! 
  protected void create(string text) 
  { 
    ::create(XML_COMMENT, "", 0, text); 
  } 
} 
 
//! 
class HeaderNode 
{ 
  inherit Node; 
  protected Node low_clone() 
  { 
    return HeaderNode(get_attributes()); 
  } 
 
  //! 
  protected void create(mapping(string:string) attrs) 
  { 
    ::create(XML_HEADER, "", attrs, ""); 
  } 
} 
 
//! 
class PINode 
{ 
  inherit Node; 
  protected Node low_clone() 
  { 
    return PINode(get_full_name(), get_attributes(), get_text()); 
  } 
 
  //! 
  protected void create(string name, mapping(string:string) attrs, 
                     string contents) 
  { 
    ::create(XML_PI, name, attrs, contents); 
  } 
} 
 
//! 
class DoctypeNode 
{ 
  inherit Node; 
  protected Node low_clone() 
  { 
    return DoctypeNode(get_full_name(), get_attributes(), 0); 
  } 
 
  //! 
  protected void create(string name, mapping(string:string) attrs, 
                     array contents) 
  { 
    ::create(XML_DOCTYPE, name, attrs, ""); 
    if (contents) { 
      replace_children(contents); 
    } 
  } 
} 
 
//! 
class DTDEntityNode 
{ 
  inherit Node; 
  protected Node low_clone() 
  { 
    return DTDEntityNode(get_full_name(), get_attributes(), get_text()); 
  } 
 
  //! 
  protected void create(string name, mapping(string:string) attrs, 
                     string contents) 
  { 
    ::create(DTD_ENTITY, name, attrs, contents); 
  } 
} 
 
//! 
class DTDElementNode 
{ 
  inherit Node; 
  inherit DTDElementHelper; 
 
  protected Node low_clone() 
  { 
    return DTDElementNode(get_full_name(), get_expression()); 
  } 
 
  //! 
  protected void create(string name, array expression) 
  { 
    this::expression = expression; 
    ::create(DTD_ELEMENT, name, 0, ""); 
  } 
} 
 
//! 
class DTDAttlistNode 
{ 
  inherit Node; 
  protected Node low_clone() 
  { 
    return DTDAttlistNode(get_full_name(), get_attributes(), get_text()); 
  } 
 
  //! 
  protected void create(string name, mapping(string:string) attrs, 
                     string contents) 
  { 
    ::create(DTD_ATTLIST, name, attrs, contents); 
  } 
} 
 
//! 
class DTDNotationNode 
{ 
  inherit Node; 
  protected Node low_clone() 
  { 
    return DTDNotationNode(get_full_name(), get_attributes(), get_text()); 
  } 
 
  //! 
  protected void create(string name, mapping(string:string) attrs, 
                     string contents) 
  { 
    ::create(DTD_NOTATION, name, attrs, contents); 
  } 
} 
 
//! 
class ElementNode 
{ 
  inherit Node; 
  protected Node low_clone() 
  { 
    return ElementNode(get_full_name(), get_attributes()); 
  } 
 
  //! 
  protected void create(string name, mapping(string:string) attrs) 
  { 
    ::create(XML_ELEMENT, name, attrs, ""); 
  } 
} 
 
//! 
class AttributeNode 
{ 
  inherit Node; 
  protected Node low_clone() 
  { 
    return AttributeNode(get_full_name(), get_text()); 
  } 
 
  //! 
  protected void create(string name, string value) 
  { 
    ::create(XML_ATTR, name, 0, value); 
  } 
}