def _getHandler(self, instance):
     """
     _getHandler(self, instance) => get the handler object
     """
     handler = AttachmentHandler.getAttachmentHandler(
         self.getContentType(instance, ),
         self, instance,
         )
     return handler
if sys.platform == "win32":
    program = "xlhtml.exe"
else:
    program = "xlhtml"

from Products.AttachmentField.global_symbols import MAX_ROWS_EXCEL
from Products.AttachmentField.global_symbols import MAX_COLS_EXCEL

class MSExcelAttachment(AttachmentHandler.AbstractHandler):
    """
    MSExcelAttachment file abstraction
    """
    icon_file = "excel.gif"
    small_icon_file = "excel_small.gif"
    content_types = ('application/excel', 'application/x-excel', 'application/excel', 'application/x-msexcel', 'application/vnd.ms-excel', )
    converter_type = "MSExcel"

    is_external_conv = True
    is_working = True
    index_path = program
    index_arguments = r"-xc:0-%d -xr:0-%d"%(MAX_COLS_EXCEL, MAX_ROWS_EXCEL) + r" %s"
    index_encoding = 'utf8'

    preview_path = program
    preview_arguments = r"-nh -xc:0-%d -xr:0-%d"%(MAX_COLS_EXCEL, MAX_ROWS_EXCEL) + r" %s"
    preview_format = "html"
    preview_encoding = 'utf8'


AttachmentHandler.registerHandler(MSExcelAttachment)
    content_types = (
        'application/vnd.oasis.opendocument.chart',
        'application/vnd.oasis.opendocument.formula',
        'application/vnd.oasis.opendocument.graphics',
        'application/vnd.oasis.opendocument.image',
        'application/vnd.oasis.opendocument.text-master',
        'application/vnd.oasis.opendocument.presentation',
        'application/vnd.oasis.opendocument.spreadsheet',
        'application/vnd.oasis.opendocument.text',
        'application/vnd.oasis.opendocument.graphics-template',
        'application/vnd.oasis.opendocument.text-web',
        'application/vnd.oasis.opendocument.presentation-template',
        'application/vnd.oasis.opendocument.spreadsheet-template',
        'application/vnd.oasis.opendocument.text-template',
        )

    is_external_conv = False
    is_working = False
    index_path = None
    index_arguments = None
    index_encoding = None

    preview_path = None
    preview_arguments = None
    preview_encoding = None
    preview_format = None



AttachmentHandler.registerHandler(OO2Attachment)
##        """
##        # Perform conversion and retreive files as well
##        fn = self.writeAttachmentFile()
##        try:
##            # Actual HTML conversion
##            ret = self.callConverter(arguments = "-d %s %s" % (self.getAttachmentFileDir(), fn, ))

##            # Retreive images
##            Log(LOG_DEBUG, "Temporary files:", os.listdir(self.getAttachmentFileDir()))
##            for fn in os.listdir(self.getAttachmentFileDir()):
##                ext = string.lower(string.split(fn, ".")[-1])
##                if ext in ('png', 'jpg', 'gif', 'wmz', 'wmf', 'tif', 'tiff', 'jpeg', ):
##                    filename = os.path.join(self.getAttachmentFileDir(), fn,)
##                    img = open(filename, "rb")
##                    self.addRelatedFile(fn, img.read(), "image/%s" % ext)
##                    Log(LOG_DEBUG, "Filename to delete", filename)

##                    # Close and delete file
##                    # We ignore temp file deletion problems
##                    img.close()
##                    try:        os.unlink(filename)
##                    except:     LogException()

##        finally:
##            self.deleteAttachmentFile()

##        return self.stripBody(ret)


