summaryrefslogtreecommitdiffstats
path: root/xml/sax/drivers/drv_pyexpat.py
blob: 5dcd6a6d7da2d19bca8bb4924129e8904d355b64 (plain) (blame)
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
# -*- coding: iso-8859-1 -*-
"""
SAX driver for the Pyexpat C module.

$Id: drv_pyexpat.py,v 1.19 2004/11/29 13:38:23 loewis Exp $
"""

# Event handling can be speeded up by bypassing the driver for some events.
# This will be implemented later when I can test this driver.
#
# This driver has been much improved by Geir Ove Gr�nmo.

version="0.13"

from xml.sax import saxlib, saxutils, SAXReaderNotAvailable

try:
    from xml.parsers import expat
except ImportError:
    raise SAXReaderNotAvailable("expat not supported",None)

import urllib2,types

# --- SAX_expat

class SAX_expat(saxlib.Parser,saxlib.Locator):
    "SAX driver for the Pyexpat C module."

    def __init__(self):
        saxlib.Parser.__init__(self)
        self.reset()

    def startElement(self,name,attrs):
        at = {}
        # Backward compatibility code, for older versions of the
        # PyExpat module
        if type(attrs) == type({}):
            at = attrs
        else:
            # Assume it's a list containing alternating names & values
            at = {}
            for i in range(0, len(attrs), 2):
                at[attrs[i]] = attrs[i+1]

        self.doc_handler.startElement(name,saxutils.AttributeMap(at))

    # FIXME: bypass!
    def endElement(self,name):
        self.doc_handler.endElement(name)

    def characters(self,data):
        self.doc_handler.characters(data,0,len(data))

    # FIXME: bypass!
    def processingInstruction(self,target,data):
        self.doc_handler.processingInstruction(target,data)

    def parse(self,sysID):
        self.parseFile(urllib2.urlopen(sysID),sysID)

    def parseFile(self,fileobj,sysID=None):
        self.reset()
        self.sysID=sysID
        self.doc_handler.startDocument()

        buf = fileobj.read(16384)
        while buf != "":
            if self.parser.Parse(buf, 0) != 1:
                self.__report_error()
            buf = fileobj.read(16384)
        self.parser.Parse("", 1)

        self.doc_handler.endDocument()
        self.close(needFinal=0)

    # --- Locator methods. Only usable after errors.

    def getSystemId(self):
        if self.sysID!=None:
            return self.sysID
        else:
            return "Unknown"

    def getLineNumber(self):
        return self.parser.ErrorLineNumber

    def getColumnNumber(self):
        return self.parser.ErrorColumnNumber

    # --- Internal

    def __report_error(self):
        errc=self.parser.ErrorCode
        msg=expat.ErrorString(errc)
        exc=saxlib.SAXParseException(msg,None,self)
        self.err_handler.fatalError(exc)

    # --- EXPERIMENTAL PYTHON SAX EXTENSIONS

    def get_parser_name(self):
        return "pyexpat"

    def get_parser_version(self):
        return "Unknown"

    def get_driver_version(self):
        return version

    def is_validating(self):
        return 0

    def is_dtd_reading(self):
        return 0

    def reset(self):
        self.sysID=None
        self.parser=expat.ParserCreate()
        self.parser.StartElementHandler = self.startElement
        self.parser.EndElementHandler = self.endElement
        self.parser.CharacterDataHandler = self.characters
        self.parser.ProcessingInstructionHandler = self.processingInstruction
        self.doc_handler.setDocumentLocator(self)

    def feed(self, data):
        if self.parser.Parse(data, 0) != 1:
            self.__report_error()

    def close(self, needFinal=1):
        if self.parser is None:
            # make sure close is idempotent
            return
        if needFinal:
            if self.parser.Parse("", 1) != 1:
                self.__report_error()
        self.parser = None

# --- An expat driver that uses the lazy map

class LazyExpatDriver(SAX_expat):

    def __init__(self):
        SAX_expat.__init__(self)
        self.map=LazyAttributeMap([])

    def startElement(self,name,attrs):
        self.map.list=attrs
        self.doc_handler.startElement(name,self.map)

# --- A lazy attribute map

# This avoids the costly conversion from a list to a hash table

class LazyAttributeMap:
    """A lazy implementation of AttributeList that takes an
    [attr,val,attr,val,...] list and uses it to implement the AttributeList
    interface."""

    def __init__(self, list):
        self.list=list

    def getLength(self):
        return len(self.list)/2

    def getName(self, i):
        try:
            return self.list[2*i]
        except IndexError,e:
            return None

    def getType(self, i):
        return "CDATA"

    def getValue(self, i):
        try:
            if type(i)==types.IntType:
                return self.list[2*i+1]
            else:
                for ix in range(0,len(self.list),2):
                    if self.list[ix]==i:
                        return self.list[ix+1]

                return None
        except IndexError,e:
            return None

    def __len__(self):
        return len(self.list)/2

    def __getitem__(self, key):
        if type(key)==types.IntType:
            return self.list[2*key+1]
        else:
            for ix in range(0,len(self.list),2):
                if self.list[ix]==key:
                    return self.list[ix+1]

            return None

    def items(self):
        result=[""]*(len(self.list)/2)
        for ix in range(0,len(self.list),2):
            result[ix/2]=(self.list[ix],self.list[ix+1])
        return result

    def keys(self):
        result=[""]*(len(self.list)/2)
        for ix in range(0,len(self.list),2):
            result[ix/2]=self.list[ix]
        return result

    def has_key(self,key):
        for ix in range(0,len(self.list),2):
            if self.list[ix]==key:
                return 1

        return 0

    def get(self, key, alternative):
        for ix in range(0,len(self.list),2):
            if self.list[ix]==key:
                return self.list[ix+1]

        return alternative

# ---

def create_parser():
    return SAX_expat()