blob: cdc861eb6894941941f02061b050a3e553ba5112 [file] [log] [blame]
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +00001"""A parser for XML, using the derived class as static DTD."""
2
Guido van Rossum5d68e8e1997-11-18 15:27:20 +00003# Author: Sjoerd Mullender.
Guido van Rossuma219efa1997-11-18 15:09:54 +00004
5import re
6import string
7
Neal Norwitzab3b9eb2002-04-10 02:04:00 +00008import warnings
9warnings.warn("The xmllib module is obsolete. Use xml.sax instead.",
10 DeprecationWarning)
11del warnings
Guido van Rossuma219efa1997-11-18 15:09:54 +000012
Guido van Rossumb35d6841999-08-26 15:52:33 +000013version = '0.3'
Guido van Rossum7e07b381998-04-03 16:02:39 +000014
Sjoerd Mullenderf7e84e12000-12-06 10:37:57 +000015class Error(RuntimeError):
16 pass
17
Guido van Rossuma219efa1997-11-18 15:09:54 +000018# Regular expressions used for parsing
19
Guido van Rossumeeb2f321998-10-19 13:28:26 +000020_S = '[ \t\r\n]+' # white space
21_opS = '[ \t\r\n]*' # optional white space
22_Name = '[a-zA-Z_:][-a-zA-Z0-9._:]*' # valid XML name
23_QStr = "(?:'[^']*'|\"[^\"]*\")" # quoted XML string
Guido van Rossum7e07b381998-04-03 16:02:39 +000024illegal = re.compile('[^\t\r\n -\176\240-\377]') # illegal chars in content
25interesting = re.compile('[]&<]')
Guido van Rossuma219efa1997-11-18 15:09:54 +000026
Guido van Rossum7e07b381998-04-03 16:02:39 +000027amp = re.compile('&')
28ref = re.compile('&(' + _Name + '|#[0-9]+|#x[0-9a-fA-F]+)[^-a-zA-Z0-9._:]')
Guido van Rossuma219efa1997-11-18 15:09:54 +000029entityref = re.compile('&(?P<name>' + _Name + ')[^-a-zA-Z0-9._:]')
30charref = re.compile('&#(?P<char>[0-9]+[^0-9]|x[0-9a-fA-F]+[^0-9a-fA-F])')
Guido van Rossum7e07b381998-04-03 16:02:39 +000031space = re.compile(_S + '$')
Guido van Rossuma219efa1997-11-18 15:09:54 +000032newline = re.compile('\n')
33
Guido van Rossumeeb2f321998-10-19 13:28:26 +000034attrfind = re.compile(
35 _S + '(?P<name>' + _Name + ')'
36 '(' + _opS + '=' + _opS +
Guido van Rossumd468bd32000-06-21 20:01:10 +000037 '(?P<value>'+_QStr+'|[-a-zA-Z0-9.:+*%?!\(\)_#=~]+))?')
Guido van Rossuma219efa1997-11-18 15:09:54 +000038starttagopen = re.compile('<' + _Name)
Guido van Rossuma219efa1997-11-18 15:09:54 +000039starttagend = re.compile(_opS + '(?P<slash>/?)>')
Guido van Rossumeeb2f321998-10-19 13:28:26 +000040starttagmatch = re.compile('<(?P<tagname>'+_Name+')'
41 '(?P<attrs>(?:'+attrfind.pattern+')*)'+
42 starttagend.pattern)
43endtagopen = re.compile('</')
Guido van Rossum7e07b381998-04-03 16:02:39 +000044endbracket = re.compile(_opS + '>')
Guido van Rossumeeb2f321998-10-19 13:28:26 +000045endbracketfind = re.compile('(?:[^>\'"]|'+_QStr+')*>')
Guido van Rossuma219efa1997-11-18 15:09:54 +000046tagfind = re.compile(_Name)
Guido van Rossum02505e41998-01-29 14:55:24 +000047cdataopen = re.compile(r'<!\[CDATA\[')
48cdataclose = re.compile(r'\]\]>')
Guido van Rossum7e07b381998-04-03 16:02:39 +000049# this matches one of the following:
50# SYSTEM SystemLiteral
51# PUBLIC PubidLiteral SystemLiteral
Guido van Rossumeeb2f321998-10-19 13:28:26 +000052_SystemLiteral = '(?P<%s>'+_QStr+')'
Guido van Rossumd468bd32000-06-21 20:01:10 +000053_PublicLiteral = '(?P<%s>"[-\'\(\)+,./:=?;!*#@$_%% \n\ra-zA-Z0-9]*"|' \
54 "'[-\(\)+,./:=?;!*#@$_%% \n\ra-zA-Z0-9]*')"
Guido van Rossum7e07b381998-04-03 16:02:39 +000055_ExternalId = '(?:SYSTEM|' \
56 'PUBLIC'+_S+_PublicLiteral%'pubid'+ \
57 ')'+_S+_SystemLiteral%'syslit'
58doctype = re.compile('<!DOCTYPE'+_S+'(?P<name>'+_Name+')'
59 '(?:'+_S+_ExternalId+')?'+_opS)
60xmldecl = re.compile('<\?xml'+_S+
Guido van Rossumeeb2f321998-10-19 13:28:26 +000061 'version'+_opS+'='+_opS+'(?P<version>'+_QStr+')'+
Guido van Rossum7e07b381998-04-03 16:02:39 +000062 '(?:'+_S+'encoding'+_opS+'='+_opS+
63 "(?P<encoding>'[A-Za-z][-A-Za-z0-9._]*'|"
64 '"[A-Za-z][-A-Za-z0-9._]*"))?'
65 '(?:'+_S+'standalone'+_opS+'='+_opS+
66 '(?P<standalone>\'(?:yes|no)\'|"(?:yes|no)"))?'+
67 _opS+'\?>')
68procopen = re.compile(r'<\?(?P<proc>' + _Name + ')' + _opS)
Guido van Rossum02505e41998-01-29 14:55:24 +000069procclose = re.compile(_opS + r'\?>')
Guido van Rossuma219efa1997-11-18 15:09:54 +000070commentopen = re.compile('<!--')
71commentclose = re.compile('-->')
72doubledash = re.compile('--')
Guido van Rossum7e07b381998-04-03 16:02:39 +000073attrtrans = string.maketrans(' \r\n\t', ' ')
Guido van Rossuma219efa1997-11-18 15:09:54 +000074
Guido van Rossumb083a9f1998-12-18 20:17:13 +000075# definitions for XML namespaces
76_NCName = '[a-zA-Z_][-a-zA-Z0-9._]*' # XML Name, minus the ":"
77ncname = re.compile(_NCName + '$')
78qname = re.compile('(?:(?P<prefix>' + _NCName + '):)?' # optional prefix
79 '(?P<local>' + _NCName + ')$')
80
81xmlns = re.compile('xmlns(?::(?P<ncname>'+_NCName+'))?$')
Guido van Rossuma219efa1997-11-18 15:09:54 +000082
83# XML parser base class -- find tags and call handler functions.
84# Usage: p = XMLParser(); p.feed(data); ...; p.close().
Guido van Rossum5d68e8e1997-11-18 15:27:20 +000085# The dtd is defined by deriving a class which defines methods with
86# special names to handle tags: start_foo and end_foo to handle <foo>
87# and </foo>, respectively. The data between tags is passed to the
88# parser by calling self.handle_data() with some data as argument (the
Jeremy Hyltona05e2932000-06-28 14:48:01 +000089# data may be split up in arbitrary chunks).
Guido van Rossuma219efa1997-11-18 15:09:54 +000090
91class XMLParser:
Guido van Rossumb083a9f1998-12-18 20:17:13 +000092 attributes = {} # default, to be overridden
93 elements = {} # default, to be overridden
Guido van Rossuma219efa1997-11-18 15:09:54 +000094
Guido van Rossumb35d6841999-08-26 15:52:33 +000095 # parsing options, settable using keyword args in __init__
96 __accept_unquoted_attributes = 0
97 __accept_missing_endtag_name = 0
98 __map_case = 0
99 __accept_utf8 = 0
Sjoerd Mullender5baaf662000-07-04 14:53:12 +0000100 __translate_attribute_references = 1
Guido van Rossumb35d6841999-08-26 15:52:33 +0000101
Guido van Rossuma219efa1997-11-18 15:09:54 +0000102 # Interface -- initialize and reset this instance
Guido van Rossumb35d6841999-08-26 15:52:33 +0000103 def __init__(self, **kw):
Guido van Rossumcc2c2911999-02-02 17:54:38 +0000104 self.__fixed = 0
Raymond Hettinger54f02222002-06-01 14:18:47 +0000105 if 'accept_unquoted_attributes' in kw:
Guido van Rossumb35d6841999-08-26 15:52:33 +0000106 self.__accept_unquoted_attributes = kw['accept_unquoted_attributes']
Raymond Hettinger54f02222002-06-01 14:18:47 +0000107 if 'accept_missing_endtag_name' in kw:
Guido van Rossumb35d6841999-08-26 15:52:33 +0000108 self.__accept_missing_endtag_name = kw['accept_missing_endtag_name']
Raymond Hettinger54f02222002-06-01 14:18:47 +0000109 if 'map_case' in kw:
Guido van Rossumb35d6841999-08-26 15:52:33 +0000110 self.__map_case = kw['map_case']
Raymond Hettinger54f02222002-06-01 14:18:47 +0000111 if 'accept_utf8' in kw:
Guido van Rossumb35d6841999-08-26 15:52:33 +0000112 self.__accept_utf8 = kw['accept_utf8']
Raymond Hettinger54f02222002-06-01 14:18:47 +0000113 if 'translate_attribute_references' in kw:
Sjoerd Mullender5baaf662000-07-04 14:53:12 +0000114 self.__translate_attribute_references = kw['translate_attribute_references']
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000115 self.reset()
Guido van Rossumfe649351999-02-01 15:35:13 +0000116
117 def __fixelements(self):
Guido van Rossumcc2c2911999-02-02 17:54:38 +0000118 self.__fixed = 1
Guido van Rossumfe649351999-02-01 15:35:13 +0000119 self.elements = {}
120 self.__fixdict(self.__dict__)
121 self.__fixclass(self.__class__)
122
123 def __fixclass(self, kl):
124 self.__fixdict(kl.__dict__)
125 for k in kl.__bases__:
126 self.__fixclass(k)
127
128 def __fixdict(self, dict):
Guido van Rossum2e3f7be1999-02-02 22:15:24 +0000129 for key in dict.keys():
Guido van Rossumfe649351999-02-01 15:35:13 +0000130 if key[:6] == 'start_':
Guido van Rossum2e3f7be1999-02-02 22:15:24 +0000131 tag = key[6:]
132 start, end = self.elements.get(tag, (None, None))
Guido van Rossumfe649351999-02-01 15:35:13 +0000133 if start is None:
Guido van Rossum2e3f7be1999-02-02 22:15:24 +0000134 self.elements[tag] = getattr(self, key), end
Guido van Rossumfe649351999-02-01 15:35:13 +0000135 elif key[:4] == 'end_':
Guido van Rossum2e3f7be1999-02-02 22:15:24 +0000136 tag = key[4:]
137 start, end = self.elements.get(tag, (None, None))
Guido van Rossumfe649351999-02-01 15:35:13 +0000138 if end is None:
Guido van Rossum2e3f7be1999-02-02 22:15:24 +0000139 self.elements[tag] = start, getattr(self, key)
Guido van Rossuma219efa1997-11-18 15:09:54 +0000140
141 # Interface -- reset this instance. Loses all unprocessed data
142 def reset(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000143 self.rawdata = ''
144 self.stack = []
145 self.nomoretags = 0
146 self.literal = 0
147 self.lineno = 1
148 self.__at_start = 1
149 self.__seen_doctype = None
150 self.__seen_starttag = 0
Guido van Rossum91c8f591998-12-21 18:15:28 +0000151 self.__use_namespaces = 0
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000152 self.__namespaces = {'xml':None} # xml is implicitly declared
Thomas Wouters7e474022000-07-16 12:04:32 +0000153 # backward compatibility hack: if elements not overridden,
Guido van Rossumcc2c2911999-02-02 17:54:38 +0000154 # fill it in ourselves
155 if self.elements is XMLParser.elements:
156 self.__fixelements()
Guido van Rossuma219efa1997-11-18 15:09:54 +0000157
158 # For derived classes only -- enter literal mode (CDATA) till EOF
159 def setnomoretags(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000160 self.nomoretags = self.literal = 1
Guido van Rossuma219efa1997-11-18 15:09:54 +0000161
162 # For derived classes only -- enter literal mode (CDATA)
163 def setliteral(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000164 self.literal = 1
Guido van Rossuma219efa1997-11-18 15:09:54 +0000165
166 # Interface -- feed some data to the parser. Call this as
167 # often as you want, with as little or as much text as you
168 # want (may include '\n'). (This just saves the text, all the
169 # processing is done by goahead().)
170 def feed(self, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000171 self.rawdata = self.rawdata + data
172 self.goahead(0)
Guido van Rossuma219efa1997-11-18 15:09:54 +0000173
174 # Interface -- handle the remaining data
175 def close(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000176 self.goahead(1)
Guido van Rossumcc2c2911999-02-02 17:54:38 +0000177 if self.__fixed:
178 self.__fixed = 0
179 # remove self.elements so that we don't leak
180 del self.elements
Guido van Rossuma219efa1997-11-18 15:09:54 +0000181
182 # Interface -- translate references
Guido van Rossum7e07b381998-04-03 16:02:39 +0000183 def translate_references(self, data, all = 1):
Sjoerd Mullender5baaf662000-07-04 14:53:12 +0000184 if not self.__translate_attribute_references:
185 return data
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000186 i = 0
187 while 1:
Guido van Rossum7e07b381998-04-03 16:02:39 +0000188 res = amp.search(data, i)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000189 if res is None:
Guido van Rossum7e07b381998-04-03 16:02:39 +0000190 return data
Sjoerd Mullenderf9850642000-07-10 08:09:48 +0000191 s = res.start(0)
192 res = ref.match(data, s)
Guido van Rossum7e07b381998-04-03 16:02:39 +0000193 if res is None:
194 self.syntax_error("bogus `&'")
Sjoerd Mullenderf9850642000-07-10 08:09:48 +0000195 i = s+1
Guido van Rossum7e07b381998-04-03 16:02:39 +0000196 continue
197 i = res.end(0)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000198 str = res.group(1)
Sjoerd Mullenderf9850642000-07-10 08:09:48 +0000199 rescan = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000200 if str[0] == '#':
201 if str[1] == 'x':
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000202 str = chr(int(str[2:], 16))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000203 else:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000204 str = chr(int(str[1:]))
Sjoerd Mullenderf9850642000-07-10 08:09:48 +0000205 if data[i - 1] != ';':
206 self.syntax_error("`;' missing after char reference")
207 i = i-1
Guido van Rossum7e07b381998-04-03 16:02:39 +0000208 elif all:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000209 if str in self.entitydefs:
Sjoerd Mullenderf9850642000-07-10 08:09:48 +0000210 str = self.entitydefs[str]
211 rescan = 1
212 elif data[i - 1] != ';':
213 self.syntax_error("bogus `&'")
214 i = s + 1 # just past the &
215 continue
Guido van Rossum7e07b381998-04-03 16:02:39 +0000216 else:
Guido van Rossumcc2c2911999-02-02 17:54:38 +0000217 self.syntax_error("reference to unknown entity `&%s;'" % str)
Sjoerd Mullenderf9850642000-07-10 08:09:48 +0000218 str = '&' + str + ';'
219 elif data[i - 1] != ';':
220 self.syntax_error("bogus `&'")
221 i = s + 1 # just past the &
222 continue
223
224 # when we get here, str contains the translated text and i points
225 # to the end of the string that is to be replaced
226 data = data[:s] + str + data[i:]
227 if rescan:
228 i = s
Guido van Rossum7e07b381998-04-03 16:02:39 +0000229 else:
Sjoerd Mullenderf9850642000-07-10 08:09:48 +0000230 i = s + len(str)
Guido van Rossuma219efa1997-11-18 15:09:54 +0000231
Sjoerd Mullender1c8feae2000-08-31 10:27:00 +0000232 # Interface - return a dictionary of all namespaces currently valid
233 def getnamespace(self):
234 nsdict = {}
235 for t, d, nst in self.stack:
236 nsdict.update(d)
237 return nsdict
238
Guido van Rossuma219efa1997-11-18 15:09:54 +0000239 # Internal -- handle data as far as reasonable. May leave state
240 # and data to be processed by a subsequent call. If 'end' is
241 # true, force handling all data as if followed by EOF marker.
242 def goahead(self, end):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000243 rawdata = self.rawdata
244 i = 0
245 n = len(rawdata)
246 while i < n:
247 if i > 0:
248 self.__at_start = 0
249 if self.nomoretags:
250 data = rawdata[i:n]
251 self.handle_data(data)
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000252 self.lineno = self.lineno + data.count('\n')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000253 i = n
254 break
255 res = interesting.search(rawdata, i)
256 if res:
Tim Peterse1190062001-01-15 03:34:38 +0000257 j = res.start(0)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000258 else:
Tim Peterse1190062001-01-15 03:34:38 +0000259 j = n
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000260 if i < j:
Guido van Rossum91c8f591998-12-21 18:15:28 +0000261 data = rawdata[i:j]
262 if self.__at_start and space.match(data) is None:
Guido van Rossum7e07b381998-04-03 16:02:39 +0000263 self.syntax_error('illegal data at start of file')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000264 self.__at_start = 0
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000265 if not self.stack and space.match(data) is None:
Guido van Rossum7e07b381998-04-03 16:02:39 +0000266 self.syntax_error('data not in content')
Guido van Rossumb35d6841999-08-26 15:52:33 +0000267 if not self.__accept_utf8 and illegal.search(data):
Guido van Rossum7e07b381998-04-03 16:02:39 +0000268 self.syntax_error('illegal character in content')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000269 self.handle_data(data)
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000270 self.lineno = self.lineno + data.count('\n')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000271 i = j
272 if i == n: break
273 if rawdata[i] == '<':
274 if starttagopen.match(rawdata, i):
275 if self.literal:
276 data = rawdata[i]
277 self.handle_data(data)
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000278 self.lineno = self.lineno + data.count('\n')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000279 i = i+1
280 continue
281 k = self.parse_starttag(i)
282 if k < 0: break
283 self.__seen_starttag = 1
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000284 self.lineno = self.lineno + rawdata[i:k].count('\n')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000285 i = k
286 continue
287 if endtagopen.match(rawdata, i):
288 k = self.parse_endtag(i)
289 if k < 0: break
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000290 self.lineno = self.lineno + rawdata[i:k].count('\n')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000291 i = k
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000292 continue
293 if commentopen.match(rawdata, i):
294 if self.literal:
295 data = rawdata[i]
296 self.handle_data(data)
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000297 self.lineno = self.lineno + data.count('\n')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000298 i = i+1
299 continue
300 k = self.parse_comment(i)
301 if k < 0: break
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000302 self.lineno = self.lineno + rawdata[i:k].count('\n')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000303 i = k
304 continue
305 if cdataopen.match(rawdata, i):
306 k = self.parse_cdata(i)
307 if k < 0: break
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000308 self.lineno = self.lineno + rawdata[i:k].count('\n')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000309 i = k
310 continue
Guido van Rossum7e07b381998-04-03 16:02:39 +0000311 res = xmldecl.match(rawdata, i)
312 if res:
313 if not self.__at_start:
314 self.syntax_error("<?xml?> declaration not at start of document")
315 version, encoding, standalone = res.group('version',
316 'encoding',
317 'standalone')
318 if version[1:-1] != '1.0':
Sjoerd Mullenderf7e84e12000-12-06 10:37:57 +0000319 raise Error('only XML version 1.0 supported')
Guido van Rossum7e07b381998-04-03 16:02:39 +0000320 if encoding: encoding = encoding[1:-1]
321 if standalone: standalone = standalone[1:-1]
322 self.handle_xml(encoding, standalone)
323 i = res.end(0)
324 continue
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000325 res = procopen.match(rawdata, i)
326 if res:
327 k = self.parse_proc(i)
328 if k < 0: break
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000329 self.lineno = self.lineno + rawdata[i:k].count('\n')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000330 i = k
331 continue
332 res = doctype.match(rawdata, i)
333 if res:
334 if self.literal:
335 data = rawdata[i]
336 self.handle_data(data)
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000337 self.lineno = self.lineno + data.count('\n')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000338 i = i+1
339 continue
340 if self.__seen_doctype:
341 self.syntax_error('multiple DOCTYPE elements')
342 if self.__seen_starttag:
343 self.syntax_error('DOCTYPE not at beginning of document')
344 k = self.parse_doctype(res)
345 if k < 0: break
346 self.__seen_doctype = res.group('name')
Guido van Rossumb35d6841999-08-26 15:52:33 +0000347 if self.__map_case:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000348 self.__seen_doctype = self.__seen_doctype.lower()
349 self.lineno = self.lineno + rawdata[i:k].count('\n')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000350 i = k
351 continue
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000352 elif rawdata[i] == '&':
Guido van Rossum204b65c1998-12-08 13:25:00 +0000353 if self.literal:
354 data = rawdata[i]
355 self.handle_data(data)
356 i = i+1
357 continue
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000358 res = charref.match(rawdata, i)
359 if res is not None:
360 i = res.end(0)
361 if rawdata[i-1] != ';':
362 self.syntax_error("`;' missing in charref")
363 i = i-1
Guido van Rossum7e07b381998-04-03 16:02:39 +0000364 if not self.stack:
365 self.syntax_error('data not in content')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000366 self.handle_charref(res.group('char')[:-1])
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000367 self.lineno = self.lineno + res.group(0).count('\n')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000368 continue
369 res = entityref.match(rawdata, i)
370 if res is not None:
371 i = res.end(0)
372 if rawdata[i-1] != ';':
373 self.syntax_error("`;' missing in entityref")
374 i = i-1
Guido van Rossum7e07b381998-04-03 16:02:39 +0000375 name = res.group('name')
Guido van Rossumb35d6841999-08-26 15:52:33 +0000376 if self.__map_case:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000377 name = name.lower()
Raymond Hettinger54f02222002-06-01 14:18:47 +0000378 if name in self.entitydefs:
Guido van Rossum7e07b381998-04-03 16:02:39 +0000379 self.rawdata = rawdata = rawdata[:res.start(0)] + self.entitydefs[name] + rawdata[i:]
380 n = len(rawdata)
381 i = res.start(0)
382 else:
Guido van Rossum7e07b381998-04-03 16:02:39 +0000383 self.unknown_entityref(name)
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000384 self.lineno = self.lineno + res.group(0).count('\n')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000385 continue
Guido van Rossum7e07b381998-04-03 16:02:39 +0000386 elif rawdata[i] == ']':
Guido van Rossum204b65c1998-12-08 13:25:00 +0000387 if self.literal:
388 data = rawdata[i]
389 self.handle_data(data)
390 i = i+1
391 continue
Guido van Rossum7e07b381998-04-03 16:02:39 +0000392 if n-i < 3:
393 break
394 if cdataclose.match(rawdata, i):
395 self.syntax_error("bogus `]]>'")
396 self.handle_data(rawdata[i])
397 i = i+1
398 continue
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000399 else:
Sjoerd Mullenderf7e84e12000-12-06 10:37:57 +0000400 raise Error('neither < nor & ??')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000401 # We get here only if incomplete matches but
402 # nothing else
Guido van Rossum7e07b381998-04-03 16:02:39 +0000403 break
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000404 # end while
Guido van Rossum7e07b381998-04-03 16:02:39 +0000405 if i > 0:
406 self.__at_start = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000407 if end and i < n:
Guido van Rossum7e07b381998-04-03 16:02:39 +0000408 data = rawdata[i]
409 self.syntax_error("bogus `%s'" % data)
Guido van Rossumb35d6841999-08-26 15:52:33 +0000410 if not self.__accept_utf8 and illegal.search(data):
Guido van Rossum7e07b381998-04-03 16:02:39 +0000411 self.syntax_error('illegal character in content')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000412 self.handle_data(data)
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000413 self.lineno = self.lineno + data.count('\n')
Guido van Rossum7e07b381998-04-03 16:02:39 +0000414 self.rawdata = rawdata[i+1:]
415 return self.goahead(end)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000416 self.rawdata = rawdata[i:]
417 if end:
Guido van Rossum7e07b381998-04-03 16:02:39 +0000418 if not self.__seen_starttag:
419 self.syntax_error('no elements in file')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000420 if self.stack:
421 self.syntax_error('missing end tags')
422 while self.stack:
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000423 self.finish_endtag(self.stack[-1][0])
Guido van Rossuma219efa1997-11-18 15:09:54 +0000424
425 # Internal -- parse comment, return length or -1 if not terminated
426 def parse_comment(self, i):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000427 rawdata = self.rawdata
Fred Drake8152d322000-12-12 23:20:45 +0000428 if rawdata[i:i+4] != '<!--':
Sjoerd Mullenderf7e84e12000-12-06 10:37:57 +0000429 raise Error('unexpected call to handle_comment')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000430 res = commentclose.search(rawdata, i+4)
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000431 if res is None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000432 return -1
Guido van Rossum7e07b381998-04-03 16:02:39 +0000433 if doubledash.search(rawdata, i+4, res.start(0)):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000434 self.syntax_error("`--' inside comment")
Guido van Rossum7e07b381998-04-03 16:02:39 +0000435 if rawdata[res.start(0)-1] == '-':
436 self.syntax_error('comment cannot end in three dashes')
Guido van Rossumb35d6841999-08-26 15:52:33 +0000437 if not self.__accept_utf8 and \
438 illegal.search(rawdata, i+4, res.start(0)):
Guido van Rossum7e07b381998-04-03 16:02:39 +0000439 self.syntax_error('illegal character in comment')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000440 self.handle_comment(rawdata[i+4: res.start(0)])
441 return res.end(0)
Guido van Rossuma219efa1997-11-18 15:09:54 +0000442
Guido van Rossum02505e41998-01-29 14:55:24 +0000443 # Internal -- handle DOCTYPE tag, return length or -1 if not terminated
444 def parse_doctype(self, res):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000445 rawdata = self.rawdata
446 n = len(rawdata)
447 name = res.group('name')
Guido van Rossumb35d6841999-08-26 15:52:33 +0000448 if self.__map_case:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000449 name = name.lower()
Guido van Rossum7e07b381998-04-03 16:02:39 +0000450 pubid, syslit = res.group('pubid', 'syslit')
451 if pubid is not None:
452 pubid = pubid[1:-1] # remove quotes
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000453 pubid = ' '.join(pubid.split()) # normalize
Guido van Rossum7e07b381998-04-03 16:02:39 +0000454 if syslit is not None: syslit = syslit[1:-1] # remove quotes
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000455 j = k = res.end(0)
Guido van Rossum7e07b381998-04-03 16:02:39 +0000456 if k >= n:
457 return -1
458 if rawdata[k] == '[':
459 level = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000460 k = k+1
Guido van Rossum7e07b381998-04-03 16:02:39 +0000461 dq = sq = 0
462 while k < n:
463 c = rawdata[k]
464 if not sq and c == '"':
465 dq = not dq
466 elif not dq and c == "'":
467 sq = not sq
468 elif sq or dq:
469 pass
470 elif level <= 0 and c == ']':
471 res = endbracket.match(rawdata, k+1)
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000472 if res is None:
Guido van Rossum7e07b381998-04-03 16:02:39 +0000473 return -1
474 self.handle_doctype(name, pubid, syslit, rawdata[j+1:k])
475 return res.end(0)
476 elif c == '<':
477 level = level + 1
478 elif c == '>':
479 level = level - 1
480 if level < 0:
481 self.syntax_error("bogus `>' in DOCTYPE")
482 k = k+1
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000483 res = endbracketfind.match(rawdata, k)
484 if res is None:
Guido van Rossum7e07b381998-04-03 16:02:39 +0000485 return -1
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000486 if endbracket.match(rawdata, k) is None:
Guido van Rossum7e07b381998-04-03 16:02:39 +0000487 self.syntax_error('garbage in DOCTYPE')
488 self.handle_doctype(name, pubid, syslit, None)
489 return res.end(0)
Guido van Rossum02505e41998-01-29 14:55:24 +0000490
491 # Internal -- handle CDATA tag, return length or -1 if not terminated
Guido van Rossuma219efa1997-11-18 15:09:54 +0000492 def parse_cdata(self, i):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000493 rawdata = self.rawdata
Fred Drake8152d322000-12-12 23:20:45 +0000494 if rawdata[i:i+9] != '<![CDATA[':
Sjoerd Mullenderf7e84e12000-12-06 10:37:57 +0000495 raise Error('unexpected call to parse_cdata')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000496 res = cdataclose.search(rawdata, i+9)
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000497 if res is None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000498 return -1
Guido van Rossumb35d6841999-08-26 15:52:33 +0000499 if not self.__accept_utf8 and \
500 illegal.search(rawdata, i+9, res.start(0)):
Guido van Rossum7e07b381998-04-03 16:02:39 +0000501 self.syntax_error('illegal character in CDATA')
502 if not self.stack:
503 self.syntax_error('CDATA not in content')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000504 self.handle_cdata(rawdata[i+9:res.start(0)])
505 return res.end(0)
Guido van Rossuma219efa1997-11-18 15:09:54 +0000506
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000507 __xml_namespace_attributes = {'ns':None, 'src':None, 'prefix':None}
Guido van Rossum02505e41998-01-29 14:55:24 +0000508 # Internal -- handle a processing instruction tag
509 def parse_proc(self, i):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000510 rawdata = self.rawdata
511 end = procclose.search(rawdata, i)
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000512 if end is None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000513 return -1
514 j = end.start(0)
Guido van Rossumb35d6841999-08-26 15:52:33 +0000515 if not self.__accept_utf8 and illegal.search(rawdata, i+2, j):
Guido van Rossum7e07b381998-04-03 16:02:39 +0000516 self.syntax_error('illegal character in processing instruction')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000517 res = tagfind.match(rawdata, i+2)
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000518 if res is None:
Sjoerd Mullenderf7e84e12000-12-06 10:37:57 +0000519 raise Error('unexpected call to parse_proc')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000520 k = res.end(0)
521 name = res.group(0)
Guido van Rossumb35d6841999-08-26 15:52:33 +0000522 if self.__map_case:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000523 name = name.lower()
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000524 if name == 'xml:namespace':
525 self.syntax_error('old-fashioned namespace declaration')
Guido van Rossum91c8f591998-12-21 18:15:28 +0000526 self.__use_namespaces = -1
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000527 # namespace declaration
528 # this must come after the <?xml?> declaration (if any)
529 # and before the <!DOCTYPE> (if any).
530 if self.__seen_doctype or self.__seen_starttag:
531 self.syntax_error('xml:namespace declaration too late in document')
532 attrdict, namespace, k = self.parse_attributes(name, k, j)
533 if namespace:
534 self.syntax_error('namespace declaration inside namespace declaration')
535 for attrname in attrdict.keys():
Raymond Hettinger54f02222002-06-01 14:18:47 +0000536 if not attrname in self.__xml_namespace_attributes:
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000537 self.syntax_error("unknown attribute `%s' in xml:namespace tag" % attrname)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000538 if not 'ns' in attrdict or not 'prefix' in attrdict:
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000539 self.syntax_error('xml:namespace without required attributes')
540 prefix = attrdict.get('prefix')
541 if ncname.match(prefix) is None:
542 self.syntax_error('xml:namespace illegal prefix value')
543 return end.end(0)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000544 if prefix in self.__namespaces:
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000545 self.syntax_error('xml:namespace prefix not unique')
546 self.__namespaces[prefix] = attrdict['ns']
547 else:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000548 if name.lower() == 'xml':
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000549 self.syntax_error('illegal processing instruction target name')
550 self.handle_proc(name, rawdata[k:j])
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000551 return end.end(0)
Guido van Rossum02505e41998-01-29 14:55:24 +0000552
553 # Internal -- parse attributes between i and j
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000554 def parse_attributes(self, tag, i, j):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000555 rawdata = self.rawdata
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000556 attrdict = {}
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000557 namespace = {}
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000558 while i < j:
559 res = attrfind.match(rawdata, i)
560 if res is None:
561 break
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000562 attrname, attrvalue = res.group('name', 'value')
Guido van Rossumb35d6841999-08-26 15:52:33 +0000563 if self.__map_case:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000564 attrname = attrname.lower()
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000565 i = res.end(0)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000566 if attrvalue is None:
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000567 self.syntax_error("no value specified for attribute `%s'" % attrname)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000568 attrvalue = attrname
569 elif attrvalue[:1] == "'" == attrvalue[-1:] or \
570 attrvalue[:1] == '"' == attrvalue[-1:]:
571 attrvalue = attrvalue[1:-1]
Guido van Rossumb35d6841999-08-26 15:52:33 +0000572 elif not self.__accept_unquoted_attributes:
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000573 self.syntax_error("attribute `%s' value not quoted" % attrname)
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000574 res = xmlns.match(attrname)
575 if res is not None:
576 # namespace declaration
577 ncname = res.group('ncname')
578 namespace[ncname or ''] = attrvalue or None
Guido van Rossum91c8f591998-12-21 18:15:28 +0000579 if not self.__use_namespaces:
580 self.__use_namespaces = len(self.stack)+1
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000581 continue
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000582 if '<' in attrvalue:
583 self.syntax_error("`<' illegal in attribute value")
Raymond Hettinger54f02222002-06-01 14:18:47 +0000584 if attrname in attrdict:
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000585 self.syntax_error("attribute `%s' specified twice" % attrname)
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000586 attrvalue = attrvalue.translate(attrtrans)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000587 attrdict[attrname] = self.translate_references(attrvalue)
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000588 return attrdict, namespace, i
Guido van Rossuma219efa1997-11-18 15:09:54 +0000589
590 # Internal -- handle starttag, return length or -1 if not terminated
591 def parse_starttag(self, i):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000592 rawdata = self.rawdata
593 # i points to start of tag
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000594 end = endbracketfind.match(rawdata, i+1)
595 if end is None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000596 return -1
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000597 tag = starttagmatch.match(rawdata, i)
598 if tag is None or tag.end(0) != end.end(0):
599 self.syntax_error('garbage in starttag')
600 return end.end(0)
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000601 nstag = tagname = tag.group('tagname')
Guido van Rossumb35d6841999-08-26 15:52:33 +0000602 if self.__map_case:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000603 nstag = tagname = nstag.lower()
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000604 if not self.__seen_starttag and self.__seen_doctype and \
605 tagname != self.__seen_doctype:
606 self.syntax_error('starttag does not match DOCTYPE')
Guido van Rossum7e07b381998-04-03 16:02:39 +0000607 if self.__seen_starttag and not self.stack:
608 self.syntax_error('multiple elements on top level')
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000609 k, j = tag.span('attrs')
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000610 attrdict, nsdict, k = self.parse_attributes(tagname, k, j)
611 self.stack.append((tagname, nsdict, nstag))
Guido van Rossum91c8f591998-12-21 18:15:28 +0000612 if self.__use_namespaces:
613 res = qname.match(tagname)
614 else:
615 res = None
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000616 if res is not None:
617 prefix, nstag = res.group('prefix', 'local')
618 if prefix is None:
619 prefix = ''
620 ns = None
621 for t, d, nst in self.stack:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000622 if prefix in d:
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000623 ns = d[prefix]
624 if ns is None and prefix != '':
625 ns = self.__namespaces.get(prefix)
626 if ns is not None:
627 nstag = ns + ' ' + nstag
628 elif prefix != '':
629 nstag = prefix + ':' + nstag # undo split
630 self.stack[-1] = tagname, nsdict, nstag
631 # translate namespace of attributes
Sjoerd Mullenderf7e84e12000-12-06 10:37:57 +0000632 attrnamemap = {} # map from new name to old name (used for error reporting)
633 for key in attrdict.keys():
634 attrnamemap[key] = key
Guido van Rossum91c8f591998-12-21 18:15:28 +0000635 if self.__use_namespaces:
636 nattrdict = {}
637 for key, val in attrdict.items():
Sjoerd Mullenderf7e84e12000-12-06 10:37:57 +0000638 okey = key
Guido van Rossum91c8f591998-12-21 18:15:28 +0000639 res = qname.match(key)
640 if res is not None:
641 aprefix, key = res.group('prefix', 'local')
Guido van Rossumb35d6841999-08-26 15:52:33 +0000642 if self.__map_case:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000643 key = key.lower()
Guido van Rossum91c8f591998-12-21 18:15:28 +0000644 if aprefix is None:
645 aprefix = ''
646 ans = None
647 for t, d, nst in self.stack:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000648 if aprefix in d:
Guido van Rossum91c8f591998-12-21 18:15:28 +0000649 ans = d[aprefix]
650 if ans is None and aprefix != '':
651 ans = self.__namespaces.get(aprefix)
652 if ans is not None:
653 key = ans + ' ' + key
654 elif aprefix != '':
655 key = aprefix + ':' + key
656 elif ns is not None:
657 key = ns + ' ' + key
658 nattrdict[key] = val
Sjoerd Mullenderf7e84e12000-12-06 10:37:57 +0000659 attrnamemap[key] = okey
Guido van Rossum91c8f591998-12-21 18:15:28 +0000660 attrdict = nattrdict
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000661 attributes = self.attributes.get(nstag)
662 if attributes is not None:
663 for key in attrdict.keys():
Raymond Hettinger54f02222002-06-01 14:18:47 +0000664 if not key in attributes:
Sjoerd Mullenderf7e84e12000-12-06 10:37:57 +0000665 self.syntax_error("unknown attribute `%s' in tag `%s'" % (attrnamemap[key], tagname))
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000666 for key, val in attributes.items():
Raymond Hettinger54f02222002-06-01 14:18:47 +0000667 if val is not None and not key in attrdict:
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000668 attrdict[key] = val
669 method = self.elements.get(nstag, (None, None))[0]
670 self.finish_starttag(nstag, attrdict, method)
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000671 if tag.group('slash') == '/':
672 self.finish_endtag(tagname)
673 return tag.end(0)
Guido van Rossuma219efa1997-11-18 15:09:54 +0000674
675 # Internal -- parse endtag
676 def parse_endtag(self, i):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000677 rawdata = self.rawdata
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000678 end = endbracketfind.match(rawdata, i+1)
679 if end is None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000680 return -1
681 res = tagfind.match(rawdata, i+2)
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000682 if res is None:
Guido van Rossumf484a331998-12-07 21:59:56 +0000683 if self.literal:
684 self.handle_data(rawdata[i])
685 return i+1
Guido van Rossumb35d6841999-08-26 15:52:33 +0000686 if not self.__accept_missing_endtag_name:
687 self.syntax_error('no name specified in end tag')
Guido van Rossumd468bd32000-06-21 20:01:10 +0000688 tag = self.stack[-1][0]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000689 k = i+2
690 else:
691 tag = res.group(0)
Guido van Rossumb35d6841999-08-26 15:52:33 +0000692 if self.__map_case:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000693 tag = tag.lower()
Guido van Rossumf484a331998-12-07 21:59:56 +0000694 if self.literal:
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000695 if not self.stack or tag != self.stack[-1][0]:
Guido van Rossumf484a331998-12-07 21:59:56 +0000696 self.handle_data(rawdata[i])
697 return i+1
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000698 k = res.end(0)
Guido van Rossumeeb2f321998-10-19 13:28:26 +0000699 if endbracket.match(rawdata, k) is None:
Guido van Rossum7e07b381998-04-03 16:02:39 +0000700 self.syntax_error('garbage in end tag')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000701 self.finish_endtag(tag)
702 return end.end(0)
Guido van Rossuma219efa1997-11-18 15:09:54 +0000703
704 # Internal -- finish processing of start tag
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000705 def finish_starttag(self, tagname, attrdict, method):
706 if method is not None:
707 self.handle_starttag(tagname, method, attrdict)
Guido van Rossum7e07b381998-04-03 16:02:39 +0000708 else:
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000709 self.unknown_starttag(tagname, attrdict)
Guido van Rossuma219efa1997-11-18 15:09:54 +0000710
711 # Internal -- finish processing of end tag
712 def finish_endtag(self, tag):
Sjoerd Mullender538453e2001-03-14 17:03:30 +0000713 self.literal = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000714 if not tag:
715 self.syntax_error('name-less end tag')
716 found = len(self.stack) - 1
717 if found < 0:
718 self.unknown_endtag(tag)
719 return
720 else:
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000721 found = -1
722 for i in range(len(self.stack)):
723 if tag == self.stack[i][0]:
724 found = i
725 if found == -1:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000726 self.syntax_error('unopened end tag')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000727 return
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000728 while len(self.stack) > found:
729 if found < len(self.stack) - 1:
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000730 self.syntax_error('missing close tag for %s' % self.stack[-1][2])
731 nstag = self.stack[-1][2]
732 method = self.elements.get(nstag, (None, None))[1]
733 if method is not None:
734 self.handle_endtag(nstag, method)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000735 else:
Guido van Rossumb083a9f1998-12-18 20:17:13 +0000736 self.unknown_endtag(nstag)
Guido van Rossum91c8f591998-12-21 18:15:28 +0000737 if self.__use_namespaces == len(self.stack):
738 self.__use_namespaces = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000739 del self.stack[-1]
Guido van Rossuma219efa1997-11-18 15:09:54 +0000740
Guido van Rossum02505e41998-01-29 14:55:24 +0000741 # Overridable -- handle xml processing instruction
742 def handle_xml(self, encoding, standalone):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000743 pass
Guido van Rossum02505e41998-01-29 14:55:24 +0000744
745 # Overridable -- handle DOCTYPE
Guido van Rossum7e07b381998-04-03 16:02:39 +0000746 def handle_doctype(self, tag, pubid, syslit, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000747 pass
Guido van Rossum02505e41998-01-29 14:55:24 +0000748
Guido van Rossuma219efa1997-11-18 15:09:54 +0000749 # Overridable -- handle start tag
750 def handle_starttag(self, tag, method, attrs):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000751 method(attrs)
Guido van Rossuma219efa1997-11-18 15:09:54 +0000752
753 # Overridable -- handle end tag
754 def handle_endtag(self, tag, method):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000755 method()
Guido van Rossuma219efa1997-11-18 15:09:54 +0000756
757 # Example -- handle character reference, no need to override
758 def handle_charref(self, name):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000759 try:
760 if name[0] == 'x':
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000761 n = int(name[1:], 16)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000762 else:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000763 n = int(name)
764 except ValueError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000765 self.unknown_charref(name)
766 return
767 if not 0 <= n <= 255:
768 self.unknown_charref(name)
769 return
770 self.handle_data(chr(n))
Guido van Rossuma219efa1997-11-18 15:09:54 +0000771
772 # Definition of entities -- derived classes may override
Guido van Rossum7e07b381998-04-03 16:02:39 +0000773 entitydefs = {'lt': '&#60;', # must use charref
774 'gt': '&#62;',
775 'amp': '&#38;', # must use charref
776 'quot': '&#34;',
777 'apos': '&#39;',
778 }
Guido van Rossuma219efa1997-11-18 15:09:54 +0000779
Guido van Rossuma219efa1997-11-18 15:09:54 +0000780 # Example -- handle data, should be overridden
781 def handle_data(self, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000782 pass
Guido van Rossuma219efa1997-11-18 15:09:54 +0000783
784 # Example -- handle cdata, could be overridden
785 def handle_cdata(self, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000786 pass
Guido van Rossuma219efa1997-11-18 15:09:54 +0000787
788 # Example -- handle comment, could be overridden
789 def handle_comment(self, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000790 pass
Guido van Rossuma219efa1997-11-18 15:09:54 +0000791
792 # Example -- handle processing instructions, could be overridden
793 def handle_proc(self, name, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000794 pass
Guido van Rossuma219efa1997-11-18 15:09:54 +0000795
Guido van Rossuma219efa1997-11-18 15:09:54 +0000796 # Example -- handle relatively harmless syntax errors, could be overridden
Guido van Rossum02505e41998-01-29 14:55:24 +0000797 def syntax_error(self, message):
Sjoerd Mullenderf7e84e12000-12-06 10:37:57 +0000798 raise Error('Syntax error at line %d: %s' % (self.lineno, message))
Guido van Rossuma219efa1997-11-18 15:09:54 +0000799
800 # To be overridden -- handlers for unknown objects
801 def unknown_starttag(self, tag, attrs): pass
802 def unknown_endtag(self, tag): pass
803 def unknown_charref(self, ref): pass
Guido van Rossumb35d6841999-08-26 15:52:33 +0000804 def unknown_entityref(self, name):
805 self.syntax_error("reference to unknown entity `&%s;'" % name)
Guido van Rossuma219efa1997-11-18 15:09:54 +0000806
807
808class TestXMLParser(XMLParser):
809
Guido van Rossumb35d6841999-08-26 15:52:33 +0000810 def __init__(self, **kw):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000811 self.testdata = ""
Guido van Rossum68468eb2003-02-27 20:14:51 +0000812 XMLParser.__init__(self, **kw)
Guido van Rossuma219efa1997-11-18 15:09:54 +0000813
Guido van Rossum02505e41998-01-29 14:55:24 +0000814 def handle_xml(self, encoding, standalone):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000815 self.flush()
816 print 'xml: encoding =',encoding,'standalone =',standalone
Guido van Rossum02505e41998-01-29 14:55:24 +0000817
Guido van Rossum7e07b381998-04-03 16:02:39 +0000818 def handle_doctype(self, tag, pubid, syslit, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000819 self.flush()
820 print 'DOCTYPE:',tag, `data`
Guido van Rossum02505e41998-01-29 14:55:24 +0000821
Guido van Rossuma219efa1997-11-18 15:09:54 +0000822 def handle_data(self, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000823 self.testdata = self.testdata + data
824 if len(`self.testdata`) >= 70:
825 self.flush()
Guido van Rossuma219efa1997-11-18 15:09:54 +0000826
827 def flush(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000828 data = self.testdata
829 if data:
830 self.testdata = ""
831 print 'data:', `data`
Guido van Rossuma219efa1997-11-18 15:09:54 +0000832
833 def handle_cdata(self, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000834 self.flush()
835 print 'cdata:', `data`
Guido van Rossuma219efa1997-11-18 15:09:54 +0000836
837 def handle_proc(self, name, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000838 self.flush()
839 print 'processing:',name,`data`
Guido van Rossuma219efa1997-11-18 15:09:54 +0000840
Guido van Rossuma219efa1997-11-18 15:09:54 +0000841 def handle_comment(self, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000842 self.flush()
843 r = `data`
844 if len(r) > 68:
845 r = r[:32] + '...' + r[-32:]
846 print 'comment:', r
Guido van Rossuma219efa1997-11-18 15:09:54 +0000847
Guido van Rossum02505e41998-01-29 14:55:24 +0000848 def syntax_error(self, message):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000849 print 'error at line %d:' % self.lineno, message
Guido van Rossuma219efa1997-11-18 15:09:54 +0000850
851 def unknown_starttag(self, tag, attrs):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000852 self.flush()
853 if not attrs:
854 print 'start tag: <' + tag + '>'
855 else:
856 print 'start tag: <' + tag,
857 for name, value in attrs.items():
858 print name + '=' + '"' + value + '"',
859 print '>'
Guido van Rossuma219efa1997-11-18 15:09:54 +0000860
861 def unknown_endtag(self, tag):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000862 self.flush()
863 print 'end tag: </' + tag + '>'
Guido van Rossuma219efa1997-11-18 15:09:54 +0000864
865 def unknown_entityref(self, ref):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000866 self.flush()
867 print '*** unknown entity ref: &' + ref + ';'
Guido van Rossuma219efa1997-11-18 15:09:54 +0000868
869 def unknown_charref(self, ref):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000870 self.flush()
871 print '*** unknown char ref: &#' + ref + ';'
Guido van Rossuma219efa1997-11-18 15:09:54 +0000872
873 def close(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000874 XMLParser.close(self)
875 self.flush()
Guido van Rossuma219efa1997-11-18 15:09:54 +0000876
877def test(args = None):
Guido van Rossumaf5add41998-12-22 14:23:50 +0000878 import sys, getopt
879 from time import time
Guido van Rossuma219efa1997-11-18 15:09:54 +0000880
881 if not args:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000882 args = sys.argv[1:]
Guido van Rossuma219efa1997-11-18 15:09:54 +0000883
Guido van Rossumaf5add41998-12-22 14:23:50 +0000884 opts, args = getopt.getopt(args, 'st')
885 klass = TestXMLParser
886 do_time = 0
887 for o, a in opts:
888 if o == '-s':
889 klass = XMLParser
890 elif o == '-t':
891 do_time = 1
Guido van Rossuma219efa1997-11-18 15:09:54 +0000892
893 if args:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000894 file = args[0]
Guido van Rossuma219efa1997-11-18 15:09:54 +0000895 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000896 file = 'test.xml'
Guido van Rossuma219efa1997-11-18 15:09:54 +0000897
898 if file == '-':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000899 f = sys.stdin
Guido van Rossuma219efa1997-11-18 15:09:54 +0000900 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000901 try:
902 f = open(file, 'r')
903 except IOError, msg:
904 print file, ":", msg
905 sys.exit(1)
Guido van Rossuma219efa1997-11-18 15:09:54 +0000906
907 data = f.read()
908 if f is not sys.stdin:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000909 f.close()
Guido van Rossuma219efa1997-11-18 15:09:54 +0000910
911 x = klass()
Guido van Rossumaf5add41998-12-22 14:23:50 +0000912 t0 = time()
Guido van Rossum7e07b381998-04-03 16:02:39 +0000913 try:
Guido van Rossumaf5add41998-12-22 14:23:50 +0000914 if do_time:
915 x.feed(data)
916 x.close()
917 else:
918 for c in data:
919 x.feed(c)
920 x.close()
Sjoerd Mullenderf7e84e12000-12-06 10:37:57 +0000921 except Error, msg:
Guido van Rossumaf5add41998-12-22 14:23:50 +0000922 t1 = time()
Guido van Rossum7e07b381998-04-03 16:02:39 +0000923 print msg
Guido van Rossumaf5add41998-12-22 14:23:50 +0000924 if do_time:
925 print 'total time: %g' % (t1-t0)
Guido van Rossum7e07b381998-04-03 16:02:39 +0000926 sys.exit(1)
Guido van Rossumaf5add41998-12-22 14:23:50 +0000927 t1 = time()
928 if do_time:
929 print 'total time: %g' % (t1-t0)
Guido van Rossuma219efa1997-11-18 15:09:54 +0000930
931
932if __name__ == '__main__':
933 test()