This file is indexed.

/usr/share/pyshared/xe.py is in python-xe 0.7.4-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
# xe -- XML elements Python classes

# This is the BSD license. For more information, see:
# http://www.opensource.org/licenses/bsd-license.php
#
# Copyright (c) 2006, Steve R. Hastings
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
# 
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in
#       the documentation and/or other materials provided with the
#       distribution.
# 
#     * Neither the name of Steve R. Hastings nor the names
#       of any contributors may be used to endorse or promote products
#       derived from this software without specific prior written
#       permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



"""
Classes to work with XML elements in a Pythonic way.

This library module contains classes to represent XML elements.

Some important classes:

    TextElement  an XML element that contains text data
    NestElement  an XML element that contains other XML elements
    Element      can act like a TextElement or a NestElement
    XMLDoc       a container for XML element items

    ListElement  an XML element that holds 0 or more elements of a type
    Collection   a container that holds 0 or more elements of a type

    XMLText      a container for pieces of text not in an element

When you are nesting elements inside other elements, you give each
nested element a name; it's usually best to use the tag name value as
the name of the nested element.

Most of this module is designed for working with structured XML data,
such as syndication feed files.  To work with unstructured data, you
will probably want to use a Collection of ElementItem; see the XMLText
class for more information.


Please send questions, comments, and bug reports to: xe@langri.com
"""


import types



module_name = "xe"
module_version = "0.7.4"
module_banner = "%s version %s" % (module_name, module_version)



def set_indent_str(s):
    """
    Set the default string used to indent tags.

    Arguments:
        s -- string to use as the new tag indent string.

    The default indent is a single tab character.
    """
    global s_indent
    global lst_indent
    s_indent = s
    lst_indent = [s_indent*i for i in range(25)]

set_indent_str("\t")



class TFC(object):
    """
    TFC stands for "Tag Format Control".
    A TFC controls how tags are converted to strings.

    Arguments to __init__():
        level  Specifies what indent level to start at.  Default 0.
        mode   Specifies how to format the output:
            mode_terse -- minimal output (not indented)
            mode_normal -- default output (indented)
            mode_verbose -- output as much information as possible

    Normally, if a nested XML item has no data, it will be left out of the
    tag string; but with mode_verbose you will get an empty compact tag.
    For example, if a foo tag contains a nested bar tag, and the bar tag
    is empty, with mode_verbose you will get:
    <foo>
        <bar/>
    </foo>

    With mode_normal or mode_terse, you would just get: "<foo/>"

    With mode_verbose on a collection, you get a comment similar to this:
        <!-- collection of Author with 0 elements -->

    Methods:
        show_all()
            Return True if TFC set to make a tag string even if the item
            is blank.
        terse()
            Return True if TFC set for terse tag strings.
        verbose()
            Return True if TFC set for verbose tag strings.

        indent_by(incr)
            Return a TFC instance that indents by incr levels.
        s_indent(extra_indent=0)
            Return an indent string.
    """
    mode_terse, mode_normal, mode_verbose = range(3)

    def __init__(self, level=None, mode=None, tfc=None):
        """
        Arguments:
            level
                Indent level at which to start.  Default: 0
            mode
                How to format the output.  Default: mode_normal
                    mode_terse -- minimal output (not indented)
                    mode_normal -- default output (indented)
                    mode_verbose -- output as much information as possible
            tfc
                If provided, initialize this TFC from specified tfc.

        See the doc string for the whole class for examples what of
        mode_verbose does for tag output.
        """
        if tfc is not None:
            # copy settings from another TFC
            self.level = tfc.level
            self.mode = tfc.mode
            self.attr_sep = tfc.attr_sep
            self.tag_sep = tfc.tag_sep
        else:
            # set defaults
            self.level = 0
            self.mode = TFC.mode_normal
            self.attr_sep = " "
            self.tag_sep = "\n"

        # override defaults if arguments specified
        if level is not None:
            self.level = level
        if mode is not None:
            self.mode = mode

    def show_all(self):
        """
        Return True if TFC is set to show even empty elements.

        Empty tags usually just don't appear in a tag string; but we
        want a tag string even for an empty tag if the current level
        is 0, so that if you print a tag you don't ever just get an
        empty string.

        And if the mode is mode_verbose, of course we always get tag
        strings for everything.

        The tag string code uses this to decide when to return a tag
        string for an empty element, and when to return just an empty
        string.
        """
        return self.level == 0 or self.mode == TFC.mode_verbose

    def terse(self):
        """
        Return True if TFC set for terse tag strings.

        Terse means there will be no indenting.
        """
        return self.mode == TFC.mode_terse

    def verbose(self):
        """
        Return True if TFC set for verbose printing.

        Normally, if an XML item has no data, nothing is printed, but with
        mode_verbose you will get an empty compact tag for blank elements.
        For a collection you will get a comment similar to this:
            <!-- collection of Author with 0 elements -->
        """
        return self.mode == TFC.mode_verbose

    def indent_by(self, incr):
        """
        Return a TFC instance that indents by incr levels.  The mode will
        be unchanged.

        Pass this to a function that takes a TFC to get a temporary indent.
        """
        return TFC(level=self.level + incr, tfc=self)

    def s_indent(self, extra_indent=0):
        """
        Return an indent string.

        Return a string of white space that indents correctly for the 
        current TFC settings.  If specified, extra_indent will be added
        to the current indent level.
        """
        if self.mode == TFC.mode_terse:
            return ""
        level = self.level + extra_indent

        try:
            return lst_indent[level]
        except IndexError:
            return s_indent * level

    def attr_join(self, lst):
        if self.terse():
            sep = self.attr_sep
        else:
            # multiline attributes are treated much like tags: they are
            # put on multiple lines, indented.
            sep = self.tag_sep + self.s_indent(extra_indent=2)
        return sep.join(lst)

    def tag_join(self, lst):
        return self.tag_sep.join(lst)




# Here are all of the possible XML items.
#
# Supported by xe:
# XML Declaration: <?xml ... ?>
# Comments: <!-- ... -->
# Elements: <tag_name>...</tag_name>
#
# Minimal support:
# Markup Declarations: <!KEYWORD ... >
# Processing Instructions (PIs): <?KEYWORD ... ?>
# CDATA sections: <![CDATA[ ... ]]>
#
# Not currently supported:
# INCLUDE and IGNORE directives: <!KEYWORD[ ... ]]>


