oood_commandtransform.py 5.69 KB
Newer Older
1 2 3
from Products.PortalTransforms.libtransforms.commandtransform import commandtransform
from Products.PortalTransforms.interfaces import idatastream
from Products.ERP5Type.Document import newTempOOoDocument
4 5 6 7 8 9 10 11
from Products.CMFCore.utils import getToolByName
try:
  from Products.ERP5OOo.OOoUtils import OOoBuilder
  import re
  from libxml2 import parseDoc, parserError
  import_succeed = 1
except ImportError:
  import_succeed = 0
12 13
from zLOG import LOG

14 15
REMOTE_URL_PATTERN = '^((?P<protocol>http(s)?://)(?P<domain>[.a-zA-Z0-9]+)+)?(?P<port>:\d{4})?(?P<path>/?\S*)'

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
class TransformError(Exception):
  pass

class OOoDocumentDataStream:
  """Handle OOoDocument in Portal Transforms"""
  __implements__ = idatastream

  def setData(self, value):
    """set the main"""
    self.value = value

  def getData(self):
    return self.value

  def setSubObjects(self, objects):
    pass

  def getSubObjects(self):
    return {}

  def getMetadata(self):
    """return a dict-like object with any optional metadata from
    the transform
    You can modify the returned dictionnary to add/change metadata
    """
    return {}

  def isCacheable(self):
    """
     True by Default
    """
    return getattr(self, '_is_cacheable', True)

  def setCachable(self, value):
    self._is_cacheable = value

class OOOdCommandTransform(commandtransform):
  """Transformer using oood"""

  def __init__(self, context, name, data, mimetype):
    commandtransform.__init__(self, name)
    if name:
      self.__name__ = name
    self.mimetype = mimetype
60 61 62 63
    self.context = context
    if import_succeed and self.mimetype == 'text/html':
      data = self.includeExternalCssList(data)
    self.data = data
64 65 66 67

  def name(self):
    return self.__name__

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  def includeImageList(self, data):
    """
      Include Images in ODF archive
    """
    builder = OOoBuilder(data)
    content = builder.extract('content.xml')
    xml_doc = parseDoc(content)
    image_tag_list = xml_doc.xpathEval('//*[name() = "draw:image"]')
    svg_ns = xml_doc.getRootElement().searchNs(xml_doc, 'svg')
    ratio_px_cm = 2.54 / 100.
    for image_tag in image_tag_list:
      frame = image_tag.parent
      #Try to get image file from ZODB
      href_attribute_list = image_tag.xpathEval('.//@*[name() = "xlink:href"]')
      href_attribute = href_attribute_list[0]
      url = href_attribute.get_content()
      matching = re.match(REMOTE_URL_PATTERN, url)
      if matching is not None:
        path = matching.groupdict().get('path')
        try:
          image = self.context.restrictedTraverse(path)
        except AttributeError:
          #Image not found, this image is probably not hosted by ZODB. Do nothing
          image = None
        if image is not None:
          content_type = image.getContentType()
          mimetype_list = getToolByName(self.context,
                                        'mimetypes_registry').lookup(content_type)
          #Need to improve default format handling
          format = 'png'
          if mimetype_list:
            format = mimetype_list[0].minor()
          try:
            #ERP5 API
            data = image.getData()
            height = image.getHeight()
            width = image.getWidth()
          except AttributeError:
            #OFS API
            data = image.data
            height = image.height
            width = image.width
          if height:
            frame.setNsProp(svg_ns, 'height', '%.3fcm' % (height * ratio_px_cm))
          if width:
            frame.setNsProp(svg_ns, 'width', '%.3fcm' % (width * ratio_px_cm))
          new_path = builder.addImage(data, format=format)
          href_attribute.setContent(new_path)
    builder.replace('content.xml', xml_doc.serialize('utf-8', 0))
    xml_doc.freeDoc()
    return builder.render()

  def includeExternalCssList(self, data):
    """
      Replace external Css link by style Element
    """
    try:
      xml_doc = parseDoc(data)
    except parserError:
      #If not valid xhtml do nothing
      return data
    xpath = '//*[local-name() = "link"][@type = "text/css"]'
    css_link_tag_list = xml_doc.xpathEval(xpath)
    for css_link_tag in css_link_tag_list:
      #Try to get css from ZODB
      href_attribute_list = css_link_tag.xpathEval('.//@href')
      href_attribute = href_attribute_list[0]
      url = href_attribute.get_content()
      matching = re.match(REMOTE_URL_PATTERN, url)
      if matching is not None:
        path = matching.groupdict().get('path')
        try:
          css = self.context.restrictedTraverse(path)
        except AttributeError:
          #Image not found, this image is probably not hosted by ZODB. Do nothing
          css = None
        if css is not None:
          css_as_text = css(client=self.context.getPortalObject())
          style_node = xml_doc.newChild(None, 'style', css_as_text)
          style_node.setProp('type', 'text/css')
          css_link_tag.replaceNode(style_node)
    #omit xml-declaration
    data = xml_doc.serialize('utf-8', 0)\
                     .replace('<?xml version="1.0" encoding="utf-8"?>\n', '')
    xml_doc.freeDoc()
    return data

155 156 157 158 159 160 161 162 163 164 165 166
  def convert(self):
    tmp_ooo = newTempOOoDocument(self.context, self.name)
    tmp_ooo.edit( base_data=self.data,
                  fname=self.name,
                  source_reference=self.name,
                  base_content_type=self.mimetype,)
    tmp_ooo.oo_data = self.data
    self.ooo = tmp_ooo

  def convertTo(self, format):
    if self.ooo.isTargetFormatAllowed(format):
      mime, data = self.ooo.convert(format)
167 168 169 170 171
      if import_succeed and self.mimetype == 'text/html':
        builder = OOoBuilder(data)
        content = builder.extract('content.xml')
        xml_doc = parseDoc(content)
        data = self.includeImageList(data)
172 173
      return data
    else:
174
      raise TransformError