diff emeraldtree/ElementTree.py @ 25:47ae505e63ea

Parse tags and attribute names always in QName. * emeraldtree/ElementTree.py - Add QName.copy. - Force usage of QName in XMLParser. - Don't convert all text to ascii. * emeraldtree/tests/test_ElementTree.py: Check for QName.
author Bastian Blank <bblank@thinkmo.de>
date Sun, 08 Jun 2008 12:09:33 +0200
parents 4c1912ff58a3
children eeb0eb69c1da
line wrap: on
line diff
--- a/emeraldtree/ElementTree.py	Fri Jun 06 22:01:07 2008 +0200
+++ b/emeraldtree/ElementTree.py	Sun Jun 08 12:09:33 2008 +0200
@@ -500,6 +500,9 @@
             return cmp(self.name, other.name)
         return cmp(self.text, other)
 
+    def copy(self):
+        return self.__class__(self.name, self.uri)
+
     @property
     def text(self):
         if self.uri is not None:
@@ -1321,45 +1324,36 @@
         err.position = value.lineno, value.offset
         raise err
 
-    def _fixtext(self, text):
-        # convert text string to ascii, if possible
-        try:
-            return text.encode("ascii")
-        except UnicodeError:
-            return text
-
     def _fixname(self, key):
         # expand qname, and convert name string to ascii, if possible
-        try:
-            name = self._names[key]
-        except KeyError:
-            name = key
-            if "}" in name:
-                name = "{" + name
-            self._names[key] = name = self._fixtext(name)
+        if key in self._names:
+            return self._names[key].copy()
+        if '}' in key:
+            name = QName(*key.split('}', 1))
+        else:
+            name = QName(key)
+        self._names[key] = name
         return name
 
     def _start(self, tag, attrib_in):
         fixname = self._fixname
-        fixtext = self._fixtext
         tag = fixname(tag)
         attrib = {}
         for key, value in attrib_in.items():
-            attrib[fixname(key)] = fixtext(value)
+            attrib[fixname(key)] = value
         return self.target.start(tag, attrib)
 
     def _start_list(self, tag, attrib_in):
         fixname = self._fixname
-        fixtext = self._fixtext
         tag = fixname(tag)
         attrib = {}
         if attrib_in:
             for i in range(0, len(attrib_in), 2):
-                attrib[fixname(attrib_in[i])] = fixtext(attrib_in[i+1])
+                attrib[fixname(attrib_in[i])] = attrib_in[i+1]
         return self.target.start(tag, attrib)
 
     def _data(self, text):
-        return self.target.data(self._fixtext(text))
+        return self.target.data(text)
 
     def _end(self, tag):
         return self.target.end(self._fixname(tag))