Commit 6448001c authored by Nicolas Delaby's avatar Nicolas Delaby

Refactor code to use lxml instead libxml2

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@25136 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 5fa214ab
...@@ -61,13 +61,9 @@ except ImportError: ...@@ -61,13 +61,9 @@ except ImportError:
from Products.ERP5.Document.Document import ConversionError from Products.ERP5.Document.Document import ConversionError
import Products.ERP5Type.Document import Products.ERP5Type.Document
try: from lxml import etree
from libxml2 import parseDoc from lxml.etree import Element
except ImportError: from copy import deepcopy
LOG('OOoUtils', INFO, "Can't import libxml2.parseDoc")
class parseDoc:
def __init__(self, *args, **kw):
raise ImportError, "Sorry, it was not possible to import libxml2 library, python2.4-libxml2 is not installed"
# Constructors # Constructors
manage_addOOoTemplate = DTMLFile("dtml/OOoTemplate_add", globals()) manage_addOOoTemplate = DTMLFile("dtml/OOoTemplate_add", globals())
...@@ -401,25 +397,23 @@ class OOoTemplate(ZopePageTemplate): ...@@ -401,25 +397,23 @@ class OOoTemplate(ZopePageTemplate):
replacement = '<text:p text:style-name="Standard">'+replacement+'</text:p>' replacement = '<text:p text:style-name="Standard">'+replacement+'</text:p>'
return replacement return replacement
xml_doc = parseDoc(text) xml_doc = etree.XML(text)
draw_ns = xml_doc.getRootElement().searchNs(xml_doc, 'draw') for office_include in xml_doc.xpath('//*[name() = "office:include"]'):
xlink_ns = xml_doc.getRootElement().searchNs(xml_doc, 'xlink') marshal_list = office_include.xpath('./marshal')
for office_include in xml_doc.xpathEval('//*[name() = "office:include"]'):
marshal_list = office_include.xpathEval('./marshal')
if marshal_list: if marshal_list:
from xml.marshal.generic import loads from xml.marshal.generic import loads
arg_dict = loads(marshal_list[0].serialize('utf-8', 0)) arg_dict = loads(etree.tostring(marshal_list[0], encoding='utf-8',
xml_declaration=True, pretty_print=False))
extra_context.update(arg_dict) extra_context.update(arg_dict)
request.other.update(arg_dict) request.other.update(arg_dict)
attr_path_list = office_include.xpathEval('./@path') path = office_include.attrib['path']
path = attr_path_list[0].content
new_path = replaceIncludes(path) new_path = replaceIncludes(path)
draw_object = xml_doc.newChild(draw_ns, 'object', None) draw_object = Element('{%s}object' % xml_doc.nsmap.get('draw'))
draw_object.setNsProp(xlink_ns, 'href', new_path) draw_object.attrib.update({'{%s}href' % xml_doc.nsmap.get('xlink'): new_path})
draw_object.copyPropList(office_include) draw_object.attrib.update(dict(office_include.attrib))
office_include.replaceNode(draw_object) office_include.getparent().replace(office_include, draw_object)
text = xml_doc.serialize('utf-8', 0) text = etree.tostring(xml_doc, encoding='utf-8', xml_declaration=True,
xml_doc.freeDoc() pretty_print=False)
text = re.sub('<\s*office:include_img\s+(.*?)\s*/\s*>(?s)', replaceIncludesImg, text) text = re.sub('<\s*office:include_img\s+(.*?)\s*/\s*>(?s)', replaceIncludesImg, text)
return (text, attached_files_dict) return (text, attached_files_dict)
......
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