summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorLars Gustäbel <lars@gustaebel.de>2000-09-24 18:39:23 (GMT)
committerLars Gustäbel <lars@gustaebel.de>2000-09-24 18:39:23 (GMT)
commit32bf12eb8a5849762721b561f9b48c6e897792e9 (patch)
treec8470ceda65c34fb1ebc302de8234d902a9ae12b /Lib
parente84bf751bb0d18851877518dbb6d382f909f16b4 (diff)
downloadcpython-32bf12eb8a5849762721b561f9b48c6e897792e9.zip
cpython-32bf12eb8a5849762721b561f9b48c6e897792e9.tar.gz
cpython-32bf12eb8a5849762721b561f9b48c6e897792e9.tar.bz2
Updated to final Attributes interface (patch 101632).
Diffstat (limited to 'Lib')
-rw-r--r--Lib/xml/sax/expatreader.py21
-rw-r--r--Lib/xml/sax/xmlreader.py64
2 files changed, 72 insertions, 13 deletions
diff --git a/Lib/xml/sax/expatreader.py b/Lib/xml/sax/expatreader.py
index 1120f17..2f1ff1c 100644
--- a/Lib/xml/sax/expatreader.py
+++ b/Lib/xml/sax/expatreader.py
@@ -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)
diff --git a/Lib/xml/sax/xmlreader.py b/Lib/xml/sax/xmlreader.py
index 3bcf2b9..6dae0b5 100644
--- a/Lib/xml/sax/xmlreader.py
+++ b/Lib/xml/sax/xmlreader.py
@@ -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()