Commit 860bb280 authored by Nicolas Delaby's avatar Nicolas Delaby

Replace libxml2 by lxml

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@25059 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 321a95fb
...@@ -6,7 +6,8 @@ from Acquisition import aq_base ...@@ -6,7 +6,8 @@ from Acquisition import aq_base
try: try:
from Products.ERP5OOo.OOoUtils import OOoBuilder from Products.ERP5OOo.OOoUtils import OOoBuilder
import re import re
from libxml2 import parseDoc, parserError from lxml import etree
from lxml.etree import ParseError, Element
import_succeed = 1 import_succeed = 1
except ImportError: except ImportError:
import_succeed = 0 import_succeed = 0
...@@ -72,16 +73,16 @@ class OOOdCommandTransform(commandtransform): ...@@ -72,16 +73,16 @@ class OOOdCommandTransform(commandtransform):
""" """
builder = OOoBuilder(data) builder = OOoBuilder(data)
content = builder.extract('content.xml') content = builder.extract('content.xml')
xml_doc = parseDoc(content) xml_doc = etree.XML(content)
image_tag_list = xml_doc.xpathEval('//*[name() = "draw:image"]') image_tag_list = xml_doc.xpath('//*[name() = "draw:image"]')
svg_ns = xml_doc.getRootElement().searchNs(xml_doc, 'svg') SVG_NAMESPACE = 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0'
XLINK_NAMESPACE = 'http://www.w3.org/1999/xlink'
ratio_px_cm = 2.54 / 100. ratio_px_cm = 2.54 / 100.
for image_tag in image_tag_list: for image_tag in image_tag_list:
frame = image_tag.parent frame = image_tag.xpath('parent::node()')[0]
#Try to get image file from ZODB #Try to get image file from ZODB
href_attribute_list = image_tag.xpathEval('.//@*[name() = "xlink:href"]') href_attribute_list = image_tag.xpath('.//@*[name() = "xlink:href"]')
href_attribute = href_attribute_list[0] url = href_attribute_list[0]
url = href_attribute.get_content()
matching = re.match(REMOTE_URL_PATTERN, url) matching = re.match(REMOTE_URL_PATTERN, url)
if matching is not None: if matching is not None:
path = matching.groupdict().get('path') path = matching.groupdict().get('path')
...@@ -109,13 +110,14 @@ class OOOdCommandTransform(commandtransform): ...@@ -109,13 +110,14 @@ class OOOdCommandTransform(commandtransform):
height = image.height height = image.height
width = image.width width = image.width
if height: if height:
frame.setNsProp(svg_ns, 'height', '%.3fcm' % (height * ratio_px_cm)) frame.attrib.update({'{%s}height' % SVG_NAMESPACE: '%.3fcm' % (height * ratio_px_cm)})
if width: if width:
frame.setNsProp(svg_ns, 'width', '%.3fcm' % (width * ratio_px_cm)) frame.attrib.update({'{%s}width' % SVG_NAMESPACE: '%.3fcm' % (width * ratio_px_cm)})
new_path = builder.addImage(data, format=format) new_path = builder.addImage(data, format=format)
href_attribute.setContent(new_path) image_tag.attrib.update({'{%s}href' % XLINK_NAMESPACE: new_path})
builder.replace('content.xml', xml_doc.serialize('utf-8', 0)) builder.replace('content.xml', etree.tostring(xml_doc, encoding='utf-8',
xml_doc.freeDoc() xml_declaration=True,
pretty_print=False))
return builder.render() return builder.render()
def includeExternalCssList(self, data): def includeExternalCssList(self, data):
...@@ -123,17 +125,16 @@ class OOOdCommandTransform(commandtransform): ...@@ -123,17 +125,16 @@ class OOOdCommandTransform(commandtransform):
Replace external Css link by style Element Replace external Css link by style Element
""" """
try: try:
xml_doc = parseDoc(data) xml_doc = etree.XML(data)
except parserError: except ParseError:
#If not valid xhtml do nothing #If not valid xhtml do nothing
return data return data
xpath = '//*[local-name() = "link"][@type = "text/css"]' xpath = '//*[local-name() = "link"][@type = "text/css"]'
css_link_tag_list = xml_doc.xpathEval(xpath) css_link_tag_list = xml_doc.xpath(xpath)
for css_link_tag in css_link_tag_list: for css_link_tag in css_link_tag_list:
#Try to get css from ZODB #Try to get css from ZODB
href_attribute_list = css_link_tag.xpathEval('.//@href') href_attribute_list = css_link_tag.xpath('.//@href')
href_attribute = href_attribute_list[0] url = href_attribute_list[0]
url = href_attribute.get_content()
matching = re.match(REMOTE_URL_PATTERN, url) matching = re.match(REMOTE_URL_PATTERN, url)
if matching is not None: if matching is not None:
path = matching.groupdict().get('path') path = matching.groupdict().get('path')
...@@ -149,14 +150,14 @@ class OOOdCommandTransform(commandtransform): ...@@ -149,14 +150,14 @@ class OOOdCommandTransform(commandtransform):
else: else:
#Other cases like files #Other cases like files
css_as_text = str(css_object) css_as_text = str(css_object)
style_node = xml_doc.newChild(None, 'style', css_as_text) parent_node = css_link_tag.xpath('parent::node()')[0]
style_node.setProp('type', 'text/css') style_node = Element('style')
css_link_tag.replaceNode(style_node) style_node.text = css_as_text
#omit xml-declaration parent_node.append(style_node)
data = xml_doc.serialize('utf-8', 0)\ style_node.attrib.update({'type': 'text/css'})
.replace('<?xml version="1.0" encoding="utf-8"?>\n', '') parent_node.remove(css_link_tag)
xml_doc.freeDoc() return etree.tostring(xml_doc, encoding='utf-8',
return data xml_declaration=False, pretty_print=False, )
def convert(self): def convert(self):
tmp_ooo = newTempOOoDocument(self.context, self.name) tmp_ooo = newTempOOoDocument(self.context, self.name)
......
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