/*************************************************************************
 *
 *  $RCSfile: dynload.hxx,v $
 *
 *  $Revision: 1.1 $
 *
 *  last change: $Author: dbo $ $Date: 2002/12/02 12:41:18 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/
#if ! defined INCLUDED_XMLSCRIPT_DYNLOAD_HXX
#define INCLUDED_XMLSCRIPT_DYNLOAD_HXX

#include <osl/module.h>

#include <xmlscript/xmldlg_imexp.hxx>
#include <xmlscript/xmlmod_imexp.hxx>
#include <xmlscript/xmllib_imexp.hxx>

#include <tools/solar.h> // for library name macros


namespace xmlscript
{

//==================================================================================================
struct XML_script
{
    // xmldlg_imexp.hxx
    virtual void SAL_CALL exportDialogModel(
        ::com::sun::star::uno::Reference<
        ::com::sun::star::xml::sax::XExtendedDocumentHandler > const & xOut,
        ::com::sun::star::uno::Reference<
        ::com::sun::star::container::XNameContainer > const & xDialogModel )
        SAL_THROW( (::com::sun::star::uno::Exception) ) = 0;
    virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler >
    SAL_CALL importDialogModel(
        ::com::sun::star::uno::Reference<
        ::com::sun::star::container::XNameContainer > const & xDialogModel,
        ::com::sun::star::uno::Reference<
        ::com::sun::star::uno::XComponentContext > const & xContext )
        SAL_THROW( (::com::sun::star::uno::Exception) ) = 0;
    virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStreamProvider >
    SAL_CALL exportDialogModel(
        ::com::sun::star::uno::Reference<
        ::com::sun::star::container::XNameContainer > const & xDialogModel,
        ::com::sun::star::uno::Reference<
        ::com::sun::star::uno::XComponentContext > const & xContext )
        SAL_THROW( (::com::sun::star::uno::Exception) ) = 0;
    virtual void SAL_CALL importDialogModel(
        ::com::sun::star::uno::Reference<
        ::com::sun::star::io::XInputStream > xInput,
        ::com::sun::star::uno::Reference<
        ::com::sun::star::container::XNameContainer > const & xDialogModel,
        ::com::sun::star::uno::Reference<
        ::com::sun::star::uno::XComponentContext > const & xContext )
        SAL_THROW( (::com::sun::star::uno::Exception) ) = 0;
    // xmlmod_imexp.hxx
    virtual void SAL_CALL exportScriptModule(
        ::com::sun::star::uno::Reference<
        ::com::sun::star::xml::sax::XExtendedDocumentHandler > const & xOut,
        const ModuleDescriptor& rMod )
		SAL_THROW( (::com::sun::star::uno::Exception) ) = 0;
    virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler >
    SAL_CALL importScriptModule(
        ModuleDescriptor& rMod )
        SAL_THROW( (::com::sun::star::uno::Exception) ) = 0;
    // xmllib_imexp.hxx
    virtual void SAL_CALL exportLibraryContainer(
        ::com::sun::star::uno::Reference<
        ::com::sun::star::xml::sax::XExtendedDocumentHandler > const & xOut,
        const LibDescriptorArray* pLibArray )
		SAL_THROW( (::com::sun::star::uno::Exception) ) = 0;
    virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler >
    SAL_CALL importLibraryContainer(
        LibDescriptorArray* pLibArray )
		SAL_THROW( (::com::sun::star::uno::Exception) ) = 0;
    virtual void SAL_CALL exportLibrary(
        ::com::sun::star::uno::Reference<
        ::com::sun::star::xml::sax::XExtendedDocumentHandler > const & xOut,
        const LibDescriptor& rLib )
		SAL_THROW( (::com::sun::star::uno::Exception) ) = 0;
    virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler >
    SAL_CALL importLibrary(
        LibDescriptor& rLib )
		SAL_THROW( (::com::sun::star::uno::Exception) ) = 0;
};

/** Use the below like:

    static ::xmlscript::XML_script * s_xmlscript = 0;
    if (0 == s_xmlscript)
        s_xmlscript = ::xmlscript::getXmlScript();
*/
inline ::xmlscript::XML_script * getXmlScript()
{
    ::rtl::OUString lib_name( RTL_CONSTASCII_USTRINGPARAM(SVLIBRARY("xcr")) );
    oslModule module =
        osl_loadModule( lib_name.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
    if (0 == module)
    {
        throw ::com::sun::star::uno::RuntimeException(
            ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("cannot load library: ") ) + lib_name,
            ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
    }
    ::rtl::OUString symbol_name( RTL_CONSTASCII_USTRINGPARAM("xmlscript_get") );
    typedef XML_script * (SAL_CALL * fptr_xmlscript_get)();
    fptr_xmlscript_get func =
        (fptr_xmlscript_get)osl_getSymbol( module, symbol_name.pData );
    if (0 == func)
    {
        throw ::com::sun::star::uno::RuntimeException(
            ::rtl::OUString(
                RTL_CONSTASCII_USTRINGPARAM(
                    "cannot get symbol \"xmlscript_get\" out of library ") ) + lib_name,
            ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
    }
    XML_script * ret = (*func)();
    if (0 == func)
    {
        throw ::com::sun::star::uno::RuntimeException(
            ::rtl::OUString(
                RTL_CONSTASCII_USTRINGPARAM("cannot get access to xmlscript functions!") ),
            ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
    }
    return ret;
}

}

#endif
