Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 1 | # A parser for XML, using the derived class as static DTD. |
Guido van Rossum | 5d68e8e | 1997-11-18 15:27:20 +0000 | [diff] [blame] | 2 | # Author: Sjoerd Mullender. |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 3 | |
| 4 | import re |
| 5 | import string |
| 6 | |
| 7 | |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 8 | version = '0.2' |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 10 | # Regular expressions used for parsing |
| 11 | |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 12 | _S = '[ \t\r\n]+' # white space |
| 13 | _opS = '[ \t\r\n]*' # optional white space |
| 14 | _Name = '[a-zA-Z_:][-a-zA-Z0-9._:]*' # valid XML name |
| 15 | _QStr = "(?:'[^']*'|\"[^\"]*\")" # quoted XML string |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 16 | illegal = re.compile('[^\t\r\n -\176\240-\377]') # illegal chars in content |
| 17 | interesting = re.compile('[]&<]') |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 19 | amp = re.compile('&') |
| 20 | ref = re.compile('&(' + _Name + '|#[0-9]+|#x[0-9a-fA-F]+)[^-a-zA-Z0-9._:]') |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 21 | entityref = re.compile('&(?P<name>' + _Name + ')[^-a-zA-Z0-9._:]') |
| 22 | charref = re.compile('&#(?P<char>[0-9]+[^0-9]|x[0-9a-fA-F]+[^0-9a-fA-F])') |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 23 | space = re.compile(_S + '$') |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 24 | newline = re.compile('\n') |
| 25 | |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 26 | attrfind = re.compile( |
| 27 | _S + '(?P<name>' + _Name + ')' |
| 28 | '(' + _opS + '=' + _opS + |
| 29 | '(?P<value>'+_QStr+'|[-a-zA-Z0-9.:+*%?!()_#=~]+))?') |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 30 | starttagopen = re.compile('<' + _Name) |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 31 | starttagend = re.compile(_opS + '(?P<slash>/?)>') |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 32 | starttagmatch = re.compile('<(?P<tagname>'+_Name+')' |
| 33 | '(?P<attrs>(?:'+attrfind.pattern+')*)'+ |
| 34 | starttagend.pattern) |
| 35 | endtagopen = re.compile('</') |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 36 | endbracket = re.compile(_opS + '>') |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 37 | endbracketfind = re.compile('(?:[^>\'"]|'+_QStr+')*>') |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 38 | tagfind = re.compile(_Name) |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 39 | cdataopen = re.compile(r'<!\[CDATA\[') |
| 40 | cdataclose = re.compile(r'\]\]>') |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 41 | # this matches one of the following: |
| 42 | # SYSTEM SystemLiteral |
| 43 | # PUBLIC PubidLiteral SystemLiteral |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 44 | _SystemLiteral = '(?P<%s>'+_QStr+')' |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 45 | _PublicLiteral = '(?P<%s>"[-\'()+,./:=?;!*#@$_%% \n\ra-zA-Z0-9]*"|' \ |
| 46 | "'[-()+,./:=?;!*#@$_%% \n\ra-zA-Z0-9]*')" |
| 47 | _ExternalId = '(?:SYSTEM|' \ |
| 48 | 'PUBLIC'+_S+_PublicLiteral%'pubid'+ \ |
| 49 | ')'+_S+_SystemLiteral%'syslit' |
| 50 | doctype = re.compile('<!DOCTYPE'+_S+'(?P<name>'+_Name+')' |
| 51 | '(?:'+_S+_ExternalId+')?'+_opS) |
| 52 | xmldecl = re.compile('<\?xml'+_S+ |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 53 | 'version'+_opS+'='+_opS+'(?P<version>'+_QStr+')'+ |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 54 | '(?:'+_S+'encoding'+_opS+'='+_opS+ |
| 55 | "(?P<encoding>'[A-Za-z][-A-Za-z0-9._]*'|" |
| 56 | '"[A-Za-z][-A-Za-z0-9._]*"))?' |
| 57 | '(?:'+_S+'standalone'+_opS+'='+_opS+ |
| 58 | '(?P<standalone>\'(?:yes|no)\'|"(?:yes|no)"))?'+ |
| 59 | _opS+'\?>') |
| 60 | procopen = re.compile(r'<\?(?P<proc>' + _Name + ')' + _opS) |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 61 | procclose = re.compile(_opS + r'\?>') |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 62 | commentopen = re.compile('<!--') |
| 63 | commentclose = re.compile('-->') |
| 64 | doubledash = re.compile('--') |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 65 | attrtrans = string.maketrans(' \r\n\t', ' ') |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 67 | # definitions for XML namespaces |
| 68 | _NCName = '[a-zA-Z_][-a-zA-Z0-9._]*' # XML Name, minus the ":" |
| 69 | ncname = re.compile(_NCName + '$') |
| 70 | qname = re.compile('(?:(?P<prefix>' + _NCName + '):)?' # optional prefix |
| 71 | '(?P<local>' + _NCName + ')$') |
| 72 | |
| 73 | xmlns = re.compile('xmlns(?::(?P<ncname>'+_NCName+'))?$') |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 74 | |
| 75 | # XML parser base class -- find tags and call handler functions. |
| 76 | # Usage: p = XMLParser(); p.feed(data); ...; p.close(). |
Guido van Rossum | 5d68e8e | 1997-11-18 15:27:20 +0000 | [diff] [blame] | 77 | # The dtd is defined by deriving a class which defines methods with |
| 78 | # special names to handle tags: start_foo and end_foo to handle <foo> |
| 79 | # and </foo>, respectively. The data between tags is passed to the |
| 80 | # parser by calling self.handle_data() with some data as argument (the |
| 81 | # data may be split up in arbutrary chunks). Entity references are |
| 82 | # passed by calling self.handle_entityref() with the entity reference |
| 83 | # as argument. |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 84 | |
| 85 | class XMLParser: |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 86 | attributes = {} # default, to be overridden |
| 87 | elements = {} # default, to be overridden |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 88 | |
| 89 | # Interface -- initialize and reset this instance |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 90 | def __init__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 91 | self.reset() |
Guido van Rossum | fe64935 | 1999-02-01 15:35:13 +0000 | [diff] [blame] | 92 | if self.elements is XMLParser.elements: |
| 93 | self.__fixelements() |
| 94 | |
| 95 | def __fixelements(self): |
| 96 | self.elements = {} |
| 97 | self.__fixdict(self.__dict__) |
| 98 | self.__fixclass(self.__class__) |
| 99 | |
| 100 | def __fixclass(self, kl): |
| 101 | self.__fixdict(kl.__dict__) |
| 102 | for k in kl.__bases__: |
| 103 | self.__fixclass(k) |
| 104 | |
| 105 | def __fixdict(self, dict): |
| 106 | for key, val in dict.items(): |
| 107 | if key[:6] == 'start_': |
| 108 | key = key[6:] |
| 109 | start, end = self.elements.get(key, (None, None)) |
| 110 | if start is None: |
| 111 | self.elements[key] = val, end |
| 112 | elif key[:4] == 'end_': |
| 113 | key = key[4:] |
| 114 | start, end = self.elements.get(key, (None, None)) |
| 115 | if end is None: |
| 116 | self.elements[key] = start, val |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 117 | |
| 118 | # Interface -- reset this instance. Loses all unprocessed data |
| 119 | def reset(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 120 | self.rawdata = '' |
| 121 | self.stack = [] |
| 122 | self.nomoretags = 0 |
| 123 | self.literal = 0 |
| 124 | self.lineno = 1 |
| 125 | self.__at_start = 1 |
| 126 | self.__seen_doctype = None |
| 127 | self.__seen_starttag = 0 |
Guido van Rossum | 91c8f59 | 1998-12-21 18:15:28 +0000 | [diff] [blame] | 128 | self.__use_namespaces = 0 |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 129 | self.__namespaces = {'xml':None} # xml is implicitly declared |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 130 | |
| 131 | # For derived classes only -- enter literal mode (CDATA) till EOF |
| 132 | def setnomoretags(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 133 | self.nomoretags = self.literal = 1 |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 134 | |
| 135 | # For derived classes only -- enter literal mode (CDATA) |
| 136 | def setliteral(self, *args): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 137 | self.literal = 1 |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 138 | |
| 139 | # Interface -- feed some data to the parser. Call this as |
| 140 | # often as you want, with as little or as much text as you |
| 141 | # want (may include '\n'). (This just saves the text, all the |
| 142 | # processing is done by goahead().) |
| 143 | def feed(self, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 144 | self.rawdata = self.rawdata + data |
| 145 | self.goahead(0) |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 146 | |
| 147 | # Interface -- handle the remaining data |
| 148 | def close(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 149 | self.goahead(1) |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 150 | |
| 151 | # Interface -- translate references |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 152 | def translate_references(self, data, all = 1): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 153 | i = 0 |
| 154 | while 1: |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 155 | res = amp.search(data, i) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 156 | if res is None: |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 157 | return data |
| 158 | res = ref.match(data, res.start(0)) |
| 159 | if res is None: |
| 160 | self.syntax_error("bogus `&'") |
| 161 | i =i+1 |
| 162 | continue |
| 163 | i = res.end(0) |
| 164 | if data[i - 1] != ';': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 165 | self.syntax_error("`;' missing after entity/char reference") |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 166 | i = i-1 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 167 | str = res.group(1) |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 168 | pre = data[:res.start(0)] |
| 169 | post = data[i:] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 170 | if str[0] == '#': |
| 171 | if str[1] == 'x': |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 172 | str = chr(string.atoi(str[2:], 16)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 173 | else: |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 174 | str = chr(string.atoi(str[1:])) |
| 175 | data = pre + str + post |
| 176 | i = res.start(0)+len(str) |
| 177 | elif all: |
| 178 | if self.entitydefs.has_key(str): |
| 179 | data = pre + self.entitydefs[str] + post |
| 180 | i = res.start(0) # rescan substituted text |
| 181 | else: |
| 182 | self.syntax_error('reference to unknown entity') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 183 | # can't do it, so keep the entity ref in |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 184 | data = pre + '&' + str + ';' + post |
| 185 | i = res.start(0) + len(str) + 2 |
| 186 | else: |
| 187 | # just translating character references |
| 188 | pass # i is already postioned correctly |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 189 | |
| 190 | # Internal -- handle data as far as reasonable. May leave state |
| 191 | # and data to be processed by a subsequent call. If 'end' is |
| 192 | # true, force handling all data as if followed by EOF marker. |
| 193 | def goahead(self, end): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 194 | rawdata = self.rawdata |
| 195 | i = 0 |
| 196 | n = len(rawdata) |
| 197 | while i < n: |
| 198 | if i > 0: |
| 199 | self.__at_start = 0 |
| 200 | if self.nomoretags: |
| 201 | data = rawdata[i:n] |
| 202 | self.handle_data(data) |
| 203 | self.lineno = self.lineno + string.count(data, '\n') |
| 204 | i = n |
| 205 | break |
| 206 | res = interesting.search(rawdata, i) |
| 207 | if res: |
| 208 | j = res.start(0) |
| 209 | else: |
| 210 | j = n |
| 211 | if i < j: |
Guido van Rossum | 91c8f59 | 1998-12-21 18:15:28 +0000 | [diff] [blame] | 212 | data = rawdata[i:j] |
| 213 | if self.__at_start and space.match(data) is None: |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 214 | self.syntax_error('illegal data at start of file') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 215 | self.__at_start = 0 |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 216 | if not self.stack and space.match(data) is None: |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 217 | self.syntax_error('data not in content') |
| 218 | if illegal.search(data): |
| 219 | self.syntax_error('illegal character in content') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 220 | self.handle_data(data) |
| 221 | self.lineno = self.lineno + string.count(data, '\n') |
| 222 | i = j |
| 223 | if i == n: break |
| 224 | if rawdata[i] == '<': |
| 225 | if starttagopen.match(rawdata, i): |
| 226 | if self.literal: |
| 227 | data = rawdata[i] |
| 228 | self.handle_data(data) |
| 229 | self.lineno = self.lineno + string.count(data, '\n') |
| 230 | i = i+1 |
| 231 | continue |
| 232 | k = self.parse_starttag(i) |
| 233 | if k < 0: break |
| 234 | self.__seen_starttag = 1 |
| 235 | self.lineno = self.lineno + string.count(rawdata[i:k], '\n') |
| 236 | i = k |
| 237 | continue |
| 238 | if endtagopen.match(rawdata, i): |
| 239 | k = self.parse_endtag(i) |
| 240 | if k < 0: break |
| 241 | self.lineno = self.lineno + string.count(rawdata[i:k], '\n') |
| 242 | i = k |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 243 | continue |
| 244 | if commentopen.match(rawdata, i): |
| 245 | if self.literal: |
| 246 | data = rawdata[i] |
| 247 | self.handle_data(data) |
| 248 | self.lineno = self.lineno + string.count(data, '\n') |
| 249 | i = i+1 |
| 250 | continue |
| 251 | k = self.parse_comment(i) |
| 252 | if k < 0: break |
| 253 | self.lineno = self.lineno + string.count(rawdata[i:k], '\n') |
| 254 | i = k |
| 255 | continue |
| 256 | if cdataopen.match(rawdata, i): |
| 257 | k = self.parse_cdata(i) |
| 258 | if k < 0: break |
| 259 | self.lineno = self.lineno + string.count(rawdata[i:i], '\n') |
| 260 | i = k |
| 261 | continue |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 262 | res = xmldecl.match(rawdata, i) |
| 263 | if res: |
| 264 | if not self.__at_start: |
| 265 | self.syntax_error("<?xml?> declaration not at start of document") |
| 266 | version, encoding, standalone = res.group('version', |
| 267 | 'encoding', |
| 268 | 'standalone') |
| 269 | if version[1:-1] != '1.0': |
| 270 | raise RuntimeError, 'only XML version 1.0 supported' |
| 271 | if encoding: encoding = encoding[1:-1] |
| 272 | if standalone: standalone = standalone[1:-1] |
| 273 | self.handle_xml(encoding, standalone) |
| 274 | i = res.end(0) |
| 275 | continue |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 276 | res = procopen.match(rawdata, i) |
| 277 | if res: |
| 278 | k = self.parse_proc(i) |
| 279 | if k < 0: break |
| 280 | self.lineno = self.lineno + string.count(rawdata[i:k], '\n') |
| 281 | i = k |
| 282 | continue |
| 283 | res = doctype.match(rawdata, i) |
| 284 | if res: |
| 285 | if self.literal: |
| 286 | data = rawdata[i] |
| 287 | self.handle_data(data) |
| 288 | self.lineno = self.lineno + string.count(data, '\n') |
| 289 | i = i+1 |
| 290 | continue |
| 291 | if self.__seen_doctype: |
| 292 | self.syntax_error('multiple DOCTYPE elements') |
| 293 | if self.__seen_starttag: |
| 294 | self.syntax_error('DOCTYPE not at beginning of document') |
| 295 | k = self.parse_doctype(res) |
| 296 | if k < 0: break |
| 297 | self.__seen_doctype = res.group('name') |
| 298 | self.lineno = self.lineno + string.count(rawdata[i:k], '\n') |
| 299 | i = k |
| 300 | continue |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 301 | elif rawdata[i] == '&': |
Guido van Rossum | 204b65c | 1998-12-08 13:25:00 +0000 | [diff] [blame] | 302 | if self.literal: |
| 303 | data = rawdata[i] |
| 304 | self.handle_data(data) |
| 305 | i = i+1 |
| 306 | continue |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 307 | res = charref.match(rawdata, i) |
| 308 | if res is not None: |
| 309 | i = res.end(0) |
| 310 | if rawdata[i-1] != ';': |
| 311 | self.syntax_error("`;' missing in charref") |
| 312 | i = i-1 |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 313 | if not self.stack: |
| 314 | self.syntax_error('data not in content') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 315 | self.handle_charref(res.group('char')[:-1]) |
| 316 | self.lineno = self.lineno + string.count(res.group(0), '\n') |
| 317 | continue |
| 318 | res = entityref.match(rawdata, i) |
| 319 | if res is not None: |
| 320 | i = res.end(0) |
| 321 | if rawdata[i-1] != ';': |
| 322 | self.syntax_error("`;' missing in entityref") |
| 323 | i = i-1 |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 324 | name = res.group('name') |
| 325 | if self.entitydefs.has_key(name): |
| 326 | self.rawdata = rawdata = rawdata[:res.start(0)] + self.entitydefs[name] + rawdata[i:] |
| 327 | n = len(rawdata) |
| 328 | i = res.start(0) |
| 329 | else: |
| 330 | self.syntax_error('reference to unknown entity') |
| 331 | self.unknown_entityref(name) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 332 | self.lineno = self.lineno + string.count(res.group(0), '\n') |
| 333 | continue |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 334 | elif rawdata[i] == ']': |
Guido van Rossum | 204b65c | 1998-12-08 13:25:00 +0000 | [diff] [blame] | 335 | if self.literal: |
| 336 | data = rawdata[i] |
| 337 | self.handle_data(data) |
| 338 | i = i+1 |
| 339 | continue |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 340 | if n-i < 3: |
| 341 | break |
| 342 | if cdataclose.match(rawdata, i): |
| 343 | self.syntax_error("bogus `]]>'") |
| 344 | self.handle_data(rawdata[i]) |
| 345 | i = i+1 |
| 346 | continue |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 347 | else: |
| 348 | raise RuntimeError, 'neither < nor & ??' |
| 349 | # We get here only if incomplete matches but |
| 350 | # nothing else |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 351 | break |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 352 | # end while |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 353 | if i > 0: |
| 354 | self.__at_start = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 355 | if end and i < n: |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 356 | data = rawdata[i] |
| 357 | self.syntax_error("bogus `%s'" % data) |
| 358 | if illegal.search(data): |
| 359 | self.syntax_error('illegal character in content') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 360 | self.handle_data(data) |
| 361 | self.lineno = self.lineno + string.count(data, '\n') |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 362 | self.rawdata = rawdata[i+1:] |
| 363 | return self.goahead(end) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 364 | self.rawdata = rawdata[i:] |
| 365 | if end: |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 366 | if not self.__seen_starttag: |
| 367 | self.syntax_error('no elements in file') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 368 | if self.stack: |
| 369 | self.syntax_error('missing end tags') |
| 370 | while self.stack: |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 371 | self.finish_endtag(self.stack[-1][0]) |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 372 | |
| 373 | # Internal -- parse comment, return length or -1 if not terminated |
| 374 | def parse_comment(self, i): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 375 | rawdata = self.rawdata |
| 376 | if rawdata[i:i+4] <> '<!--': |
| 377 | raise RuntimeError, 'unexpected call to handle_comment' |
| 378 | res = commentclose.search(rawdata, i+4) |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 379 | if res is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 380 | return -1 |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 381 | if doubledash.search(rawdata, i+4, res.start(0)): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 382 | self.syntax_error("`--' inside comment") |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 383 | if rawdata[res.start(0)-1] == '-': |
| 384 | self.syntax_error('comment cannot end in three dashes') |
| 385 | if illegal.search(rawdata, i+4, res.start(0)): |
| 386 | self.syntax_error('illegal character in comment') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 387 | self.handle_comment(rawdata[i+4: res.start(0)]) |
| 388 | return res.end(0) |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 389 | |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 390 | # Internal -- handle DOCTYPE tag, return length or -1 if not terminated |
| 391 | def parse_doctype(self, res): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 392 | rawdata = self.rawdata |
| 393 | n = len(rawdata) |
| 394 | name = res.group('name') |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 395 | pubid, syslit = res.group('pubid', 'syslit') |
| 396 | if pubid is not None: |
| 397 | pubid = pubid[1:-1] # remove quotes |
| 398 | pubid = string.join(string.split(pubid)) # normalize |
| 399 | if syslit is not None: syslit = syslit[1:-1] # remove quotes |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 400 | j = k = res.end(0) |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 401 | if k >= n: |
| 402 | return -1 |
| 403 | if rawdata[k] == '[': |
| 404 | level = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 405 | k = k+1 |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 406 | dq = sq = 0 |
| 407 | while k < n: |
| 408 | c = rawdata[k] |
| 409 | if not sq and c == '"': |
| 410 | dq = not dq |
| 411 | elif not dq and c == "'": |
| 412 | sq = not sq |
| 413 | elif sq or dq: |
| 414 | pass |
| 415 | elif level <= 0 and c == ']': |
| 416 | res = endbracket.match(rawdata, k+1) |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 417 | if res is None: |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 418 | return -1 |
| 419 | self.handle_doctype(name, pubid, syslit, rawdata[j+1:k]) |
| 420 | return res.end(0) |
| 421 | elif c == '<': |
| 422 | level = level + 1 |
| 423 | elif c == '>': |
| 424 | level = level - 1 |
| 425 | if level < 0: |
| 426 | self.syntax_error("bogus `>' in DOCTYPE") |
| 427 | k = k+1 |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 428 | res = endbracketfind.match(rawdata, k) |
| 429 | if res is None: |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 430 | return -1 |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 431 | if endbracket.match(rawdata, k) is None: |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 432 | self.syntax_error('garbage in DOCTYPE') |
| 433 | self.handle_doctype(name, pubid, syslit, None) |
| 434 | return res.end(0) |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 435 | |
| 436 | # Internal -- handle CDATA tag, return length or -1 if not terminated |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 437 | def parse_cdata(self, i): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 438 | rawdata = self.rawdata |
| 439 | if rawdata[i:i+9] <> '<![CDATA[': |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 440 | raise RuntimeError, 'unexpected call to parse_cdata' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 441 | res = cdataclose.search(rawdata, i+9) |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 442 | if res is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 443 | return -1 |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 444 | if illegal.search(rawdata, i+9, res.start(0)): |
| 445 | self.syntax_error('illegal character in CDATA') |
| 446 | if not self.stack: |
| 447 | self.syntax_error('CDATA not in content') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 448 | self.handle_cdata(rawdata[i+9:res.start(0)]) |
| 449 | return res.end(0) |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 450 | |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 451 | __xml_namespace_attributes = {'ns':None, 'src':None, 'prefix':None} |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 452 | # Internal -- handle a processing instruction tag |
| 453 | def parse_proc(self, i): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 454 | rawdata = self.rawdata |
| 455 | end = procclose.search(rawdata, i) |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 456 | if end is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 457 | return -1 |
| 458 | j = end.start(0) |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 459 | if illegal.search(rawdata, i+2, j): |
| 460 | self.syntax_error('illegal character in processing instruction') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 461 | res = tagfind.match(rawdata, i+2) |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 462 | if res is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 463 | raise RuntimeError, 'unexpected call to parse_proc' |
| 464 | k = res.end(0) |
| 465 | name = res.group(0) |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 466 | if name == 'xml:namespace': |
| 467 | self.syntax_error('old-fashioned namespace declaration') |
Guido van Rossum | 91c8f59 | 1998-12-21 18:15:28 +0000 | [diff] [blame] | 468 | self.__use_namespaces = -1 |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 469 | # namespace declaration |
| 470 | # this must come after the <?xml?> declaration (if any) |
| 471 | # and before the <!DOCTYPE> (if any). |
| 472 | if self.__seen_doctype or self.__seen_starttag: |
| 473 | self.syntax_error('xml:namespace declaration too late in document') |
| 474 | attrdict, namespace, k = self.parse_attributes(name, k, j) |
| 475 | if namespace: |
| 476 | self.syntax_error('namespace declaration inside namespace declaration') |
| 477 | for attrname in attrdict.keys(): |
| 478 | if not self.__xml_namespace_attributes.has_key(attrname): |
| 479 | self.syntax_error("unknown attribute `%s' in xml:namespace tag" % attrname) |
| 480 | if not attrdict.has_key('ns') or not attrdict.has_key('prefix'): |
| 481 | self.syntax_error('xml:namespace without required attributes') |
| 482 | prefix = attrdict.get('prefix') |
| 483 | if ncname.match(prefix) is None: |
| 484 | self.syntax_error('xml:namespace illegal prefix value') |
| 485 | return end.end(0) |
| 486 | if self.__namespaces.has_key(prefix): |
| 487 | self.syntax_error('xml:namespace prefix not unique') |
| 488 | self.__namespaces[prefix] = attrdict['ns'] |
| 489 | else: |
| 490 | if string.find(string.lower(name), 'xml') >= 0: |
| 491 | self.syntax_error('illegal processing instruction target name') |
| 492 | self.handle_proc(name, rawdata[k:j]) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 493 | return end.end(0) |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 494 | |
| 495 | # Internal -- parse attributes between i and j |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 496 | def parse_attributes(self, tag, i, j): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 497 | rawdata = self.rawdata |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 498 | attrdict = {} |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 499 | namespace = {} |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 500 | while i < j: |
| 501 | res = attrfind.match(rawdata, i) |
| 502 | if res is None: |
| 503 | break |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 504 | attrname, attrvalue = res.group('name', 'value') |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 505 | i = res.end(0) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 506 | if attrvalue is None: |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 507 | self.syntax_error("no value specified for attribute `%s'" % attrname) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 508 | attrvalue = attrname |
| 509 | elif attrvalue[:1] == "'" == attrvalue[-1:] or \ |
| 510 | attrvalue[:1] == '"' == attrvalue[-1:]: |
| 511 | attrvalue = attrvalue[1:-1] |
| 512 | else: |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 513 | self.syntax_error("attribute `%s' value not quoted" % attrname) |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 514 | res = xmlns.match(attrname) |
| 515 | if res is not None: |
| 516 | # namespace declaration |
| 517 | ncname = res.group('ncname') |
| 518 | namespace[ncname or ''] = attrvalue or None |
Guido van Rossum | 91c8f59 | 1998-12-21 18:15:28 +0000 | [diff] [blame] | 519 | if not self.__use_namespaces: |
| 520 | self.__use_namespaces = len(self.stack)+1 |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 521 | continue |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 522 | if '<' in attrvalue: |
| 523 | self.syntax_error("`<' illegal in attribute value") |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 524 | if attrdict.has_key(attrname): |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 525 | self.syntax_error("attribute `%s' specified twice" % attrname) |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 526 | attrvalue = string.translate(attrvalue, attrtrans) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 527 | attrdict[attrname] = self.translate_references(attrvalue) |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 528 | return attrdict, namespace, i |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 529 | |
| 530 | # Internal -- handle starttag, return length or -1 if not terminated |
| 531 | def parse_starttag(self, i): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 532 | rawdata = self.rawdata |
| 533 | # i points to start of tag |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 534 | end = endbracketfind.match(rawdata, i+1) |
| 535 | if end is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 536 | return -1 |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 537 | tag = starttagmatch.match(rawdata, i) |
| 538 | if tag is None or tag.end(0) != end.end(0): |
| 539 | self.syntax_error('garbage in starttag') |
| 540 | return end.end(0) |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 541 | nstag = tagname = tag.group('tagname') |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 542 | if not self.__seen_starttag and self.__seen_doctype and \ |
| 543 | tagname != self.__seen_doctype: |
| 544 | self.syntax_error('starttag does not match DOCTYPE') |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 545 | if self.__seen_starttag and not self.stack: |
| 546 | self.syntax_error('multiple elements on top level') |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 547 | k, j = tag.span('attrs') |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 548 | attrdict, nsdict, k = self.parse_attributes(tagname, k, j) |
| 549 | self.stack.append((tagname, nsdict, nstag)) |
Guido van Rossum | 91c8f59 | 1998-12-21 18:15:28 +0000 | [diff] [blame] | 550 | if self.__use_namespaces: |
| 551 | res = qname.match(tagname) |
| 552 | else: |
| 553 | res = None |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 554 | if res is not None: |
| 555 | prefix, nstag = res.group('prefix', 'local') |
| 556 | if prefix is None: |
| 557 | prefix = '' |
| 558 | ns = None |
| 559 | for t, d, nst in self.stack: |
| 560 | if d.has_key(prefix): |
| 561 | ns = d[prefix] |
| 562 | if ns is None and prefix != '': |
| 563 | ns = self.__namespaces.get(prefix) |
| 564 | if ns is not None: |
| 565 | nstag = ns + ' ' + nstag |
| 566 | elif prefix != '': |
| 567 | nstag = prefix + ':' + nstag # undo split |
| 568 | self.stack[-1] = tagname, nsdict, nstag |
| 569 | # translate namespace of attributes |
Guido van Rossum | 91c8f59 | 1998-12-21 18:15:28 +0000 | [diff] [blame] | 570 | if self.__use_namespaces: |
| 571 | nattrdict = {} |
| 572 | for key, val in attrdict.items(): |
| 573 | res = qname.match(key) |
| 574 | if res is not None: |
| 575 | aprefix, key = res.group('prefix', 'local') |
| 576 | if aprefix is None: |
| 577 | aprefix = '' |
| 578 | ans = None |
| 579 | for t, d, nst in self.stack: |
| 580 | if d.has_key(aprefix): |
| 581 | ans = d[aprefix] |
| 582 | if ans is None and aprefix != '': |
| 583 | ans = self.__namespaces.get(aprefix) |
| 584 | if ans is not None: |
| 585 | key = ans + ' ' + key |
| 586 | elif aprefix != '': |
| 587 | key = aprefix + ':' + key |
| 588 | elif ns is not None: |
| 589 | key = ns + ' ' + key |
| 590 | nattrdict[key] = val |
| 591 | attrdict = nattrdict |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 592 | attributes = self.attributes.get(nstag) |
| 593 | if attributes is not None: |
| 594 | for key in attrdict.keys(): |
| 595 | if not attributes.has_key(key): |
| 596 | self.syntax_error("unknown attribute `%s' in tag `%s'" % (key, tagname)) |
| 597 | for key, val in attributes.items(): |
| 598 | if val is not None and not attrdict.has_key(key): |
| 599 | attrdict[key] = val |
| 600 | method = self.elements.get(nstag, (None, None))[0] |
| 601 | self.finish_starttag(nstag, attrdict, method) |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 602 | if tag.group('slash') == '/': |
| 603 | self.finish_endtag(tagname) |
| 604 | return tag.end(0) |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 605 | |
| 606 | # Internal -- parse endtag |
| 607 | def parse_endtag(self, i): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 608 | rawdata = self.rawdata |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 609 | end = endbracketfind.match(rawdata, i+1) |
| 610 | if end is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 611 | return -1 |
| 612 | res = tagfind.match(rawdata, i+2) |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 613 | if res is None: |
Guido van Rossum | f484a33 | 1998-12-07 21:59:56 +0000 | [diff] [blame] | 614 | if self.literal: |
| 615 | self.handle_data(rawdata[i]) |
| 616 | return i+1 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 617 | self.syntax_error('no name specified in end tag') |
| 618 | tag = '' |
| 619 | k = i+2 |
| 620 | else: |
| 621 | tag = res.group(0) |
Guido van Rossum | f484a33 | 1998-12-07 21:59:56 +0000 | [diff] [blame] | 622 | if self.literal: |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 623 | if not self.stack or tag != self.stack[-1][0]: |
Guido van Rossum | f484a33 | 1998-12-07 21:59:56 +0000 | [diff] [blame] | 624 | self.handle_data(rawdata[i]) |
| 625 | return i+1 |
| 626 | self.literal = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 627 | k = res.end(0) |
Guido van Rossum | eeb2f32 | 1998-10-19 13:28:26 +0000 | [diff] [blame] | 628 | if endbracket.match(rawdata, k) is None: |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 629 | self.syntax_error('garbage in end tag') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 630 | self.finish_endtag(tag) |
| 631 | return end.end(0) |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 632 | |
| 633 | # Internal -- finish processing of start tag |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 634 | def finish_starttag(self, tagname, attrdict, method): |
| 635 | if method is not None: |
| 636 | self.handle_starttag(tagname, method, attrdict) |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 637 | else: |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 638 | self.unknown_starttag(tagname, attrdict) |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 639 | |
| 640 | # Internal -- finish processing of end tag |
| 641 | def finish_endtag(self, tag): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 642 | if not tag: |
| 643 | self.syntax_error('name-less end tag') |
| 644 | found = len(self.stack) - 1 |
| 645 | if found < 0: |
| 646 | self.unknown_endtag(tag) |
| 647 | return |
| 648 | else: |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 649 | found = -1 |
| 650 | for i in range(len(self.stack)): |
| 651 | if tag == self.stack[i][0]: |
| 652 | found = i |
| 653 | if found == -1: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 654 | self.syntax_error('unopened end tag') |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 655 | method = self.elements.get(tag, (None, None))[1] |
| 656 | if method is not None: |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 657 | self.handle_endtag(tag, method) |
| 658 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 659 | self.unknown_endtag(tag) |
| 660 | return |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 661 | while len(self.stack) > found: |
| 662 | if found < len(self.stack) - 1: |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 663 | self.syntax_error('missing close tag for %s' % self.stack[-1][2]) |
| 664 | nstag = self.stack[-1][2] |
| 665 | method = self.elements.get(nstag, (None, None))[1] |
| 666 | if method is not None: |
| 667 | self.handle_endtag(nstag, method) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 668 | else: |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 669 | self.unknown_endtag(nstag) |
Guido van Rossum | 91c8f59 | 1998-12-21 18:15:28 +0000 | [diff] [blame] | 670 | if self.__use_namespaces == len(self.stack): |
| 671 | self.__use_namespaces = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 672 | del self.stack[-1] |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 673 | |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 674 | # Overridable -- handle xml processing instruction |
| 675 | def handle_xml(self, encoding, standalone): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 676 | pass |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 677 | |
| 678 | # Overridable -- handle DOCTYPE |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 679 | def handle_doctype(self, tag, pubid, syslit, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 680 | pass |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 681 | |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 682 | # Overridable -- handle start tag |
| 683 | def handle_starttag(self, tag, method, attrs): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 684 | method(attrs) |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 685 | |
| 686 | # Overridable -- handle end tag |
| 687 | def handle_endtag(self, tag, method): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 688 | method() |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 689 | |
| 690 | # Example -- handle character reference, no need to override |
| 691 | def handle_charref(self, name): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 692 | try: |
| 693 | if name[0] == 'x': |
| 694 | n = string.atoi(name[1:], 16) |
| 695 | else: |
| 696 | n = string.atoi(name) |
| 697 | except string.atoi_error: |
| 698 | self.unknown_charref(name) |
| 699 | return |
| 700 | if not 0 <= n <= 255: |
| 701 | self.unknown_charref(name) |
| 702 | return |
| 703 | self.handle_data(chr(n)) |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 704 | |
| 705 | # Definition of entities -- derived classes may override |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 706 | entitydefs = {'lt': '<', # must use charref |
| 707 | 'gt': '>', |
| 708 | 'amp': '&', # must use charref |
| 709 | 'quot': '"', |
| 710 | 'apos': ''', |
| 711 | } |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 712 | |
| 713 | # Example -- handle entity reference, no need to override |
| 714 | def handle_entityref(self, name): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 715 | table = self.entitydefs |
| 716 | if table.has_key(name): |
| 717 | self.handle_data(table[name]) |
| 718 | else: |
| 719 | self.unknown_entityref(name) |
| 720 | return |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 721 | |
| 722 | # Example -- handle data, should be overridden |
| 723 | def handle_data(self, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 724 | pass |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 725 | |
| 726 | # Example -- handle cdata, could be overridden |
| 727 | def handle_cdata(self, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 728 | pass |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 729 | |
| 730 | # Example -- handle comment, could be overridden |
| 731 | def handle_comment(self, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 732 | pass |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 733 | |
| 734 | # Example -- handle processing instructions, could be overridden |
| 735 | def handle_proc(self, name, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 736 | pass |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 737 | |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 738 | # Example -- handle relatively harmless syntax errors, could be overridden |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 739 | def syntax_error(self, message): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 740 | raise RuntimeError, 'Syntax error at line %d: %s' % (self.lineno, message) |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 741 | |
| 742 | # To be overridden -- handlers for unknown objects |
| 743 | def unknown_starttag(self, tag, attrs): pass |
| 744 | def unknown_endtag(self, tag): pass |
| 745 | def unknown_charref(self, ref): pass |
| 746 | def unknown_entityref(self, ref): pass |
| 747 | |
| 748 | |
| 749 | class TestXMLParser(XMLParser): |
| 750 | |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 751 | def __init__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 752 | self.testdata = "" |
Guido van Rossum | b083a9f | 1998-12-18 20:17:13 +0000 | [diff] [blame] | 753 | XMLParser.__init__(self) |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 754 | |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 755 | def handle_xml(self, encoding, standalone): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 756 | self.flush() |
| 757 | print 'xml: encoding =',encoding,'standalone =',standalone |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 758 | |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 759 | def handle_doctype(self, tag, pubid, syslit, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 760 | self.flush() |
| 761 | print 'DOCTYPE:',tag, `data` |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 762 | |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 763 | def handle_entity(self, name, strval, pubid, syslit, ndata): |
| 764 | self.flush() |
| 765 | print 'ENTITY:',`data` |
| 766 | |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 767 | def handle_data(self, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 768 | self.testdata = self.testdata + data |
| 769 | if len(`self.testdata`) >= 70: |
| 770 | self.flush() |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 771 | |
| 772 | def flush(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 773 | data = self.testdata |
| 774 | if data: |
| 775 | self.testdata = "" |
| 776 | print 'data:', `data` |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 777 | |
| 778 | def handle_cdata(self, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 779 | self.flush() |
| 780 | print 'cdata:', `data` |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 781 | |
| 782 | def handle_proc(self, name, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 783 | self.flush() |
| 784 | print 'processing:',name,`data` |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 785 | |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 786 | def handle_comment(self, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 787 | self.flush() |
| 788 | r = `data` |
| 789 | if len(r) > 68: |
| 790 | r = r[:32] + '...' + r[-32:] |
| 791 | print 'comment:', r |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 792 | |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 793 | def syntax_error(self, message): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 794 | print 'error at line %d:' % self.lineno, message |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 795 | |
| 796 | def unknown_starttag(self, tag, attrs): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 797 | self.flush() |
| 798 | if not attrs: |
| 799 | print 'start tag: <' + tag + '>' |
| 800 | else: |
| 801 | print 'start tag: <' + tag, |
| 802 | for name, value in attrs.items(): |
| 803 | print name + '=' + '"' + value + '"', |
| 804 | print '>' |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 805 | |
| 806 | def unknown_endtag(self, tag): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 807 | self.flush() |
| 808 | print 'end tag: </' + tag + '>' |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 809 | |
| 810 | def unknown_entityref(self, ref): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 811 | self.flush() |
| 812 | print '*** unknown entity ref: &' + ref + ';' |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 813 | |
| 814 | def unknown_charref(self, ref): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 815 | self.flush() |
| 816 | print '*** unknown char ref: &#' + ref + ';' |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 817 | |
| 818 | def close(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 819 | XMLParser.close(self) |
| 820 | self.flush() |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 821 | |
| 822 | def test(args = None): |
Guido van Rossum | af5add4 | 1998-12-22 14:23:50 +0000 | [diff] [blame] | 823 | import sys, getopt |
| 824 | from time import time |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 825 | |
| 826 | if not args: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 827 | args = sys.argv[1:] |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 828 | |
Guido van Rossum | af5add4 | 1998-12-22 14:23:50 +0000 | [diff] [blame] | 829 | opts, args = getopt.getopt(args, 'st') |
| 830 | klass = TestXMLParser |
| 831 | do_time = 0 |
| 832 | for o, a in opts: |
| 833 | if o == '-s': |
| 834 | klass = XMLParser |
| 835 | elif o == '-t': |
| 836 | do_time = 1 |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 837 | |
| 838 | if args: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 839 | file = args[0] |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 840 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 841 | file = 'test.xml' |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 842 | |
| 843 | if file == '-': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 844 | f = sys.stdin |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 845 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 846 | try: |
| 847 | f = open(file, 'r') |
| 848 | except IOError, msg: |
| 849 | print file, ":", msg |
| 850 | sys.exit(1) |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 851 | |
| 852 | data = f.read() |
| 853 | if f is not sys.stdin: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 854 | f.close() |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 855 | |
| 856 | x = klass() |
Guido van Rossum | af5add4 | 1998-12-22 14:23:50 +0000 | [diff] [blame] | 857 | t0 = time() |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 858 | try: |
Guido van Rossum | af5add4 | 1998-12-22 14:23:50 +0000 | [diff] [blame] | 859 | if do_time: |
| 860 | x.feed(data) |
| 861 | x.close() |
| 862 | else: |
| 863 | for c in data: |
| 864 | x.feed(c) |
| 865 | x.close() |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 866 | except RuntimeError, msg: |
Guido van Rossum | af5add4 | 1998-12-22 14:23:50 +0000 | [diff] [blame] | 867 | t1 = time() |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 868 | print msg |
Guido van Rossum | af5add4 | 1998-12-22 14:23:50 +0000 | [diff] [blame] | 869 | if do_time: |
| 870 | print 'total time: %g' % (t1-t0) |
Guido van Rossum | 7e07b38 | 1998-04-03 16:02:39 +0000 | [diff] [blame] | 871 | sys.exit(1) |
Guido van Rossum | af5add4 | 1998-12-22 14:23:50 +0000 | [diff] [blame] | 872 | t1 = time() |
| 873 | if do_time: |
| 874 | print 'total time: %g' % (t1-t0) |
Guido van Rossum | a219efa | 1997-11-18 15:09:54 +0000 | [diff] [blame] | 875 | |
| 876 | |
| 877 | if __name__ == '__main__': |
| 878 | test() |