AttachmentHandler.registerHandler(MSWordAttachment)
    def _old_testFileIndexing(self, file, handler, output_encoding):
        """
        testFileIndexing(self, file, handler, output_encoding) => dict
        the dict keys are :
          - content_type: MIME type
          - content_length: length
          - filename: filename (!)
          - raw_content: file.read()
          - index: indexable value
          - preview: html preview
          - encoding: same as output_encoding
        handler id and output encoding must be provided.
        """
        import AttachmentHandler
        filename = file.filename
        content = file.read()
        content_type = string.lower(file.headers['Content-Type'])
        ret = {
            "content_type": content_type,
            "content_length": len(content),
            "filename": filename,
            "raw_content": content,
            "handler": "(unknown)",
            "handler_mime": "(unknown)",
            "index": "(unavailable)",
            "index_sample": "(unavailable)",
            "preview": "(unavailable)",
            "encoding": output_encoding,
            }
        handlers = AttachmentHandler.__HANDLERS__
        hnd = handler
        name = None
        mime = None

        if hnd:
            # Specific handler : get it
            for klass, handler in handlers:
                name = klass
                if name == hnd:
                    break
        else:
            # Try to guess the handler according to content-type
            handler = AttachmentHandler.getAttachmentHandler(
                content_type, None, self,
                )
            name = handler.__class__.__name__
            mime = handler.content_types

        # Ok, we've found the right handler
        ret["handler_mime"] = mime
        ret["handler"] = name
        try:
            # use the converter
            index = handler.convertStringToIndex(content, content_type, self)

            # Provide a more useable presentation
            index_sample = index[:INDEX_SAMPLE_LENGTH]
            index = "%d words." % (
                len(string.split(index)),
                )

        except:
            s = StringIO.StringIO()
            traceback.print_exc(file = s, )
            s.seek(0)
            index = s.read()
            index_sample = ""

        try:
            preview = handler.convertStringToPreview(content, content_type, self)
            preview = preview.encode(output_encoding, "replace")

        except:
            s = StringIO.StringIO()
            traceback.print_exc(file = s, )
            s.seek(0)
            preview = s.read()

        ret['index'] = index
        ret['index_sample'] = index_sample
        ret['preview'] = preview

        return ret
else:
    program = "unrtf"


class RTFAttachment(AttachmentHandler.AbstractHandler):
    """
    RTF attachment file abstraction
    """
    icon_file = "text.gif"
    small_icon_file = "text_small.gif"
    content_types = (
        'application/rtf',
        'text/richtext',
        'text/rtf',
        )
    converter_type = "RTF"

    is_external_conv = True
    is_working = True
    index_path = program
    index_arguments = r"%s --nopict -t text"
    index_encoding = ("CP1252", "utf8", "latin1", )

    preview_path = program
    preview_arguments = r"%s --nopict -t html"
    preview_format = "html"
    preview_encoding = ("CP1252", "utf8", "latin1", )


AttachmentHandler.registerHandler(RTFAttachment)
    program = None
else:
    program = "tar"


class TarGzAttachment(CompressedAttachment.CompressedAttachment):
    """
    Tar / compressed file abstraction
    """
    converter_type = "TarGz"
    content_types = (
        'application/x-tar',
        'application/x-gtar',
        'application/tgz',
        )

    is_external_conv = True
    is_working = True
    index_path = program
    index_arguments = r'-tzvf %s'
    index_encoding = ('iso-8859-15', 'utf8')

    preview_path = program
    preview_arguments = r'-tzvf %s'
    preview_encoding = ('iso-8859-15', 'utf8')
    preview_format = 'pre'



AttachmentHandler.registerHandler(TarGzAttachment)
class AudioAttachment(AttachmentHandler.AbstractHandler):
    """
    Audio file abstraction
    """
    converter_type = "Audio"
    icon_file = "audio.gif"
    small_icon_file = "audio_small.gif"
    content_types = (
        'audio/mpeg',
        'audio/x-wav',
        'audio/x-pn-realaudio',
        "audio/x-realaudio",
        "audio/x-mpegurl",
        )


    is_external_conv = False
    is_working = False
    index_path = None
    index_arguments = None
    index_encoding = None

    preview_path = None
    preview_arguments = None
    preview_encoding = None
    preview_format = None



