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