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