summaryrefslogtreecommitdiffstats
path: root/Lib/xml/dom/minidom.py
blob: 32d2d2b1bfa89eb5403af83d810120f600e56f13 (plain)
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
import pulldom
import string
from StringIO import StringIO
import types

"""
minidom.py -- a lightweight DOM implementation based on SAX.

Todo:
=====
 * convenience methods for getting elements and text.
 * more testing
 * bring some of the writer and linearizer code into conformance with this
        interface
 * SAX 2 namespaces
"""

class Node:
    ELEMENT_NODE                = 1
    ATTRIBUTE_NODE              = 2
    TEXT_NODE                   = 3
    CDATA_SECTION_NODE          = 4
    ENTITY_REFERENCE_NODE       = 5
    ENTITY_NODE                 = 6
    PROCESSING_INSTRUCTION_NODE = 7
    COMMENT_NODE                = 8
    DOCUMENT_NODE               = 9
    DOCUMENT_TYPE_NODE          = 10
    DOCUMENT_FRAGMENT_NODE      = 11
    NOTATION_NODE               = 12

    allnodes=[]

    def __init__( self ):
        self.childNodes=[]
        Node.allnodes.append( repr( id( self ))+repr( self.__class__ ))

    def __getattr__( self, key ):
        if key[0:2]=="__": raise AttributeError
        # getattr should never call getattr!
        if self.__dict__.has_key("inGetAttr"): 
            del self.inGetAttr
            raise AttributeError, key

        prefix,attrname=key[:5],key[5:]
        if prefix=="_get_":
            self.inGetAttr=1
            if hasattr( self, attrname ): 
                del self.inGetAttr
                return (lambda self=self, attrname=attrname: 
                                getattr( self, attrname ))
            else:
                del self.inGetAttr
                raise AttributeError, key
        else:
            self.inGetAttr=1
            try:
                func = getattr( self, "_get_"+key )
            except AttributeError:
                raise AttributeError, key
            del self.inGetAttr
            return func()

    def __nonzero__(self): return 1

    def toxml( self ):
        writer=StringIO()
        self.writexml( writer )
        return writer.getvalue()

    def hasChildNodes( self ):
        if self.childNodes: return 1
        else: return 0

    def insertBefore( self, newChild, refChild):
        index=self.childNodes.index( refChild )
        self.childNodes.insert( index, newChild )

    def appendChild( self, node ):
        self.childNodes.append( node )

    def unlink( self ):
        self.parentNode=None
        while self.childNodes:
            self.childNodes[-1].unlink()
            del self.childNodes[-1] # probably not most efficient!
        self.childNodes=None
        if self.attributes:
            for attr in self.attributes.values():
                attr.unlink()
        self.attributes=None
        index=Node.allnodes.index( repr( id( self ))+repr( self.__class__ ))
        del Node.allnodes[index]

def _write_data( writer, data):
    "Writes datachars to writer."
    data=string.replace(data,"&","&")
    data=string.replace(data,"<","&lt;")
    data=string.replace(data,"\"","&quot;")
    data=string.replace(data,">","&gt;")
    writer.write(data)

def _closeElement( element ):
    del element.parentNode
    for node in element.elements:
        _closeElement( node )

def _getElementsByTagNameHelper( parent, name, rc ):
    for node in parent.childNodes:
        if node.nodeType==Node.ELEMENT_NODE and\
            (name=="*" or node.tagName==name):
            rc.append( node )
        _getElementsByTagNameHelper( node, name, rc )
    return rc

def _getElementsByTagNameNSHelper( parent, nsURI, localName, rc ):
    for node in parent.childNodes:
        if (node.nodeType==Node.ELEMENT_NODE ):
            if ((localName=="*" or node.tagName==localName) and
            (nsURI=="*" or node.namespaceURI==nsURI)):
                rc.append( node )
            _getElementsByTagNameNSHelper( node, name, rc )