class XMLItem(object):
    """
    Abstract base class for all xe classes that represent XML.

    All xe classes that work with XML data inherit from this
    class.  All it does is provide a few default methods, and be a root
    for the inheritance tree.

    An XMLItem has several methods that return an XML tag representation
    of its contents.  Each XMLItem knows how to make an XML string
    representation of itself (its "tag string").  An XMLItem that
    contains other XMLItems will ask each one to make a tag string; so
    asking the top-level XMLItem for a tag string will cause the entire
    tree of XMLItems to recursively make tag strings, and you get a full
    XML representation with tags appropriately nested and indented.
    """
    def _tag_not_visible(self, tfc):
        if tfc.show_all():
            return False
        return not self

    def _s_tag(self, tfc):
        """
        A stub which must always be overridden by child classes.
        """
        assert False, "XMLItem is an abstract class with no tag strings."

    def s_tag(self, level=0):
        """
        Return a tag string.

        The XML tag string will be indented.

        If the item is empty, and it's not a top-level item (level is
        not 0), an empty string ("") will be returned.  If it is a
        top-level item, an empty compact tag string will be returned,
        like this:  <tagname/>
        """
        tfc = TFC(level, TFC.mode_normal)
        return self._s_tag(tfc)

    def s_tag_verbose(self, level=0):
        """
        Return a tag string showing all possible information.

        The XML tag will be indented.  All empty elements will have
        empty compact tag strings like this:  <tagname/>

        Empty Collection elements will have an XML Comment describing
        the collection.
        """
        tfc = TFC(level, TFC.mode_verbose)
        return self._s_tag(tfc)

    def s_tag_terse(self, level=0):
        """
        Return a minimal tag string without indentation.

        If the item is empty, and it's not a top-level item (level is
        not 0), an empty string ("") will be returned.  If it is a
        top-level item, an empty compact tag string will be returned,
        like this:  <tagname/>
        """
        tfc = TFC(level, TFC.mode_terse)
        return self._s_tag(tfc)

    def __str__(self):
        return self.s_tag()

    def level(self):
        """
        Return an integer describing what level this tag is.

        The root tag of an XML document is level 0; document-level comments
        or other document-level declarations are also level 0.  Tags nested
        inside the root tag are level 1, tags nested inside those tags are
        level 2, and so on.

        This is currently only used by the debug_tree() functions.  When
        printing tags normally, the code that walks the tree keeps track of
        what level is current.
        """
        level = 0
        while self._parent != None:
            self = self._parent
            if isinstance(self, ElementItem):
                level += 1
        return level

    def s_name(self):
        """
        Return a name for the current item.

        Used only by the debug_tree() functions.
        """
        if self._name:
            return self._name
        return "unnamed_instance_of_" + type(self).__name__

    def debug_tree(self):
        """
        Return a verbose tree showing the current tag and its children.

        This is for debugging; it's not valid XML syntax.
        """
        level = self.level()
        return "%2d) %s -- %s" % (level, self.s_name(), str(self))

    def import_xml(self, source, lst_errors):
        """
        Import XML data from source; log errors to lst_errors.

        Get as much data as possible; any data that is not imported will
        be appended to lst_errors, in text form.
        """
        assert False, "need to overload this to actually work"



class DocItem(XMLItem):
    """
    Abstract class: any XMLItem that can be document-level.

    An XML document has a "root element", with all the XML elements
    nested inside it; but there are some items that can appear outside
    the root element, such as comments and processing instructions.  All
    such items inherit from this class.
    """
    pass



class ElementItem(XMLItem):
    """
    Abstract class: any XMLItem that can be a common element.

    Basically, any XMLItem that can be inside the root element (or
    can BE the root element) will inherit from this class.
    """
    pass



class Comment(DocItem,ElementItem):
    """
    An XML comment.

    Attributes:
        text -- the text of the comment
    """
    def __init__(self, text=""):
        """
        text -- the text of the comment
        """
        self._parent = None
        self._name = ""
        self._direct_types = []
        self.tag_name = "comment"
        self.text = text

    def _s_tag(self, tfc):
        if self._tag_not_visible(tfc):
            return ""

        if self.text == "":
            return tfc.s_indent() + "<!-- -->"

        if self.text.find("\n") >= 0:
            lst = []
            lst.append(tfc.s_indent() + "<!--")
            lst.append(self.text)
            lst.append(tfc.s_indent() + "-->")
            return tfc.tag_join(lst)

        s = "%s%s%s%s" % (tfc.s_indent(), "<!-- ", self.text, " -->")
        return s

    def __nonzero__(self):
        # Returns True if there is any comment text.
        # Returns False otherwise.
        return not not self.text

    def text_check(self):
        pass



class PI(DocItem,ElementItem):
    """
    XML Processing Instruction (PI).

    Attributes:
        keyword
        text
    """
    def __init__(self, keyword, text=""):
        self._parent = None
        self._name = ""
        self._direct_types = []
        self.keyword = keyword
        self.text = ""

    def _s_tag(self, tfc):
        if self._tag_not_visible(tfc):
            return ""

        if self.text.find("\n") >= 0:
            lst = []
            lst.append("%s%s%s" % (tfc.s_indent(), "<?", self.keyword))
            lst.append(self.text)
            lst.append("%s%s" % (tfc.s_indent(), "?>"))
            return tfc.tag_join(lst)

        s = "%s%s%s %s%s"% \
                (tfc.s_indent(), "<?", self.keyword, self.text, "?>")
        return s

    def __nonzero__(self):
        # Returns True if there is any keyword.
        # Returns False otherwise.
        return not not self.keyword
    def text_check(self):
        pass



class MarkupDecl(DocItem):
    """
    XML Markup Declaration.

    Used for <!ENTITY ...>, <!ATTLIST ...>, etc. declarations.

    Attributes:
        keyword
        text
    """
    def __init__(self):
        self._parent = None
        self._name = ""
        self.keyword = ""
        self.text = ""

    def _s_tag(self, tfc):
        if self._tag_not_visible(tfc):
            return ""

        # REVIEW: can I factor out a common _s_tag()?
        if self.text.find("\n") >= 0:
            lst = []
            lst.append("%s%s%s" % (tfc.s_indent(), "<!", self.keyword))
            lst.append(self.text)
            lst.append("%s%s" % (tfc.s_indent(), ">"))
            return tfc.tag_join(lst)

        s = "%s%s%s %s%s" % \
                (tfc.s_indent(), "<!", self.keyword, self.text, ">")
        return s

    def __nonzero__(self):
        # Returns True if there is any keyword.
        # Returns False otherwise.
        return not not self.keyword
    def text_check(self):
        pass



class CDATA(DocItem):
    """
    CDATA declaration.

    Attributes:
        text
    """
    def __init__(self):
        self._parent = None
        self._name = ""
        self.keyword = ""
        self.text = ""

    def _s_tag(self, tfc):
        if self._tag_not_visible(tfc):
            return ""

        s = "%s%s%s%s" % (tfc.s_indent(), "<![CDATA[", self.text, "]]>")
        return s

    def __nonzero__(self):
        # Returns True if there is any keyword.
        # Returns False otherwise.
        return not not self.keyword
    def text_check(self):
        pass



def _assign_compatible(o, value):
    """
    Return True if value is type compatible for assigning to object o.

    value is compatible with object o when:
        * both o and value have the exact same type
        * o is set to None
        * both o and value are string types
    """
    t_o = type(o)
    t_val = type(value)

    return t_o is t_val or \
            t_o is types.NoneType or \
            t_o in types.StringTypes and t_val in types.StringTypes

