Updated to final Attributes interface (patch 101632).
This commit is contained in:
parent
e84bf751bb
commit
32bf12eb8a
|
@ -20,6 +20,9 @@ from xml.sax._exceptions import *
|
|||
from xml.parsers import expat
|
||||
from xml.sax import xmlreader
|
||||
|
||||
AttributesImpl = xmlreader.AttributesImpl
|
||||
AttributesNSImpl = xmlreader.AttributesNSImpl
|
||||
|
||||
# --- ExpatParser
|
||||
|
||||
class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
|
||||
|
@ -31,7 +34,6 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
|
|||
self._parser = None
|
||||
self._namespaces = namespaceHandling
|
||||
self._parsing = 0
|
||||
self._attrs = xmlreader.AttributesImpl({}, {})
|
||||
|
||||
# XMLReader methods
|
||||
|
||||
|
@ -137,7 +139,7 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
|
|||
|
||||
# event handlers
|
||||
def start_element(self, name, attrs):
|
||||
self._cont_handler.startElement(name, self._attrs)
|
||||
self._cont_handler.startElement(name, AttributesImpl(attrs))
|
||||
|
||||
def end_element(self, name):
|
||||
self._cont_handler.endElement(name)
|
||||
|
@ -147,12 +149,23 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
|
|||
if len(pair) == 1:
|
||||
pair = (None, name)
|
||||
|
||||
self._cont_handler.startElementNS(pair, None, self._attrs)
|
||||
newattrs = {}
|
||||
for (aname, value) in attrs.items():
|
||||
apair = aname.split()
|
||||
if len(apair) == 1:
|
||||
apair = (None, aname)
|
||||
else:
|
||||
apair = tuple(apair)
|
||||
|
||||
newattrs[apair] = value
|
||||
|
||||
self._cont_handler.startElementNS(pair, None,
|
||||
AttributesNSImpl(newattrs, {}))
|
||||
|
||||
def end_element_ns(self, name):
|
||||
pair = name.split()
|
||||
if len(pair) == 1:
|
||||
name = (None, name)
|
||||
pair = (None, name)
|
||||
|
||||
self._cont_handler.endElementNS(pair, None)
|
||||
|
||||
|
|
|
@ -151,6 +151,7 @@ class IncrementalParser(XMLReader):
|
|||
raise NotImplementedError("This method must be implemented!")
|
||||
|
||||
# ===== LOCATOR =====
|
||||
|
||||
class Locator:
|
||||
"""Interface for associating a SAX event with a document
|
||||
location. A locator object will return valid results only during
|
||||
|
@ -173,11 +174,15 @@ class Locator:
|
|||
"Return the system identifier for the current event."
|
||||
return None
|
||||
|
||||
# --- AttributesImpl
|
||||
# ===== ATTRIBUTESIMPL =====
|
||||
|
||||
class AttributesImpl:
|
||||
def __init__(self, attrs, rawnames):
|
||||
|
||||
def __init__(self, attrs):
|
||||
"""Non-NS-aware implementation.
|
||||
|
||||
attrs should be of the form {name : value}."""
|
||||
self._attrs = attrs
|
||||
self._rawnames = rawnames
|
||||
|
||||
def getLength(self):
|
||||
return len(self._attrs)
|
||||
|
@ -189,16 +194,23 @@ class AttributesImpl:
|
|||
return self._attrs[name]
|
||||
|
||||
def getValueByQName(self, name):
|
||||
return self._attrs[self._rawnames[name]]
|
||||
return self._attrs[name]
|
||||
|
||||
def getNameByQName(self, name):
|
||||
return self._rawnames[name]
|
||||
if not self._attrs.has_key(name):
|
||||
raise KeyError
|
||||
return name
|
||||
|
||||
def getQNameByName(self, name):
|
||||
if not self._attrs.has_key(name):
|
||||
raise KeyError
|
||||
return name
|
||||
|
||||
def getNames(self):
|
||||
return self._attrs.keys()
|
||||
|
||||
def getQNames(self):
|
||||
return self._rawnames.keys()
|
||||
return self._attrs.keys()
|
||||
|
||||
def __len__(self):
|
||||
return len(self._attrs)
|
||||
|
@ -216,7 +228,7 @@ class AttributesImpl:
|
|||
return self._attrs.get(name, alternative)
|
||||
|
||||
def copy(self):
|
||||
return self.__class__(self._attrs, self._rawnames)
|
||||
return self.__class__(self._attrs)
|
||||
|
||||
def items(self):
|
||||
return self._attrs.items()
|
||||
|
@ -224,12 +236,46 @@ class AttributesImpl:
|
|||
def values(self):
|
||||
return self._attrs.values()
|
||||
|
||||
# ===== ATTRIBUTESNSIMPL =====
|
||||
|
||||
class AttributesNSImpl(AttributesImpl):
|
||||
|
||||
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]
|
||||
|
||||
raise KeyError
|
||||
|
||||
def getNameByQName(self, name):
|
||||
for (nsname, qname) in self._qnames.items():
|
||||
if qname == name:
|
||||
return nsname
|
||||
|
||||
raise KeyError
|
||||
|
||||
def getQNameByName(self, name):
|
||||
return self._qnames[name]
|
||||
|
||||
def getQNames(self):
|
||||
return self._qnames.values()
|
||||
|
||||
def copy(self):
|
||||
return self.__class__(self._attrs, self._qnames)
|
||||
|
||||
|
||||
def _test():
|
||||
XMLReader()
|
||||
IncrementalParser()
|
||||
Locator()
|
||||
AttributesImpl()
|
||||
|
||||
if __name__ == "__main__":
|
||||
_test()
|
||||
|
|
Loading…
Reference in New Issue