AttachmentHandler.registerHandler(AudioAttachment)
from global_symbols import *

import os
import string
import AttachmentHandler

class MSProjectAttachment(AttachmentHandler.AbstractHandler):
    """
    MSProject file abstraction
    """
    converter_type = "MSProject"
    icon_file = "msproject.gif"
    small_icon_file = "msproject_small.gif"
    content_types = ('application/msproject', 'application/vnd.ms-project', )

    is_external_conv = False
    is_working = False
    index_path = None
    index_arguments = None
    index_encoding = None

    preview_path = None
    preview_arguments = None
    preview_encoding = None
    preview_format = None



AttachmentHandler.registerHandler(MSProjectAttachment)
from global_symbols import *

import AttachmentHandler

attach_globals = globals()

class DummyAttachment(AttachmentHandler.AbstractHandler):
    """
    ZDummyAttachment -> This class simulates an empty attachment so that ValueErrors won't be called whenever
    ZAttachmentAttribute's __underlyingFile__ will be None...
    """
    # attachment properties (MUST BE DERIVED)
    icon_file = "unknown.gif"
    small_icon_file = "unknown_small.gif"
    content_types = (None, )
    converter_type = None

    is_external_conv = False
    is_working = False
    index_path = None
    index_arguments = None
    index_encoding = None

    preview_path = None
    preview_arguments = None
    preview_encoding = None
    preview_format = None

AttachmentHandler.registerHandler(DummyAttachment)
##raise "test", AttachmentHandler.__HANDLERS__

if sys.platform == "win32":
    program = "pstotext.exe"
else:
    program = "pstotext"


class PSAttachment(AttachmentHandler.AbstractHandler):
    """
    Postscript attachment file abstraction
    """
    icon_file = "pdf.gif"
    small_icon_file = "pdf_small.gif"
    content_types = ('application/postscript', )
    converter_type = "PS"

    is_external_conv = True
    is_working = True
    index_path = program
    index_arguments = '%s'
    index_encoding = "iso-8859-15"

    preview_path = program
    preview_arguments = '%s'
    preview_format = "text"
    preview_encoding = "iso-8859-15"


AttachmentHandler.registerHandler(PSAttachment)
    converter_type = "Compressed"
    icon_file = "zip.gif"
    small_icon_file = "zip_small.gif"
    content_types = (
        'application/sit',
        'application/x-stuffit',
        'application/gtar',
        'application/tar',
        'application/gz',
        'application/x-gzip',
        'application/x-gzip-compressed',
        'application/x-rar-compressed',
        )


    is_external_conv = False
    is_working = False
    index_path = None
    index_arguments = None
    index_encoding = None
    index_path = None

    preview_path = None
    preview_arguments = None
    preview_encoding = None
    preview_format = None



AttachmentHandler.registerHandler(CompressedAttachment)
import string
import AttachmentHandler

class PhotoshopAttachment(AttachmentHandler.AbstractHandler):
    """
    Photoshop file abstraction
    """
    converter_type = "Photoshop"
    icon_file = "psd.gif"
    small_icon_file = "psd_small.gif"
    content_types = ("image/psd",
                     "image/photoshop",
                     "image/x-photoshop",
                     "application/psd",
                     "application/photoshop",
                     "application/x-photoshop",
                     "zz-application/zz-winassoc-psd",)

    is_external_conv = False
    is_working = False
    index_path = None
    index_arguments = None
    index_encoding = None

    preview_path = None
    preview_arguments = None
    preview_encoding = None
    preview_format = None

AttachmentHandler.registerHandler(PhotoshopAttachment)
if sys.platform == "win32":
    program = "type"
else:
    program = "cat"