class Node(ElementItem):
    """
    Abstract base class describing things common to nodes.

    XMLText and all of the XML element classes inherit from this.
    """
    def __init__(self):
        self._lock = False
        self._parent = None
        self._name = ""
        self._direct_types = []
        self._lock = True

    def __delattr__(self, name):
        raise TypeError, "cannot delete elements"

    def __getattr__(self, name):
        if name == "_lock":
            # If the "_lock" hasn't been created yet, we always want it
            # to be False, i.e. we are not locked.
            return False
        else:
            raise AttributeError, name

    def __setattr__(self, name, value):
        # Here's how this works:
        #
        # 0) "self._lock" is a boolean, set to False during __init__()
        # but turned True afterwards.  When it's False, you can add new
        # members to the class instance without any sort of checks; once
        # it's set True, __setattr__() starts checking assignments.
        # By default, when _lock is True, you cannot add a new member to
        # the class instance, and any assignment to an old member has to
        # be of matching type.  So if you say "a.text = string", the
        # .text member has to exist and be a string member.
        #
        # This is the default __setattr__() for all element types.  It
        # gets overloaded by the __setattr__() in NestElement, because
        # for nested elments, it makes sense to be able to add new
        # elements nested inside.
        #
        # This is moderately nice.  But later in class Nest there is a
        # version of __setattr__() that is *very* nice; check it out.
        #
        # 1) This checks assignments to _parent, and makes sure they are
        # plausible (either an XMLItem, or None).

        try:
            _lock = self._lock
        except AttributeError:
            _lock = False

        if not _lock:
            self.__dict__[name] = value
            return

        if not name in self.__dict__:
            # brand-new item
            if _lock:
                raise TypeError, "element cannot nest other elements"

        if name == "_parent":
            if not (isinstance(value, XMLItem) or value is None):
                raise TypeError, "only XMLItem or None is permitted"
            self.__dict__[name] = value
            return

        # locked item so do checks
        if not _assign_compatible(self.__dict__[name], value):
            raise TypeError, \
                    "value's type is not compatible:" + str(type(value))

        self.__dict__[name] = value
        

    def has_contents(self):
        """
        Return True if the contents are not empty.

        Note that the contents could be empty but the attributes might
        not be; an element is only empty if both attributs and contents
        are empty.
        """
        assert False, "Node is an abstract class; it has no contents."

    def multiline_contents(self):
        """
        Return True if the contents do not all fit on one line.
        """
        assert False, "Node is an abstract class; it has no contents."

    def s_contents(self, tfc):
        """
        Return a string with any contents of the element.

        For simple contents, just return the contents.
        If the contents are nested tags, they should be correctly
        indented, so this needs to take a TFC to control the indenting.
        """
        assert False, "Node is an abstract class; it has no contents."

    def direct(self, value):
        """
        Handle direct assign of a value to the element's contents.

        Some elements can handle a direct assign.  Those elements need
        to overload this method and make it do the right thing.
        
        For example, you might be able to assign a time float value to a
        timestamp element.  In that case, the timestamp element needs to
        add types.FloatType to its direct_types list, and add a
        .direct() method that overloads this default one to be able to
        correctly handle a float value.
        """
        assert False, "cannot call Node.direct(); must subclass it"



class XMLText(Node):
    """
    Class to represent simple, bare text in an XML document.

    This is NOT an XML element.  It has no tag name or attributes.

    If you want to try working with unstructured XML data, I suggest you
    create a Collection() of XMLItem() so you can tuck in all the
    XMLText items and XML elements you encounter, without needing to
    invent names for each one.
    """
    def __init__(self, text=""):
        Node.__init__(self)
        self._lock = False
        self.text = text
        self._lock = True

    def __nonzero__(self):
        """
        Return True if there are any contents.

        Return False otherwise.
        """
        return self.text != ""

    def text_check(self):
        pass

    def has_contents(self):
        """
        Return True if the contents are not empty.

        Return False otherwise.
        """
        return self.text != ""

    def multiline_contents(self):
        """
        Return True if the contents do not all fit on one line.
        """
        return self.text.find("\n") >= 0

    def s_contents(self, tfc):
        """
        Return a string with any contents.
        """
        return self.text

    def _s_tag(self, tfc):
        if self.text == "":
            return ""
        return tfc.s_indent() + self.text

    def __str__(self):
        return self.text



# string constants
_s_text = "text"
_s_value = "value"
_s_tf = "tf"
_s_time_offset = "time_offset"



class Attrs(object):
    """
    Class to manage a dictionary of attribute values.

    Remembers in what order the attributes were assigned; when creating
    a string representation of the attributes, they will always appear
    in the same order.
    """
    def __init__(self):
        self._attrs_names = []
        self._attrs_dict = {}

    def __cmp__(self, o):
        return cmp(self._attrs_dict, o._attrs_dict)

    def __getitem__(self, k):
        return self._attrs_dict[k]

    def __setitem__(self, k, value):
        if k not in self._attrs_dict:
            # first time assigned; also update _attrs_names
            self._attrs_names.append(k)
        self._attrs_dict[k] = value

    def __delitem__(self, k):
        self._attrs_names.remove(k)
        del(self._attrs_dict[k])

    def __nonzero__(self):
        for value in self._attrs_dict.values():
            if str(value):
                return True
        return False

    def lst_attrs(self):
        lst = []
        for name in self._attrs_names:
            s_value = str(self._attrs_dict[name])
            if s_value:
                s = '%s="%s"' % (name, s_value)
                lst.append(s)
        return lst

    def print_attrs(self):
        print "Attributes:"
        print "    " + "\n    ".join(self.lst_attrs())

    def set_names(self, attr_names):
        for name in attr_names:
            if name not in self._attrs_dict:
                self.__setitem__(name, "")



class CoreElement(Node):
    """
    Abstract base class describing things common to all XML elements.

    All of the XML element classes inherit from this.
    """
    def __init__(self, tag_name, def_attr_name=None, def_attr_value=None,
            attr_names=[], direct_types=[]):
        """
        Arguments:
            tag_name -- the XML tag name of this item
            def_attr_name -- name of any default attribute this item has
            def_attr_value -- default value of any default attribute
            attr_names -- a list of expected attribute names
            direct_types -- a list of types that can be direct assigned

            attr_names also sets the order in which the attribute names
            will be put in the tag string.

            See the doc string for direct() to learn about direct_types
            and direct assigns.
        """
        Node.__init__(self)
        self._lock = False
        # dictionary of attributes and their values
        self.tag_name = tag_name
        self.attrs = Attrs()
        if def_attr_name:
            self.attrs[def_attr_name] = def_attr_value
        self.attrs.set_names(attr_names)
        self._direct_types = direct_types
        self._lock = True

    def __nonzero__(self):
        """
        Return True if any attrs are set or there are any contents.

        Return False otherwise.
        """
        return self.attrs.__nonzero__() or self.has_contents()

    def has_contents(self):
        """
        Return True if the contents are not empty.

        Note that the contents could be empty but the attributes might
        not be; an element is only empty if both attributs and contents
        are empty.
        """
        assert False, "CoreElement is an abstract class; it has no contents."

    def multiline_contents(self):
        """
        Return True if the contents do not all fit on one line.
        """
        assert False, "CoreElement is an abstract class; it has no contents."

    def s_contents(self, tfc):
        """
        Return a string with any contents of the element.

        For simple contents, just return the contents.
        If the contents are nested tags, they should be correctly
        indented, so this needs to take a TFC to control the indenting.
        """
        assert False, "CoreElement is an abstract class; it has no contents."

    def _s_start_tag_name_attrs(self, tfc):
        """
        Return a string with the start tag name, and any attributes.

        Wrap this in correct angle brackets to get a start tag, or a
        compact tag.
        """
        lst_attrs = self.attrs.lst_attrs()
        if len(lst_attrs) == 0:
            return self.tag_name

        if len(lst_attrs) == 1:
            # just one attr so do on one line
            return "%s %s" % (self.tag_name, lst_attrs[0])

        # more than one attr so do a nice nested tag
        assert len(lst_attrs) > 1

        lst = [self.tag_name] + lst_attrs

        return tfc.attr_join(lst)

    def _s_tag(self, tfc):
        if self._tag_not_visible(tfc):
            return ""

        lst = []
        si = tfc.s_indent()

        lst.append(si + "<" + self._s_start_tag_name_attrs(tfc))

        if not self.has_contents():
            lst.append("/>")
        else:
            if self.multiline_contents():
                lst_contents = [">", self.s_contents(tfc.indent_by(1)), si]
                s = tfc.tag_join(lst_contents)
                lst.append(s)
            else:
                lst.append(">")
                lst.append(self.s_contents(tfc))
            lst.append("</" + self.tag_name + ">")

        return "".join(lst)

    def s_start_tag(self, tfc):
        return tfc.s_indent() + "<" + self._s_start_tag_name_attrs(tfc) + ">"

    def s_end_tag(self):
        return "</" + self.tag_name + ">"

    def s_compact_tag(self, tfc):
        return tfc.s_indent() + "<" + self._s_start_tag_name_attrs(tfc) + "/>"

    def direct(self, value):
        """
        Handle direct assign of a value to the element's contents.

        Some elements can handle a direct assign.  Those elements need
        to overload this method and make it do the right thing.
        
        For example, you might be able to assign a time float value to a
        timestamp element.  In that case, the timestamp element needs to
        add types.FloatType to its direct_types list, and add a
        .direct() method that overloads this default one to be able to
        correctly handle a float value.
        """
        assert False, "cannot call CoreElement.direct(); must subclass it"

    def import_xml(self, source, lst_errors=None):
        """
        Import XML data from source; log errors to lst_errors.

        "source" can be a filename, a URL, a string, or an xml.dom node
        data structure (as returned by xml.dom.minidom.parse()).

        Get as much data as possible; any data that is not imported will
        be appended to lst_errors, in text form.

        lst_errors is optional.
        """
        return _xe_import_xml(self, source, lst_errors)