class Attr(Node):
    nodeType=Node.ATTRIBUTE_NODE
    def __init__( self, qName, namespaceURI="", prefix="",
                  localName=None ):
        Node.__init__( self )
        assert qName
        # skip setattr for performance
        self.__dict__["nodeName"] = self.__dict__["name"] = qName
        self.__dict__["localName"]=localName or qName
        self.__dict__["prefix"]=prefix
        self.__dict__["namespaceURI"]=namespaceURI
        # nodeValue and value are set elsewhere
        self.attributes=None

    def __setattr__( self, name, value ):
        if name in ("value", "nodeValue" ):
            self.__dict__["value"]=self.__dict__["nodeValue"]=value
        else:
            self.__dict__[name]=value

class AttributeList:
    # the attribute list is a transient interface to the underlying dictionaries
    # mutations here will change the underlying element's dictionary
    def __init__( self, attrs, attrsNS ):
        self.__attrs=attrs
        self.__attrsNS=attrs
        self.length=len( self.__attrs.keys() )

    def item( self, index ):
        try:
            return self[self.keys()[index]]
        except IndexError:
            return None
        
    def items( self ):
        return map( lambda node: (node.tagName, node.value),
                    self.__attrs.values() )

    def itemsNS( self ):
        return map( lambda node: ((node.URI, node.localName), node.value),
                    self.__attrs.values() )
    
    def keys( self ):
        return self.__attrs.keys()

    def keysNS( self ):
        return self.__attrsNS.keys()

    def values( self ):
        return self.__attrs.values()

    def __len__( self ):
        return self.length

    def __cmp__( self, other ):
        if self.__attrs is other.__attrs: 
            return 0
        else: 
            return cmp( id( self ), id( other ) )

    #FIXME: is it appropriate to return .value?
    def __getitem__( self, attname_or_tuple ):
        if type( attname_or_tuple ) == type( (1,2) ):
            return self.__attrsNS[attname_or_tuple].value
        else:
            return self.__attrs[attname_or_tuple].value

    def __setitem__( self, attname ):
        raise TypeError, "object does not support item assignment"
        
class Element( Node ):
    nodeType=Node.ELEMENT_NODE
    def __init__( self, tagName, namespaceURI="", prefix="",
                  localName=None ):
        Node.__init__( self )
        self.tagName = self.nodeName = tagName
        self.localName=localName or tagName
        self.prefix=prefix
        self.namespaceURI=namespaceURI
        self.nodeValue=None

        self.__attrs={}  # attributes are double-indexed:
        self.__attrsNS={}#    tagName -> Attribute
                #    URI,localName -> Attribute
                # in the future: consider lazy generation of attribute objects
                #                this is too tricky for now because of headaches
                #                with namespaces.

    def getAttribute( self, attname ):
        return self.__attrs[attname].value

    def getAttributeNS( self, namespaceURI, localName ):
        return self.__attrsNS[(namespaceURI, localName)].value
    
    def setAttribute( self, attname, value ):
        attr=Attr( attname )
        # for performance
        attr.__dict__["value"]=attr.__dict__["nodeValue"]=value
        self.setAttributeNode( attr )

    def setAttributeNS( self, namespaceURI, qualifiedName, value ):
        attr=createAttributeNS( namespaceURI, qualifiedName )
        # for performance
        attr.__dict__["value"]=attr.__dict__["nodeValue"]=value
        self.setAttributeNode( attr )

    def setAttributeNode( self, attr ):
        self.__attrs[attr.name]=attr
        self.__attrsNS[(attr.namespaceURI,attr.localName)]=attr

    def removeAttribute( self, name ):
        attr = self.__attrs[name]
        self.removeAttributeNode( attr )

    def removeAttributeNS( self, namespaceURI, localName ):
        attr = self.__attrsNS[(uri, localName)]
        self.removeAttributeNode( attr )

    def removeAttributeNode( self, node ):
        del self.__attrs[node.name]
        del self.__attrsNS[(node.namespaceURI, node.localName)]
        
    def getElementsByTagName( self, name ):
        return _getElementsByTagNameHelper( self, name, [] )

    def getElementsByTagNameNS(self,namespaceURI,localName):
        _getElementsByTagNameNSHelper( self, namespaceURI, localName, [] )

    def __repr__( self ):
        return "<DOM Element:"+self.tagName+" at "+`id( self )` +" >"

    def writexml(self, writer):
        writer.write("<"+self.tagName)
            
        a_names=self._get_attributes().keys()
        a_names.sort()

        for a_name in a_names:
            writer.write(" "+a_name+"=\"")
            _write_data(writer, self._get_attributes()[a_name])
            writer.write("\"")
        if self.childNodes:
            writer.write(">")
            for node in self.childNodes:
                node.writexml( writer )
            writer.write("</"+self.tagName+">")
        else:
            writer.write("/>")

    def _get_attributes( self ):
        return AttributeList( self.__attrs, self.__attrsNS )

