From ca2f16b7227ef46b6f8b4a212e0066655569225c Mon Sep 17 00:00:00 2001
From: Nicolas Delaby <nicolas@nexedi.com>
Date: Wed, 16 Apr 2008 18:16:30 +0000
Subject: [PATCH] Support Editing of Any XML File contained in ODF Archive

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@20593 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5OOo/OOoTemplate.py            | 22 ++++++++++++----------
 product/ERP5OOo/OOoUtils.py               |  7 ++-----
 product/ERP5OOo/dtml/OOoTemplate_add.dtml | 17 +++++++++++++++++
 product/ERP5OOo/www/formSettings.zpt      |  7 ++++++-
 4 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/product/ERP5OOo/OOoTemplate.py b/product/ERP5OOo/OOoTemplate.py
index 12f73d39be..4b2c1b2d87 100644
--- a/product/ERP5OOo/OOoTemplate.py
+++ b/product/ERP5OOo/OOoTemplate.py
@@ -63,15 +63,16 @@ import Products.ERP5Type.Document
 # Constructors
 manage_addOOoTemplate = DTMLFile("dtml/OOoTemplate_add", globals())
 
-def addOOoTemplate(self, id, title="", REQUEST=None):
+def addOOoTemplate(self, id, title="", xml_file_id="content.xml", REQUEST=None):
   """Add OOo template to folder.
 
   id     -- the id of the new OOo template to add
   title  -- the title of the OOo to add
+  xml_file_id -- The Id of edited xml file
   Result -- empty string
   """
   # add actual object
-  id = self._setObject(id, OOoTemplate(id, title))
+  id = self._setObject(id, OOoTemplate(id, title, xml_file_id))
   if REQUEST is not None:
     file = REQUEST.form.get('file')
     if file.filename:
@@ -142,6 +143,7 @@ class OOoTemplate(ZopePageTemplate):
 
   # Default Attributes
   ooo_stylesheet = 'Base_getODTStyleSheet'
+  ooo_xml_file_id = 'content.xml'
 
   # Default content type
   #content_type = 'application/vnd.sun.xml.writer' # Writer type by default
@@ -160,11 +162,11 @@ class OOoTemplate(ZopePageTemplate):
                                   __name__='formSettings')
   formSettings._owner = None
 
-  def __init__(self,*args,**kw):
-    ZopePageTemplate.__init__(self,*args,**kw)
+  def __init__(self, id, title, xml_file_id='content.xml', *args,**kw):
+    ZopePageTemplate.__init__(self, id, title, *args, **kw)
     # we store the attachments of the uploaded document
     self.OLE_documents_zipstring = None
-
+    self.ooo_xml_file_id = xml_file_id
   # Every OOoTemplate uses UTF-8 or Unicode, so a special StringIO class
   # must be used, which does not care about response.
   def StringIO(self):
@@ -207,8 +209,7 @@ class OOoTemplate(ZopePageTemplate):
       xsl_content = None
       if xsl_dtml is not None:
         xsl_content = xsl_dtml()
-      file = builder.prepareContentXml(xsl_content)
-
+      file = builder.prepareContentXml(self.ooo_xml_file_id, xsl_content)
     return ZopePageTemplate.pt_upload(self, REQUEST, file)
 
   security.declareProtected('Change Page Templates', 'pt_edit')
@@ -220,13 +221,14 @@ class OOoTemplate(ZopePageTemplate):
     self.write(text)
 
   security.declareProtected('Change Page Templates', 'doSettings')
-  def doSettings(self, REQUEST, title, ooo_stylesheet):
+  def doSettings(self, REQUEST, title, xml_file_id, ooo_stylesheet):
     """
-      Change title and ooo_stylesheet.
+      Change title, xml_file_id and ooo_stylesheet.
     """
     if SUPPORTS_WEBDAV_LOCKS and self.wl_isLocked():
       raise ResourceLockedError, "File is locked via WebDAV"
     self.ooo_stylesheet = ooo_stylesheet
+    self.ooo_xml_file_id = xml_file_id
     self.pt_setTitle(title)
     #REQUEST.set('text', self.read()) # May not equal 'text'!
     message = "Saved changes."
@@ -507,7 +509,7 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
       return doc_xml
 
     # Replace content.xml in master openoffice template
-    ooo_builder.replace('content.xml', doc_xml)
+    ooo_builder.replace(self.ooo_xml_file_id, doc_xml)
 
     # Old templates correction
     try:
diff --git a/product/ERP5OOo/OOoUtils.py b/product/ERP5OOo/OOoUtils.py
index d9471fc110..9bcbc8692d 100644
--- a/product/ERP5OOo/OOoUtils.py
+++ b/product/ERP5OOo/OOoUtils.py
@@ -134,13 +134,13 @@ class OOoBuilder(Implicit):
   def getMimeType(self):
     return self.extract('mimetype')
 
-  def prepareContentXml(self, xsl_content=None):
+  def prepareContentXml(self, ooo_xml_file_id, xsl_content=None):
     """
       extracts content.xml text and prepare it :
         - add tal namespace
         - indent the xml
     """
-    content_xml = self.extract('content.xml')
+    content_xml = self.extract(ooo_xml_file_id)
     output = StringIO()
     try:
       import libxml2
@@ -156,9 +156,6 @@ class OOoBuilder(Implicit):
       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.setNs(tal)
-      root.setNs(i18n)
-      root.setNs(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)
diff --git a/product/ERP5OOo/dtml/OOoTemplate_add.dtml b/product/ERP5OOo/dtml/OOoTemplate_add.dtml
index 94af8f2e06..fde30a0d79 100644
--- a/product/ERP5OOo/dtml/OOoTemplate_add.dtml
+++ b/product/ERP5OOo/dtml/OOoTemplate_add.dtml
@@ -34,6 +34,23 @@ templates.
     </td>
   </tr>
 
+  <tr>
+    <td align="left" valign="top">
+    <div class="form-label">
+    Edited XML File Id
+    </div>
+    </td>
+    <td align="left" valign="top">
+    <select type="select" name="xml_file_id">
+      <option value=""></option>
+      <option value="content.xml" selected="selected">Content</option>
+      <option value="styles.xml">Styles</option>
+      <option value="meta.xml">Meta</option>
+      <option value="settings.xml">Settings</option>
+    </select>
+    </td>
+  </tr>
+
   <tr>
     <td align="left" valign="top">
       <div class="form-optional">
diff --git a/product/ERP5OOo/www/formSettings.zpt b/product/ERP5OOo/www/formSettings.zpt
index b23aa20b4e..901d0fbe31 100644
--- a/product/ERP5OOo/www/formSettings.zpt
+++ b/product/ERP5OOo/www/formSettings.zpt
@@ -10,12 +10,17 @@
       <td><input name="title" value="" type="text" size="20"
                 tal:attributes="value request/title | here/title | nothing"/></td>
     </tr>
+    <tr>
+      <td class="form-label">OOo XML File Id (Manipulated File)</td>
+      <td><input name="xml_file_id" value="default_xml_file_id"
+                 type="text" size="20"
+                 tal:attributes="value request/ooo_xml_file_id | here/ooo_xml_file_id | nothing"/></td>
+    </tr>
     <tr>
       <td class="form-label">OOo Stylesheet (Master Document)</td>
       <td><input name="ooo_stylesheet" value="default_ooo_template" type="text" size="20"
                 tal:attributes="value request/ooo_stylesheet | here/ooo_stylesheet | nothing"/></td>
     </tr>
-
     <tr>
       <td align="left" valign="top">
       <div class="form-element">
-- 
2.30.9