class TextElement(CoreElement):
    """
    An element with simple text data.

    Cannot have other elements nested inside it.

    Attributes:
        attrs
        text
    """
    def __init__(self, tag_name, text="",
            def_attr_name=None, def_attr_value=None, attr_names=[]):
        CoreElement.__init__(self, tag_name,
                def_attr_name, def_attr_value, attr_names)
        self._lock = False
        self.text = text
        self._lock = True

    def text_check(self):
        pass

    def has_contents(self):
        return not not self.text

    def multiline_contents(self):
        return self.text.find("\n") >= 0

    def s_contents(self, tfc):
        return self.text



class CustomTimestampElement(CoreElement):
    def __init__(self, tag_name, tf, time_offset, s_offset_default,
            s_from_tf, tf_from_s, cleanup_time_offset):
        lst = [types.FloatType, types.NoneType] + list(types.StringTypes)
        CoreElement.__init__(self, tag_name, direct_types=lst)
        self._lock = False

        self.s_from_tf = s_from_tf
        self.tf_from_s = tf_from_s
        self.cleanup_time_offset = cleanup_time_offset

        self.s_offset_default = s_offset_default

        self.tf = None
        if time_offset is None:
            self.time_offset = s_offset_default
        else:
            self.time_offset = cleanup_time_offset(time_offset)

        self._lock = True
        self.direct(tf)

    def check_tf(self, tf):
        try:
            tf = float(tf)
        except (TypeError, ValueError):
            raise TypeError, "invalid time float value"
        return tf

    def __getattr__(self, name):
        if name == _s_text:
            return self.s_from_tf(self.tf, self.time_offset)
        return CoreElement.__getattr__(self, name)

    def __setattr__(self, name, value):
        if name == _s_text:
            if type(value) not in types.StringTypes:
                raise TypeError, "can only assign a string to .text"
            tf = self.tf_from_s(value)
            if tf is None:
                raise ValueError, "value must be a valid timestamp string"
            self.__dict__[_s_tf] = tf
            return

        if name == _s_tf:
            if value is None:
                self.__dict__[name] = None
            else:
                self.__dict__[name] = self.check_tf(value)
            return

        if name == _s_time_offset:
            if value is None:
                self.__dict__[name] = None
            else:
                try:
                    value = self.cleanup_time_offset(value)
                except (TypeError, ValueError):
                    raise ValueError, \
                            "value must be a valid time offset string"
                self.__dict__[name] = value
            return

        CoreElement.__setattr__(self, name, value)

    def __nonzero__(self):
        return self.tf is not None

    def __cmp__(self, o):
        try:
            n = cmp(self.tf, o.tf)
        except AttributeError:
            return cmp(self.tf, o)

        if n:
            return n
        # cmp() == zero, so values match; compare attrs to break the tie.
        # If values and attrs match, self and o should be considered equal.
        return cmp(self.attrs, o.attrs)

    def has_contents(self):
        return self.tf is not None

    def multiline_contents(self):
        return False

    def s_contents(self, tfc):
        if self.tf is None:
            return ""
        return self.s_from_tf(self.tf, self.time_offset)

    def direct(self, value):
        """
        Handle direct assignment.

        Supported types for direct assignment: float, string
        """
        assert self._direct_types == \
                [types.FloatType, types.NoneType] + list(types.StringTypes)
        assert type(value) in self._direct_types

        if value is None:
            self.tf = value
        elif type(value) == types.FloatType:
            self.tf = value
        elif type(value) in types.StringTypes:
            tf = self.tf_from_s(value)
            if tf is None:
                raise ValueError, "value must be a valid timestamp string"
            self.tf = tf
        else:
            # direct should never even be called unless the type is
            # compatible so this error should never happen
            raise TypeError, "cannot direct assign that type"

    def update(self):
        from time import time as tf_utc

        self.tf = tf_utc()
        return self

try:
    import feed.date.rfc3339 as rfc3339

    class TimestampElement(CustomTimestampElement):
        def __init__(self, tag_name, tf=None, time_offset=None):
            CustomTimestampElement.__init__(self,
                    tag_name,
                    tf,
                    time_offset,
                    rfc3339.s_offset_default,
                    rfc3339.timestamp_from_tf,
                    rfc3339.tf_from_timestamp,
                    rfc3339.cleanup_time_offset)
except ImportError:
    pass




class CustomElement(CoreElement):
    """
    Class to represent arbitrary data structures as xe elements.

    Make a class that inherits from CustomElement.  This class should
    then define four class methods:
      __init__() -- call CustomElement.__init__(), then do any custom
              setup
      check_value() -- return value; raise ValueError or TypeError if
              there is a problem with the value
      value_from_s() -- convert a string to the custom value; raise
              ValueError if there is any problem
      s_from_value() -- convert self.value to a string

    These three functions should be class methods so they can inspect
    the self values.  See the IntElement class for a simple example of a
    class implemented with CustomElement.

    """
    # If you need to provide multiple values to your conversion
    # functions, make the conversion functions part of your subclass
    # and have them get those values through self.
    def __init__(self, tag_name, value, cust_type):
        lst = [cust_type, types.NoneType] + list(types.StringTypes)
        CoreElement.__init__(self, tag_name, direct_types=lst)
        self._lock = False
        self.cust_type = cust_type
        # get .value added to dict in case .direct() needs it
        self.value = None
        self._lock = True

        # use .direct() so that init can be done with value or string
        self.direct(value)

    def __getattr__(self, name):
        if name == _s_text:
            return self.s_from_value()
        return CoreElement.__getattr__(self, name)

    def __setattr__(self, name, value):
        if name == _s_text:
            if type(value) not in types.StringTypes:
                raise TypeError, "can only assign a string to .text"
            self.value = self.value_from_s(value)
            return

        if name == _s_value:
            if value is None:
                self.__dict__[name] = None
            else:
                self.__dict__[name] = self.check_value(value)
            return

        CoreElement.__setattr__(self, name, value)

    def __nonzero__(self):
        return self.value is not None

    def __cmp__(self, o):
        try:
            n = cmp(self.value, o.value)
        except AttributeError:
            return cmp(self.value, o)

        if n:
            return n
        # cmp() == zero, so values match; compare attrs to break the tie.
        # If values and attrs match, self and o should be considered equal.
        return cmp(self.attrs, o.attrs)

    def has_contents(self):
        return self.value is not None

    def multiline_contents(self):
        # overload this for any type that can be have multiline contents
        return False

    def s_contents(self, tfc):
        return self.s_from_value()

    def direct(self, value):
        """
        Handle direct assignment.

        Supported types for direct assignment are in self._direct_types
        """
        assert self._direct_types == \
                [self.cust_type, types.NoneType] + list(types.StringTypes)
        assert type(value) in self._direct_types

        if value is None:
            self.value = None
        elif type(value) in types.StringTypes:
            self.value = self.value_from_s(value)
        else:
            self.value = self.check_value(value)



