| 1 |
|
| 2 |
|
| 3 |
<!DOCTYPE html> |
| 4 |
<html class="writer-html5" lang="en" data-content_root="../../../"> |
| 5 |
<head> |
| 6 |
<meta charset="utf-8" /> |
| 7 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 8 |
<title>xml.sax.handler — lxml documentation</title> |
| 9 |
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css?v=b86133f3" /> |
| 10 |
<link rel="stylesheet" type="text/css" href="../../../_static/css/theme.css?v=9edc463e" /> |
| 11 |
|
| 12 |
|
| 13 |
<script src="../../../_static/jquery.js?v=5d32c60e"></script> |
| 14 |
<script src="../../../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script> |
| 15 |
<script src="../../../_static/documentation_options.js?v=5929fcd5"></script> |
| 16 |
<script src="../../../_static/doctools.js?v=fd6eb6e6"></script> |
| 17 |
<script src="../../../_static/sphinx_highlight.js?v=6ffebe34"></script> |
| 18 |
<script src="../../../_static/js/theme.js"></script> |
| 19 |
<link rel="index" title="Index" href="../../../genindex.html" /> |
| 20 |
<link rel="search" title="Search" href="../../../search.html" /> |
| 21 |
</head> |
| 22 |
|
| 23 |
<body class="wy-body-for-nav"> |
| 24 |
<div class="wy-grid-for-nav"> |
| 25 |
<nav data-toggle="wy-nav-shift" class="wy-nav-side"> |
| 26 |
<div class="wy-side-scroll"> |
| 27 |
<div class="wy-side-nav-search" > |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
<a href="../../../index.html" class="icon icon-home"> |
| 32 |
lxml |
| 33 |
<img src="../../../_static/python-xml.png" class="logo" alt="Logo"/> |
| 34 |
</a> |
| 35 |
<div role="search"> |
| 36 |
<form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get"> |
| 37 |
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" /> |
| 38 |
<input type="hidden" name="check_keywords" value="yes" /> |
| 39 |
<input type="hidden" name="area" value="default" /> |
| 40 |
</form> |
| 41 |
</div> |
| 42 |
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu"> |
| 43 |
<ul> |
| 44 |
<li class="toctree-l1"><a class="reference internal" href="../../../lxml.html">lxml package</a><ul> |
| 45 |
<li class="toctree-l2"><a class="reference internal" href="../../../lxml.html.html">lxml.html package</a><ul> |
| 46 |
<li class="toctree-l3"><a class="reference internal" href="../../../lxml.html.ElementSoup.html">lxml.html.ElementSoup module</a></li> |
| 47 |
<li class="toctree-l3"><a class="reference internal" href="../../../lxml.html.builder.html">lxml.html.builder module</a></li> |
| 48 |
<li class="toctree-l3"><a class="reference internal" href="../../../lxml.html.clean.html">lxml.html.clean module</a></li> |
| 49 |
<li class="toctree-l3"><a class="reference internal" href="../../../lxml.html.defs.html">lxml.html.defs module</a></li> |
| 50 |
<li class="toctree-l3"><a class="reference internal" href="../../../lxml.html.diff.html">lxml.html.diff module</a></li> |
| 51 |
<li class="toctree-l3"><a class="reference internal" href="../../../lxml.html.formfill.html">lxml.html.formfill module</a></li> |
| 52 |
<li class="toctree-l3"><a class="reference internal" href="../../../lxml.html.html5parser.html">lxml.html.html5parser module</a></li> |
| 53 |
<li class="toctree-l3"><a class="reference internal" href="../../../lxml.html.soupparser.html">lxml.html.soupparser module</a></li> |
| 54 |
</ul> |
| 55 |
</li> |
| 56 |
<li class="toctree-l2"><a class="reference internal" href="../../../lxml.isoschematron.html">lxml.isoschematron package</a></li> |
| 57 |
<li class="toctree-l2"><a class="reference internal" href="../../../lxml.ElementInclude.html">lxml.ElementInclude module</a></li> |
| 58 |
<li class="toctree-l2"><a class="reference internal" href="../../../lxml._elementpath.html">lxml._elementpath module</a></li> |
| 59 |
<li class="toctree-l2"><a class="reference internal" href="../../../lxml.builder.html">lxml.builder module</a></li> |
| 60 |
<li class="toctree-l2"><a class="reference internal" href="../../../lxml.cssselect.html">lxml.cssselect module</a></li> |
| 61 |
<li class="toctree-l2"><a class="reference internal" href="../../../lxml.doctestcompare.html">lxml.doctestcompare module</a></li> |
| 62 |
<li class="toctree-l2"><a class="reference internal" href="../../../lxml.etree.html">lxml.etree module</a></li> |
| 63 |
<li class="toctree-l2"><a class="reference internal" href="../../../lxml.objectify.html">lxml.objectify module</a></li> |
| 64 |
<li class="toctree-l2"><a class="reference internal" href="../../../lxml.sax.html">lxml.sax module</a></li> |
| 65 |
</ul> |
| 66 |
</li> |
| 67 |
</ul> |
| 68 |
|
| 69 |
</div> |
| 70 |
</div> |
| 71 |
</nav> |
| 72 |
|
| 73 |
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" > |
| 74 |
<i data-toggle="wy-nav-top" class="fa fa-bars"></i> |
| 75 |
<a href="../../../index.html">lxml</a> |
| 76 |
</nav> |
| 77 |
|
| 78 |
<div class="wy-nav-content"> |
| 79 |
<div class="rst-content"> |
| 80 |
<div role="navigation" aria-label="Page navigation"> |
| 81 |
<ul class="wy-breadcrumbs"> |
| 82 |
<li><a href="../../../index.html" class="icon icon-home" aria-label="Home"></a></li> |
| 83 |
<li class="breadcrumb-item"><a href="../../index.html">Module code</a></li> |
| 84 |
<li class="breadcrumb-item active">xml.sax.handler</li> |
| 85 |
<li class="wy-breadcrumbs-aside"> |
| 86 |
</li> |
| 87 |
</ul> |
| 88 |
<hr/> |
| 89 |
</div> |
| 90 |
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> |
| 91 |
<div itemprop="articleBody"> |
| 92 |
|
| 93 |
<h1>Source code for xml.sax.handler</h1><div class="highlight"><pre> |
| 94 |
<span></span><span class="sd">"""</span> |
| 95 |
<span class="sd">This module contains the core classes of version 2.0 of SAX for Python.</span> |
| 96 |
<span class="sd">This file provides only default classes with absolutely minimum</span> |
| 97 |
<span class="sd">functionality, from which drivers and applications can be subclassed.</span> |
| 98 |
|
| 99 |
<span class="sd">Many of these classes are empty and are included only as documentation</span> |
| 100 |
<span class="sd">of the interfaces.</span> |
| 101 |
|
| 102 |
<span class="sd">$Id$</span> |
| 103 |
<span class="sd">"""</span> |
| 104 |
|
| 105 |
<span class="n">version</span> <span class="o">=</span> <span class="s1">'2.0beta'</span> |
| 106 |
|
| 107 |
<span class="c1">#============================================================================</span> |
| 108 |
<span class="c1">#</span> |
| 109 |
<span class="c1"># HANDLER INTERFACES</span> |
| 110 |
<span class="c1">#</span> |
| 111 |
<span class="c1">#============================================================================</span> |
| 112 |
|
| 113 |
<span class="c1"># ===== ERRORHANDLER =====</span> |
| 114 |
|
| 115 |
<span class="k">class</span><span class="w"> </span><span class="nc">ErrorHandler</span><span class="p">:</span> |
| 116 |
<span class="w"> </span><span class="sd">"""Basic interface for SAX error handlers.</span> |
| 117 |
|
| 118 |
<span class="sd"> If you create an object that implements this interface, then</span> |
| 119 |
<span class="sd"> register the object with your XMLReader, the parser will call the</span> |
| 120 |
<span class="sd"> methods in your object to report all warnings and errors. There</span> |
| 121 |
<span class="sd"> are three levels of errors available: warnings, (possibly)</span> |
| 122 |
<span class="sd"> recoverable errors, and unrecoverable errors. All methods take a</span> |
| 123 |
<span class="sd"> SAXParseException as the only parameter."""</span> |
| 124 |
|
| 125 |
<span class="k">def</span><span class="w"> </span><span class="nf">error</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exception</span><span class="p">):</span> |
| 126 |
<span class="s2">"Handle a recoverable error."</span> |
| 127 |
<span class="k">raise</span> <span class="n">exception</span> |
| 128 |
|
| 129 |
<span class="k">def</span><span class="w"> </span><span class="nf">fatalError</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exception</span><span class="p">):</span> |
| 130 |
<span class="s2">"Handle a non-recoverable error."</span> |
| 131 |
<span class="k">raise</span> <span class="n">exception</span> |
| 132 |
|
| 133 |
<span class="k">def</span><span class="w"> </span><span class="nf">warning</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exception</span><span class="p">):</span> |
| 134 |
<span class="s2">"Handle a warning."</span> |
| 135 |
<span class="nb">print</span><span class="p">(</span><span class="n">exception</span><span class="p">)</span> |
| 136 |
|
| 137 |
|
| 138 |
<span class="c1"># ===== CONTENTHANDLER =====</span> |
| 139 |
|
| 140 |
<span class="k">class</span><span class="w"> </span><span class="nc">ContentHandler</span><span class="p">:</span> |
| 141 |
<span class="w"> </span><span class="sd">"""Interface for receiving logical document content events.</span> |
| 142 |
|
| 143 |
<span class="sd"> This is the main callback interface in SAX, and the one most</span> |
| 144 |
<span class="sd"> important to applications. The order of events in this interface</span> |
| 145 |
<span class="sd"> mirrors the order of the information in the document."""</span> |
| 146 |
|
| 147 |
<span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> |
| 148 |
<span class="bp">self</span><span class="o">.</span><span class="n">_locator</span> <span class="o">=</span> <span class="kc">None</span> |
| 149 |
|
| 150 |
<span class="k">def</span><span class="w"> </span><span class="nf">setDocumentLocator</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">locator</span><span class="p">):</span> |
| 151 |
<span class="w"> </span><span class="sd">"""Called by the parser to give the application a locator for</span> |
| 152 |
<span class="sd"> locating the origin of document events.</span> |
| 153 |
|
| 154 |
<span class="sd"> SAX parsers are strongly encouraged (though not absolutely</span> |
| 155 |
<span class="sd"> required) to supply a locator: if it does so, it must supply</span> |
| 156 |
<span class="sd"> the locator to the application by invoking this method before</span> |
| 157 |
<span class="sd"> invoking any of the other methods in the DocumentHandler</span> |
| 158 |
<span class="sd"> interface.</span> |
| 159 |
|
| 160 |
<span class="sd"> The locator allows the application to determine the end</span> |
| 161 |
<span class="sd"> position of any document-related event, even if the parser is</span> |
| 162 |
<span class="sd"> not reporting an error. Typically, the application will use</span> |
| 163 |
<span class="sd"> this information for reporting its own errors (such as</span> |
| 164 |
<span class="sd"> character content that does not match an application's</span> |
| 165 |
<span class="sd"> business rules). The information returned by the locator is</span> |
| 166 |
<span class="sd"> probably not sufficient for use with a search engine.</span> |
| 167 |
|
| 168 |
<span class="sd"> Note that the locator will return correct information only</span> |
| 169 |
<span class="sd"> during the invocation of the events in this interface. The</span> |
| 170 |
<span class="sd"> application should not attempt to use it at any other time."""</span> |
| 171 |
<span class="bp">self</span><span class="o">.</span><span class="n">_locator</span> <span class="o">=</span> <span class="n">locator</span> |
| 172 |
|
| 173 |
<span class="k">def</span><span class="w"> </span><span class="nf">startDocument</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> |
| 174 |
<span class="w"> </span><span class="sd">"""Receive notification of the beginning of a document.</span> |
| 175 |
|
| 176 |
<span class="sd"> The SAX parser will invoke this method only once, before any</span> |
| 177 |
<span class="sd"> other methods in this interface or in DTDHandler (except for</span> |
| 178 |
<span class="sd"> setDocumentLocator)."""</span> |
| 179 |
|
| 180 |
<span class="k">def</span><span class="w"> </span><span class="nf">endDocument</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> |
| 181 |
<span class="w"> </span><span class="sd">"""Receive notification of the end of a document.</span> |
| 182 |
|
| 183 |
<span class="sd"> The SAX parser will invoke this method only once, and it will</span> |
| 184 |
<span class="sd"> be the last method invoked during the parse. The parser shall</span> |
| 185 |
<span class="sd"> not invoke this method until it has either abandoned parsing</span> |
| 186 |
<span class="sd"> (because of an unrecoverable error) or reached the end of</span> |
| 187 |
<span class="sd"> input."""</span> |
| 188 |
|
| 189 |
<span class="k">def</span><span class="w"> </span><span class="nf">startPrefixMapping</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">uri</span><span class="p">):</span> |
| 190 |
<span class="w"> </span><span class="sd">"""Begin the scope of a prefix-URI Namespace mapping.</span> |
| 191 |
|
| 192 |
<span class="sd"> The information from this event is not necessary for normal</span> |
| 193 |
<span class="sd"> Namespace processing: the SAX XML reader will automatically</span> |
| 194 |
<span class="sd"> replace prefixes for element and attribute names when the</span> |
| 195 |
<span class="sd"> http://xml.org/sax/features/namespaces feature is true (the</span> |
| 196 |
<span class="sd"> default).</span> |
| 197 |
|
| 198 |
<span class="sd"> There are cases, however, when applications need to use</span> |
| 199 |
<span class="sd"> prefixes in character data or in attribute values, where they</span> |
| 200 |
<span class="sd"> cannot safely be expanded automatically; the</span> |
| 201 |
<span class="sd"> start/endPrefixMapping event supplies the information to the</span> |
| 202 |
<span class="sd"> application to expand prefixes in those contexts itself, if</span> |
| 203 |
<span class="sd"> necessary.</span> |
| 204 |
|
| 205 |
<span class="sd"> Note that start/endPrefixMapping events are not guaranteed to</span> |
| 206 |
<span class="sd"> be properly nested relative to each-other: all</span> |
| 207 |
<span class="sd"> startPrefixMapping events will occur before the corresponding</span> |
| 208 |
<span class="sd"> startElement event, and all endPrefixMapping events will occur</span> |
| 209 |
<span class="sd"> after the corresponding endElement event, but their order is</span> |
| 210 |
<span class="sd"> not guaranteed."""</span> |
| 211 |
|
| 212 |
<span class="k">def</span><span class="w"> </span><span class="nf">endPrefixMapping</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prefix</span><span class="p">):</span> |
| 213 |
<span class="w"> </span><span class="sd">"""End the scope of a prefix-URI mapping.</span> |
| 214 |
|
| 215 |
<span class="sd"> See startPrefixMapping for details. This event will always</span> |
| 216 |
<span class="sd"> occur after the corresponding endElement event, but the order</span> |
| 217 |
<span class="sd"> of endPrefixMapping events is not otherwise guaranteed."""</span> |
| 218 |
|
| 219 |
<span class="k">def</span><span class="w"> </span><span class="nf">startElement</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span> |
| 220 |
<span class="w"> </span><span class="sd">"""Signals the start of an element in non-namespace mode.</span> |
| 221 |
|
| 222 |
<span class="sd"> The name parameter contains the raw XML 1.0 name of the</span> |
| 223 |
<span class="sd"> element type as a string and the attrs parameter holds an</span> |
| 224 |
<span class="sd"> instance of the Attributes class containing the attributes of</span> |
| 225 |
<span class="sd"> the element."""</span> |
| 226 |
|
| 227 |
<span class="k">def</span><span class="w"> </span><span class="nf">endElement</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span> |
| 228 |
<span class="w"> </span><span class="sd">"""Signals the end of an element in non-namespace mode.</span> |
| 229 |
|
| 230 |
<span class="sd"> The name parameter contains the name of the element type, just</span> |
| 231 |
<span class="sd"> as with the startElement event."""</span> |
| 232 |
|
| 233 |
<span class="k">def</span><span class="w"> </span><span class="nf">startElementNS</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">qname</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span> |
| 234 |
<span class="w"> </span><span class="sd">"""Signals the start of an element in namespace mode.</span> |
| 235 |
|
| 236 |
<span class="sd"> The name parameter contains the name of the element type as a</span> |
| 237 |
<span class="sd"> (uri, localname) tuple, the qname parameter the raw XML 1.0</span> |
| 238 |
<span class="sd"> name used in the source document, and the attrs parameter</span> |
| 239 |
<span class="sd"> holds an instance of the Attributes class containing the</span> |
| 240 |
<span class="sd"> attributes of the element.</span> |
| 241 |
|
| 242 |
<span class="sd"> The uri part of the name tuple is None for elements which have</span> |
| 243 |
<span class="sd"> no namespace."""</span> |
| 244 |
|
| 245 |
<span class="k">def</span><span class="w"> </span><span class="nf">endElementNS</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">qname</span><span class="p">):</span> |
| 246 |
<span class="w"> </span><span class="sd">"""Signals the end of an element in namespace mode.</span> |
| 247 |
|
| 248 |
<span class="sd"> The name parameter contains the name of the element type, just</span> |
| 249 |
<span class="sd"> as with the startElementNS event."""</span> |
| 250 |
|
| 251 |
<span class="k">def</span><span class="w"> </span><span class="nf">characters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">content</span><span class="p">):</span> |
| 252 |
<span class="w"> </span><span class="sd">"""Receive notification of character data.</span> |
| 253 |
|
| 254 |
<span class="sd"> The Parser will call this method to report each chunk of</span> |
| 255 |
<span class="sd"> character data. SAX parsers may return all contiguous</span> |
| 256 |
<span class="sd"> character data in a single chunk, or they may split it into</span> |
| 257 |
<span class="sd"> several chunks; however, all of the characters in any single</span> |
| 258 |
<span class="sd"> event must come from the same external entity so that the</span> |
| 259 |
<span class="sd"> Locator provides useful information."""</span> |
| 260 |
|
| 261 |
<span class="k">def</span><span class="w"> </span><span class="nf">ignorableWhitespace</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">whitespace</span><span class="p">):</span> |
| 262 |
<span class="w"> </span><span class="sd">"""Receive notification of ignorable whitespace in element content.</span> |
| 263 |
|
| 264 |
<span class="sd"> Validating Parsers must use this method to report each chunk</span> |
| 265 |
<span class="sd"> of ignorable whitespace (see the W3C XML 1.0 recommendation,</span> |
| 266 |
<span class="sd"> section 2.10): non-validating parsers may also use this method</span> |
| 267 |
<span class="sd"> if they are capable of parsing and using content models.</span> |
| 268 |
|
| 269 |
<span class="sd"> SAX parsers may return all contiguous whitespace in a single</span> |
| 270 |
<span class="sd"> chunk, or they may split it into several chunks; however, all</span> |
| 271 |
<span class="sd"> of the characters in any single event must come from the same</span> |
| 272 |
<span class="sd"> external entity, so that the Locator provides useful</span> |
| 273 |
<span class="sd"> information."""</span> |
| 274 |
|
| 275 |
<span class="k">def</span><span class="w"> </span><span class="nf">processingInstruction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">target</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span> |
| 276 |
<span class="w"> </span><span class="sd">"""Receive notification of a processing instruction.</span> |
| 277 |
|
| 278 |
<span class="sd"> The Parser will invoke this method once for each processing</span> |
| 279 |
<span class="sd"> instruction found: note that processing instructions may occur</span> |
| 280 |
<span class="sd"> before or after the main document element.</span> |
| 281 |
|
| 282 |
<span class="sd"> A SAX parser should never report an XML declaration (XML 1.0,</span> |
| 283 |
<span class="sd"> section 2.8) or a text declaration (XML 1.0, section 4.3.1)</span> |
| 284 |
<span class="sd"> using this method."""</span> |
| 285 |
|
| 286 |
<span class="k">def</span><span class="w"> </span><span class="nf">skippedEntity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span> |
| 287 |
<span class="w"> </span><span class="sd">"""Receive notification of a skipped entity.</span> |
| 288 |
|
| 289 |
<span class="sd"> The Parser will invoke this method once for each entity</span> |
| 290 |
<span class="sd"> skipped. Non-validating processors may skip entities if they</span> |
| 291 |
<span class="sd"> have not seen the declarations (because, for example, the</span> |
| 292 |
<span class="sd"> entity was declared in an external DTD subset). All processors</span> |
| 293 |
<span class="sd"> may skip external entities, depending on the values of the</span> |
| 294 |
<span class="sd"> http://xml.org/sax/features/external-general-entities and the</span> |
| 295 |
<span class="sd"> http://xml.org/sax/features/external-parameter-entities</span> |
| 296 |
<span class="sd"> properties."""</span> |
| 297 |
|
| 298 |
|
| 299 |
<span class="c1"># ===== DTDHandler =====</span> |
| 300 |
|
| 301 |
<span class="k">class</span><span class="w"> </span><span class="nc">DTDHandler</span><span class="p">:</span> |
| 302 |
<span class="w"> </span><span class="sd">"""Handle DTD events.</span> |
| 303 |
|
| 304 |
<span class="sd"> This interface specifies only those DTD events required for basic</span> |
| 305 |
<span class="sd"> parsing (unparsed entities and attributes)."""</span> |
| 306 |
|
| 307 |
<span class="k">def</span><span class="w"> </span><span class="nf">notationDecl</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">publicId</span><span class="p">,</span> <span class="n">systemId</span><span class="p">):</span> |
| 308 |
<span class="s2">"Handle a notation declaration event."</span> |
| 309 |
|
| 310 |
<span class="k">def</span><span class="w"> </span><span class="nf">unparsedEntityDecl</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">publicId</span><span class="p">,</span> <span class="n">systemId</span><span class="p">,</span> <span class="n">ndata</span><span class="p">):</span> |
| 311 |
<span class="s2">"Handle an unparsed entity declaration event."</span> |
| 312 |
|
| 313 |
|
| 314 |
<span class="c1"># ===== ENTITYRESOLVER =====</span> |
| 315 |
|
| 316 |
<span class="k">class</span><span class="w"> </span><span class="nc">EntityResolver</span><span class="p">:</span> |
| 317 |
<span class="w"> </span><span class="sd">"""Basic interface for resolving entities. If you create an object</span> |
| 318 |
<span class="sd"> implementing this interface, then register the object with your</span> |
| 319 |
<span class="sd"> Parser, the parser will call the method in your object to</span> |
| 320 |
<span class="sd"> resolve all external entities. Note that DefaultHandler implements</span> |
| 321 |
<span class="sd"> this interface with the default behaviour."""</span> |
| 322 |
|
| 323 |
<span class="k">def</span><span class="w"> </span><span class="nf">resolveEntity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">publicId</span><span class="p">,</span> <span class="n">systemId</span><span class="p">):</span> |
| 324 |
<span class="w"> </span><span class="sd">"""Resolve the system identifier of an entity and return either</span> |
| 325 |
<span class="sd"> the system identifier to read from as a string, or an InputSource</span> |
| 326 |
<span class="sd"> to read from."""</span> |
| 327 |
<span class="k">return</span> <span class="n">systemId</span> |
| 328 |
|
| 329 |
|
| 330 |
<span class="c1">#============================================================================</span> |
| 331 |
<span class="c1">#</span> |
| 332 |
<span class="c1"># CORE FEATURES</span> |
| 333 |
<span class="c1">#</span> |
| 334 |
<span class="c1">#============================================================================</span> |
| 335 |
|
| 336 |
<span class="n">feature_namespaces</span> <span class="o">=</span> <span class="s2">"http://xml.org/sax/features/namespaces"</span> |
| 337 |
<span class="c1"># true: Perform Namespace processing (default).</span> |
| 338 |
<span class="c1"># false: Optionally do not perform Namespace processing</span> |
| 339 |
<span class="c1"># (implies namespace-prefixes).</span> |
| 340 |
<span class="c1"># access: (parsing) read-only; (not parsing) read/write</span> |
| 341 |
|
| 342 |
<span class="n">feature_namespace_prefixes</span> <span class="o">=</span> <span class="s2">"http://xml.org/sax/features/namespace-prefixes"</span> |
| 343 |
<span class="c1"># true: Report the original prefixed names and attributes used for Namespace</span> |
| 344 |
<span class="c1"># declarations.</span> |
| 345 |
<span class="c1"># false: Do not report attributes used for Namespace declarations, and</span> |
| 346 |
<span class="c1"># optionally do not report original prefixed names (default).</span> |
| 347 |
<span class="c1"># access: (parsing) read-only; (not parsing) read/write</span> |
| 348 |
|
| 349 |
<span class="n">feature_string_interning</span> <span class="o">=</span> <span class="s2">"http://xml.org/sax/features/string-interning"</span> |
| 350 |
<span class="c1"># true: All element names, prefixes, attribute names, Namespace URIs, and</span> |
| 351 |
<span class="c1"># local names are interned using the built-in intern function.</span> |
| 352 |
<span class="c1"># false: Names are not necessarily interned, although they may be (default).</span> |
| 353 |
<span class="c1"># access: (parsing) read-only; (not parsing) read/write</span> |
| 354 |
|
| 355 |
<span class="n">feature_validation</span> <span class="o">=</span> <span class="s2">"http://xml.org/sax/features/validation"</span> |
| 356 |
<span class="c1"># true: Report all validation errors (implies external-general-entities and</span> |
| 357 |
<span class="c1"># external-parameter-entities).</span> |
| 358 |
<span class="c1"># false: Do not report validation errors.</span> |
| 359 |
<span class="c1"># access: (parsing) read-only; (not parsing) read/write</span> |
| 360 |
|
| 361 |
<span class="n">feature_external_ges</span> <span class="o">=</span> <span class="s2">"http://xml.org/sax/features/external-general-entities"</span> |
| 362 |
<span class="c1"># true: Include all external general (text) entities.</span> |
| 363 |
<span class="c1"># false: Do not include external general entities.</span> |
| 364 |
<span class="c1"># access: (parsing) read-only; (not parsing) read/write</span> |
| 365 |
|
| 366 |
<span class="n">feature_external_pes</span> <span class="o">=</span> <span class="s2">"http://xml.org/sax/features/external-parameter-entities"</span> |
| 367 |
<span class="c1"># true: Include all external parameter entities, including the external</span> |
| 368 |
<span class="c1"># DTD subset.</span> |
| 369 |
<span class="c1"># false: Do not include any external parameter entities, even the external</span> |
| 370 |
<span class="c1"># DTD subset.</span> |
| 371 |
<span class="c1"># access: (parsing) read-only; (not parsing) read/write</span> |
| 372 |
|
| 373 |
<span class="n">all_features</span> <span class="o">=</span> <span class="p">[</span><span class="n">feature_namespaces</span><span class="p">,</span> |
| 374 |
<span class="n">feature_namespace_prefixes</span><span class="p">,</span> |
| 375 |
<span class="n">feature_string_interning</span><span class="p">,</span> |
| 376 |
<span class="n">feature_validation</span><span class="p">,</span> |
| 377 |
<span class="n">feature_external_ges</span><span class="p">,</span> |
| 378 |
<span class="n">feature_external_pes</span><span class="p">]</span> |
| 379 |
|
| 380 |
|
| 381 |
<span class="c1">#============================================================================</span> |
| 382 |
<span class="c1">#</span> |
| 383 |
<span class="c1"># CORE PROPERTIES</span> |
| 384 |
<span class="c1">#</span> |
| 385 |
<span class="c1">#============================================================================</span> |
| 386 |
|
| 387 |
<span class="n">property_lexical_handler</span> <span class="o">=</span> <span class="s2">"http://xml.org/sax/properties/lexical-handler"</span> |
| 388 |
<span class="c1"># data type: xml.sax.sax2lib.LexicalHandler</span> |
| 389 |
<span class="c1"># description: An optional extension handler for lexical events like comments.</span> |
| 390 |
<span class="c1"># access: read/write</span> |
| 391 |
|
| 392 |
<span class="n">property_declaration_handler</span> <span class="o">=</span> <span class="s2">"http://xml.org/sax/properties/declaration-handler"</span> |
| 393 |
<span class="c1"># data type: xml.sax.sax2lib.DeclHandler</span> |
| 394 |
<span class="c1"># description: An optional extension handler for DTD-related events other</span> |
| 395 |
<span class="c1"># than notations and unparsed entities.</span> |
| 396 |
<span class="c1"># access: read/write</span> |
| 397 |
|
| 398 |
<span class="n">property_dom_node</span> <span class="o">=</span> <span class="s2">"http://xml.org/sax/properties/dom-node"</span> |
| 399 |
<span class="c1"># data type: org.w3c.dom.Node</span> |
| 400 |
<span class="c1"># description: When parsing, the current DOM node being visited if this is</span> |
| 401 |
<span class="c1"># a DOM iterator; when not parsing, the root DOM node for</span> |
| 402 |
<span class="c1"># iteration.</span> |
| 403 |
<span class="c1"># access: (parsing) read-only; (not parsing) read/write</span> |
| 404 |
|
| 405 |
<span class="n">property_xml_string</span> <span class="o">=</span> <span class="s2">"http://xml.org/sax/properties/xml-string"</span> |
| 406 |
<span class="c1"># data type: String</span> |
| 407 |
<span class="c1"># description: The literal string of characters that was the source for</span> |
| 408 |
<span class="c1"># the current event.</span> |
| 409 |
<span class="c1"># access: read-only</span> |
| 410 |
|
| 411 |
<span class="n">property_encoding</span> <span class="o">=</span> <span class="s2">"http://www.python.org/sax/properties/encoding"</span> |
| 412 |
<span class="c1"># data type: String</span> |
| 413 |
<span class="c1"># description: The name of the encoding to assume for input data.</span> |
| 414 |
<span class="c1"># access: write: set the encoding, e.g. established by a higher-level</span> |
| 415 |
<span class="c1"># protocol. May change during parsing (e.g. after</span> |
| 416 |
<span class="c1"># processing a META tag)</span> |
| 417 |
<span class="c1"># read: return the current encoding (possibly established through</span> |
| 418 |
<span class="c1"># auto-detection.</span> |
| 419 |
<span class="c1"># initial value: UTF-8</span> |
| 420 |
<span class="c1">#</span> |
| 421 |
|
| 422 |
<span class="n">property_interning_dict</span> <span class="o">=</span> <span class="s2">"http://www.python.org/sax/properties/interning-dict"</span> |
| 423 |
<span class="c1"># data type: Dictionary</span> |
| 424 |
<span class="c1"># description: The dictionary used to intern common strings in the document</span> |
| 425 |
<span class="c1"># access: write: Request that the parser uses a specific dictionary, to</span> |
| 426 |
<span class="c1"># allow interning across different documents</span> |
| 427 |
<span class="c1"># read: return the current interning dictionary, or None</span> |
| 428 |
<span class="c1">#</span> |
| 429 |
|
| 430 |
<span class="n">all_properties</span> <span class="o">=</span> <span class="p">[</span><span class="n">property_lexical_handler</span><span class="p">,</span> |
| 431 |
<span class="n">property_dom_node</span><span class="p">,</span> |
| 432 |
<span class="n">property_declaration_handler</span><span class="p">,</span> |
| 433 |
<span class="n">property_xml_string</span><span class="p">,</span> |
| 434 |
<span class="n">property_encoding</span><span class="p">,</span> |
| 435 |
<span class="n">property_interning_dict</span><span class="p">]</span> |
| 436 |
|
| 437 |
|
| 438 |
<span class="k">class</span><span class="w"> </span><span class="nc">LexicalHandler</span><span class="p">:</span> |
| 439 |
<span class="w"> </span><span class="sd">"""Optional SAX2 handler for lexical events.</span> |
| 440 |
|
| 441 |
<span class="sd"> This handler is used to obtain lexical information about an XML</span> |
| 442 |
<span class="sd"> document, that is, information about how the document was encoded</span> |
| 443 |
<span class="sd"> (as opposed to what it contains, which is reported to the</span> |
| 444 |
<span class="sd"> ContentHandler), such as comments and CDATA marked section</span> |
| 445 |
<span class="sd"> boundaries.</span> |
| 446 |
|
| 447 |
<span class="sd"> To set the LexicalHandler of an XMLReader, use the setProperty</span> |
| 448 |
<span class="sd"> method with the property identifier</span> |
| 449 |
<span class="sd"> 'http://xml.org/sax/properties/lexical-handler'."""</span> |
| 450 |
|
| 451 |
<span class="k">def</span><span class="w"> </span><span class="nf">comment</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">content</span><span class="p">):</span> |
| 452 |
<span class="w"> </span><span class="sd">"""Reports a comment anywhere in the document (including the</span> |
| 453 |
<span class="sd"> DTD and outside the document element).</span> |
| 454 |
|
| 455 |
<span class="sd"> content is a string that holds the contents of the comment."""</span> |
| 456 |
|
| 457 |
<span class="k">def</span><span class="w"> </span><span class="nf">startDTD</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">public_id</span><span class="p">,</span> <span class="n">system_id</span><span class="p">):</span> |
| 458 |
<span class="w"> </span><span class="sd">"""Report the start of the DTD declarations, if the document</span> |
| 459 |
<span class="sd"> has an associated DTD.</span> |
| 460 |
|
| 461 |
<span class="sd"> A startEntity event will be reported before declaration events</span> |
| 462 |
<span class="sd"> from the external DTD subset are reported, and this can be</span> |
| 463 |
<span class="sd"> used to infer from which subset DTD declarations derive.</span> |
| 464 |
|
| 465 |
<span class="sd"> name is the name of the document element type, public_id the</span> |
| 466 |
<span class="sd"> public identifier of the DTD (or None if none were supplied)</span> |
| 467 |
<span class="sd"> and system_id the system identifier of the external subset (or</span> |
| 468 |
<span class="sd"> None if none were supplied)."""</span> |
| 469 |
|
| 470 |
<span class="k">def</span><span class="w"> </span><span class="nf">endDTD</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> |
| 471 |
<span class="w"> </span><span class="sd">"""Signals the end of DTD declarations."""</span> |
| 472 |
|
| 473 |
<span class="k">def</span><span class="w"> </span><span class="nf">startCDATA</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> |
| 474 |
<span class="w"> </span><span class="sd">"""Reports the beginning of a CDATA marked section.</span> |
| 475 |
|
| 476 |
<span class="sd"> The contents of the CDATA marked section will be reported</span> |
| 477 |
<span class="sd"> through the characters event."""</span> |
| 478 |
|
| 479 |
<span class="k">def</span><span class="w"> </span><span class="nf">endCDATA</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> |
| 480 |
<span class="w"> </span><span class="sd">"""Reports the end of a CDATA marked section."""</span> |
| 481 |
</pre></div> |
| 482 |
|
| 483 |
</div> |
| 484 |
</div> |
| 485 |
<footer> |
| 486 |
|
| 487 |
<hr/> |
| 488 |
|
| 489 |
<div role="contentinfo"> |
| 490 |
<p>© Copyright 2026, lxml dev team.</p> |
| 491 |
</div> |
| 492 |
|
| 493 |
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a |
| 494 |
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a> |
| 495 |
provided by <a href="https://readthedocs.org">Read the Docs</a>. |
| 496 |
|
| 497 |
|
| 498 |
</footer> |
| 499 |
</div> |
| 500 |
</div> |
| 501 |
</section> |
| 502 |
</div> |
| 503 |
<script> |
| 504 |
jQuery(function () { |
| 505 |
SphinxRtdTheme.Navigation.enable(true); |
| 506 |
}); |
| 507 |
</script> |
| 508 |
|
| 509 |
</body> |
| 510 |
</html> |