class Comment( Node ):
    nodeType=Node.COMMENT_NODE
    def __init__(self, data ):
        Node.__init__( self )
        self.data=self.nodeValue=data
        self.nodeName="#comment"
        self.attributes=None

    def writexml( self, writer ):
        writer.write( "<!--" + self.data + "-->" )

class ProcessingInstruction( Node ):
    nodeType=Node.PROCESSING_INSTRUCTION_NODE
    def __init__(self, target, data ):
        Node.__init__( self )
        self.target = self.nodeName = target
        self.data = self.nodeValue = data
        self.attributes=None

    def writexml( self, writer ):
        writer.write( "<?" + self.target +" " + self.data+ "?>" )

class Text( Node ):
    nodeType=Node.TEXT_NODE
    nodeName="#text"
    def __init__(self, data ):
        Node.__init__( self )
        self.data = self.nodeValue = data
        self.attributes=None

    def __repr__(self):
        if len( self.data )> 10:
            dotdotdot="..."
        else:
            dotdotdot=""
        return "<DOM Text node \"" + self.data[0:10] + dotdotdot+"\">"

    def writexml( self, writer ):
        _write_data( writer, self.data )

class Document( Node ):
    nodeType=Node.DOCUMENT_NODE
    def __init__( self ):
        Node.__init__( self )
        self.documentElement=None
        self.attributes=None
        self.nodeName="#document"
        self.nodeValue=None

    createElement=Element

    createTextNode=Text

    createComment=Comment

    createProcessingInstruction=ProcessingInstruction

    createAttribute=Attr

    def createElementNS(self, namespaceURI, qualifiedName):
        fields = string.split(qualifiedName, ':')
        if len(fields) == 2:
            prefix = fields[0]
            localName = fields[1]
        elif len(fields) == 1:
            prefix = ''
            localName = fields[0]            
        return Element(self, qualifiedName, namespaceURI, prefix, localName)

    def createAttributeNS(self, namespaceURI, qualifiedName):
        fields = string.split(qualifiedName,':')
        if len(fields) == 2:
            localName = fields[1]
            prefix = fields[0]
        elif len(fields) == 1:
            localName = fields[0]
            prefix = None
        return Attr(qualifiedName, namespaceURI, prefix, localName)

    def getElementsByTagNameNS(self,namespaceURI,localName):
        _getElementsByTagNameNSHelper( self, namespaceURI, localName )

    def close( self ):
        for node in self.elements:
            _closeElement( node )

    def unlink( self ):
        self.documentElement=None
        Node.unlink( self )

    def getElementsByTagName( self, name ):
        rc=[]
        _getElementsByTagNameHelper( self, name, rc )
        return rc

    def writexml( self, writer ):
        for node in self.childNodes:
            node.writexml( writer )

def _doparse( func, args, kwargs ):
    events=apply( func, args, kwargs )
    (toktype, rootNode)=events.getEvent()
    events.expandNode( rootNode )
    return rootNode

def parse( *args, **kwargs ):
    return _doparse( pulldom.parse, args, kwargs )

def parseString( *args, **kwargs ):
    return _doparse( pulldom.parseString, args, kwargs )