class IntElement(CustomElement):
    def __init__(self, tag_name, value=None, min=None, max=None):
        # define min and max first because check_value() needs them
        self._lock = False
        self.min = min
        self.max = max
        self._lock = True
        # now it's safe to init the value (and everything else too)
        CustomElement.__init__(self, tag_name, value, types.IntType)

    def check_value(self, value):
        assert self.cust_type is types.IntType
        try:
            value = int(value)
        except (TypeError, ValueError):
            raise TypeError, "value must be an integer type"

        if self.min is not None and value < self.min:
            raise ValueError, "minimum value is %d" % self.min
        if self.max is not None and value > self.max:
            raise ValueError, "maximum value is %d" % self.max

        return value

    def value_from_s(self, s):
        assert type(s) in types.StringTypes

        value = int(s)
        return self.check_value(value)

    def s_from_value(self):
        if self.value is None:
            return ""
        return str(self.value)



class FloatElement(CustomElement):
    def __init__(self, tag_name, value=None, min=None, max=None,
            s_format=None):
        CustomElement.__init__(self, tag_name, value, types.FloatType)
        self._lock = False
        self.min = min
        self.max = max
        self.s_format = s_format
        self._lock = True

    def check_value(self, value):
        assert self.cust_type is types.FloatType
        try:
            value = float(value)
        except (TypeError, ValueError):
            raise TypeError, "value must be a float type"

        if self.min is not None and value < self.min:
            raise ValueError, "minimum value is %d" % self.min
        if self.max is not None and value > self.max:
            raise ValueError, "maximum value is %d" % self.max

        return value

    def value_from_s(self, s):
        assert type(s) in types.StringTypes

        value = float(s)
        return self.check_value(value)

    def s_from_value(self):
        if self.value is None:
            return ""
        if self.s_format is None:
            return str(self.value)
        return self.s_format % self.value



class Nest(XMLItem):
    """
    A data structure that can store XML elements, nested inside it.

    Note: this is not an XML element!  Because it is not an XML
    element, it has no tags.  Its string representation is the
    representations of the elements nested inside it.

    NestElement and XMLDoc inherit from this.
    """
    def __init__(self):
        self._lock = False
        self._parent = None
        self._name = ""
        self._element_names = []
        self._lock = True

    def _do_setattr(self, name, value):
        if isinstance(value, XMLItem):
            value._parent = self
            value._name = name
            if name not in self.__dict__:
                # item being added for the first time ever
                self._element_names.append(name)
        self.__dict__[name] = value

    def __setattr__(self, name, value):
        # Lots of magic here!  This is important stuff.  Here's how it works:
        #
        # 0) self._lock is a boolean, set to False initially and then set
        # to True at the end of __init__().  When it's False, you can add new
        # members to the class instance without any sort of checks; once
        # it's set True, __setattr__() starts checking assignments.  By
        # default, when _lock is True, any assignment to an old member
        # has to be of matching type.  You can add a new member to the
        # class instance, but __setattr__() checks to ensure that the
        # new member is an XMLItem.
        #
        # 1) Whether self._lock is set or not, if the value is an XMLitem,
        # then this will properly add the XMLItem into the tree
        # structure.  The XMLItem will have _parent set to the parent,
        # will have _name set to its name in the parent, and will be
        # added to the parent's elements list.  This is handled by
        # _do_setattr().
        #
        # 2) As a convenience for the user, if the user is assigning a
        # string, and self is an XMLItem that has a .text value, this
        # will assign the string to the .text value.  This allows usages
        # like "e.title = string", which is very nice.  Before I added
        # this, I frequently wrote "e.title = string" and was frustrated
        # when my title element was replaced by a simple string; it's
        # better if this can just work.
        #
        # 3) Similar to the above, an element can advertise that it will
        # accept certain types directly.  If you are assigning "value"
        # to a subobject o, then if type(value) is in the o._direct_types
        # list, this will call o.direct(value) to do the direct
        # assignment.  Example: see the Timestamp elements in atomfeed.
        #
        # 4) This checks assignments to _parent, and makes sure they are
        # plausible (either an XMLItem, or None).

        try:
            _lock = self._lock
        except AttributeError:
            _lock = False

        if not _lock:
            self._do_setattr(name, value)
            return

        if not name in self.__dict__:
            # brand-new item
            if _lock:
                self.nest_check()
                if not isinstance(value, XMLItem):
                    # Often, this error is because of a typo.  The user
                    # didn't mean to insert a new item, but misspelled
                    # an existing item.
                    raise TypeError, \
                    "cannot insert non-XMLItem (did you misspell something?)"
            self._do_setattr(name, value)
            return

        if name == "_parent":
            if not (isinstance(value, XMLItem) or value is None):
                raise TypeError, "only XMLItem or None is permitted"
            self.__dict__[name] = value
            return

        if name == "_name" and type(value) in types.StringTypes:
            self.__dict__[name] = value
            return

        o = self.__dict__[name]

        # direct-assign the value if that is supported
        if isinstance(o, ElementItem) and type(value) in o._direct_types:
            o.direct(value)
            return

        # Allow string assignment to go to the .text attribute, for
        # elements that allow it.  All TextElements allow it;
        # Elements will allow it if they do not nave nested elements.
        # text_check() raises an error if it's not allowed.
        if isinstance(o, ElementItem) and \
                _s_text in o.__dict__ and \
                type(value) in types.StringTypes:
            o.text_check()
            o.text = value
            return

        # locked item so do checks
        if not _assign_compatible(o, value):
            raise TypeError, "value's type is not compatible"

        self._do_setattr(name, value)
        
    def __delattr__(self, name):
        # This won't be used often, if ever, but if anyone tries it, it
        # should work.  Can only delete nested ElementItems.
        if not isinstance(self.name, ElementItem):
            raise TypeError, "cannot delete %s" % name
        self._element_names.remove(name)
        del(self.__dict__[name])

    def nest_check(self):
        pass

    def has_contents(self):
        for name in self._element_names:
            if self.__dict__[name]:
                return True
        # empty iff all of the elements were empty
        return False

    def __nonzero__(self):
        return self.has_contents()

    def multiline_contents(self):
        # for any contents, we want multiline so nested tags are indented
        return self.has_contents()

    def s_contents(self, tfc):
        if len(self._element_names) == 0:
            return ""

        lst = []
        for name in self._element_names:
            element = self.__dict__[name]
            s = element._s_tag(tfc)
            if s:
                lst.append(s)

        return tfc.tag_join(lst)

    def debug_tree(self):
        lst = []
        level = self.level()
        num_elem = len(self._element_names)
        tup = (level, self.s_name(), self.__class__.__name__, num_elem)
        s = "%2d) %s -- %s containing %d elements" % tup
        lst.append(s)
        for name in self._element_names:
            element = self.__dict__[name]
            s = element.debug_tree()
            lst.append(s)
        return tfc.tag_join(lst)

    def _s_tag(self, tfc):
        return self.s_contents(tfc)




class NestElement(Nest,CoreElement):
    """
    An element that can have other elements nested inside it.

    Attributes:
        attrs -- a dictionary mapping XML attribute names to values
        _element_names -- a list of other elements nested inside
    """
    def __init__(self, tag_name, def_attr_name=None, def_attr_value=None,
            attr_names=[], direct_types=[]):
        """
        Arguments:
            tag_name -- the XML tag name of this item
            def_attr_name -- name of any default attribute this item has
            def_attr_value -- default value of any default attribute
            attr_names -- a list of expected attribute names
            direct_types -- a list of types that can be direct assigned

            attr_names also sets the order in which the attribute names
            will be put in the tag string.

            See the doc string for CoreElement.direct() to learn about
            direct_types and direct assigns.
        """
        CoreElement.__init__(self, tag_name, def_attr_name, def_attr_value,
                attr_names, direct_types)
        self._lock = False
        self._element_names = []
        self._lock = True

    def __nonzero__(self):
        return CoreElement.__nonzero__(self)

    def _s_tag(self, tfc):
        return CoreElement._s_tag(self, tfc)



