1 """An XML Reader is the SAX 2 name for an XML parser. XML Parsers
2 should be based on this code. """
6 from _exceptions
import SAXNotSupportedException
, SAXNotRecognizedException
9 # ===== XMLREADER =====
12 """Interface for reading an XML document using callbacks.
14 XMLReader is the interface that an XML parser's SAX2 driver must
15 implement. This interface allows an application to set and query
16 features and properties in the parser, to register event handlers
17 for document processing, and to initiate a document parse.
19 All SAX interfaces are assumed to be synchronous: the parse
20 methods must not return until parsing is complete, and readers
21 must wait for an event-handler callback to return before reporting
25 self
._cont
_handler
= handler
.ContentHandler()
26 self
._dtd
_handler
= handler
.DTDHandler()
27 self
._ent
_handler
= handler
.EntityResolver()
28 self
._err
_handler
= handler
.ErrorHandler()
30 def parse(self
, source
):
31 "Parse an XML document from a system identifier or an InputSource."
32 raise NotImplementedError("This method must be implemented!")
34 def getContentHandler(self
):
35 "Returns the current ContentHandler."
36 return self
._cont
_handler
38 def setContentHandler(self
, handler
):
39 "Registers a new object to receive document content events."
40 self
._cont
_handler
= handler
42 def getDTDHandler(self
):
43 "Returns the current DTD handler."
44 return self
._dtd
_handler
46 def setDTDHandler(self
, handler
):
47 "Register an object to receive basic DTD-related events."
48 self
._dtd
_handler
= handler
50 def getEntityResolver(self
):
51 "Returns the current EntityResolver."
52 return self
._ent
_handler
54 def setEntityResolver(self
, resolver
):
55 "Register an object to resolve external entities."
56 self
._ent
_handler
= resolver
58 def getErrorHandler(self
):
59 "Returns the current ErrorHandler."
60 return self
._err
_handler
62 def setErrorHandler(self
, handler
):
63 "Register an object to receive error-message events."
64 self
._err
_handler
= handler
66 def setLocale(self
, locale
):
67 """Allow an application to set the locale for errors and warnings.
69 SAX parsers are not required to provide localization for errors
70 and warnings; if they cannot support the requested locale,
71 however, they must throw a SAX exception. Applications may
72 request a locale change in the middle of a parse."""
73 raise SAXNotSupportedException("Locale support not implemented")
75 def getFeature(self
, name
):
76 "Looks up and returns the state of a SAX2 feature."
77 raise SAXNotRecognizedException("Feature '%s' not recognized" % name
)
79 def setFeature(self
, name
, state
):
80 "Sets the state of a SAX2 feature."
81 raise SAXNotRecognizedException("Feature '%s' not recognized" % name
)
83 def getProperty(self
, name
):
84 "Looks up and returns the value of a SAX2 property."
85 raise SAXNotRecognizedException("Property '%s' not recognized" % name
)
87 def setProperty(self
, name
, value
):
88 "Sets the value of a SAX2 property."
89 raise SAXNotRecognizedException("Property '%s' not recognized" % name
)
91 class IncrementalParser(XMLReader
):
92 """This interface adds three extra methods to the XMLReader
93 interface that allow XML parsers to support incremental
94 parsing. Support for this interface is optional, since not all
95 underlying XML parsers support this functionality.
97 When the parser is instantiated it is ready to begin accepting
98 data from the feed method immediately. After parsing has been
99 finished with a call to close the reset method must be called to
100 make the parser ready to accept new data, either from feed or
101 using the parse method.
103 Note that these methods must _not_ be called during parsing, that
104 is, after parse has been called and before it returns.
106 By default, the class also implements the parse method of the XMLReader
107 interface using the feed, close and reset methods of the
108 IncrementalParser interface as a convenience to SAX 2.0 driver
111 def __init__(self
, bufsize
=2**16):
112 self
._bufsize
= bufsize
113 XMLReader
.__init
__(self
)
115 def parse(self
, source
):
117 source
= saxutils
.prepare_input_source(source
)
119 self
.prepareParser(source
)
120 file = source
.getByteStream()
121 buffer = file.read(self
._bufsize
)
124 buffer = file.read(self
._bufsize
)
127 def feed(self
, data
):
128 """This method gives the raw XML data in the data parameter to
129 the parser and makes it parse the data, emitting the
130 corresponding events. It is allowed for XML constructs to be
131 split across several calls to feed.
133 feed may raise SAXException."""
134 raise NotImplementedError("This method must be implemented!")
136 def prepareParser(self
, source
):
137 """This method is called by the parse implementation to allow
138 the SAX 2.0 driver to prepare itself for parsing."""
139 raise NotImplementedError("prepareParser must be overridden!")
142 """This method is called when the entire XML document has been
143 passed to the parser through the feed method, to notify the
144 parser that there are no more data. This allows the parser to
145 do the final checks on the document and empty the internal
148 The parser will not be ready to parse another document until
149 the reset method has been called.
151 close may raise SAXException."""
152 raise NotImplementedError("This method must be implemented!")
155 """This method is called after close has been called to reset
156 the parser so that it is ready to parse new documents. The
157 results of calling parse or feed after close without calling
158 reset are undefined."""
159 raise NotImplementedError("This method must be implemented!")
161 # ===== LOCATOR =====
164 """Interface for associating a SAX event with a document
165 location. A locator object will return valid results only during
166 calls to DocumentHandler methods; at any other time, the
167 results are unpredictable."""
169 def getColumnNumber(self
):
170 "Return the column number where the current event ends."
173 def getLineNumber(self
):
174 "Return the line number where the current event ends."
177 def getPublicId(self
):
178 "Return the public identifier for the current event."
181 def getSystemId(self
):
182 "Return the system identifier for the current event."
185 # ===== INPUTSOURCE =====
188 """Encapsulation of the information needed by the XMLReader to
191 This class may include information about the public identifier,
192 system identifier, byte stream (possibly with character encoding
193 information) and/or the character stream of an entity.
195 Applications will create objects of this class for use in the
196 XMLReader.parse method and for returning from
197 EntityResolver.resolveEntity.
199 An InputSource belongs to the application, the XMLReader is not
200 allowed to modify InputSource objects passed to it from the
201 application, although it may make copies and modify those."""
203 def __init__(self
, system_id
= None):
204 self
.__system
_id
= system_id
205 self
.__public
_id
= None
206 self
.__encoding
= None
207 self
.__bytefile
= None
208 self
.__charfile
= None
210 def setPublicId(self
, public_id
):
211 "Sets the public identifier of this InputSource."
212 self
.__public
_id
= public_id
214 def getPublicId(self
):
215 "Returns the public identifier of this InputSource."
216 return self
.__public
_id
218 def setSystemId(self
, system_id
):
219 "Sets the system identifier of this InputSource."
220 self
.__system
_id
= system_id
222 def getSystemId(self
):
223 "Returns the system identifier of this InputSource."
224 return self
.__system
_id
226 def setEncoding(self
, encoding
):
227 """Sets the character encoding of this InputSource.
229 The encoding must be a string acceptable for an XML encoding
230 declaration (see section 4.3.3 of the XML recommendation).
232 The encoding attribute of the InputSource is ignored if the
233 InputSource also contains a character stream."""
234 self
.__encoding
= encoding
236 def getEncoding(self
):
237 "Get the character encoding of this InputSource."
238 return self
.__encoding
240 def setByteStream(self
, bytefile
):
241 """Set the byte stream (a Python file-like object which does
242 not perform byte-to-character conversion) for this input
245 The SAX parser will ignore this if there is also a character
246 stream specified, but it will use a byte stream in preference
247 to opening a URI connection itself.
249 If the application knows the character encoding of the byte
250 stream, it should set it with the setEncoding method."""
251 self
.__bytefile
= bytefile
253 def getByteStream(self
):
254 """Get the byte stream for this input source.
256 The getEncoding method will return the character encoding for
257 this byte stream, or None if unknown."""
258 return self
.__bytefile
260 def setCharacterStream(self
, charfile
):
261 """Set the character stream for this input source. (The stream
262 must be a Python 2.0 Unicode-wrapped file-like that performs
263 conversion to Unicode strings.)
265 If there is a character stream specified, the SAX parser will
266 ignore any byte stream and will not attempt to open a URI
267 connection to the system identifier."""
268 self
.__charfile
= charfile
270 def getCharacterStream(self
):
271 "Get the character stream for this input source."
272 return self
.__charfile
274 # ===== ATTRIBUTESIMPL =====
276 class AttributesImpl
:
278 def __init__(self
, attrs
):
279 """Non-NS-aware implementation.
281 attrs should be of the form {name : value}."""
285 return len(self
._attrs
)
287 def getType(self
, name
):
290 def getValue(self
, name
):
291 return self
._attrs
[name
]
293 def getValueByQName(self
, name
):
294 return self
._attrs
[name
]
296 def getNameByQName(self
, name
):
297 if not self
._attrs
.has_key(name
):
301 def getQNameByName(self
, name
):
302 if not self
._attrs
.has_key(name
):
307 return self
._attrs
.keys()
310 return self
._attrs
.keys()
313 return len(self
._attrs
)
315 def __getitem__(self
, name
):
316 return self
._attrs
[name
]
319 return self
._attrs
.keys()
321 def has_key(self
, name
):
322 return self
._attrs
.has_key(name
)
324 def get(self
, name
, alternative
=None):
325 return self
._attrs
.get(name
, alternative
)
328 return self
.__class
__(self
._attrs
)
331 return self
._attrs
.items()
334 return self
._attrs
.values()
336 # ===== ATTRIBUTESNSIMPL =====
338 class AttributesNSImpl(AttributesImpl
):
340 def __init__(self
, attrs
, qnames
):
341 """NS-aware implementation.
343 attrs should be of the form {(ns_uri, lname): value, ...}.
344 qnames of the form {(ns_uri, lname): qname, ...}."""
346 self
._qnames
= qnames
348 def getValueByQName(self
, name
):
349 for (nsname
, qname
) in self
._qnames
.items():
351 return self
._attrs
[nsname
]
355 def getNameByQName(self
, name
):
356 for (nsname
, qname
) in self
._qnames
.items():
362 def getQNameByName(self
, name
):
363 return self
._qnames
[name
]
366 return self
._qnames
.values()
369 return self
.__class
__(self
._attrs
, self
._qnames
)
377 if __name__
== "__main__":