From 7decf50d6fa266e11ef50cbf04fef4c43af8622c Mon Sep 17 00:00:00 2001
From: Nicolas Delaby <nicolas@nexedi.com>
Date: Thu, 8 Jan 2009 17:50:55 +0000
Subject: [PATCH] replace libxml2 by etree

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@25066 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5OOo/OOoUtils.py | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/product/ERP5OOo/OOoUtils.py b/product/ERP5OOo/OOoUtils.py
index 792d39deb5..95fb586989 100644
--- a/product/ERP5OOo/OOoUtils.py
+++ b/product/ERP5OOo/OOoUtils.py
@@ -151,24 +151,28 @@ class OOoBuilder(Implicit):
     content_xml = self.extract(ooo_xml_file_id)
     output = StringIO()
     try:
-      import libxml2
-      import libxslt
+      from lxml import etree
+      from lxml.etree import Element, SubElement
+      from copy import deepcopy
       if xsl_content is None:
         raise ImportError
-      stylesheet_doc = libxml2.parseDoc(xsl_content)
-      stylesheet = libxslt.parseStylesheetDoc(stylesheet_doc)
-      content_doc = libxml2.parseDoc(content_xml)
-      result_doc = stylesheet.applyStylesheet(content_doc, None)
+      stylesheet_doc = etree.XML(xsl_content)
+      stylesheet = etree.XSLT(stylesheet_doc)
+      content_doc = etree.XML(content_xml)
+      result_doc = stylesheet(content_doc)
+      root = result_doc.getroot()
       #Declare zope namespaces
-      root = result_doc.getRootElement()
-      tal = root.newNs('http://xml.zope.org/namespaces/tal', 'tal')
-      i18n = root.newNs('http://xml.zope.org/namespaces/i18n', 'i18n')
-      metal = root.newNs('http://xml.zope.org/namespaces/metal', 'metal')
-      root.setNsProp(tal, 'attributes', 'dummy python:request.RESPONSE.setHeader(\'Content-Type\', \'text/html;; charset=utf-8\')')
-      buff = libxml2.createOutputBuffer(output, 'utf-8')
-      result_doc.saveFormatFileTo(buff, 'utf-8', 1)
-      stylesheet_doc.freeDoc(); content_doc.freeDoc(); result_doc.freeDoc()
-      return output.getvalue()
+      NSMAP = {'tal': 'http://xml.zope.org/namespaces/tal',
+               'i18n': 'http://xml.zope.org/namespaces/i18n',
+               'metal': 'http://xml.zope.org/namespaces/metal'}
+      NSMAP.update(root.nsmap)
+      new_root = Element(root.tag, nsmap=NSMAP)
+      new_root.attrib.update(dict(root.attrib))
+      new_root.attrib.update({'{%s}attributes' % NSMAP.get('tal'): 'dummy python:request.RESPONSE.setHeader(\'Content-Type\', \'text/html;; charset=utf-8\')'})
+      for child in root.getchildren():
+        new_root.append(deepcopy(child))
+      return etree.tostring(new_root, encoding='utf-8', xml_declaration=True,
+                            pretty_print=True)
     except ImportError:
       document = Parse(content_xml)
       document_element = document.documentElement
-- 
2.30.9