class Element(NestElement,TextElement):
    """
    A class to represent an arbitrary XML tag.  Can either have other XML
    elements nested inside it, or else can have a text string value, but
    never both at the same time.

    This is intended for user-defined XML tags.  The user can just use
    "Element" for all custom tags.

    This isn't essential.  You can use TextElement for tags with a text
    string value, and NestElement for tags that nest other elements.
    This is here for convenience; you can just use Element for
    everything if you like.

    Attributes:
        attrs -- a dictionary mapping XML attribute names to values
        _element_names -- a list of other elements nested inside, if any
        text -- a text string value, if any

    Note: if text is set, elements will be empty, and vice-versa.  If you
    have elements nested inside and try to set the .text, this will raise
    an exception, and vice-versa.
    """
    def __init__(self, tag_name, def_attr_name=None, def_attr_value=None,
            attr_names=[], direct_types=[]):
        """
        Arguments:
            tag_name -- the XML tag name of this item
            def_attr_name -- name of any default attribute this item has
            def_attr_value -- default value of any default attribute
            attr_names -- a list of expected attribute names
            direct_types -- a list of types that can be direct assigned

            attr_names also sets the order in which the attribute names
            will be put in the tag string.

            See the doc string for CoreElement.direct() to learn about
            direct_types and direct assigns.
        """
        NestElement.__init__(self, tag_name, def_attr_name, def_attr_value,
                attr_names, direct_types)
        self._lock = False
        self.text = ""
        self._lock = True

    def nest_check(self):
        if self.text:
            raise TypeError, "Element has text contents so cannot nest"

    def text_check(self):
        if len(self._element_names) > 0:
            raise TypeError, "Element has nested elements so cannot assign text"

    def has_contents(self):
        return NestElement.has_contents(self) or TextElement.has_contents(self)

    def multiline_contents(self):
        return NestElement.has_contents(self) or self.text.find("\n") >= 0

    def s_contents(self, tfc):
        if len(self._element_names) > 0:
            return NestElement.s_contents(self, tfc)
        if self.text:
            return TextElement.s_contents(self, tfc)
        return ""

    def debug_tree(self):
        lst = []
        num_elem = len(self._element_names)
        level = self.level()
        if self.text:
            return XMLItem.debug_tree(self)
        elif num_elem > 0:
            tup = (self.s_name(), self.__class__.__name__, num_elem, level)
            s = "# %s -- instance of %s containing %d elements (current level: %d)" % tup
            lst.append(s)
            for name in self._element_names:
                element = self.__dict__[name]
                s = element.debug_tree()
                lst.append(s)
            return tfc.tag_join(lst)
        else:
            tup = (self.s_name(), level)
            s = "%s -- empty Element (current level: %d)" % tup
            return s
        assert(False, "not possible to reach this line.")



class Collection(XMLItem):
    """
    A container for 0 or more XML elements of a type.

    Note: this is not an XML element!  Because it is not an XML
    element, it has no tags.  Its string representation is the
    representations of the elements nested inside it.

    A Collection contains 0 or more Elements, but isn't an XML element.
    Use where a run of 0 or more Elements of the same type is legal.

    When you init your Collection, you specify what class of Element it
    will contain.  Attempts to append an Element of a different class
    will raise an exception.  Note, however, that the various Element
    classes all inherit from base classes, and you can specify a class
    from higher up in the inheritance tree.  You could, if you wanted,
    make a Collection containing "XMLItem" and then any XML element
    class would be legal in that collection.  (Example: XMLDoc, which
    contains two collections of DocItem.)

    Attributes:
        contains -- the class of element this Collection will contain
        items -- a list of elements nested inside

    Note: The string representation of a Collection is just the string
    representations of the elements inside it.  However, a verbose
    string reprentation will have an XML comment like this:

    <!-- Collection of <class> with <n> elements -->

    where <n> is the number of elements in the Collection and <class> is
    the name of the class in this Collection.
    """
    class Flags(object):
        def __init__(self):
            self.unique_values = False
            self.sorted = False

    def __init__(self, element_class):
        """
        Arguments:
            element_class -- the class of XML elements to allow

            To allow any XML element, use "XMLItem" for element_class
        """
        self._lock = False
        self._parent = None
        self._name = ""
        self._flags = Collection.Flags()
        self.items = []
        self.contains = element_class
        self._lock = True
    def __len__(self):
        return len(self.items)
    def __getitem__(self, i):
        return self.items[i]
    def __setitem__(self, i, value):
        o = self.items[i]
        # direct-assign the value if that is supported
        if isinstance(o, ElementItem) and type(value) in o._direct_types:
            o.direct(value)
            return

        if not isinstance(value, self.contains):
            raise TypeError, "object is the wrong type for this collection"

        self.items[i] = value
    def __delitem__(self, i):
        del(self.items[i])

    def __nonzero__(self):
        # there are no attrs; collection is nonzero if any element is
        for element in self.items:
            if element:
                return True
        return False

    def s_coll(self):
        """
        Return a string describing the collection.

        The string will be something like:

        "collection of XMLItem with 2 elements."
        """
        name = self.contains.__name__
        n = len(self.items)
        if n == 1:
            el = "element"
        else:
            el = "elements"
        return "collection of %s with %d %s" % (name, n, el)

    def append(self, element):
        assert issubclass(self.contains, XMLItem)
        if not isinstance(element, self.contains):
            raise TypeError, "object is the wrong type for this collection"
        if self._flags.unique_values:
            for o in self.items:
                if element == o:
                    return
        element._parent = self
        self.items.append(element)
        if self._flags.sorted:
            self.items.sort()

    def insert(self, i, element):
        """
        Insert element before index i.

        """
        assert issubclass(self.contains, XMLItem)
        if not isinstance(element, self.contains):
            raise TypeError, "object is the wrong type for this collection"
        if self._flags.unique_values:
            for o in self.items:
                if element == o:
                    return
        element._parent = self
        self.items.insert(i, element)
        if self._flags.sorted:
            self.items.sort()

    def _s_tag(self, tfc):
        # A collection exists only as a place to put real elements.
        # There are no start or end tags...
        # When tfc.verbose() is true, we do put an XML comment
        # identifying the collection, and we indent the elements under
        # that comment, to better show the structure.

        if not self.items and not tfc.show_all():
            return ""

        lst = []

        if tfc.verbose():
            s = "%s%s%s%s" % (tfc.s_indent(), "<!-- ", self.s_coll(), " -->")
            lst.append(s)
            tfc = tfc.indent_by(1)

        for element in self.items:
            s = element._s_tag(tfc)
            if s:
                lst.append(s)

        return tfc.tag_join(lst)

    def debug_tree(self):
        level = self.level()
        s = "%2d) %s -- %s" % (level, self.s_name(), self.s_coll())
        lst = []
        lst.append(s)
        for element in self.items:
            s = element.debug_tree()
            lst.append(s)
        return tfc.tag_join(lst)



