#
# -*- coding: utf-8 -*-

'''XSLT abstraction layer
'''

############################################################################################################
#
#   IMPORTS
#
#===========================================================================================================

import logging
log = logging.getLogger("biocase.tools")
from Ft.Xml import InputSource, CreateInputSource
from Ft.Xml.Xslt import Processor

############################################################################################################
#
#   GENERAL FUNCTIONS
#
#===========================================================================================================

def transformXML(xmlString, xslLoc, paras={}):
    src_isrc = CreateInputSource(xmlString)
    sty_isrc = CreateInputSource(xslLoc)
    
    proc = Processor.Processor()
    proc.appendStylesheet(sty_isrc)
    log.info("APPLY STYLESHEET %s"%xslLoc)
    try:
        result = proc.run(src_isrc, topLevelParams=paras)
        log.info("  OK")
    except:
        log.exception("ERROR when applying the stylesheet")
        result = 'XSLT NOT WORKING !!!'
    return result


if __name__ ==  "__main__":
    import logging
    logging.basicConfig(filename='/Users/markus/Desktop/test.log')
    #XSL = 'http://cvs.4suite.org/viewcvs/*checkout*/4Suite/Ft/Data/identity.xslt'
    #XML = 'http://rs.tdwg.org/dwc/tdwg_dw_core.xsd'
    XSL='/Users/markus/Sites/biocase/configuration/querytool/skins/default/en_ABCD2.0_DataSetOverview_short.xslt'
    XML=file('/Users/markus/Desktop/pywrapper.cgi2.xml').read()
    print XML
    print "-"*80
    print "-"*80
    print "-"*80
    print transformXML(XML, XSL)
    