class TextAttachment(AttachmentHandler.AbstractHandler):
    """
    TextAttachment file abstraction
    """
    icon_file = "text.gif"
    small_icon_file = "text_small.gif"
    content_types = ('text/plain', )
    converter_type = "Text"

    is_external_conv = True
    is_working = True
    index_path = program
    index_arguments = r"%s"
    index_encoding = None

    preview_path = program
    preview_arguments = r"%s"
    preview_encoding = None
    preview_format = "text"


AttachmentHandler.registerHandler(TextAttachment)
import sys

if sys.platform == "win32":
    program = "ppthtml.exe"
else:
    program = "ppthtml"


class MSPowerpointAttachment(AttachmentHandler.AbstractHandler):
    """
    MSPowerpointAttachment file abstraction
    """
    icon_file = "powerpoint.gif"
    small_icon_file = "powerpoint_small.gif"
    content_types = ('application/powerpoint', 'application/mspowerpoint', 'application/x-powerpoint', 'application/x-mspowerpoint', 'application/vnd.ms-powerpoint', )
    converter_type = "MSPowerpoint"

    is_external_conv = True
    is_working = True
    index_path = program
    index_arguments = r"%s"
    index_encoding = 'utf8'

    preview_path = program
    preview_arguments = r"%s"
    preview_encoding = 'utf8'
    preview_format = "html"


AttachmentHandler.registerHandler(MSPowerpointAttachment)
from global_symbols import *

import os
import string
import AttachmentHandler

class FlashAttachment(AttachmentHandler.AbstractHandler):
    """
    Flash file abstraction
    """
    converter_type = "Flash"
    icon_file = "flash.gif"
    small_icon_file = "flash_small.gif"
    content_types = ('application/x-shockwave-flash','application/swf')

    is_external_conv = False
    is_working = False
    index_path = None
    index_arguments = None
    index_encoding = None

    preview_path = None
    preview_arguments = None
    preview_encoding = None
    preview_format = None



AttachmentHandler.registerHandler(FlashAttachment)
if sys.platform == "win32":
    program = None
else:
    program = "zipinfo"


class ZipAttachment(CompressedAttachment.CompressedAttachment):
    """
    Zip / compressed file abstraction
    """
    converter_type = "Zip"
    content_types = (
        'application/zip',
        'application/x-zip-compressed',
        )

    is_external_conv = True
    is_working = True
    index_path = program
    index_arguments = r'-2z %s'
    index_encoding = ('iso-8859-15', 'utf8')

    preview_path = program
    preview_arguments = r'-2zt %s'
    preview_encoding = ('iso-8859-15', 'utf8')
    preview_format = 'pre'



AttachmentHandler.registerHandler(ZipAttachment)
from global_symbols import *

import os
import string
import AttachmentHandler

class MSAccessAttachment(AttachmentHandler.AbstractHandler):
    """
    MSAccess file abstraction
    """
    converter_type = "MSAccess"
    icon_file = "access.gif"
    small_icon_file = "access_small.gif"
    content_types = ('application/msaccess', 'application/vnd.ms-access', )

    is_external_conv = False
    is_working = True
    index_path = None
    index_arguments = None
    index_encoding = None

    preview_path = None
    preview_arguments = None
    preview_encoding = None
    preview_format = None



AttachmentHandler.registerHandler(MSAccessAttachment)
class ImageAttachment(AttachmentHandler.AbstractHandler):
    """
    ImageAttachment file abstraction
    """
    icon_file = "image.gif"
    small_icon_file = "image_small.gif"
    content_types = (
        'image/gif',
        'image/jpeg',
        "image/jpg",
        "image/png",
        "image/xpm",
        "image/bmp",
        "image/x-windows-bmp",
        )
    converter_type = "Image"

    is_external_conv = False
    is_working = True
    index_path = None
    index_arguments = None
    index_encoding = None

    preview_path = None
    preview_arguments = None
    preview_encoding = None
    preview_format = None