class ListElement(Collection, CoreElement):
    """
    A container for 0 or more XML elements of a type.

    Similar to a Collection, but this is an element.  It has attributes
    and its representation is an XML tag.

    When you init your ListElement, you specify what class of Element it
    will contain.  Attempts to append an Element of a different class
    will raise an exception.  Note, however, that the various Element
    classes all inherit from base classes, and you can specify a class
    from higher up in the inheritance tree.  You could, if you wanted,
    make a ListElement containing "ElementItem" and then any XML element
    would be allowed.  (For working with unstructured XML, you will
    probably want to do just that: use a ListElement of ElementItem.)

    Attributes:
        contains -- the class of element this ListElement will contain
        items -- a list of elements nested inside
    """
    def __init__(self, element_class, tag_name,
             def_attr_name=None, def_attr_value=None,
            attr_names=[], direct_types=[]):
        """
        Arguments:
            element_class -- the class of XML elements to allow

            To allow any XML element, use "XMLItem" for element_class
        """
        if not issubclass(element_class, XMLItem):
          raise TypeError, "element_class must be an XMLItem"

        if type(tag_name) not in types.StringTypes:
            raise TypeError, "tag_name must be a string"

        CoreElement.__init__(self, tag_name, def_attr_name, def_attr_value,
                attr_names, direct_types)
        Collection.__init__(self, element_class)
        self._flags.show_when_empty = False

#    # REVIEW: just inherit these from Collection?
#    def __len__(self):
#        return len(self.items)
#    def __getitem__(self, i):
#        return self.items[i]
#    def __setitem__(self, i, value):
#        o = self.items[i]
#        # direct-assign the value if that is supported
#        if isinstance(o, ElementItem) and type(value) in o._direct_types:
#            o.direct(value)
#            return
#
#        if not isinstance(value, self.contains):
#            raise TypeError, "object is the wrong type for this ListElement"
#
#        self.items[i] = value
#    def __delitem__(self, i):
#        del(self.items[i])

    def _tag_not_visible(self, tfc):
        if self._flags.show_when_empty:
            return False
        return CoreElement._tag_not_visible(self, tfc)

    def has_contents(self):
        return Collection.__nonzero__(self)

    def multiline_contents(self):
        # if we have any contents at all, they should be multiline
        return self.has_contents()

    def s_contents(self, tfc):
        return Collection._s_tag(self, tfc)

    def __nonzero__(self):
        return CoreElement.__nonzero__(self)

    def _s_tag(self, tfc):
        return CoreElement._s_tag(self, tfc)



class XMLDeclaration(DocItem):
    def __init__(self):
        self._parent = None
        self._name = ""
        self.attrs = Attrs()
        self.attrs["version"] = "1.0"
        self.attrs["encoding"] = "utf-8"
        self.attrs["standalone"] = ""

    def _s_tag(self, tfc):
        # An XMLDeclaration() instance is never empty, so always prints.

        lst_attrs = self.attrs.lst_attrs()
        s_attrs = " ".join(lst_attrs)

        s = "%s%s %s%s" % (tfc.s_indent(), "<?xml", s_attrs, "?>")

        return s

    def __nonzero__(self):
        # Returns True because the XML Declaration is never empty.
        return True



class XMLDoc(Nest):
    """
    A data structure to represent an XML Document.  It will have the
    following structure:

    the XML Declaration item
    0 or more document-level XML items
    exactly one XML item (the "root tag")
    0 or more document-level XML items

    document level XML items are: Comment, PI, MarkupDecl


    Attributes:
        xml_decl
            the XMLDeclaration item
        top
            a collection of DocItem at top (above root_element)
        root_element
            the XML tag containing your data
        end
            a collection of DocItem at end (below root_element)

    Note: usually the root_element has lots of ElementItems nested inside!
    """
    def __init__(self, root_element=None):
        """
        Arguments:
            root_element -- an ElementItem to hold all the data in the
                    XMLDoc.  Usually this will be a NestElement or
                    Element with lots of ElementItems inside.
        """
        Nest.__init__(self)

        self._name = "XMLDoc"

        self.xml_decl = XMLDeclaration()
        self.top = Collection(DocItem)

        if root_element is None:
            root_element = Comment("no root element yet")
        self.root_element = root_element

        self.end = Collection(DocItem)

    def __setattr__(self, name, value):
        # root_element may always be set to any ElementItem
        if name == "root_element":
            if not (isinstance(value, ElementItem)):
                raise TypeError, "only ElementItem is permitted"

            Nest._do_setattr(self, name, value)
        else:
            # for all other, fall through to inherited behavior
            Nest.__setattr__(self, name, value)

    def Validate(self):
        """
        Return True if XMLDoc is valid.

        Currently doesn't test very much.
        """
        # XMLDoc never has parent.  Never change this!
        assert self._parent == None
        return True


import xml.dom.minidom as mdom

