Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`xml.sax.saxutils` --- SAX Utilities |
| 2 | ========================================= |
| 3 | |
| 4 | .. module:: xml.sax.saxutils |
| 5 | :synopsis: Convenience functions and classes for use with SAX. |
| 6 | .. moduleauthor:: Lars Marius Garshol <larsga@garshol.priv.no> |
| 7 | .. sectionauthor:: Martin v. Lรถwis <martin@v.loewis.de> |
| 8 | |
| 9 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | The module :mod:`xml.sax.saxutils` contains a number of classes and functions |
| 11 | that are commonly useful when creating SAX applications, either in direct use, |
| 12 | or as base classes. |
| 13 | |
| 14 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 15 | .. function:: escape(data, entities={}) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | |
| 17 | Escape ``'&'``, ``'<'``, and ``'>'`` in a string of data. |
| 18 | |
| 19 | You can escape other strings of data by passing a dictionary as the optional |
| 20 | *entities* parameter. The keys and values must all be strings; each key will be |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 21 | replaced with its corresponding value. The characters ``'&'``, ``'<'`` and |
| 22 | ``'>'`` are always escaped, even if *entities* is provided. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | |
| 24 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 25 | .. function:: unescape(data, entities={}) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
| 27 | Unescape ``'&'``, ``'<'``, and ``'>'`` in a string of data. |
| 28 | |
| 29 | You can unescape other strings of data by passing a dictionary as the optional |
| 30 | *entities* parameter. The keys and values must all be strings; each key will be |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 31 | replaced with its corresponding value. ``'&'``, ``'<'``, and ``'>'`` |
| 32 | are always unescaped, even if *entities* is provided. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 33 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 35 | .. function:: quoteattr(data, entities={}) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | |
| 37 | Similar to :func:`escape`, but also prepares *data* to be used as an |
| 38 | attribute value. The return value is a quoted version of *data* with any |
| 39 | additional required replacements. :func:`quoteattr` will select a quote |
| 40 | character based on the content of *data*, attempting to avoid encoding any |
| 41 | quote characters in the string. If both single- and double-quote characters |
| 42 | are already in *data*, the double-quote characters will be encoded and *data* |
| 43 | will be wrapped in double-quotes. The resulting string can be used directly |
| 44 | as an attribute value:: |
| 45 | |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 46 | >>> print("<element attr=%s>" % quoteattr("ab ' cd \" ef")) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 | <element attr="ab ' cd " ef"> |
| 48 | |
| 49 | This function is useful when generating attribute values for HTML or any SGML |
| 50 | using the reference concrete syntax. |
| 51 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 53 | .. class:: XMLGenerator(out=None, encoding='iso-8859-1') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 | |
| 55 | This class implements the :class:`ContentHandler` interface by writing SAX |
| 56 | events back into an XML document. In other words, using an :class:`XMLGenerator` |
| 57 | as the content handler will reproduce the original document being parsed. *out* |
| 58 | should be a file-like object which will default to *sys.stdout*. *encoding* is |
| 59 | the encoding of the output stream which defaults to ``'iso-8859-1'``. |
| 60 | |
| 61 | |
| 62 | .. class:: XMLFilterBase(base) |
| 63 | |
| 64 | This class is designed to sit between an :class:`XMLReader` and the client |
| 65 | application's event handlers. By default, it does nothing but pass requests up |
| 66 | to the reader and events on to the handlers unmodified, but subclasses can |
| 67 | override specific methods to modify the event stream or the configuration |
| 68 | requests as they pass through. |
| 69 | |
| 70 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 71 | .. function:: prepare_input_source(source, base='') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 | |
| 73 | This function takes an input source and an optional base URL and returns a fully |
| 74 | resolved :class:`InputSource` object ready for reading. The input source can be |
| 75 | given as a string, a file-like object, or an :class:`InputSource` object; |
| 76 | parsers will use this function to implement the polymorphic *source* argument to |
| 77 | their :meth:`parse` method. |
| 78 | |