This file is indexed.

/usr/lib/python3/dist-packages/suds/sax/element.py is in python3-suds 0.7~git20150727.94664dd-3.

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
# This program is free software; you can redistribute it and/or modify it under
# the terms of the (LGPL) GNU Lesser General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Library Lesser General Public License
# for more details at ( http://www.gnu.org/licenses/lgpl.html ).
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# written by: Jeff Ortel ( jortel@redhat.com )

"""
XML I{element} classes.

"""

from suds import *
from suds.sax import *
from suds.sax.text import Text
from suds.sax.attribute import Attribute


class Element(UnicodeMixin):
    """
    An XML element object.

    @ivar parent: The node containing this attribute.
    @type parent: L{Element}
    @ivar prefix: The I{optional} namespace prefix.
    @type prefix: basestring
    @ivar name: The I{unqualified} name of the attribute.
    @type name: basestring
    @ivar expns: An explicit namespace (xmlns="...").
    @type expns: (I{prefix}, I{name})
    @ivar nsprefixes: A mapping of prefixes to namespaces.
    @type nsprefixes: dict
    @ivar attributes: A list of XML attributes.
    @type attributes: [I{Attribute},...]
    @ivar text: The element's I{text} content.
    @type text: basestring
    @ivar children: A list of child elements.
    @type children: [I{Element},...]
    @cvar matcher: A collection of I{lambda} for string matching.
    @cvar specialprefixes: A dictionary of builtin-special prefixes.

    """

    matcher = {
        "eq": lambda a, b: a == b,
        "startswith": lambda a, b: a.startswith(b),
        "endswith": lambda a, b: a.endswith(b),
        "contains": lambda a, b: b in a}

    specialprefixes = {Namespace.xmlns[0]: Namespace.xmlns[1]}

    @classmethod
    def buildPath(self, parent, path):
        """
        Build the specified path as a/b/c.

        Any missing intermediate nodes are built automatically.

        @param parent: A parent element on which the path is built.
        @type parent: I{Element}
        @param path: A simple path separated by (/).
        @type path: basestring
        @return: The leaf node of I{path}.
        @rtype: L{Element}

        """
        for tag in path.split("/"):
            child = parent.getChild(tag)
            if child is None:
                child = Element(tag, parent)
            parent = child
        return child

    def __init__(self, name, parent=None, ns=None):
        """
        @param name: The element's (tag) name. May contain a prefix.
        @type name: basestring
        @param parent: An optional parent element.
        @type parent: I{Element}
        @param ns: An optional namespace.
        @type ns: (I{prefix}, I{name})

        """
        self.rename(name)
        self.expns = None
        self.nsprefixes = {}
        self.attributes = []
        self.text = None
        if parent is not None and not isinstance(parent, Element):
            raise Exception("parent (%s) not-valid" %
                (parent.__class__.__name__,))
        self.parent = parent
        self.children = []
        self.applyns(ns)

    def rename(self, name):
        """
        Rename the element.

        @param name: A new name for the element.
        @type name: basestring

        """
        if name is None:
            raise Exception("name (%s) not-valid" % (name,))
        self.prefix, self.name = splitPrefix(name)

    def setPrefix(self, p, u=None):
        """
        Set the element namespace prefix.

        @param p: A new prefix for the element.
        @type p: basestring
        @param u: A namespace URI to be mapped to the prefix.
        @type u: basestring
        @return: self
        @rtype: L{Element}

        """
        self.prefix = p
        if p is not None and u is not None:
            self.expns = None
            self.addPrefix(p, u)
        return self

    def qname(self):
        """
        Get this element's B{fully} qualified name.

        @return: The fully qualified name.
        @rtype: basestring

        """
        if self.prefix is None:
            return self.name
        return "%s:%s" % (self.prefix, self.name)

    def getRoot(self):
        """
        Get the root (top) node of the tree.

        @return: The I{top} node of this tree.
        @rtype: I{Element}

        """
        if self.parent is None:
            return self
        return self.parent.getRoot()

    def clone(self, parent=None):
        """
        Deep clone of this element and children.

        @param parent: An optional parent for the copied fragment.
        @type parent: I{Element}
        @return: A deep copy parented by I{parent}
        @rtype: I{Element}

        """
        root = Element(self.qname(), parent, self.namespace())
        for a in self.attributes:
            root.append(a.clone(self))
        for c in self.children:
            root.append(c.clone(self))
        for ns in list(self.nsprefixes.items()):
            root.addPrefix(ns[0], ns[1])
        return root

    def detach(self):
        """
        Detach from parent.

        @return: This element removed from its parent's child list and
            I{parent}=I{None}.
        @rtype: L{Element}

        """
        if self.parent is not None:
            if self in self.parent.children:
                self.parent.children.remove(self)
            self.parent = None
        return self

    def set(self, name, value):
        """
        Set an attribute's value.

        @param name: The name of the attribute.
        @type name: basestring
        @param value: The attribute value.
        @type value: basestring
        @see: __setitem__()

        """
        attr = self.getAttribute(name)
        if attr is None:
            attr = Attribute(name, value)
            self.append(attr)
        else:
            attr.setValue(value)

    def unset(self, name):
        """
        Unset (remove) an attribute.

        @param name: The attribute name.
        @type name: str
        @return: self
        @rtype: L{Element}

        """
        try:
            attr = self.getAttribute(name)
            self.attributes.remove(attr)
        except Exception:
            pass
        return self

    def get(self, name, ns=None, default=None):
        """
        Get the value of an attribute by name.

        @param name: The name of the attribute.
        @type name: basestring
        @param ns: The optional attribute's namespace.
        @type ns: (I{prefix}, I{name})
        @param default: An optional value to be returned when either the
            attribute does not exist or has no value.
        @type default: basestring
        @return: The attribute's value or I{default}.
        @rtype: basestring
        @see: __getitem__()

        """
        attr = self.getAttribute(name, ns)
        if attr is None or attr.value is None:
            return default
        return attr.getValue()

    def setText(self, value):
        """
        Set the element's L{Text} content.

        @param value: The element's text value.
        @type value: basestring
        @return: self
        @rtype: I{Element}

        """
        if not isinstance(value, Text):
            value = Text(value)
        self.text = value
        return self

    def getText(self, default=None):
        """
        Get the element's L{Text} content with optional default.

        @param default: A value to be returned when no text content exists.
        @type default: basestring
        @return: The text content, or I{default}.
        @rtype: L{Text}

        """
        if self.hasText():
            return self.text
        return default

    def trim(self):
        """
        Trim leading and trailing whitespace.

        @return: self
        @rtype: L{Element}

        """
        if self.hasText():
            self.text = self.text.trim()
        return self

    def hasText(self):
        """
        Get whether the element has non-empty I{text} string.

        @return: True when has I{text}.
        @rtype: boolean

        """
        return bool(self.text)

    def namespace(self):
        """
        Get the element's namespace.

        @return: The element's namespace by resolving the prefix, the explicit
            namespace or the inherited namespace.
        @rtype: (I{prefix}, I{name})

        """
        if self.prefix is None:
            return self.defaultNamespace()
        return self.resolvePrefix(self.prefix)

    def defaultNamespace(self):
        """
        Get the default (unqualified namespace).

        This is the expns of the first node (looking up the tree) that has it
        set.

        @return: The namespace of a node when not qualified.
        @rtype: (I{prefix}, I{name})

        """
        p = self
        while p is not None:
            if p.expns is not None:
                return None, p.expns
            p = p.parent
        return Namespace.default

    def append(self, objects):
        """
        Append the specified child based on whether it is an element or an
        attribute.

        @param objects: A (single|collection) of attribute(s) or element(s) to
            be added as children.
        @type objects: (L{Element}|L{Attribute})
        @return: self
        @rtype: L{Element}

        """
        if not isinstance(objects, (list, tuple)):
            objects = (objects,)
        for child in objects:
            if isinstance(child, Element):
                self.children.append(child)
                child.parent = self
                continue
            if isinstance(child, Attribute):
                self.attributes.append(child)
                child.parent = self
                continue
            raise Exception("append %s not-valid" %
                (child.__class__.__name__,))
        return self

    def insert(self, objects, index=0):
        """
        Insert an L{Element} content at the specified index.

        @param objects: A (single|collection) of attribute(s) or element(s) to
            be added as children.
        @type objects: (L{Element}|L{Attribute})
        @param index: The position in the list of children to insert.
        @type index: int
        @return: self
        @rtype: L{Element}

        """
        objects = (objects,)
        for child in objects:
            if not isinstance(child, Element):
                raise Exception("append %s not-valid" %
                    (child.__class__.__name__,))
            self.children.insert(index, child)
            child.parent = self
        return self

    def remove(self, child):
        """
        Remove the specified child element or attribute.

        @param child: A child to remove.
        @type child: L{Element}|L{Attribute}
        @return: The detached I{child} when I{child} is an element, else None.
        @rtype: L{Element}|None

        """
        if isinstance(child, Element):
            return child.detach()
        if isinstance(child, Attribute):
            self.attributes.remove(child)

    def replaceChild(self, child, content):
        """
        Replace I{child} with the specified I{content}.

        @param child: A child element.
        @type child: L{Element}
        @param content: An element or collection of elements.
        @type content: L{Element} or [L{Element},...]

        """
        if child not in self.children:
            raise Exception("child not-found")
        index = self.children.index(child)
        self.remove(child)
        if not isinstance(content, (list, tuple)):
            content = (content,)
        for node in content:
            self.children.insert(index, node.detach())
            node.parent = self
            index += 1

    def getAttribute(self, name, ns=None, default=None):
        """
        Get an attribute by name and (optional) namespace.

        @param name: The name of a contained attribute (may contain prefix).
        @type name: basestring
        @param ns: An optional namespace
        @type ns: (I{prefix}, I{name})
        @param default: Returned when attribute not-found.
        @type default: L{Attribute}
        @return: The requested attribute object.
        @rtype: L{Attribute}

        """
        if ns is None:
            prefix, name = splitPrefix(name)
            if prefix is not None:
                ns = self.resolvePrefix(prefix)
        for a in self.attributes:
            if a.match(name, ns):
                return a
        return default

    def getChild(self, name, ns=None, default=None):
        """
        Get a child by (optional) name and/or (optional) namespace.

        @param name: The name of a child element (may contain prefix).
        @type name: basestring
        @param ns: An optional namespace used to match the child.
        @type ns: (I{prefix}, I{name})
        @param default: Returned when child not-found.
        @type default: L{Element}
        @return: The requested child, or I{default} when not-found.
        @rtype: L{Element}

        """
        if ns is None:
            prefix, name = splitPrefix(name)
            if prefix is not None:
                ns = self.resolvePrefix(prefix)
        for c in self.children:
            if c.match(name, ns):
                return c
        return default

    def childAtPath(self, path):
        """
        Get a child at I{path} where I{path} is a (/) separated list of element
        names that are expected to be children.

        @param path: A (/) separated list of element names.
        @type path: basestring
        @return: The leaf node at the end of I{path}.
        @rtype: L{Element}

        """
        result = None
        node = self
        for name in path.split("/"):
            if not name:
                continue
            ns = None
            prefix, name = splitPrefix(name)
            if prefix is not None:
                ns = node.resolvePrefix(prefix)
            result = node.getChild(name, ns)
            if result is None:
                return
            node = result
        return result

    def childrenAtPath(self, path):
        """
        Get a list of children at I{path} where I{path} is a (/) separated list
        of element names expected to be children.

        @param path: A (/) separated list of element names.
        @type path: basestring
        @return: The collection leaf nodes at the end of I{path}.
        @rtype: [L{Element},...]

        """
        parts = [p for p in path.split("/") if p]
        if len(parts) == 1:
            return self.getChildren(path)
        return self.__childrenAtPath(parts)

    def getChildren(self, name=None, ns=None):
        """
        Get a list of children by (optional) name and/or (optional) namespace.

        @param name: The name of a child element (may contain a prefix).
        @type name: basestring
        @param ns: An optional namespace used to match the child.
        @type ns: (I{prefix}, I{name})
        @return: The list of matching children.
        @rtype: [L{Element},...]

        """
        if ns is None:
            if name is None:
                return self.children
            prefix, name = splitPrefix(name)
            if prefix is not None:
                ns = self.resolvePrefix(prefix)
        return [c for c in self.children if c.match(name, ns)]

    def detachChildren(self):
        """
        Detach and return this element's children.

        @return: The element's children (detached).
        @rtype: [L{Element},...]

        """
        detached = self.children
        self.children = []
        for child in detached:
            child.parent = None
        return detached

    def resolvePrefix(self, prefix, default=Namespace.default):
        """
        Resolve the specified prefix to a namespace. The I{nsprefixes} is
        searched. If not found, walk up the tree until either resolved or the
        top of the tree is reached. Searching up the tree provides for
        inherited mappings.

        @param prefix: A namespace prefix to resolve.
        @type prefix: basestring
        @param default: An optional value to be returned when the prefix cannot
            be resolved.
        @type default: (I{prefix}, I{URI})
        @return: The namespace that is mapped to I{prefix} in this context.
        @rtype: (I{prefix}, I{URI})

        """
        n = self
        while n is not None:
            if prefix in n.nsprefixes:
                return prefix, n.nsprefixes[prefix]
            if prefix in self.specialprefixes:
                return prefix, self.specialprefixes[prefix]
            n = n.parent
        return default

    def addPrefix(self, p, u):
        """
        Add or update a prefix mapping.

        @param p: A prefix.
        @type p: basestring
        @param u: A namespace URI.
        @type u: basestring
        @return: self
        @rtype: L{Element}
        """
        self.nsprefixes[p] = u
        return self

    def updatePrefix(self, p, u):
        """
        Update (redefine) a prefix mapping for the branch.

        @param p: A prefix.
        @type p: basestring
        @param u: A namespace URI.
        @type u: basestring
        @return: self
        @rtype: L{Element}
        @note: This method traverses down the entire branch!

        """
        if p in self.nsprefixes:
            self.nsprefixes[p] = u
        for c in self.children:
            c.updatePrefix(p, u)
        return self

    def clearPrefix(self, prefix):
        """
        Clear the specified prefix from the prefix mappings.

        @param prefix: A prefix to clear.
        @type prefix: basestring
        @return: self
        @rtype: L{Element}

        """
        if prefix in self.nsprefixes:
            del self.nsprefixes[prefix]
        return self

    def findPrefix(self, uri, default=None):
        """
        Find the first prefix that has been mapped to a namespace URI.

        The local mapping is searched, then walks up the tree until it reaches
        the top or finds a match.

        @param uri: A namespace URI.
        @type uri: basestring
        @param default: A default prefix when not found.
        @type default: basestring
        @return: A mapped prefix.
        @rtype: basestring

        """
        for item in list(self.nsprefixes.items()):
            if item[1] == uri:
                return item[0]
        for item in list(self.specialprefixes.items()):
            if item[1] == uri:
                return item[0]
        if self.parent is not None:
            return self.parent.findPrefix(uri, default)
        return default

    def findPrefixes(self, uri, match="eq"):
        """
        Find all prefixes that have been mapped to a namespace URI.

        The local mapping is searched, then walks up the tree until it reaches
        the top, collecting all matches.

        @param uri: A namespace URI.
        @type uri: basestring
        @param match: A matching function L{Element.matcher}.
        @type match: basestring
        @return: A list of mapped prefixes.
        @rtype: [basestring,...]

        """
        result = []
        for item in list(self.nsprefixes.items()):
            if self.matcher[match](item[1], uri):
                prefix = item[0]
                result.append(prefix)
        for item in list(self.specialprefixes.items()):
            if self.matcher[match](item[1], uri):
                prefix = item[0]
                result.append(prefix)
        if self.parent is not None:
            result += self.parent.findPrefixes(uri, match)
        return result

    def promotePrefixes(self):
        """
        Push prefix declarations up the tree as far as possible.

        Prefix mapping are pushed to its parent unless the parent has the
        prefix mapped to another URI or the parent has the prefix. This is
        propagated up the tree until the top is reached.

        @return: self
        @rtype: L{Element}

        """
        for c in self.children:
            c.promotePrefixes()
        if self.parent is None:
            return
        for p, u in list(self.nsprefixes.items()):
            if p in self.parent.nsprefixes:
                pu = self.parent.nsprefixes[p]
                if pu == u:
                    del self.nsprefixes[p]
                continue
            if p != self.parent.prefix:
                self.parent.nsprefixes[p] = u
                del self.nsprefixes[p]
        return self

    def refitPrefixes(self):
        """
        Refit namespace qualification by replacing prefixes with explicit
        namespaces. Also purges prefix mapping table.

        @return: self
        @rtype: L{Element}

        """
        for c in self.children:
            c.refitPrefixes()
        if self.prefix is not None:
            ns = self.resolvePrefix(self.prefix)
            if ns[1] is not None:
                self.expns = ns[1]
        self.prefix = None
        self.nsprefixes = {}
        return self

    def normalizePrefixes(self):
        """
        Normalize the namespace prefixes.

        This generates unique prefixes for all namespaces. Then retrofits all
        prefixes and prefix mappings. Further, it will retrofix attribute
        values that have values containing (:).

        @return: self
        @rtype: L{Element}

        """
        PrefixNormalizer.apply(self)
        return self

    def isempty(self, content=True):
        """
        Get whether the element has no children.

        @param content: Test content (children & text) only.
        @type content: boolean
        @return: True when element has not children.
        @rtype: boolean

        """
        nochildren = not self.children
        notext = self.text is None
        nocontent = nochildren and notext
        if content:
            return nocontent
        noattrs = not len(self.attributes)
        return nocontent and noattrs

    def isnil(self):
        """
        Get whether the element is I{nil} as defined by having an
        I{xsi:nil="true"} attribute.

        @return: True if I{nil}, else False
        @rtype: boolean

        """
        nilattr = self.getAttribute("nil", ns=Namespace.xsins)
        return nilattr is not None and (nilattr.getValue().lower() == "true")

    def setnil(self, flag=True):
        """
        Set this node to I{nil} as defined by having an I{xsi:nil}=I{flag}
        attribute.

        @param flag: A flag indicating how I{xsi:nil} will be set.
        @type flag: boolean
        @return: self
        @rtype: L{Element}

        """
        p, u = Namespace.xsins
        name = ":".join((p, "nil"))
        self.set(name, str(flag).lower())
        self.addPrefix(p, u)
        if flag:
            self.text = None
        return self

    def applyns(self, ns):
        """
        Apply the namespace to this node.

        If the prefix is I{None} then this element's explicit namespace
        I{expns} is set to the URI defined by I{ns}. Otherwise, the I{ns} is
        simply mapped.

        @param ns: A namespace.
        @type ns: (I{prefix}, I{URI})

        """
        if ns is None:
            return
        if not isinstance(ns, (list, tuple)):
            raise Exception("namespace must be a list or a tuple")
        if ns[0] is None:
            self.expns = ns[1]
        else:
            self.prefix = ns[0]
            self.nsprefixes[ns[0]] = ns[1]

    def str(self, indent=0):
        """
        Get a string representation of this XML fragment.

        @param indent: The indent to be used in formatting the output.
        @type indent: int
        @return: A I{pretty} string.
        @rtype: basestring

        """
        tab = "%*s" % (indent * 3, "")
        result = []
        result.append("%s<%s" % (tab, self.qname()))
        result.append(self.nsdeclarations())
        for a in self.attributes:
            result.append(" %s" % (str(a),))
        if self.isempty():
            result.append("/>")
            return "".join(result)
        result.append(">")
        if self.hasText():
            result.append(self.text.escape())
        for c in self.children:
            result.append("\n")
            result.append(c.str(indent + 1))
        if len(self.children):
            result.append("\n%s" % (tab,))
        result.append("</%s>" % (self.qname(),))
        return "".join(result)

    def plain(self):
        """
        Get a string representation of this XML fragment.

        @return: A I{plain} string.
        @rtype: basestring

        """
        result = ["<%s" % (self.qname(),), self.nsdeclarations()]
        for a in self.attributes:
            result.append(" %s" % (str(a),))
        if self.isempty():
            result.append("/>")
            return "".join(result)
        result.append(">")
        if self.hasText():
            result.append(self.text.escape())
        for c in self.children:
            result.append(c.plain())
        result.append("</%s>" % (self.qname(),))
        return "".join(result)

    def nsdeclarations(self):
        """
        Get a string representation for all namespace declarations as xmlns=""
        and xmlns:p="".

        @return: A separated list of declarations.
        @rtype: basestring

        """
        s = []
        myns = None, self.expns
        if self.parent is None:
            pns = Namespace.default
        else:
            pns = None, self.parent.expns
        if myns[1] != pns[1]:
            if self.expns is not None:
                s.append(' xmlns="%s"' % (self.expns,))
        for item in list(self.nsprefixes.items()):
            p, u = item
            if self.parent is not None:
                ns = self.parent.resolvePrefix(p)
                if ns[1] == u:
                    continue
            s.append(' xmlns:%s="%s"' % (p, u))
        return "".join(s)

    def match(self, name=None, ns=None):
        """
        Match by (optional) name and/or (optional) namespace.

        @param name: The optional element tag name.
        @type name: str
        @param ns: An optional namespace.
        @type ns: (I{prefix}, I{name})
        @return: True if matched.
        @rtype: boolean

        """
        byname = name is None or (self.name == name)
        byns = ns is None or (self.namespace()[1] == ns[1])
        return byname and byns

    def branch(self):
        """
        Get a flattened representation of the branch.

        @return: A flat list of nodes.
        @rtype: [L{Element},...]

        """
        branch = [self]
        for c in self.children:
            branch += c.branch()
        return branch

    def ancestors(self):
        """
        Get a list of ancestors.

        @return: A list of ancestors.
        @rtype: [L{Element},...]

        """
        ancestors = []
        p = self.parent
        while p is not None:
            ancestors.append(p)
            p = p.parent
        return ancestors

    def walk(self, visitor):
        """
        Walk the branch and call the visitor function on each node.

        @param visitor: A function.
        @type visitor: single argument function
        @return: self
        @rtype: L{Element}

        """
        visitor(self)
        for c in self.children:
            c.walk(visitor)
        return self

    def prune(self):
        """Prune the branch of empty nodes."""
        pruned = []
        for c in self.children:
            c.prune()
            if c.isempty(False):
                pruned.append(c)
        for p in pruned:
            self.children.remove(p)

    def __childrenAtPath(self, parts):
        result = []
        node = self
        ancestors = parts[:-1]
        leaf = parts[-1]
        for name in ancestors:
            ns = None
            prefix, name = splitPrefix(name)
            if prefix is not None:
                ns = node.resolvePrefix(prefix)
            child = node.getChild(name, ns)
            if child is None:
                break
            node = child
        if child is not None:
            ns = None
            prefix, leaf = splitPrefix(leaf)
            if prefix is not None:
                ns = node.resolvePrefix(prefix)
            result = child.getChildren(leaf)
        return result

    def __len__(self):
        return len(self.children)

    def __getitem__(self, index):
        if isinstance(index, str):
            return self.get(index)
        if index < len(self.children):
            return self.children[index]

    def __setitem__(self, index, value):
        if isinstance(index, str):
            self.set(index, value)
        else:
            if index < len(self.children) and isinstance(value, Element):
                self.children.insert(index, value)

    def __eq__(self, rhs):
        return (isinstance(rhs, Element) and
            self.match(rhs.name, rhs.namespace()))

    def __repr__(self):
        return "Element (prefix=%s, name=%s)" % (self.prefix, self.name)

    def __unicode__(self):
        return self.str()

    def __iter__(self):
        return NodeIterator(self)


