"""
$RCSfile: Generators.py,v $

Default generators for result objects

Author: Philipp von Weitershausen <philikon@philikon.de>

$Id: Generators.py,v 1.2 2003/04/29 09:22:08 philikon Exp $
"""

from zope.interface import implements
from interfaces import IGenerator
from GeneratorRegistry import GeneratorRegistry

class DTMLMethodGenerator:

    implements(IGenerator)

    def createObject(self, id, title, rawString, **kw):
        from OFS.DTMLMethod import DTMLMethod
        resultObj = DTMLMethod(rawString)
        resultObj.id = id
        resultObj.title  = title
        for name, value in kw.items():
            setattr(resultObj, name, value)
        return resultObj

    def getResult(self, obj, client, REQUEST=None, RESPONSE=None):
        return obj.__of__(client)(client, REQUEST, RESPONSE)

DTMLMethodGenerator = DTMLMethodGenerator()
GeneratorRegistry.register("DTML Method", DTMLMethodGenerator)

class DTMLDocumentGenerator:

    implements(IGenerator)

    def createObject(self, id, title, rawString, **kw):
        from OFS.DTMLDocument import DTMLDocument
        resultObj = DTMLDocument(rawString)
        resultObj.id = id
        resultObj.title  = title
        for name, value in kw.items():
            setattr(resultObj, name, value)
        return resultObj

    def getResult(self, obj, client, REQUEST=None, RESPONSE=None):
        return obj.__of__(client)(client, REQUEST, RESPONSE)

DTMLDocumentGenerator = DTMLDocumentGenerator()
GeneratorRegistry.register("DTML Document", DTMLDocumentGenerator)

class FileGenerator:

    implements(IGenerator)

    def createObject(self, id, title, rawString, **kw):
        from OFS.Image import File
        content_type = kw.get("content_type", "text/plain")
        resultObj = File(id, title, rawString, content_type)
        for name, value in kw.items():
            setattr(resultObj, name, value)
        return resultObj

    def getResult(self, obj, client, REQUEST=None, RESPONSE=None):
        return obj.__of__(client).index_html(REQUEST, RESPONSE)

FileGenerator = FileGenerator()
GeneratorRegistry.register("File", FileGenerator)

class ZPTGenerator:

    implements(IGenerator)

    def createObject(self, id, title, rawString, **kw):
        from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
        content_type = kw.get("content_type", "text/html")
        resultObj = ZopePageTemplate(id, rawString, content_type)
        resultObj.pt_setTitle(title)
        for name, value in kw.items():
            setattr(resultObj, name, value)
        return resultObj

    def getResult(self, obj, client, REQUEST=None, RESPONSE=None):
        return obj.__of__(client)(client, REQUEST, RESPONSE)

ZPTGenerator = ZPTGenerator()
try:
    import Products.PageTemplates
    GeneratorRegistry.register("Page Template", ZPTGenerator)
except ImportError:
    pass

class ParsedXMLGenerator:

    implements(IGenerator)

    def createObject(self, id, title, rawString, **kw):
        from Products.ParsedXML.ParsedXML import ParsedXML
        content_type = kw.get("content_type", "text/xml")
        resultObj = ParsedXML(id, rawString, contentType = content_type)
        return resultObj

    def getResult(self, obj, client, REQUEST=None, RESPONSE=None):
        return obj.__of__(client).index_html(REQUEST, RESPONSE)

ParsedXMLGenerator = ParsedXMLGenerator()
try:
    import Products.ParsedXML
    GeneratorRegistry.register("Parsed XML", ParsedXMLGenerator)
except ImportError:
    pass
