Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 1 | """An XML Reader is the SAX 2 name for an XML parser. XML Parsers |
| 2 | should be based on this code. """ |
Fred Drake | 07cbc4e | 2000-09-21 17:43:48 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 3b27105 | 2006-08-17 09:10:09 +0000 | [diff] [blame] | 4 | from . import handler |
Martin v. Löwis | 2c07195 | 2001-06-07 05:52:17 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 3b27105 | 2006-08-17 09:10:09 +0000 | [diff] [blame] | 6 | from ._exceptions import SAXNotSupportedException, SAXNotRecognizedException |
Martin v. Löwis | 2c07195 | 2001-06-07 05:52:17 +0000 | [diff] [blame] | 7 | |
Fred Drake | 904f2fc | 2001-03-14 22:43:47 +0000 | [diff] [blame] | 8 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 9 | # ===== XMLREADER ===== |
| 10 | |
| 11 | class XMLReader: |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 12 | """Interface for reading an XML document using callbacks. |
Lars Gustäbel | bb75713 | 2000-09-24 20:38:18 +0000 | [diff] [blame] | 13 | |
| 14 | XMLReader is the interface that an XML parser's SAX2 driver must |
| 15 | implement. This interface allows an application to set and query |
| 16 | features and properties in the parser, to register event handlers |
| 17 | for document processing, and to initiate a document parse. |
| 18 | |
| 19 | All SAX interfaces are assumed to be synchronous: the parse |
| 20 | methods must not return until parsing is complete, and readers |
| 21 | must wait for an event-handler callback to return before reporting |
| 22 | the next event.""" |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 23 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 24 | def __init__(self): |
Skip Montanaro | f9059eb | 2000-07-06 03:01:40 +0000 | [diff] [blame] | 25 | self._cont_handler = handler.ContentHandler() |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 26 | self._dtd_handler = handler.DTDHandler() |
| 27 | self._ent_handler = handler.EntityResolver() |
Fred Drake | 07cbc4e | 2000-09-21 17:43:48 +0000 | [diff] [blame] | 28 | self._err_handler = handler.ErrorHandler() |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 29 | |
| 30 | def parse(self, source): |
Skip Montanaro | f9059eb | 2000-07-06 03:01:40 +0000 | [diff] [blame] | 31 | "Parse an XML document from a system identifier or an InputSource." |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 32 | raise NotImplementedError("This method must be implemented!") |
| 33 | |
| 34 | def getContentHandler(self): |
| 35 | "Returns the current ContentHandler." |
| 36 | return self._cont_handler |
| 37 | |
| 38 | def setContentHandler(self, handler): |
| 39 | "Registers a new object to receive document content events." |
| 40 | self._cont_handler = handler |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 41 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 42 | def getDTDHandler(self): |
| 43 | "Returns the current DTD handler." |
| 44 | return self._dtd_handler |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 45 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 46 | def setDTDHandler(self, handler): |
Skip Montanaro | f9059eb | 2000-07-06 03:01:40 +0000 | [diff] [blame] | 47 | "Register an object to receive basic DTD-related events." |
| 48 | self._dtd_handler = handler |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 49 | |
| 50 | def getEntityResolver(self): |
| 51 | "Returns the current EntityResolver." |
| 52 | return self._ent_handler |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 53 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 54 | def setEntityResolver(self, resolver): |
Skip Montanaro | f9059eb | 2000-07-06 03:01:40 +0000 | [diff] [blame] | 55 | "Register an object to resolve external entities." |
| 56 | self._ent_handler = resolver |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 57 | |
| 58 | def getErrorHandler(self): |
| 59 | "Returns the current ErrorHandler." |
| 60 | return self._err_handler |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 61 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 62 | def setErrorHandler(self, handler): |
Skip Montanaro | f9059eb | 2000-07-06 03:01:40 +0000 | [diff] [blame] | 63 | "Register an object to receive error-message events." |
| 64 | self._err_handler = handler |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 65 | |
| 66 | def setLocale(self, locale): |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 67 | """Allow an application to set the locale for errors and warnings. |
| 68 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 69 | SAX parsers are not required to provide localization for errors |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 70 | and warnings; if they cannot support the requested locale, |
| 71 | however, they must throw a SAX exception. Applications may |
| 72 | request a locale change in the middle of a parse.""" |
| 73 | raise SAXNotSupportedException("Locale support not implemented") |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 74 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 75 | def getFeature(self, name): |
| 76 | "Looks up and returns the state of a SAX2 feature." |
| 77 | raise SAXNotRecognizedException("Feature '%s' not recognized" % name) |
| 78 | |
| 79 | def setFeature(self, name, state): |
| 80 | "Sets the state of a SAX2 feature." |
| 81 | raise SAXNotRecognizedException("Feature '%s' not recognized" % name) |
| 82 | |
| 83 | def getProperty(self, name): |
| 84 | "Looks up and returns the value of a SAX2 property." |
| 85 | raise SAXNotRecognizedException("Property '%s' not recognized" % name) |
| 86 | |
| 87 | def setProperty(self, name, value): |
| 88 | "Sets the value of a SAX2 property." |
| 89 | raise SAXNotRecognizedException("Property '%s' not recognized" % name) |
| 90 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 91 | class IncrementalParser(XMLReader): |
| 92 | """This interface adds three extra methods to the XMLReader |
| 93 | interface that allow XML parsers to support incremental |
| 94 | parsing. Support for this interface is optional, since not all |
| 95 | underlying XML parsers support this functionality. |
| 96 | |
| 97 | When the parser is instantiated it is ready to begin accepting |
| 98 | data from the feed method immediately. After parsing has been |
| 99 | finished with a call to close the reset method must be called to |
| 100 | make the parser ready to accept new data, either from feed or |
| 101 | using the parse method. |
| 102 | |
| 103 | Note that these methods must _not_ be called during parsing, that |
| 104 | is, after parse has been called and before it returns. |
| 105 | |
| 106 | By default, the class also implements the parse method of the XMLReader |
| 107 | interface using the feed, close and reset methods of the |
| 108 | IncrementalParser interface as a convenience to SAX 2.0 driver |
| 109 | writers.""" |
Fred Drake | 07cbc4e | 2000-09-21 17:43:48 +0000 | [diff] [blame] | 110 | |
| 111 | def __init__(self, bufsize=2**16): |
| 112 | self._bufsize = bufsize |
| 113 | XMLReader.__init__(self) |
| 114 | |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 115 | def parse(self, source): |
Guido van Rossum | 3b27105 | 2006-08-17 09:10:09 +0000 | [diff] [blame] | 116 | from . import saxutils |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 117 | source = saxutils.prepare_input_source(source) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 118 | |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 119 | self.prepareParser(source) |
| 120 | file = source.getByteStream() |
| 121 | buffer = file.read(self._bufsize) |
Benjamin Peterson | a7f4f5a | 2008-09-04 02:22:52 +0000 | [diff] [blame^] | 122 | while buffer: |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 123 | self.feed(buffer) |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 124 | buffer = file.read(self._bufsize) |
Martin v. Löwis | 31b485f | 2000-10-06 21:12:12 +0000 | [diff] [blame] | 125 | self.close() |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 126 | |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 127 | def feed(self, data): |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 128 | """This method gives the raw XML data in the data parameter to |
| 129 | the parser and makes it parse the data, emitting the |
| 130 | corresponding events. It is allowed for XML constructs to be |
| 131 | split across several calls to feed. |
| 132 | |
| 133 | feed may raise SAXException.""" |
| 134 | raise NotImplementedError("This method must be implemented!") |
Fred Drake | 07cbc4e | 2000-09-21 17:43:48 +0000 | [diff] [blame] | 135 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 136 | def prepareParser(self, source): |
| 137 | """This method is called by the parse implementation to allow |
| 138 | the SAX 2.0 driver to prepare itself for parsing.""" |
| 139 | raise NotImplementedError("prepareParser must be overridden!") |
| 140 | |
| 141 | def close(self): |
| 142 | """This method is called when the entire XML document has been |
| 143 | passed to the parser through the feed method, to notify the |
| 144 | parser that there are no more data. This allows the parser to |
| 145 | do the final checks on the document and empty the internal |
| 146 | data buffer. |
| 147 | |
| 148 | The parser will not be ready to parse another document until |
| 149 | the reset method has been called. |
| 150 | |
| 151 | close may raise SAXException.""" |
| 152 | raise NotImplementedError("This method must be implemented!") |
| 153 | |
| 154 | def reset(self): |
| 155 | """This method is called after close has been called to reset |
| 156 | the parser so that it is ready to parse new documents. The |
| 157 | results of calling parse or feed after close without calling |
| 158 | reset are undefined.""" |
| 159 | raise NotImplementedError("This method must be implemented!") |
| 160 | |
| 161 | # ===== LOCATOR ===== |
Lars Gustäbel | 32bf12e | 2000-09-24 18:39:23 +0000 | [diff] [blame] | 162 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 163 | class Locator: |
| 164 | """Interface for associating a SAX event with a document |
| 165 | location. A locator object will return valid results only during |
| 166 | calls to DocumentHandler methods; at any other time, the |
| 167 | results are unpredictable.""" |
| 168 | |
| 169 | def getColumnNumber(self): |
Skip Montanaro | f9059eb | 2000-07-06 03:01:40 +0000 | [diff] [blame] | 170 | "Return the column number where the current event ends." |
| 171 | return -1 |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 172 | |
| 173 | def getLineNumber(self): |
Skip Montanaro | f9059eb | 2000-07-06 03:01:40 +0000 | [diff] [blame] | 174 | "Return the line number where the current event ends." |
| 175 | return -1 |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 176 | |
| 177 | def getPublicId(self): |
Skip Montanaro | f9059eb | 2000-07-06 03:01:40 +0000 | [diff] [blame] | 178 | "Return the public identifier for the current event." |
| 179 | return None |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 180 | |
| 181 | def getSystemId(self): |
Skip Montanaro | f9059eb | 2000-07-06 03:01:40 +0000 | [diff] [blame] | 182 | "Return the system identifier for the current event." |
| 183 | return None |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 184 | |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 185 | # ===== INPUTSOURCE ===== |
| 186 | |
| 187 | class InputSource: |
| 188 | """Encapsulation of the information needed by the XMLReader to |
| 189 | read entities. |
| 190 | |
| 191 | This class may include information about the public identifier, |
| 192 | system identifier, byte stream (possibly with character encoding |
| 193 | information) and/or the character stream of an entity. |
| 194 | |
| 195 | Applications will create objects of this class for use in the |
| 196 | XMLReader.parse method and for returning from |
| 197 | EntityResolver.resolveEntity. |
| 198 | |
| 199 | An InputSource belongs to the application, the XMLReader is not |
| 200 | allowed to modify InputSource objects passed to it from the |
| 201 | application, although it may make copies and modify those.""" |
| 202 | |
| 203 | def __init__(self, system_id = None): |
| 204 | self.__system_id = system_id |
| 205 | self.__public_id = None |
| 206 | self.__encoding = None |
| 207 | self.__bytefile = None |
| 208 | self.__charfile = None |
| 209 | |
| 210 | def setPublicId(self, public_id): |
| 211 | "Sets the public identifier of this InputSource." |
| 212 | self.__public_id = public_id |
| 213 | |
| 214 | def getPublicId(self): |
| 215 | "Returns the public identifier of this InputSource." |
| 216 | return self.__public_id |
| 217 | |
| 218 | def setSystemId(self, system_id): |
| 219 | "Sets the system identifier of this InputSource." |
| 220 | self.__system_id = system_id |
| 221 | |
| 222 | def getSystemId(self): |
| 223 | "Returns the system identifier of this InputSource." |
| 224 | return self.__system_id |
| 225 | |
| 226 | def setEncoding(self, encoding): |
| 227 | """Sets the character encoding of this InputSource. |
| 228 | |
| 229 | The encoding must be a string acceptable for an XML encoding |
| 230 | declaration (see section 4.3.3 of the XML recommendation). |
| 231 | |
| 232 | The encoding attribute of the InputSource is ignored if the |
| 233 | InputSource also contains a character stream.""" |
| 234 | self.__encoding = encoding |
| 235 | |
| 236 | def getEncoding(self): |
| 237 | "Get the character encoding of this InputSource." |
| 238 | return self.__encoding |
| 239 | |
| 240 | def setByteStream(self, bytefile): |
| 241 | """Set the byte stream (a Python file-like object which does |
| 242 | not perform byte-to-character conversion) for this input |
| 243 | source. |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 244 | |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 245 | The SAX parser will ignore this if there is also a character |
| 246 | stream specified, but it will use a byte stream in preference |
| 247 | to opening a URI connection itself. |
| 248 | |
| 249 | If the application knows the character encoding of the byte |
| 250 | stream, it should set it with the setEncoding method.""" |
| 251 | self.__bytefile = bytefile |
| 252 | |
| 253 | def getByteStream(self): |
| 254 | """Get the byte stream for this input source. |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 255 | |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 256 | The getEncoding method will return the character encoding for |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 257 | this byte stream, or None if unknown.""" |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 258 | return self.__bytefile |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 259 | |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 260 | def setCharacterStream(self, charfile): |
| 261 | """Set the character stream for this input source. (The stream |
Martin v. Löwis | 711a5bd | 2001-01-27 08:56:24 +0000 | [diff] [blame] | 262 | must be a Python 2.0 Unicode-wrapped file-like that performs |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 263 | conversion to Unicode strings.) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 264 | |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 265 | If there is a character stream specified, the SAX parser will |
| 266 | ignore any byte stream and will not attempt to open a URI |
| 267 | connection to the system identifier.""" |
| 268 | self.__charfile = charfile |
| 269 | |
| 270 | def getCharacterStream(self): |
| 271 | "Get the character stream for this input source." |
| 272 | return self.__charfile |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 273 | |
Lars Gustäbel | 32bf12e | 2000-09-24 18:39:23 +0000 | [diff] [blame] | 274 | # ===== ATTRIBUTESIMPL ===== |
| 275 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 276 | class AttributesImpl: |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 277 | |
Lars Gustäbel | 32bf12e | 2000-09-24 18:39:23 +0000 | [diff] [blame] | 278 | def __init__(self, attrs): |
| 279 | """Non-NS-aware implementation. |
| 280 | |
| 281 | attrs should be of the form {name : value}.""" |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 282 | self._attrs = attrs |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 283 | |
| 284 | def getLength(self): |
| 285 | return len(self._attrs) |
| 286 | |
| 287 | def getType(self, name): |
| 288 | return "CDATA" |
| 289 | |
| 290 | def getValue(self, name): |
| 291 | return self._attrs[name] |
| 292 | |
| 293 | def getValueByQName(self, name): |
Lars Gustäbel | 32bf12e | 2000-09-24 18:39:23 +0000 | [diff] [blame] | 294 | return self._attrs[name] |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 295 | |
| 296 | def getNameByQName(self, name): |
Guido van Rossum | 1b01e5c | 2006-08-19 02:45:06 +0000 | [diff] [blame] | 297 | if name not in self._attrs: |
Collin Winter | 70e7980 | 2007-08-24 18:57:22 +0000 | [diff] [blame] | 298 | raise KeyError(name) |
Lars Gustäbel | 32bf12e | 2000-09-24 18:39:23 +0000 | [diff] [blame] | 299 | return name |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 300 | |
Lars Gustäbel | 32bf12e | 2000-09-24 18:39:23 +0000 | [diff] [blame] | 301 | def getQNameByName(self, name): |
Guido van Rossum | 1b01e5c | 2006-08-19 02:45:06 +0000 | [diff] [blame] | 302 | if name not in self._attrs: |
Collin Winter | 70e7980 | 2007-08-24 18:57:22 +0000 | [diff] [blame] | 303 | raise KeyError(name) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 304 | return name |
| 305 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 306 | def getNames(self): |
Guido van Rossum | 091153d | 2007-02-11 18:44:55 +0000 | [diff] [blame] | 307 | return list(self._attrs.keys()) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 308 | |
| 309 | def getQNames(self): |
Guido van Rossum | 091153d | 2007-02-11 18:44:55 +0000 | [diff] [blame] | 310 | return list(self._attrs.keys()) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 311 | |
| 312 | def __len__(self): |
| 313 | return len(self._attrs) |
| 314 | |
| 315 | def __getitem__(self, name): |
| 316 | return self._attrs[name] |
| 317 | |
| 318 | def keys(self): |
Guido van Rossum | 091153d | 2007-02-11 18:44:55 +0000 | [diff] [blame] | 319 | return list(self._attrs.keys()) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 320 | |
Raymond Hettinger | 0e44923 | 2003-01-30 00:56:33 +0000 | [diff] [blame] | 321 | def __contains__(self, name): |
Guido van Rossum | 1b01e5c | 2006-08-19 02:45:06 +0000 | [diff] [blame] | 322 | return name in self._attrs |
Raymond Hettinger | 0e44923 | 2003-01-30 00:56:33 +0000 | [diff] [blame] | 323 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 324 | def get(self, name, alternative=None): |
| 325 | return self._attrs.get(name, alternative) |
| 326 | |
| 327 | def copy(self): |
Lars Gustäbel | 32bf12e | 2000-09-24 18:39:23 +0000 | [diff] [blame] | 328 | return self.__class__(self._attrs) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 329 | |
| 330 | def items(self): |
Guido van Rossum | 091153d | 2007-02-11 18:44:55 +0000 | [diff] [blame] | 331 | return list(self._attrs.items()) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 332 | |
| 333 | def values(self): |
Guido van Rossum | 091153d | 2007-02-11 18:44:55 +0000 | [diff] [blame] | 334 | return list(self._attrs.values()) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 335 | |
Lars Gustäbel | 32bf12e | 2000-09-24 18:39:23 +0000 | [diff] [blame] | 336 | # ===== ATTRIBUTESNSIMPL ===== |
| 337 | |
| 338 | class AttributesNSImpl(AttributesImpl): |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 339 | |
Lars Gustäbel | 32bf12e | 2000-09-24 18:39:23 +0000 | [diff] [blame] | 340 | def __init__(self, attrs, qnames): |
| 341 | """NS-aware implementation. |
| 342 | |
| 343 | attrs should be of the form {(ns_uri, lname): value, ...}. |
| 344 | qnames of the form {(ns_uri, lname): qname, ...}.""" |
| 345 | self._attrs = attrs |
| 346 | self._qnames = qnames |
| 347 | |
| 348 | def getValueByQName(self, name): |
| 349 | for (nsname, qname) in self._qnames.items(): |
| 350 | if qname == name: |
| 351 | return self._attrs[nsname] |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 352 | |
Collin Winter | 70e7980 | 2007-08-24 18:57:22 +0000 | [diff] [blame] | 353 | raise KeyError(name) |
Lars Gustäbel | 32bf12e | 2000-09-24 18:39:23 +0000 | [diff] [blame] | 354 | |
| 355 | def getNameByQName(self, name): |
| 356 | for (nsname, qname) in self._qnames.items(): |
| 357 | if qname == name: |
| 358 | return nsname |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 359 | |
Collin Winter | 70e7980 | 2007-08-24 18:57:22 +0000 | [diff] [blame] | 360 | raise KeyError(name) |
Lars Gustäbel | 32bf12e | 2000-09-24 18:39:23 +0000 | [diff] [blame] | 361 | |
| 362 | def getQNameByName(self, name): |
| 363 | return self._qnames[name] |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 364 | |
Lars Gustäbel | 32bf12e | 2000-09-24 18:39:23 +0000 | [diff] [blame] | 365 | def getQNames(self): |
Guido van Rossum | 091153d | 2007-02-11 18:44:55 +0000 | [diff] [blame] | 366 | return list(self._qnames.values()) |
Lars Gustäbel | 32bf12e | 2000-09-24 18:39:23 +0000 | [diff] [blame] | 367 | |
| 368 | def copy(self): |
| 369 | return self.__class__(self._attrs, self._qnames) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 370 | |
Fred Drake | 07cbc4e | 2000-09-21 17:43:48 +0000 | [diff] [blame] | 371 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 372 | def _test(): |
| 373 | XMLReader() |
| 374 | IncrementalParser() |
| 375 | Locator() |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 376 | |
Fred Drake | 07cbc4e | 2000-09-21 17:43:48 +0000 | [diff] [blame] | 377 | if __name__ == "__main__": |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 378 | _test() |