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