2000-06-29 16:34:54 -03:00
|
|
|
"""An XML Reader is the SAX 2 name for an XML parser. XML Parsers
|
|
|
|
should be based on this code. """
|
2000-09-21 14:43:48 -03:00
|
|
|
|
2006-08-17 06:10:09 -03:00
|
|
|
from . import handler
|
2001-06-07 02:52:17 -03:00
|
|
|
|
2006-08-17 06:10:09 -03:00
|
|
|
from ._exceptions import SAXNotSupportedException, SAXNotRecognizedException
|
2001-06-07 02:52:17 -03:00
|
|
|
|
2001-03-14 18:43:47 -04:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
# ===== XMLREADER =====
|
|
|
|
|
|
|
|
class XMLReader:
|
2000-10-23 15:09:50 -03:00
|
|
|
"""Interface for reading an XML document using callbacks.
|
2000-09-24 17:38:18 -03:00
|
|
|
|
|
|
|
XMLReader is the interface that an XML parser's SAX2 driver must
|
|
|
|
implement. This interface allows an application to set and query
|
|
|
|
features and properties in the parser, to register event handlers
|
|
|
|
for document processing, and to initiate a document parse.
|
|
|
|
|
|
|
|
All SAX interfaces are assumed to be synchronous: the parse
|
|
|
|
methods must not return until parsing is complete, and readers
|
|
|
|
must wait for an event-handler callback to return before reporting
|
|
|
|
the next event."""
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
def __init__(self):
|
2000-07-06 00:01:40 -03:00
|
|
|
self._cont_handler = handler.ContentHandler()
|
2000-09-24 17:19:45 -03:00
|
|
|
self._dtd_handler = handler.DTDHandler()
|
|
|
|
self._ent_handler = handler.EntityResolver()
|
2000-09-21 14:43:48 -03:00
|
|
|
self._err_handler = handler.ErrorHandler()
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def parse(self, source):
|
2000-07-06 00:01:40 -03:00
|
|
|
"Parse an XML document from a system identifier or an InputSource."
|
2000-06-29 16:34:54 -03:00
|
|
|
raise NotImplementedError("This method must be implemented!")
|
|
|
|
|
|
|
|
def getContentHandler(self):
|
|
|
|
"Returns the current ContentHandler."
|
|
|
|
return self._cont_handler
|
|
|
|
|
|
|
|
def setContentHandler(self, handler):
|
|
|
|
"Registers a new object to receive document content events."
|
|
|
|
self._cont_handler = handler
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
def getDTDHandler(self):
|
|
|
|
"Returns the current DTD handler."
|
|
|
|
return self._dtd_handler
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
def setDTDHandler(self, handler):
|
2000-07-06 00:01:40 -03:00
|
|
|
"Register an object to receive basic DTD-related events."
|
|
|
|
self._dtd_handler = handler
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def getEntityResolver(self):
|
|
|
|
"Returns the current EntityResolver."
|
|
|
|
return self._ent_handler
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
def setEntityResolver(self, resolver):
|
2000-07-06 00:01:40 -03:00
|
|
|
"Register an object to resolve external entities."
|
|
|
|
self._ent_handler = resolver
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def getErrorHandler(self):
|
|
|
|
"Returns the current ErrorHandler."
|
|
|
|
return self._err_handler
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
def setErrorHandler(self, handler):
|
2000-07-06 00:01:40 -03:00
|
|
|
"Register an object to receive error-message events."
|
|
|
|
self._err_handler = handler
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def setLocale(self, locale):
|
2000-10-23 15:09:50 -03:00
|
|
|
"""Allow an application to set the locale for errors and warnings.
|
|
|
|
|
2000-07-16 09:04:32 -03:00
|
|
|
SAX parsers are not required to provide localization for errors
|
2000-06-29 16:34:54 -03:00
|
|
|
and warnings; if they cannot support the requested locale,
|
2012-12-18 15:14:22 -04:00
|
|
|
however, they must raise a SAX exception. Applications may
|
2000-06-29 16:34:54 -03:00
|
|
|
request a locale change in the middle of a parse."""
|
|
|
|
raise SAXNotSupportedException("Locale support not implemented")
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
def getFeature(self, name):
|
|
|
|
"Looks up and returns the state of a SAX2 feature."
|
|
|
|
raise SAXNotRecognizedException("Feature '%s' not recognized" % name)
|
|
|
|
|
|
|
|
def setFeature(self, name, state):
|
|
|
|
"Sets the state of a SAX2 feature."
|
|
|
|
raise SAXNotRecognizedException("Feature '%s' not recognized" % name)
|
|
|
|
|
|
|
|
def getProperty(self, name):
|
|
|
|
"Looks up and returns the value of a SAX2 property."
|
|
|
|
raise SAXNotRecognizedException("Property '%s' not recognized" % name)
|
|
|
|
|
|
|
|
def setProperty(self, name, value):
|
|
|
|
"Sets the value of a SAX2 property."
|
|
|
|
raise SAXNotRecognizedException("Property '%s' not recognized" % name)
|
|
|
|
|
|
|
|
class IncrementalParser(XMLReader):
|
|
|
|
"""This interface adds three extra methods to the XMLReader
|
|
|
|
interface that allow XML parsers to support incremental
|
|
|
|
parsing. Support for this interface is optional, since not all
|
|
|
|
underlying XML parsers support this functionality.
|
|
|
|
|
|
|
|
When the parser is instantiated it is ready to begin accepting
|
|
|
|
data from the feed method immediately. After parsing has been
|
|
|
|
finished with a call to close the reset method must be called to
|
|
|
|
make the parser ready to accept new data, either from feed or
|
|
|
|
using the parse method.
|
|
|
|
|
|
|
|
Note that these methods must _not_ be called during parsing, that
|
|
|
|
is, after parse has been called and before it returns.
|
|
|
|
|
|
|
|
By default, the class also implements the parse method of the XMLReader
|
|
|
|
interface using the feed, close and reset methods of the
|
|
|
|
IncrementalParser interface as a convenience to SAX 2.0 driver
|
|
|
|
writers."""
|
2000-09-21 14:43:48 -03:00
|
|
|
|
|
|
|
def __init__(self, bufsize=2**16):
|
|
|
|
self._bufsize = bufsize
|
|
|
|
XMLReader.__init__(self)
|
|
|
|
|
2000-09-24 15:54:49 -03:00
|
|
|
def parse(self, source):
|
2006-08-17 06:10:09 -03:00
|
|
|
from . import saxutils
|
2000-09-24 15:54:49 -03:00
|
|
|
source = saxutils.prepare_input_source(source)
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-09-24 15:54:49 -03:00
|
|
|
self.prepareParser(source)
|
2015-04-02 15:00:13 -03:00
|
|
|
file = source.getCharacterStream()
|
|
|
|
if file is None:
|
|
|
|
file = source.getByteStream()
|
2022-11-26 18:33:25 -04:00
|
|
|
while buffer := file.read(self._bufsize):
|
2000-06-29 16:34:54 -03:00
|
|
|
self.feed(buffer)
|
2000-10-06 18:12:12 -03:00
|
|
|
self.close()
|
2000-06-29 16:34:54 -03:00
|
|
|
|
2000-10-23 15:09:50 -03:00
|
|
|
def feed(self, data):
|
2000-06-29 16:34:54 -03:00
|
|
|
"""This method gives the raw XML data in the data parameter to
|
|
|
|
the parser and makes it parse the data, emitting the
|
|
|
|
corresponding events. It is allowed for XML constructs to be
|
|
|
|
split across several calls to feed.
|
|
|
|
|
|
|
|
feed may raise SAXException."""
|
|
|
|
raise NotImplementedError("This method must be implemented!")
|
2000-09-21 14:43:48 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
def prepareParser(self, source):
|
|
|
|
"""This method is called by the parse implementation to allow
|
|
|
|
the SAX 2.0 driver to prepare itself for parsing."""
|
|
|
|
raise NotImplementedError("prepareParser must be overridden!")
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""This method is called when the entire XML document has been
|
|
|
|
passed to the parser through the feed method, to notify the
|
|
|
|
parser that there are no more data. This allows the parser to
|
|
|
|
do the final checks on the document and empty the internal
|
|
|
|
data buffer.
|
|
|
|
|
|
|
|
The parser will not be ready to parse another document until
|
|
|
|
the reset method has been called.
|
|
|
|
|
|
|
|
close may raise SAXException."""
|
|
|
|
raise NotImplementedError("This method must be implemented!")
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
"""This method is called after close has been called to reset
|
|
|
|
the parser so that it is ready to parse new documents. The
|
|
|
|
results of calling parse or feed after close without calling
|
|
|
|
reset are undefined."""
|
|
|
|
raise NotImplementedError("This method must be implemented!")
|
|
|
|
|
|
|
|
# ===== LOCATOR =====
|
2000-09-24 15:39:23 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
class Locator:
|
|
|
|
"""Interface for associating a SAX event with a document
|
|
|
|
location. A locator object will return valid results only during
|
|
|
|
calls to DocumentHandler methods; at any other time, the
|
|
|
|
results are unpredictable."""
|
|
|
|
|
|
|
|
def getColumnNumber(self):
|
2000-07-06 00:01:40 -03:00
|
|
|
"Return the column number where the current event ends."
|
|
|
|
return -1
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def getLineNumber(self):
|
2000-07-06 00:01:40 -03:00
|
|
|
"Return the line number where the current event ends."
|
|
|
|
return -1
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def getPublicId(self):
|
2000-07-06 00:01:40 -03:00
|
|
|
"Return the public identifier for the current event."
|
|
|
|
return None
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def getSystemId(self):
|
2000-07-06 00:01:40 -03:00
|
|
|
"Return the system identifier for the current event."
|
|
|
|
return None
|
2000-06-29 16:34:54 -03:00
|
|
|
|
2000-09-24 15:54:49 -03:00
|
|
|
# ===== INPUTSOURCE =====
|
|
|
|
|
|
|
|
class InputSource:
|
|
|
|
"""Encapsulation of the information needed by the XMLReader to
|
|
|
|
read entities.
|
|
|
|
|
|
|
|
This class may include information about the public identifier,
|
|
|
|
system identifier, byte stream (possibly with character encoding
|
|
|
|
information) and/or the character stream of an entity.
|
|
|
|
|
|
|
|
Applications will create objects of this class for use in the
|
|
|
|
XMLReader.parse method and for returning from
|
|
|
|
EntityResolver.resolveEntity.
|
|
|
|
|
|
|
|
An InputSource belongs to the application, the XMLReader is not
|
|
|
|
allowed to modify InputSource objects passed to it from the
|
|
|
|
application, although it may make copies and modify those."""
|
|
|
|
|
|
|
|
def __init__(self, system_id = None):
|
|
|
|
self.__system_id = system_id
|
|
|
|
self.__public_id = None
|
|
|
|
self.__encoding = None
|
|
|
|
self.__bytefile = None
|
|
|
|
self.__charfile = None
|
|
|
|
|
|
|
|
def setPublicId(self, public_id):
|
|
|
|
"Sets the public identifier of this InputSource."
|
|
|
|
self.__public_id = public_id
|
|
|
|
|
|
|
|
def getPublicId(self):
|
|
|
|
"Returns the public identifier of this InputSource."
|
|
|
|
return self.__public_id
|
|
|
|
|
|
|
|
def setSystemId(self, system_id):
|
|
|
|
"Sets the system identifier of this InputSource."
|
|
|
|
self.__system_id = system_id
|
|
|
|
|
|
|
|
def getSystemId(self):
|
|
|
|
"Returns the system identifier of this InputSource."
|
|
|
|
return self.__system_id
|
|
|
|
|
|
|
|
def setEncoding(self, encoding):
|
|
|
|
"""Sets the character encoding of this InputSource.
|
|
|
|
|
|
|
|
The encoding must be a string acceptable for an XML encoding
|
|
|
|
declaration (see section 4.3.3 of the XML recommendation).
|
|
|
|
|
|
|
|
The encoding attribute of the InputSource is ignored if the
|
|
|
|
InputSource also contains a character stream."""
|
|
|
|
self.__encoding = encoding
|
|
|
|
|
|
|
|
def getEncoding(self):
|
|
|
|
"Get the character encoding of this InputSource."
|
|
|
|
return self.__encoding
|
|
|
|
|
|
|
|
def setByteStream(self, bytefile):
|
|
|
|
"""Set the byte stream (a Python file-like object which does
|
|
|
|
not perform byte-to-character conversion) for this input
|
|
|
|
source.
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-09-24 15:54:49 -03:00
|
|
|
The SAX parser will ignore this if there is also a character
|
|
|
|
stream specified, but it will use a byte stream in preference
|
|
|
|
to opening a URI connection itself.
|
|
|
|
|
|
|
|
If the application knows the character encoding of the byte
|
|
|
|
stream, it should set it with the setEncoding method."""
|
|
|
|
self.__bytefile = bytefile
|
|
|
|
|
|
|
|
def getByteStream(self):
|
|
|
|
"""Get the byte stream for this input source.
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-09-24 15:54:49 -03:00
|
|
|
The getEncoding method will return the character encoding for
|
2000-10-23 15:09:50 -03:00
|
|
|
this byte stream, or None if unknown."""
|
2000-09-24 15:54:49 -03:00
|
|
|
return self.__bytefile
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-09-24 15:54:49 -03:00
|
|
|
def setCharacterStream(self, charfile):
|
|
|
|
"""Set the character stream for this input source. (The stream
|
2001-01-27 04:56:24 -04:00
|
|
|
must be a Python 2.0 Unicode-wrapped file-like that performs
|
2000-09-24 15:54:49 -03:00
|
|
|
conversion to Unicode strings.)
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-09-24 15:54:49 -03:00
|
|
|
If there is a character stream specified, the SAX parser will
|
|
|
|
ignore any byte stream and will not attempt to open a URI
|
|
|
|
connection to the system identifier."""
|
|
|
|
self.__charfile = charfile
|
|
|
|
|
|
|
|
def getCharacterStream(self):
|
|
|
|
"Get the character stream for this input source."
|
|
|
|
return self.__charfile
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-09-24 15:39:23 -03:00
|
|
|
# ===== ATTRIBUTESIMPL =====
|
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
class AttributesImpl:
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-09-24 15:39:23 -03:00
|
|
|
def __init__(self, attrs):
|
|
|
|
"""Non-NS-aware implementation.
|
|
|
|
|
|
|
|
attrs should be of the form {name : value}."""
|
2000-06-29 16:34:54 -03:00
|
|
|
self._attrs = attrs
|
|
|
|
|
|
|
|
def getLength(self):
|
|
|
|
return len(self._attrs)
|
|
|
|
|
|
|
|
def getType(self, name):
|
|
|
|
return "CDATA"
|
|
|
|
|
|
|
|
def getValue(self, name):
|
|
|
|
return self._attrs[name]
|
|
|
|
|
|
|
|
def getValueByQName(self, name):
|
2000-09-24 15:39:23 -03:00
|
|
|
return self._attrs[name]
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def getNameByQName(self, name):
|
2006-08-18 23:45:06 -03:00
|
|
|
if name not in self._attrs:
|
2007-08-24 15:57:22 -03:00
|
|
|
raise KeyError(name)
|
2000-09-24 15:39:23 -03:00
|
|
|
return name
|
|
|
|
|
|
|
|
def getQNameByName(self, name):
|
2006-08-18 23:45:06 -03:00
|
|
|
if name not in self._attrs:
|
2007-08-24 15:57:22 -03:00
|
|
|
raise KeyError(name)
|
2000-10-23 15:09:50 -03:00
|
|
|
return name
|
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
def getNames(self):
|
2007-02-11 14:44:55 -04:00
|
|
|
return list(self._attrs.keys())
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def getQNames(self):
|
2007-02-11 14:44:55 -04:00
|
|
|
return list(self._attrs.keys())
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._attrs)
|
|
|
|
|
|
|
|
def __getitem__(self, name):
|
|
|
|
return self._attrs[name]
|
|
|
|
|
|
|
|
def keys(self):
|
2007-02-11 14:44:55 -04:00
|
|
|
return list(self._attrs.keys())
|
2000-06-29 16:34:54 -03:00
|
|
|
|
2003-01-29 20:56:33 -04:00
|
|
|
def __contains__(self, name):
|
2006-08-18 23:45:06 -03:00
|
|
|
return name in self._attrs
|
2003-01-29 20:56:33 -04:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
def get(self, name, alternative=None):
|
|
|
|
return self._attrs.get(name, alternative)
|
|
|
|
|
|
|
|
def copy(self):
|
2000-09-24 15:39:23 -03:00
|
|
|
return self.__class__(self._attrs)
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def items(self):
|
2007-02-11 14:44:55 -04:00
|
|
|
return list(self._attrs.items())
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def values(self):
|
2007-02-11 14:44:55 -04:00
|
|
|
return list(self._attrs.values())
|
2000-06-29 16:34:54 -03:00
|
|
|
|
2000-09-24 15:39:23 -03:00
|
|
|
# ===== ATTRIBUTESNSIMPL =====
|
|
|
|
|
|
|
|
class AttributesNSImpl(AttributesImpl):
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-09-24 15:39:23 -03:00
|
|
|
def __init__(self, attrs, qnames):
|
|
|
|
"""NS-aware implementation.
|
|
|
|
|
|
|
|
attrs should be of the form {(ns_uri, lname): value, ...}.
|
|
|
|
qnames of the form {(ns_uri, lname): qname, ...}."""
|
|
|
|
self._attrs = attrs
|
|
|
|
self._qnames = qnames
|
|
|
|
|
|
|
|
def getValueByQName(self, name):
|
|
|
|
for (nsname, qname) in self._qnames.items():
|
|
|
|
if qname == name:
|
|
|
|
return self._attrs[nsname]
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2007-08-24 15:57:22 -03:00
|
|
|
raise KeyError(name)
|
2000-09-24 15:39:23 -03:00
|
|
|
|
|
|
|
def getNameByQName(self, name):
|
|
|
|
for (nsname, qname) in self._qnames.items():
|
|
|
|
if qname == name:
|
|
|
|
return nsname
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2007-08-24 15:57:22 -03:00
|
|
|
raise KeyError(name)
|
2000-09-24 15:39:23 -03:00
|
|
|
|
|
|
|
def getQNameByName(self, name):
|
|
|
|
return self._qnames[name]
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-09-24 15:39:23 -03:00
|
|
|
def getQNames(self):
|
2007-02-11 14:44:55 -04:00
|
|
|
return list(self._qnames.values())
|
2000-09-24 15:39:23 -03:00
|
|
|
|
|
|
|
def copy(self):
|
|
|
|
return self.__class__(self._attrs, self._qnames)
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-09-21 14:43:48 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
def _test():
|
|
|
|
XMLReader()
|
|
|
|
IncrementalParser()
|
|
|
|
Locator()
|
|
|
|
|
2000-09-21 14:43:48 -03:00
|
|
|
if __name__ == "__main__":
|
2000-06-29 16:34:54 -03:00
|
|
|
_test()
|