From ae6b376eeda2294b6d02c25d6041dea34e5cb0f0 Mon Sep 17 00:00:00 2001
From: Nicolas Delaby <nicolas@nexedi.com>
Date: Mon, 7 Jan 2008 11:48:53 +0000
Subject: [PATCH] Use XSL stylesheet if available when OOoTemplate creation.
 You need to install libxml2 and libxslt python wrapping library

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@18611 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5OOo/OOoTemplate.py |  7 ++++-
 product/ERP5OOo/OOoUtils.py    | 47 ++++++++++++++++++++++++----------
 2 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/product/ERP5OOo/OOoTemplate.py b/product/ERP5OOo/OOoTemplate.py
index b560742e9d..01ce3ae9b0 100644
--- a/product/ERP5OOo/OOoTemplate.py
+++ b/product/ERP5OOo/OOoTemplate.py
@@ -184,7 +184,12 @@ class OOoTemplate(ZopePageTemplate):
         memory_file.seek(0)
         self.OLE_documents_zipstring = memory_file.read()
       self.content_type = builder.getMimeType()
-      file = builder.prepareContentXml()
+      #get XSL stylesheet from portal_skins
+      xsl_dtml = getattr(self, 'ERP5OOoTemplate_ooo2zpt', None)
+      xsl_content = None
+      if xsl_dtml is not None:
+        xsl_content = xsl_dtml()
+      file = builder.prepareContentXml(xsl_content)
 
     return ZopePageTemplate.pt_upload(self, REQUEST, file)
 
diff --git a/product/ERP5OOo/OOoUtils.py b/product/ERP5OOo/OOoUtils.py
index 172adda56a..658545ff47 100644
--- a/product/ERP5OOo/OOoUtils.py
+++ b/product/ERP5OOo/OOoUtils.py
@@ -134,27 +134,46 @@ class OOoBuilder(Implicit):
   def getMimeType(self):
     return self.extract('mimetype')
 
-  def prepareContentXml(self) :
+  def prepareContentXml(self, xsl_content=None):
     """
       extracts content.xml text and prepare it :
         - add tal namespace
         - indent the xml
     """
-    import pprint
     content_xml = self.extract('content.xml')
-    reader = PyExpat.Reader()
-    document = reader.fromString(content_xml)
-    document_element = document.documentElement
-    from xml.dom.ext import PrettyPrint
     output = StringIO()
-    PrettyPrint(document_element, output)
-    return output.getvalue().replace(
-      "office:version='1.0'",
-      """ xmlns:tal='http://xml.zope.org/namespaces/tal'
-          xmlns:i18n='http://xml.zope.org/namespaces/i18n'
-          xmlns:metal='http://xml.zope.org/namespaces/metal'
-          tal:attributes='dummy python:request.RESPONSE.setHeader("Content-Type", "text/html;; charset=utf-8")'
-         office:version='1.0'""")
+    try:
+      import libxml2
+      import libxslt
+      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)
+      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().replace(
+        'office:version="1.0">',
+        """ xmlns:tal="http://xml.zope.org/namespaces/tal"
+            xmlns:i18n="http://xml.zope.org/namespaces/i18n"
+            xmlns:metal="http://xml.zope.org/namespaces/metal"
+            tal:attributes="dummy python:request.RESPONSE.setHeader('Content-Type', 'text/html;; charset=utf-8')"
+          office:version="1.0">""")
+    except ImportError:
+      reader = PyExpat.Reader()
+      document = reader.fromString(content_xml)
+      document_element = document.documentElement
+      from xml.dom.ext import PrettyPrint
+      PrettyPrint(document_element, output)
+      return output.getvalue().replace(
+        "office:version='1.0'",
+        """ xmlns:tal='http://xml.zope.org/namespaces/tal'
+            xmlns:i18n='http://xml.zope.org/namespaces/i18n'
+            xmlns:metal='http://xml.zope.org/namespaces/metal'
+            tal:attributes='dummy python:request.RESPONSE.setHeader("Content-Type", "text/html;; charset=utf-8")'
+          office:version='1.0'""")
 
   def addFileEntry(self, full_path, media_type, content=None):
       """ Add a file entry to the manifest and possibly is content """
-- 
2.30.9