[4suite-checkins] In Amara/lib, files saxtools.py

Uche Ogbuji uogbuji at 4suite.org
Mon Dec 18 18:52:14 MST 2006


Modified Files:
    saxtools.py

Log Message:
Implement saxtools.sax_writer
Add lower-level memory leak test (UNIX only), tested on Linux only

ViewCVS diff:
  http://cvs.4suite.org/viewcvs/Amara/lib/saxtools.py.diff?r1=1.18&r2=1.19
ViewCVS view:
  http://cvs.4suite.org/viewcvs/Amara/lib/saxtools.py?rev=1.19&content-type=text/vnd.viewcvs-markup

Index: saxtools.py
===================================================================
RCS file: /var/local/cvsroot/Amara/lib/saxtools.py,v
retrieving revision 1.18
retrieving revision 1.19
diff -U2 -r1.18 -r1.19
--- saxtools.py	26 Oct 2006 23:14:24 -0000	1.18
+++ saxtools.py	19 Dec 2006 01:52:14 -0000	1.19
@@ -74,4 +74,58 @@
 
 
+class sax_writer:
+    """
+    A ContentHandler that serializes the result using a 4Suite writer
+    """
+
+    #def __init__(self, writer=XmlPrinter(sys.stdout, 'utf-8')):
+    def __init__(self, writer, docfrag=False):
+        """
+        writer - object to handle output
+        docfrag - True means assume the SAX input is a document fragment,
+                  so ignore startDocument and endDocument events
+        """
+        self._docfrag = docfrag
+        self._writer = writer
+        try:
+            self._writer.reset()
+        except AttributeError:
+            pass
+        self._namespaces = {}
+        return
+
+    def startDocument(self):
+        if not self._docfrag:
+            self._writer.startDocument()
+        return
+
+    def endDocument(self):
+        if not self._docfrag:
+            self._writer.endDocument()
+        return
+
+    def startPrefixMapping(self, prefix, uri):
+        self._namespaces[prefix] = uri
+        return
+
+    def startElementNS(self, (namespaceURI, localName), qualifiedName,
+                       attributes):
+        self._writer.startElement(qualifiedName, namespaceURI, 
+                                   self._namespaces)
+        self._namespaces = {}
+        for (ns, local), value in attributes.items():
+            qname = attributes.getQNameByName((ns, local))
+            self._writer.attribute(qname, value, ns)
+        return
+
+    def endElementNS(self, (namespaceURI, localName), qualifiedName):
+        self._writer.endElement(qualifiedName, namespaceURI)
+        return
+
+    def characters(self, data):
+        self._writer.text(data)
+        return
+
+
 #
 # Tenorsax framework: helps linerarize SAX logic


More information about the 4suite-checkins mailing list