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