AttachmentHandler.registerHandler(ImageAttachment)
    icon_file = "video.gif"
    small_icon_file = "video_small.gif"
    content_types = (
        "video/x-ms-wmv",
        "video/x-mpegurl",
        "video/mpeg",
        "video/quicktime",
        "video/vnd.vivo",
        "video/x-ms-asf",
        "video/x-ms-wm",
        "video/x-ms-wvx",
        "video/x-msvideo",
        "video/x-mx-wmx",
        "video/x-sgi-movie",
        )

    is_external_conv = False
    is_working = False
    index_path = None
    index_arguments = None
    index_encoding = None

    preview_path = None
    preview_arguments = None
    preview_encoding = None
    preview_format = None



AttachmentHandler.registerHandler(VideoAttachment)

if sys.platform == "win32":
    program = "type"
else:
    program = "cat"


class HTMLAttachment(AttachmentHandler.AbstractHandler):
    """
    MSWord file abstraction
    """
    icon_file = "html.gif"
    small_icon_file = "html_small.gif"
    content_types = ('text/html', )
    converter_type = "HTML"

    is_external_conv = True
    is_working = True
    index_path = program
    index_arguments = r"%s"
    index_encoding = None

    preview_path = program
    preview_arguments = r" %s"
    preview_encoding = None
    preview_format = "html"


AttachmentHandler.registerHandler(HTMLAttachment)
from global_symbols import *

import os
import string
import AttachmentHandler

class AutocadAttachment(AttachmentHandler.AbstractHandler):
    """
    Autocad file abstraction
    """
    converter_type = "Autocad"
    icon_file = "autocad.gif"
    small_icon_file = "autocad_small.gif"
    content_types = ('application/acad', 'application/dxf', )

    is_external_conv = False
    is_working = False
    index_path = None
    index_arguments = None
    index_encoding = None

    preview_path = None
    preview_arguments = None
    preview_encoding = None
    preview_format = None



AttachmentHandler.registerHandler(AutocadAttachment)
        content = field.get(instance)
        content_type = field.getContentType(instance)
        doc = openxmllib.openXmlDocument(content.data, self.content_types[0])
        return doc.indexableText().encode(instance.getCharset(), 'replace')

    def getPreview(self, field, instance):
        return None


class OpenXmlDocm(OpenXmlBaseAttachment):
    """OOffice Word 2007 XML macro enabled document"""

    filename_extension = 'docm'
    content_types = (ct.CT_WORDPROC_DOCM_PUBLIC,)

AttachmentHandler.registerHandler(OpenXmlDocm)

class OpenXmlDocx(OpenXmlBaseAttachment):
    """Office Word 2007 XML document"""

    filename_extension = 'docx'
    content_types = (ct.CT_WORDPROC_DOCX_PUBLIC,)

AttachmentHandler.registerHandler(OpenXmlDocx)

class OpenXmlDotm(OpenXmlBaseAttachment):
    """Office Word 2007 XML macro-enabled template"""

    filename_extension = 'dotm'
    content_types = (ct.CT_WORDPROC_DOTM_PUBLIC,)
        """Return the portal_transforms tool"""
        return instance.portal_transforms

    def getMimetypesRegistry(self, field, instance):
        """Return the mimetypes_registry tool"""
        return instance.mimetypes_registry

    def _getTransformPath(self, input, output):
        """
        _getTransformPath(self, input, output) => chain or None
        Try to build a transform chain from 'input' mime type to 'output' mime type.
        If it's not possible to build such a chain, return None.
        Nota: this code is taken from TransformEngine.py
        """
        ## get a path to output mime type
        transform = self.getPortalTransforms()
        requirements = transform._policies.get(target_mt, [])
        path = transform._findPath(orig_mt, target_mt, list(requirements))

        if not path and requirements:
            LOG.debug("Unable to satisfy requirements %s" % (", ".join(requirements),))
            path = transform._findPath(orig_mt, target_mt)

        if not path:
            LOG.debug("NO PATH FROM %s TO %s : %s" % (orig_mt, target_mimetype, path))
            return None
        return path


AttachmentHandler.registerHandler(PortalTransformsAttachment)