summaryrefslogtreecommitdiffstats
path: root/Lib/xml/dom/pulldom.py
blob: 9f93d6aa9de7a43fab811d72cb019a5f633ffd3d (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
import minidom
import xml.sax,xml.sax.handler

START_ELEMENT = "START_ELEMENT"
END_ELEMENT = "END_ELEMENT"
COMMENT = "COMMENT"
START_DOCUMENT = "START_DOCUMENT"
END_DOCUMENT = "END_DOCUMENT"
PROCESSING_INSTRUCTION = "PROCESSING_INSTRUCTION"
IGNORABLE_WHITESPACE = "IGNORABLE_WHITESPACE"
CHARACTERS = "CHARACTERS"

class PullDOM(xml.sax.ContentHandler):
    def __init__(self):
        self.firstEvent = [None, None]
        self.lastEvent = self.firstEvent
        self._ns_contexts = [{}] # contains uri -> prefix dicts
        self._current_context = self._ns_contexts[-1]

    def setDocumentLocator(self, locator): pass

    def startPrefixMapping(self, prefix, uri):
        self._ns_contexts.append(self._current_context.copy())
        self._current_context[uri] = prefix

    def endPrefixMapping(self, prefix):
        del self._ns_contexts[-1]

    def startElementNS(self, name, tagName , attrs):
        if name[0]:
            # When using namespaces, the reader may or may not
            # provide us with the original name. If not, create
            # *a* valid tagName from the current context.
            if tagName is None:
                tagName = self._current_context[name[0]] + ":" + name[1]
            node = self.document.createElementNS(name[0], tagName)
        else:
            # When the tagname is not prefixed, it just appears as
            # name[1]
            node = self.document.createElement(name[1])

        for aname,value in attrs.items():
            if aname[0]:
                qname = self._current_context[name[0]] + ":" + aname[1]
                attr = self.document.createAttributeNS(name[0], qname)
            else:
                attr = self.document.createAttribute(name[0], name[1])
            attr.value = value
            node.setAttributeNode(qname, attr)
        
        parent = self.curNode
        node.parentNode = parent
        if parent.childNodes:
            node.previousSibling = parent.childNodes[-1]
            node.previousSibling.nextSibling = node
        self.curNode = node

        self.lastEvent[1] = [(START_ELEMENT, node), None]
        self.lastEvent = self.lastEvent[1]
        #self.events.append((START_ELEMENT, node))

    def endElementNS(self, name, tagName):
        node = self.curNode
        self.lastEvent[1] = [(END_ELEMENT, node), None]
        self.lastEvent = self.lastEvent[1]
        #self.events.append((END_ELEMENT, node))
        self.curNode = node.parentNode

    def comment(self, s):
        node = self.document.createComment(s)
        parent = self.curNode
        node.parentNode = parent
        if parent.childNodes:
            node.previousSibling = parent.childNodes[-1]
            node.previousSibling.nextSibling = node
        self.lastEvent[1] = [(COMMENT, node), None]
        self.lastEvent = self.lastEvent[1]
        #self.events.append((COMMENT, node))

    def processingInstruction(self, target, data):
        node = self.document.createProcessingInstruction(target, data)
        #self.appendChild(node)
        
        parent = self.curNode
        node.parentNode = parent
        if parent.childNodes:
            node.previousSibling = parent.childNodes[-1]
            node.previousSibling.nextSibling = node
        self.lastEvent[1] = [(PROCESSING_INSTRUCTION, node), None]
        self.lastEvent = self.lastEvent[1]
        #self.events.append((PROCESSING_INSTRUCTION, node))

    def ignorableWhitespace(self, chars):
        node = self.document.createTextNode(chars[start:start + length])
        parent = self.curNode
        node.parentNode = parent
        if parent.childNodes:
            node.previousSibling = parent.childNodes[-1]
            node.previousSibling.nextSibling = node
        self.lastEvent[1] = [(IGNORABLE_WHITESPACE, node), None]
        self.lastEvent = self.lastEvent[1]
        #self.events.append((IGNORABLE_WHITESPACE, node))

    def characters(self, chars):
        node = self.document.createTextNode(chars)
        node.parentNode = self.curNode
        self.lastEvent[1] = [(CHARACTERS, node), None]
        self.lastEvent = self.lastEvent[1]

    def startDocument(self):
        node = self.curNode = self.document = minidom.Document()
        node.parentNode = None
        self.lastEvent[1] = [(START_DOCUMENT, node), None]
        self.lastEvent = self.lastEvent[1]
        #self.events.append((START_DOCUMENT, node))

    def endDocument(self):
        assert not self.curNode.parentNode
        for node in self.curNode.childNodes:
            if node.nodeType == node.ELEMENT_NODE:
                self.document.documentElement = node
        #if not self.document.documentElement:
        #    raise Error, "No document element"

        self.lastEvent[1] = [(END_DOCUMENT, node), None]
        #self.events.append((END_DOCUMENT, self.curNode))

class ErrorHandler:
    def warning(self, exception):
        print exception
    def error(self, exception):
        raise exception 
    def fatalError(self, exception):
        raise exception 

class DOMEventStream:
    def __init__(self, stream, parser, bufsize):
        self.stream = stream
        self.parser = parser
        self.bufsize = bufsize
        self.reset()

    def reset(self):
        self.pulldom = PullDOM()
        # This content handler relies on namespace support
        self.parser.setFeature(xml.sax.handler.feature_namespaces,1)
        self.parser.setContentHandler(self.pulldom)

    def __getitem__(self, pos):
        rc = self.getEvent()
        if rc:
            return rc
        raise IndexError

    def expandNode(self, node):
        event = self.getEvent()
        while event:
            token, cur_node = event
            if cur_node is node:
                return
            if token != END_ELEMENT:
                cur_node.parentNode.appendChild(cur_node)
            event = self.getEvent()

    def getEvent(self):
        if not self.pulldom.firstEvent[1]:
            self.pulldom.lastEvent = self.pulldom.firstEvent
        while not self.pulldom.firstEvent[1]:
            buf=self.stream.read(self.bufsize)
            if not buf:
                #FIXME: why doesn't Expat close work?
                #self.parser.close()
                return None
            self.parser.feed(buf)
        rc = self.pulldom.firstEvent[1][0]
        self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1]
        return rc

default_bufsize = (2 ** 14) - 20

# FIXME: move into sax package for common usage
def parse(stream_or_string, parser=None, bufsize=default_bufsize):
    if type(stream_or_string) is type(""):
        stream = open(stream_or_string)
    else:
        stream = stream_or_string
    if not parser: 
        parser = xml.sax.make_parser()
    return DOMEventStream(stream, parser, bufsize)

def parseString(string, parser=None):
    try:
        from cStringIO import StringIO
    except ImportError:
        from StringIO import StringIO
        
    bufsize = len(string)
    buf = StringIO(string)
    if not parser:
        parser = xml.sax.make_parser()
    return DOMEventStream(buf, parser, bufsize)