def _xe_import_xml(x, source, lst_errors=None):
    """
    Import XML data from source; log errors to lst_errors.

    "source" can be a filename, a URL, a string, or an xml.dom node
    data structure (as returned by xml.dom.minidom.parse()).

    Get as much data as possible; any data that is not imported will
    be appended to lst_errors, in text form.

    Arguments:
        x -- an xe object
        source -- filename, URL, string, or xml.dom node data structure
        lst_errors -- a list to receive any errors

    If lst_errors is not specified or None, errors will not be logged.

    """

    # NOTES
    #
    # These functions take a "lst_errors" argument.  When they find an XML
    # node they cannot import, they will append the text form of the node
    # to the lst_errors list.  On a perfectly successful import,
    # lst_errors will not have anything added to it.
    #
    # The way this works:
    #
    # If you point the import code at XML data, it will use Python's
    # xml.dom.minidom module to read and parse the XML.  (If you need 
    # more control over the process, you can do this parsing by hand, and
    # pass the xml.dom.minidom node data structure to the import code.)
    # The import code walks the node data structure, and matches up tag
    # names with tag names inside your xe data structure.  If everything
    # matches nicely, it imports the node into the data structure.
    #
    # If it sees a tag name that your data structure doesn't know about,
    # and the node is a simple text element node, xe will simply add a new
    # TextElement to your xe data structure and fill in the information
    # from the node.
    #
    # Any XML nodes that the import code cannot handle will be converted
    # to XML text strings, and those strings will be appended to
    # lst_errors.
    #
    #
    # Key functions:
    #     import_node_element() -- smart node import function

    def log_error(node, lst_errors):
        if lst_errors is None:
            return
        s = node.toxml()
        lst_errors.append(s)

    def parse_info(x):
        if not isinstance(x, XMLItem):
            raise TypeError

        if isinstance(x, Collection):
            o = x.contains()
            if "tag_name" in o.__dict__:
                tup = (o.tag_name, x)
            else:
                # FUTURE: handle this case later someday
                # for now, we will only read in by specific tag names
                tup = ("*", x)
            return tup
        else:
            tup = (x.tag_name, x)
            return tup

    def import_attrs(x, node):
        if node.hasAttributes():
            for name, value in node.attributes.items():
                x.attrs[name] = value

    def make_py_name(tag_name):
        """
        Return legal Python identifier name translated from tag_name.

        Any character not legal for a Python identifier becomes an
        underscore character ("_").

        Example:
        >>> print make_py_name("foaf:name")
        foaf_name
        """
        lst = []
        for c in tag_name:
            if c.isalnum() or c == "_":
                lst.append(c)
            else:
                lst.append("_")
        if lst[0].isnumeric():
            # if first character is a number, prepend something.
            lst.insert("elem", 0)
        return "".join(lst)

    def import_node_nest(x, node, lst_errors):
        assert node.nodeType == mdom.Node.ELEMENT_NODE
        if not isinstance(x, Nest):
            print "tried to import node on non-Nest"
            print "x:", str(x)
            print "node:", node.toxml()
            raise TypeError, "attempt to nest in non-Nest:" + str(type(x))

        try:
            # if parse info dict was cached earlier, just use it
            d = x._lst_parse_info
        except AttributeError:
            # need to build the parse info dict
            lst_elements = [x.__dict__[name] for name in x._element_names]
            d = dict([parse_info(e) for e in lst_elements])

            # cache the parse info dict for possible later reuse
            x._lock = False
            x._lst_parse_info = d
            x._lock = True

        try:
            import_attrs(x, node)
        except:
            log_error(node, lst_errors)

        for n in node.childNodes:
            if n.nodeType != mdom.Node.ELEMENT_NODE:
                # if it's not an element, we don't handle it at all
                # FUTURE: handle non-element nodes
                if n.nodeType == mdom.Node.TEXT_NODE and \
                        n.nodeValue.lstrip() == "":
                    # if it's a text node with just some white space,
                    # just skip it; don't even log it
                    continue
                log_error(n, lst_errors)
            elif n.tagName in d:
                import_node_element(d[n.tagName], n, lst_errors)
            else:
                # Whatever it is, treat it as a text element and add it
                tag_name = n.tagName
                o = TextElement(tag_name)
                x.__setattr__(make_py_name(tag_name), o)
                import_node_element(o, n, lst_errors)

    def import_node_text(x, node, lst_errors):
        try:
            import_attrs(x, node)
            lst = []
            for child in node.childNodes:
                if child.nodeType == mdom.Node.TEXT_NODE:
                    lst.append(child.nodeValue)
                else:
                    lst.append(child.toxml())
            x.text = "".join(lst)
        except:
            log_error(node, lst_errors)

    def import_node_element(x, node, lst_errors=None):
        """
        Import data from xml.dom.minidom node; log errors to lst_errors.

        Get as much data as possible; any data that is not imported will
        be appended to lst_errors, in text form.

        Arguments:
            x -- an xe object
            node -- an xml.dom.minidom node object
            lst_errors -- a list to receive any errors

        If lst_errors is not specified or None, errors will not be logged.
        """
        assert node.nodeType == mdom.Node.ELEMENT_NODE

        if isinstance(x, Collection):
            o = x.contains()
            import_node_element(o, node, lst_errors)
            x.append(o)
            return

        if isinstance(x, Nest):
            import_node_nest(x, node, lst_errors)
        else:
            import_node_text(x, node, lst_errors)
        return x

    def mini_open_anything(source):
        """
        Return file-like object from source.

        Try source several ways, to see if it is:
            * a file-like object (e.g. an already-opened file)
            * a URL
            * a filename

        This is inspired by the wonderful openAnything() by Mark Pilgrim:
        http://diveintopython.org/http_web_services/
        """
        try:
            # check to see if source already has readlines and close
            # methods
            if callable(source.readlines) and callable(source.close):
                # okay, looks like a file-like object; return it
                return source
        except AttributeError:
            pass

        if type(source) not in types.StringTypes:
            raise TypeError, "source isn't even a string"

        import urllib
        try:
            # see if source works as a URL
            return urllib.urlopen(source)
        except (IOError, OSError):
            pass

        try:
            # see if source works as a filename
            return open(source)
        except (IOError, OSError):
            pass

        return None


    # The actual _xe_import_xml() function body is below!
    # def _xe_import_xml(x, source, lst_errors=None):

    if not isinstance(x, XMLItem):
        # not an xe element
        raise TypeError, "x must be an xe element with a tag_name"
    try:
        tag_name = x.tag_name
    except AttributeError:
        raise TypeError, "x must be an xe element with a tag_name"

    temp_doc = None
    if isinstance(source, mdom.Node):
        # source is already an xml.dom.minidom node structure
        lst_nodes = source.getElementsByTagName(tag_name)
    else:
        # Not a Node, so we need to parse source.
        try:
            f = mini_open_anything(source)
        except TypeError:
            raise TypeError, "source is the wrong type; cannot import it"

        if f is not None:
            # We don't catch the exception for parse(); if it fails,
            # we want the exception raised.
            temp_doc = mdom.parse(f)
            lst_nodes = temp_doc.getElementsByTagName(tag_name)
        else:
            # We couldn't open it.  Maybe it's a string of XML?
            # We don't catch the exception for parseString(); if it fails,
            # we want the exception raised.
            temp_doc = mdom.parseString(source)
            lst_nodes = temp_doc.getElementsByTagName(tag_name)

    if len(lst_nodes) != 1:
        temp_doc and temp_doc.unlink()
        raise ValueError, "source contains more than one element to load"

    node = lst_nodes[0]
    import_node_element(x, node, lst_errors)
    temp_doc and temp_doc.unlink()
    return x

def print_log(lst_errors):
    """
    Print lst_errors in a nice format.
    """
    if len(lst_errors) == 0:
        print "No errors!"
    else:
        print "Could not handle these lines of XML:"
        print "\t" + "\n\t".join(lst_errors)




if __name__ == "__main__":
    def diff(s0, s1):
        """
        Compare two strings, line by line; return a report on any differences.
        """
        from difflib import ndiff
        lst0 = s0.split("\n")
        lst1 = s1.split("\n")
        report = '\n'.join(ndiff(lst0, lst1))
        return report

    def self_test_diff(message):
        """
        Check to see if a test failed; if so, print a diff.

        message: string to print on test failure

        Implicit arguments:
            failed_tests -- count of failed tests; will be incremented
            correct -- the expected result of the test
            result -- the actual result of the test
        """
        global failed_tests

        if result != correct:
            failed_tests += 1
            print module_banner
            print "%s: test case failed, diff follows:" % message
            print diff(correct, result)
            print



    failed_tests = 0

    # Since this file is indented using spaces, let's indent our test
    # code using spaces too so it will compare right.
    set_indent_str("    ")


    # Test: generate a trivial XML document

    xmldoc = XMLDoc()
    correct = """\
<?xml version="1.0" encoding="utf-8"?>
<!-- no root element yet -->"""

    result = str(xmldoc)
    self_test_diff("generate trivial XML document")


    # Test: verify that xmldoc.Validate() succeeds

    try:
        b = xmldoc.Validate()
    except:
        b = False

    if not b:
        failed_tests += 1
        print "test case failed:"
        print "xmldoc.Validate() failed."
        print


    # Test: does Element work both nested an non-nested?
    correct = """\
<test>
    some text
    <test:agent number="007">James Bond</test:agent>
    <test:pet
            type="cat"
            nickname="Mei-Mei">Matrix</test:pet>
    some more text
    <secret_agent agency="U.N.C.L.E.">Napoleon Solo</secret_agent>
</test>"""

    class TestPet(Element):
        def __init__(self, name=""):
            Element.__init__(self, "test:pet")
            self.text = name

    class TestAgent(Element):
        def __init__(self, name=""):
            Element.__init__(self, "test:agent")
            self.text = name

    class Test(Element):
        def __init__(self):
            Element.__init__(self, "test")
            self.text0 = XMLText()
            self.test_agent = TestAgent()
            self.test_pet = TestPet()
            self.text1 = XMLText()

    test = Test()
    test.text0 = "some text"
    test.test_agent = "James Bond"
    test.test_agent.attrs["number"] = "007"
    test.test_pet = "Matrix"
    test.test_pet.attrs["type"] = "cat"
    test.test_pet.attrs["nickname"] = "Mei-Mei"
    test.text1 = "some more text"
    test.secret_agent = TextElement("secret_agent")
    test.secret_agent = "Napoleon Solo"
    test.secret_agent.attrs["agency"] = "U.N.C.L.E."

    result = str(test)
    self_test_diff("test Element with both text and nested")

    test1 = NestElement("paper_size")
    correct = """\
<paper_size>
    <width>8.5</width>
    <height>11</height>
</paper_size>"""
    test1.import_xml(correct)
    result = str(test1)
    self_test_diff("test Element import")


    from sys import exit
    s_module = module_name + " " + module_version
    if failed_tests == 0:
        print s_module + ": self-test: all tests succeeded!"
        exit(0)
    elif failed_tests == 1:
        print s_module + " self-test: 1 test failed."
        exit(1)
    else:
        print s_module + " self-test: %d tests failed." % failed_tests
        exit(1)