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