From 8d30656b9812526b74133650d9ba6fd904ab8d04 Mon Sep 17 00:00:00 2001
From: Nicolas Delaby <nicolas@nexedi.com>
Date: Mon, 31 May 2010 16:28:05 +0000
Subject: [PATCH] Add new parameter for replaceIdFromXML to specify id of
 replacement attribute

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@35817 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5SyncML/Conduit/ERP5Conduit.py | 34 +++++++++++------------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/product/ERP5SyncML/Conduit/ERP5Conduit.py b/product/ERP5SyncML/Conduit/ERP5Conduit.py
index a161bafe27..c49f83d16d 100644
--- a/product/ERP5SyncML/Conduit/ERP5Conduit.py
+++ b/product/ERP5SyncML/Conduit/ERP5Conduit.py
@@ -625,21 +625,21 @@ class ERP5Conduit(XMLSyncUtilsMixin):
         return self.convertXmlValue(subnode)
     return None
 
-  def replaceIdFromXML(self, xml, new_id):
+  def replaceIdFromXML(self, xml, attribute_name, new_id, as_string=True):
     """
       return a xml with id replace by a new id
     """
-    if xml is not None and new_id is not None:
-      if isinstance(xml, str):
-        xml = etree.XML(xml, parser=parser)
+    if isinstance(xml, str):
+      xml = etree.XML(xml, parser=parser)
+    else:
       #copy of xml object for modification
-      xml_copy = deepcopy(xml)
-      if xml_copy.tag == self.xml_object_tag:
-        object_element = xml_copy
-      else:
-        object_element = xml_copy.xpath('//object')[0]
-      object_element.attrib['id'] = new_id
-      return etree.tostring(xml_copy)
+      xml = deepcopy(xml)
+    object_element = xml.find('object')
+    del object_element.attrib['id']
+    object_element.attrib[attribute_name] = new_id
+    if as_string:
+      return etree.tostring(xml)
+    return xml
 
   def getXMLFromObjectWithId(self, object, xml_mapping):
     """
@@ -653,20 +653,20 @@ class ERP5Conduit(XMLSyncUtilsMixin):
       xml = func()
     return xml
 
-  def getXMLFromObjectWithGid(self, object, gid, xml_mapping=None):
+  def getXMLFromObjectWithGid(self, object, gid, xml_mapping, as_string=True):
     """
       return the xml with Gid of Object
     """
-    xml_id = self.getXMLFromObjectWithId(object, xml_mapping)
-    xml_gid = self.replaceIdFromXML(xml_id, gid)
-    return xml_gid
+    xml_with_id = self.getXMLFromObjectWithId(object, xml_mapping)
+    return self.replaceIdFromXML(xml_with_id, 'gid', gid, as_string=as_string)
+
 
-  def getXMLFromObjectWithRid(self, object, rid, xml_mapping=None):
+  def getXMLFromObjectWithRid(self, object, rid, xml_mapping, as_string=True):
     """
       return the xml with Rid of Object
     """
     xml_id = self.getXMLFromObjectWithId(object, xml_mapping)
-    xml_rid = self.replaceIdFromXML(xml_id, rid)
+    xml_rid = self.replaceIdFromXML(xml_id, 'rid', rid, as_string=as_string)
     return xml_rid
 
   security.declareProtected(Permissions.AccessContentsInformation,'convertToXml')
-- 
2.30.9