ยปCore Development>Code coverage>Lib/xml/sax/handler.py

Python code coverage for Lib/xml/sax/handler.py

#countcontent
1n/a"""
2n/aThis module contains the core classes of version 2.0 of SAX for Python.
3n/aThis file provides only default classes with absolutely minimum
4n/afunctionality, from which drivers and applications can be subclassed.
5n/a
6n/aMany of these classes are empty and are included only as documentation
7n/aof the interfaces.
8n/a
9n/a$Id$
10n/a"""
11n/a
12n/aversion = '2.0beta'
13n/a
14n/a#============================================================================
15n/a#
16n/a# HANDLER INTERFACES
17n/a#
18n/a#============================================================================
19n/a
20n/a# ===== ERRORHANDLER =====
21n/a
22n/aclass ErrorHandler:
23n/a """Basic interface for SAX error handlers.
24n/a
25n/a If you create an object that implements this interface, then
26n/a register the object with your XMLReader, the parser will call the
27n/a methods in your object to report all warnings and errors. There
28n/a are three levels of errors available: warnings, (possibly)
29n/a recoverable errors, and unrecoverable errors. All methods take a
30n/a SAXParseException as the only parameter."""
31n/a
32n/a def error(self, exception):
33n/a "Handle a recoverable error."
34n/a raise exception
35n/a
36n/a def fatalError(self, exception):
37n/a "Handle a non-recoverable error."
38n/a raise exception
39n/a
40n/a def warning(self, exception):
41n/a "Handle a warning."
42n/a print(exception)
43n/a
44n/a
45n/a# ===== CONTENTHANDLER =====
46n/a
47n/aclass ContentHandler:
48n/a """Interface for receiving logical document content events.
49n/a
50n/a This is the main callback interface in SAX, and the one most
51n/a important to applications. The order of events in this interface
52n/a mirrors the order of the information in the document."""
53n/a
54n/a def __init__(self):
55n/a self._locator = None
56n/a
57n/a def setDocumentLocator(self, locator):
58n/a """Called by the parser to give the application a locator for
59n/a locating the origin of document events.
60n/a
61n/a SAX parsers are strongly encouraged (though not absolutely
62n/a required) to supply a locator: if it does so, it must supply
63n/a the locator to the application by invoking this method before
64n/a invoking any of the other methods in the DocumentHandler
65n/a interface.
66n/a
67n/a The locator allows the application to determine the end
68n/a position of any document-related event, even if the parser is
69n/a not reporting an error. Typically, the application will use
70n/a this information for reporting its own errors (such as
71n/a character content that does not match an application's
72n/a business rules). The information returned by the locator is
73n/a probably not sufficient for use with a search engine.
74n/a
75n/a Note that the locator will return correct information only
76n/a during the invocation of the events in this interface. The
77n/a application should not attempt to use it at any other time."""
78n/a self._locator = locator
79n/a
80n/a def startDocument(self):
81n/a """Receive notification of the beginning of a document.
82n/a
83n/a The SAX parser will invoke this method only once, before any
84n/a other methods in this interface or in DTDHandler (except for
85n/a setDocumentLocator)."""
86n/a
87n/a def endDocument(self):
88n/a """Receive notification of the end of a document.
89n/a
90n/a The SAX parser will invoke this method only once, and it will
91n/a be the last method invoked during the parse. The parser shall
92n/a not invoke this method until it has either abandoned parsing
93n/a (because of an unrecoverable error) or reached the end of
94n/a input."""
95n/a
96n/a def startPrefixMapping(self, prefix, uri):
97n/a """Begin the scope of a prefix-URI Namespace mapping.
98n/a
99n/a The information from this event is not necessary for normal
100n/a Namespace processing: the SAX XML reader will automatically
101n/a replace prefixes for element and attribute names when the
102n/a http://xml.org/sax/features/namespaces feature is true (the
103n/a default).
104n/a
105n/a There are cases, however, when applications need to use
106n/a prefixes in character data or in attribute values, where they
107n/a cannot safely be expanded automatically; the
108n/a start/endPrefixMapping event supplies the information to the
109n/a application to expand prefixes in those contexts itself, if
110n/a necessary.
111n/a
112n/a Note that start/endPrefixMapping events are not guaranteed to
113n/a be properly nested relative to each-other: all
114n/a startPrefixMapping events will occur before the corresponding
115n/a startElement event, and all endPrefixMapping events will occur
116n/a after the corresponding endElement event, but their order is
117n/a not guaranteed."""
118n/a
119n/a def endPrefixMapping(self, prefix):
120n/a """End the scope of a prefix-URI mapping.
121n/a
122n/a See startPrefixMapping for details. This event will always
123n/a occur after the corresponding endElement event, but the order
124n/a of endPrefixMapping events is not otherwise guaranteed."""
125n/a
126n/a def startElement(self, name, attrs):
127n/a """Signals the start of an element in non-namespace mode.
128n/a
129n/a The name parameter contains the raw XML 1.0 name of the
130n/a element type as a string and the attrs parameter holds an
131n/a instance of the Attributes class containing the attributes of
132n/a the element."""
133n/a
134n/a def endElement(self, name):
135n/a """Signals the end of an element in non-namespace mode.
136n/a
137n/a The name parameter contains the name of the element type, just
138n/a as with the startElement event."""
139n/a
140n/a def startElementNS(self, name, qname, attrs):
141n/a """Signals the start of an element in namespace mode.
142n/a
143n/a The name parameter contains the name of the element type as a
144n/a (uri, localname) tuple, the qname parameter the raw XML 1.0
145n/a name used in the source document, and the attrs parameter
146n/a holds an instance of the Attributes class containing the
147n/a attributes of the element.
148n/a
149n/a The uri part of the name tuple is None for elements which have
150n/a no namespace."""
151n/a
152n/a def endElementNS(self, name, qname):
153n/a """Signals the end of an element in namespace mode.
154n/a
155n/a The name parameter contains the name of the element type, just
156n/a as with the startElementNS event."""
157n/a
158n/a def characters(self, content):
159n/a """Receive notification of character data.
160n/a
161n/a The Parser will call this method to report each chunk of
162n/a character data. SAX parsers may return all contiguous
163n/a character data in a single chunk, or they may split it into
164n/a several chunks; however, all of the characters in any single
165n/a event must come from the same external entity so that the
166n/a Locator provides useful information."""
167n/a
168n/a def ignorableWhitespace(self, whitespace):
169n/a """Receive notification of ignorable whitespace in element content.
170n/a
171n/a Validating Parsers must use this method to report each chunk
172n/a of ignorable whitespace (see the W3C XML 1.0 recommendation,
173n/a section 2.10): non-validating parsers may also use this method
174n/a if they are capable of parsing and using content models.
175n/a
176n/a SAX parsers may return all contiguous whitespace in a single
177n/a chunk, or they may split it into several chunks; however, all
178n/a of the characters in any single event must come from the same
179n/a external entity, so that the Locator provides useful
180n/a information."""
181n/a
182n/a def processingInstruction(self, target, data):
183n/a """Receive notification of a processing instruction.
184n/a
185n/a The Parser will invoke this method once for each processing
186n/a instruction found: note that processing instructions may occur
187n/a before or after the main document element.
188n/a
189n/a A SAX parser should never report an XML declaration (XML 1.0,
190n/a section 2.8) or a text declaration (XML 1.0, section 4.3.1)
191n/a using this method."""
192n/a
193n/a def skippedEntity(self, name):
194n/a """Receive notification of a skipped entity.
195n/a
196n/a The Parser will invoke this method once for each entity
197n/a skipped. Non-validating processors may skip entities if they
198n/a have not seen the declarations (because, for example, the
199n/a entity was declared in an external DTD subset). All processors
200n/a may skip external entities, depending on the values of the
201n/a http://xml.org/sax/features/external-general-entities and the
202n/a http://xml.org/sax/features/external-parameter-entities
203n/a properties."""
204n/a
205n/a
206n/a# ===== DTDHandler =====
207n/a
208n/aclass DTDHandler:
209n/a """Handle DTD events.
210n/a
211n/a This interface specifies only those DTD events required for basic
212n/a parsing (unparsed entities and attributes)."""
213n/a
214n/a def notationDecl(self, name, publicId, systemId):
215n/a "Handle a notation declaration event."
216n/a
217n/a def unparsedEntityDecl(self, name, publicId, systemId, ndata):
218n/a "Handle an unparsed entity declaration event."
219n/a
220n/a
221n/a# ===== ENTITYRESOLVER =====
222n/a
223n/aclass EntityResolver:
224n/a """Basic interface for resolving entities. If you create an object
225n/a implementing this interface, then register the object with your
226n/a Parser, the parser will call the method in your object to
227n/a resolve all external entities. Note that DefaultHandler implements
228n/a this interface with the default behaviour."""
229n/a
230n/a def resolveEntity(self, publicId, systemId):
231n/a """Resolve the system identifier of an entity and return either
232n/a the system identifier to read from as a string, or an InputSource
233n/a to read from."""
234n/a return systemId
235n/a
236n/a
237n/a#============================================================================
238n/a#
239n/a# CORE FEATURES
240n/a#
241n/a#============================================================================
242n/a
243n/afeature_namespaces = "http://xml.org/sax/features/namespaces"
244n/a# true: Perform Namespace processing (default).
245n/a# false: Optionally do not perform Namespace processing
246n/a# (implies namespace-prefixes).
247n/a# access: (parsing) read-only; (not parsing) read/write
248n/a
249n/afeature_namespace_prefixes = "http://xml.org/sax/features/namespace-prefixes"
250n/a# true: Report the original prefixed names and attributes used for Namespace
251n/a# declarations.
252n/a# false: Do not report attributes used for Namespace declarations, and
253n/a# optionally do not report original prefixed names (default).
254n/a# access: (parsing) read-only; (not parsing) read/write
255n/a
256n/afeature_string_interning = "http://xml.org/sax/features/string-interning"
257n/a# true: All element names, prefixes, attribute names, Namespace URIs, and
258n/a# local names are interned using the built-in intern function.
259n/a# false: Names are not necessarily interned, although they may be (default).
260n/a# access: (parsing) read-only; (not parsing) read/write
261n/a
262n/afeature_validation = "http://xml.org/sax/features/validation"
263n/a# true: Report all validation errors (implies external-general-entities and
264n/a# external-parameter-entities).
265n/a# false: Do not report validation errors.
266n/a# access: (parsing) read-only; (not parsing) read/write
267n/a
268n/afeature_external_ges = "http://xml.org/sax/features/external-general-entities"
269n/a# true: Include all external general (text) entities.
270n/a# false: Do not include external general entities.
271n/a# access: (parsing) read-only; (not parsing) read/write
272n/a
273n/afeature_external_pes = "http://xml.org/sax/features/external-parameter-entities"
274n/a# true: Include all external parameter entities, including the external
275n/a# DTD subset.
276n/a# false: Do not include any external parameter entities, even the external
277n/a# DTD subset.
278n/a# access: (parsing) read-only; (not parsing) read/write
279n/a
280n/aall_features = [feature_namespaces,
281n/a feature_namespace_prefixes,
282n/a feature_string_interning,
283n/a feature_validation,
284n/a feature_external_ges,
285n/a feature_external_pes]
286n/a
287n/a
288n/a#============================================================================
289n/a#
290n/a# CORE PROPERTIES
291n/a#
292n/a#============================================================================
293n/a
294n/aproperty_lexical_handler = "http://xml.org/sax/properties/lexical-handler"
295n/a# data type: xml.sax.sax2lib.LexicalHandler
296n/a# description: An optional extension handler for lexical events like comments.
297n/a# access: read/write
298n/a
299n/aproperty_declaration_handler = "http://xml.org/sax/properties/declaration-handler"
300n/a# data type: xml.sax.sax2lib.DeclHandler
301n/a# description: An optional extension handler for DTD-related events other
302n/a# than notations and unparsed entities.
303n/a# access: read/write
304n/a
305n/aproperty_dom_node = "http://xml.org/sax/properties/dom-node"
306n/a# data type: org.w3c.dom.Node
307n/a# description: When parsing, the current DOM node being visited if this is
308n/a# a DOM iterator; when not parsing, the root DOM node for
309n/a# iteration.
310n/a# access: (parsing) read-only; (not parsing) read/write
311n/a
312n/aproperty_xml_string = "http://xml.org/sax/properties/xml-string"
313n/a# data type: String
314n/a# description: The literal string of characters that was the source for
315n/a# the current event.
316n/a# access: read-only
317n/a
318n/aproperty_encoding = "http://www.python.org/sax/properties/encoding"
319n/a# data type: String
320n/a# description: The name of the encoding to assume for input data.
321n/a# access: write: set the encoding, e.g. established by a higher-level
322n/a# protocol. May change during parsing (e.g. after
323n/a# processing a META tag)
324n/a# read: return the current encoding (possibly established through
325n/a# auto-detection.
326n/a# initial value: UTF-8
327n/a#
328n/a
329n/aproperty_interning_dict = "http://www.python.org/sax/properties/interning-dict"
330n/a# data type: Dictionary
331n/a# description: The dictionary used to intern common strings in the document
332n/a# access: write: Request that the parser uses a specific dictionary, to
333n/a# allow interning across different documents
334n/a# read: return the current interning dictionary, or None
335n/a#
336n/a
337n/aall_properties = [property_lexical_handler,
338n/a property_dom_node,
339n/a property_declaration_handler,
340n/a property_xml_string,
341n/a property_encoding,
342n/a property_interning_dict]