class NodeIterator:
    """
    The L{Element} child node iterator.

    @ivar pos: The current position
    @type pos: int
    @ivar children: A list of a child nodes.
    @type children: [L{Element},...]

    """

    def __init__(self, parent):
        """
        @param parent: An element to iterate.
        @type parent: L{Element}

        """
        self.pos = 0
        self.children = parent.children

    def __next__(self):
        """
        Get the next child.

        @return: The next child.
        @rtype: L{Element}
        @raise StopIterator: At the end.

        """
        try:
            child = self.children[self.pos]
            self.pos += 1
            return child
        except Exception:
            raise StopIteration()


class PrefixNormalizer:
    """
    The prefix normalizer provides namespace prefix normalization.

    @ivar node: A node to normalize.
    @type node: L{Element}
    @ivar branch: The nodes flattened branch.
    @type branch: [L{Element},...]
    @ivar namespaces: A unique list of namespaces (URI).
    @type namespaces: [str,...]
    @ivar prefixes: A reverse dict of prefixes.
    @type prefixes: {u: p}

    """

    @classmethod
    def apply(cls, node):
        """
        Normalize the specified node.

        @param node: A node to normalize.
        @type node: L{Element}
        @return: The normalized node.
        @rtype: L{Element}

        """
        return PrefixNormalizer(node).refit()

    def __init__(self, node):
        """
        @param node: A node to normalize.
        @type node: L{Element}

        """
        self.node = node
        self.branch = node.branch()
        self.namespaces = self.getNamespaces()
        self.prefixes = self.genPrefixes()

    def getNamespaces(self):
        """
        Get the I{unique} set of namespaces referenced in the branch.

        @return: A set of namespaces.
        @rtype: set

        """
        s = set()
        for n in self.branch + self.node.ancestors():
            if self.permit(n.expns):
                s.add(n.expns)
            s = s.union(self.pset(n))
        return s

    def pset(self, n):
        """
        Convert the nodes nsprefixes into a set.

        @param n: A node.
        @type n: L{Element}
        @return: A set of namespaces.
        @rtype: set

        """
        s = set()
        for ns in list(n.nsprefixes.items()):
            if self.permit(ns):
                s.add(ns[1])
        return s

    def genPrefixes(self):
        """
        Generate a I{reverse} mapping of unique prefixes for all namespaces.

        @return: A reverse dict of prefixes.
        @rtype: {u: p}

        """
        prefixes = {}
        n = 0
        for u in self.namespaces:
            prefixes[u] = "ns%d" % (n,)
            n += 1
        return prefixes

    def refit(self):
        """Refit (normalize) the prefixes in the node."""
        self.refitNodes()
        self.refitMappings()

    def refitNodes(self):
        """Refit (normalize) all of the nodes in the branch."""
        for n in self.branch:
            if n.prefix is not None:
                ns = n.namespace()
                if self.permit(ns):
                    n.prefix = self.prefixes[ns[1]]
            self.refitAttrs(n)

    def refitAttrs(self, n):
        """
        Refit (normalize) all of the attributes in the node.

        @param n: A node.
        @type n: L{Element}

        """
        for a in n.attributes:
            self.refitAddr(a)

    def refitAddr(self, a):
        """
        Refit (normalize) the attribute.

        @param a: An attribute.
        @type a: L{Attribute}

        """
        if a.prefix is not None:
            ns = a.namespace()
            if self.permit(ns):
                a.prefix = self.prefixes[ns[1]]
        self.refitValue(a)

    def refitValue(self, a):
        """
        Refit (normalize) the attribute's value.

        @param a: An attribute.
        @type a: L{Attribute}

        """
        p, name = splitPrefix(a.getValue())
        if p is None:
            return
        ns = a.resolvePrefix(p)
        if self.permit(ns):
            p = self.prefixes[ns[1]]
            a.setValue(":".join((p, name)))

    def refitMappings(self):
        """Refit (normalize) all of the nsprefix mappings."""
        for n in self.branch:
            n.nsprefixes = {}
        n = self.node
        for u, p in list(self.prefixes.items()):
            n.addPrefix(p, u)

    def permit(self, ns):
        """
        Get whether the I{ns} is to be normalized.

        @param ns: A namespace.
        @type ns: (p, u)
        @return: True if to be included.
        @rtype: boolean

        """
        return not self.skip(ns)

    def skip(self, ns):
        """
        Get whether the I{ns} is to B{not} be normalized.

        @param ns: A namespace.
        @type ns: (p, u)
        @return: True if to be skipped.
        @rtype: boolean

        """
        return ns is None or ns in (
            Namespace.default,
            Namespace.xsdns,
            Namespace.xsins,
            Namespace.xmlns)