blob: 27b19f9198f594d794fdc6a16b3be586ead50992 [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'
13#============================================================================
14#
15# HANDLER INTERFACES
16#
17#============================================================================
18# ===== ErrorHandler =====
19class ErrorHandler:
20 """Basic interface for SAX error handlers. If you create an object
21 that implements this interface, then register the object with your
22 Parser, the parser will call the methods in your object to report
23 all warnings and errors. There are three levels of errors
24 available: warnings, (possibly) recoverable errors, and
25 unrecoverable errors. All methods take a SAXParseException as the
26 only parameter."""
27
28 def error(self, exception):
Skip Montanaroa2dccfb2000-07-06 02:55:41 +000029 "Handle a recoverable error."
Fred Drake45cd9de2000-06-29 19:34:54 +000030 raise exception
31
32 def fatalError(self, exception):
Skip Montanaroa2dccfb2000-07-06 02:55:41 +000033 "Handle a non-recoverable error."
Fred Drake45cd9de2000-06-29 19:34:54 +000034 raise exception
35
36 def warning(self, exception):
Skip Montanaroa2dccfb2000-07-06 02:55:41 +000037 "Handle a warning."
Fred Drake45cd9de2000-06-29 19:34:54 +000038 print exception
39
40# ===== CONTENTHANDLER =====
41
42class ContentHandler:
43 """Interface for receiving logical document content events.
44
45 This is the main callback interface in SAX, and the one most
46 important to applications. The order of events in this interface
47 mirrors the order of the information in the document."""
48
49 def __init__(self):
50 self._locator = None
51
52 def setDocumentLocator(self, locator):
53 """Called by the parser to give the application a locator for
54 locating the origin of document events.
55
56 SAX parsers are strongly encouraged (though not absolutely
57 required) to supply a locator: if it does so, it must supply
58 the locator to the application by invoking this method before
59 invoking any of the other methods in the DocumentHandler
60 interface.
61
62 The locator allows the application to determine the end
63 position of any document-related event, even if the parser is
64 not reporting an error. Typically, the application will use
65 this information for reporting its own errors (such as
66 character content that does not match an application's
67 business rules). The information returned by the locator is
68 probably not sufficient for use with a search engine.
69
70 Note that the locator will return correct information only
71 during the invocation of the events in this interface. The
72 application should not attempt to use it at any other time."""
73 self._locator = locator
74
75 def startDocument(self):
76 """Receive notification of the beginning of a document.
77
78 The SAX parser will invoke this method only once, before any
79 other methods in this interface or in DTDHandler (except for
80 setDocumentLocator)."""
81
82 def endDocument(self):
83 """Receive notification of the end of a document.
84
85 The SAX parser will invoke this method only once, and it will
86 be the last method invoked during the parse. The parser shall
87 not invoke this method until it has either abandoned parsing
88 (because of an unrecoverable error) or reached the end of
89 input."""
90
91 def startPrefixMapping(self, prefix, uri):
92 """Begin the scope of a prefix-URI Namespace mapping.
93
94 The information from this event is not necessary for normal
95 Namespace processing: the SAX XML reader will automatically
96 replace prefixes for element and attribute names when the
97 http://xml.org/sax/features/namespaces feature is true (the
98 default).
99
100 There are cases, however, when applications need to use
101 prefixes in character data or in attribute values, where they
102 cannot safely be expanded automatically; the
103 start/endPrefixMapping event supplies the information to the
104 application to expand prefixes in those contexts itself, if
105 necessary.
106
107 Note that start/endPrefixMapping events are not guaranteed to
108 be properly nested relative to each-other: all
109 startPrefixMapping events will occur before the corresponding
110 startElement event, and all endPrefixMapping events will occur
111 after the corresponding endElement event, but their order is
112 not guaranteed."""
113
114 def endPrefixMapping(self, prefix):
115 """End the scope of a prefix-URI mapping.
116
117 See startPrefixMapping for details. This event will always
118 occur after the corresponding endElement event, but the order
119 of endPrefixMapping events is not otherwise guaranteed."""
120
121 def startElement(self, name, attrs):
122 """Signals the start of an element.
123
124 The name parameter contains the name of the element type as a
125 (uri ,localname) tuple, the qname parameter the raw XML 1.0
126 name used in the source document, and the attrs parameter
127 holds an instance of the Attributes class containing the
128 attributes of the element."""
129
130 def endElement(self, name ):
131 """Signals the end of an element.
132
133 The name parameter contains the name of the element type, just
134 as with the startElement event."""
135
136 def characters(self, content):
137 """Receive notification of character data.
138
139 The Parser will call this method to report each chunk of
140 character data. SAX parsers may return all contiguous
141 character data in a single chunk, or they may split it into
142 several chunks; however, all of the characters in any single
143 event must come from the same external entity so that the
144 Locator provides useful information."""
145
146 def ignorableWhitespace(self ):
147 """Receive notification of ignorable whitespace in element content.
148
149 Validating Parsers must use this method to report each chunk
150 of ignorable whitespace (see the W3C XML 1.0 recommendation,
151 section 2.10): non-validating parsers may also use this method
152 if they are capable of parsing and using content models.
153
154 SAX parsers may return all contiguous whitespace in a single
155 chunk, or they may split it into several chunks; however, all
156 of the characters in any single event must come from the same
157 external entity, so that the Locator provides useful
158 information.
159
160 The application must not attempt to read from the array
161 outside of the specified range."""
162
163 def processingInstruction(self, target, data):
164 """Receive notification of a processing instruction.
165
166 The Parser will invoke this method once for each processing
167 instruction found: note that processing instructions may occur
168 before or after the main document element.
169
170 A SAX parser should never report an XML declaration (XML 1.0,
171 section 2.8) or a text declaration (XML 1.0, section 4.3.1)
172 using this method."""
173
174 def skippedEntity(self, name):
175 """Receive notification of a skipped entity.
176
177 The Parser will invoke this method once for each entity
178 skipped. Non-validating processors may skip entities if they
179 have not seen the declarations (because, for example, the
180 entity was declared in an external DTD subset). All processors
181 may skip external entities, depending on the values of the
182 http://xml.org/sax/features/external-general-entities and the
183 http://xml.org/sax/features/external-parameter-entities
184 properties."""
185
186#============================================================================
187#
188# CORE FEATURES
189#
190#============================================================================
191
192feature_namespaces = "http://xml.org/sax/features/namespaces"
193# true: Perform Namespace processing (default).
194# false: Optionally do not perform Namespace processing
195# (implies namespace-prefixes).
196# access: (parsing) read-only; (not parsing) read/write
197
198feature_namespace_prefixes = "http://xml.org/sax/features/namespace-prefixes"
199# true: Report the original prefixed names and attributes used for Namespace
200# declarations.
201# false: Do not report attributes used for Namespace declarations, and
202# optionally do not report original prefixed names (default).
203# access: (parsing) read-only; (not parsing) read/write
204
205feature_string_interning = "http://xml.org/sax/features/string-interning"
206# true: All element names, prefixes, attribute names, Namespace URIs, and
207# local names are interned using the built-in intern function.
208# false: Names are not necessarily interned, although they may be (default).
209# access: (parsing) read-only; (not parsing) read/write
210
211feature_validation = "http://xml.org/sax/features/validation"
212# true: Report all validation errors (implies external-general-entities and
213# external-parameter-entities).
214# false: Do not report validation errors.
215# access: (parsing) read-only; (not parsing) read/write
216
217feature_external_ges = "http://xml.org/sax/features/external-general-entities"
218# true: Include all external general (text) entities.
219# false: Do not include external general entities.
220# access: (parsing) read-only; (not parsing) read/write
221
222feature_external_pes = "http://xml.org/sax/features/external-parameter-entities"
223# true: Include all external parameter entities, including the external
224# DTD subset.
225# false: Do not include any external parameter entities, even the external
226# DTD subset.
227# access: (parsing) read-only; (not parsing) read/write
228
229all_features = [feature_namespaces,
230 feature_namespace_prefixes,
231 feature_string_interning,
232 feature_validation,
233 feature_external_ges,
234 feature_external_pes]
235
236
237#============================================================================
238#
239# CORE PROPERTIES
240#
241#============================================================================
242
243property_lexical_handler = "http://xml.org/sax/properties/lexical-handler"
244# data type: xml.sax.sax2lib.LexicalHandler
245# description: An optional extension handler for lexical events like comments.
246# access: read/write
247
248property_declaration_handler = "http://xml.org/sax/properties/declaration-handler"
249# data type: xml.sax.sax2lib.DeclHandler
250# description: An optional extension handler for DTD-related events other
251# than notations and unparsed entities.
252# access: read/write
253
254property_dom_node = "http://xml.org/sax/properties/dom-node"
255# data type: org.w3c.dom.Node
256# description: When parsing, the current DOM node being visited if this is
257# a DOM iterator; when not parsing, the root DOM node for
258# iteration.
259# access: (parsing) read-only; (not parsing) read/write
260
261property_xml_string = "http://xml.org/sax/properties/xml-string"
262# data type: String
263# description: The literal string of characters that was the source for
264# the current event.
265# access: read-only
266
267all_properties = [property_lexical_handler,
268 property_dom_node,
269 property_declaration_handler,
270 property_xml_string]