Commit b10439fa authored by Nicolas Delaby's avatar Nicolas Delaby

merge addXMLObject and replaceXMLObject into unique method

_createAddOrReplaceNode


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@35807 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 25dde018
...@@ -318,37 +318,11 @@ class XMLSyncUtilsMixin(SyncCode): ...@@ -318,37 +318,11 @@ class XMLSyncUtilsMixin(SyncCode):
else: else:
raise ValueError, "Sorry, the given namespace is not supported" raise ValueError, "Sorry, the given namespace is not supported"
def addXMLObject(self, cmd_id=0, object=None, xml_string=None, def addXMLObject(self, **kw):
more_data=0, gid=None, media_type=None):
""" """
Add an object with the SyncML protocol Add an object with the SyncML protocol
""" """
data_node = E.Data() return self._createAddOrReplaceNode('Add', **kw)
if media_type == self.MEDIA_TYPE['TEXT_XML'] and isinstance(xml_string, str):
data_node.append(etree.XML(xml_string, parser=parser))
elif media_type == self.MEDIA_TYPE['TEXT_XML'] and \
not isinstance(xml_string, str):
#xml_string could be Partial element if partial XML
data_node.append(xml_string)
else:
cdata = etree.CDATA(xml_string.decode('utf-8'))
data_node.text = cdata
xml = (E.Add(
E.CmdID('%s' % cmd_id),
E.Meta(
E.Type(media_type)
),
E.Item(
E.Source(
E.LocURI(gid)
),
data_node
)
))
if more_data:
item_node = xml.find('{%s}Item' % SYNCML_NAMESPACE)
item_node.append(E.MoreData())
return etree.tostring(xml, encoding='utf-8', pretty_print=True)
def deleteXMLObject(self, cmd_id=0, object_gid=None, rid=None): def deleteXMLObject(self, cmd_id=0, object_gid=None, rid=None):
""" """
...@@ -366,34 +340,40 @@ class XMLSyncUtilsMixin(SyncCode): ...@@ -366,34 +340,40 @@ class XMLSyncUtilsMixin(SyncCode):
)) ))
return etree.tostring(xml, encoding='utf-8', pretty_print=True) return etree.tostring(xml, encoding='utf-8', pretty_print=True)
def replaceXMLObject(self, cmd_id=0, object=None, xml_string=None, def replaceXMLObject(self, **kw):
more_data=0, gid=None, rid=None, media_type=None):
""" """
Replace an object with the SyncML protocol Replace an object with the SyncML protocol
""" """
if rid: return self._createAddOrReplaceNode('Replace', **kw)
elem_to_append = E.Target(E.LocURI('%s' % rid))
else: def _createAddOrReplaceNode(self, id_tag, cmd_id=0, object=None,
elem_to_append = E.Source(E.LocURI('%s' % gid)) xml_string=None, more_data=False, gid=None,
rid=None, media_type=None):
"""Mixin for addXMLObject() and replaceXMLObject()
"""
data_node = E.Data() data_node = E.Data()
if not isinstance(xml_string, (str, unicode)): if media_type == self.MEDIA_TYPE['TEXT_XML']:
if isinstance(xml_string, str):
data_node.append(etree.XML(xml_string, parser=parser))
elif isinstance(xml_string, etree.CDATA):
#xml_string could be Data element if partial XML
data_node.text = xml_string
else:
data_node.append(xml_string) data_node.append(xml_string)
else: else:
data_node.append(etree.XML(xml_string, parser=parser)) if isinstance(xml_string, etree.CDATA):
xml = (E.Replace( data_node.text = xml_string
E.CmdID('%s' % cmd_id), else:
E.Meta( cdata = etree.CDATA(xml_string.decode('utf-8'))
E.Type(media_type) data_node.text = cdata
), main_tag = Element('{%s}%s' % (SYNCML_NAMESPACE, id_tag))
E.Item( main_tag.append(E.CmdID('%s' % cmd_id))
elem_to_append, main_tag.append(E.Meta(E.Type(media_type)))
data_node main_tag.append(E.Item(E.Source(E.LocURI(gid)), data_node))
)
))
if more_data: if more_data:
item_node = xml.find('{%s}Item' % SYNCML_NAMESPACE) item_node = main_tag.find('{%s}Item' % SYNCML_NAMESPACE)
item_node.append(E.MoreData()) item_node.append(E.MoreData())
return etree.tostring(xml, encoding='utf-8', pretty_print=True) return etree.tostring(main_tag, encoding='utf-8', pretty_print=True)
def getXupdateObject(self, object_xml=None, old_xml=None): def getXupdateObject(self, object_xml=None, old_xml=None):
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment