<commit_before>/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org 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 version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_dbaccess.hxx"
#include "TokenWriter.hxx"
#include <tools/debug.hxx>
#include <tools/diagnose_ex.h>
#include <osl/diagnose.h>
#include "RtfReader.hxx"
#include "HtmlReader.hxx"
#include "dbustrings.hrc"
#include <connectivity/dbtools.hxx>
#include <comphelper/types.hxx>
#include <com/sun/star/sdbc/XConnection.hpp>
#include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
#include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
#include <com/sun/star/sdbc/XRowSet.hpp>
#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
#include <com/sun/star/sdb/XQueriesSupplier.hpp>
#include <com/sun/star/sdbc/XDataSource.hpp>
#include <com/sun/star/awt/FontWeight.hpp>
#include <com/sun/star/awt/FontStrikeout.hpp>
#include <com/sun/star/awt/FontSlant.hpp>
#include <com/sun/star/awt/FontUnderline.hpp>
#include <com/sun/star/document/XDocumentProperties.hpp>
#include <svtools/htmlkywd.hxx>
#include <svtools/rtfkeywd.hxx>
#include <tools/color.hxx>
#include <svtools/htmlout.hxx>
#include <sfx2/frmhtmlw.hxx>
#include <svl/numuno.hxx>
#include <vcl/svapp.hxx>
#include "UITools.hxx"
#include <toolkit/helper/vclunohelper.hxx>
#include <vcl/outdev.hxx>
#include <svtools/rtfout.hxx>
#include <svtools/htmlcfg.hxx>
#include <connectivity/formattedcolumnvalue.hxx>
#include <unotools/syslocale.hxx>
#include <comphelper/componentcontext.hxx>
#include <rtl/logfile.hxx>

using namespace dbaui;
using namespace dbtools;
using namespace svx;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::sdb;
using namespace ::com::sun::star::frame;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::sdbcx;
using namespace ::com::sun::star::awt;
using namespace ::com::sun::star::util;
using ::com::sun::star::frame::XModel;

#if defined(UNX)
const char ODatabaseImportExport::sNewLine = '\012';
#else
const char ODatabaseImportExport::sNewLine[] = "\015\012";
#endif

const static char sMyBegComment[]   = "<!-- ";
const static char sMyEndComment[]   = " -->";
const static char sFontFamily[]     = "font-family: ";
const static char sFontSize[]       = "font-size: ";

#define SBA_FORMAT_SELECTION_COUNT  4
#define CELL_X                      1437

DBG_NAME(ODatabaseImportExport)
//======================================================================
ODatabaseImportExport::ODatabaseImportExport(const ::svx::ODataAccessDescriptor& _aDataDescriptor,
                                             const Reference< XMultiServiceFactory >& _rM,
                                             const Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
                                             const String& rExchange)
    :m_bBookmarkSelection( sal_False )
    ,m_xFormatter(_rxNumberF)
    ,m_xFactory(_rM)
    ,m_nCommandType(CommandType::TABLE)
    ,m_bNeedToReInitialize(sal_False)
    ,m_pReader(NULL)
    ,m_pRowMarker(NULL)
    ,m_bInInitialize(sal_False)
    ,m_bCheckOnly(sal_False)
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::ODatabaseImportExport" );
    DBG_CTOR(ODatabaseImportExport,NULL);

    m_eDestEnc = osl_getThreadTextEncoding();

    osl_incrementInterlockedCount( &m_refCount );
    impl_initFromDescriptor( _aDataDescriptor, false );

    xub_StrLen nCount = rExchange.GetTokenCount(char(11));
    if( nCount > SBA_FORMAT_SELECTION_COUNT && rExchange.GetToken(4).Len())
    {
        m_pRowMarker = new sal_Int32[nCount-SBA_FORMAT_SELECTION_COUNT];
        for(xub_StrLen i=SBA_FORMAT_SELECTION_COUNT;i<nCount;++i)
            m_pRowMarker[i-SBA_FORMAT_SELECTION_COUNT] = rExchange.GetToken(i,char(11)).ToInt32();
    }
    osl_decrementInterlockedCount( &m_refCount );
}
// -----------------------------------------------------------------------------
// import data
ODatabaseImportExport::ODatabaseImportExport( const ::dbtools::SharedConnection& _rxConnection,
        const Reference< XNumberFormatter >& _rxNumberF, const Reference< XMultiServiceFactory >& _rM )
    :m_bBookmarkSelection( sal_False )
    ,m_xConnection(_rxConnection)
    ,m_xFormatter(_rxNumberF)
    ,m_xFactory(_rM)
    ,m_nCommandType(::com::sun::star::sdb::CommandType::TABLE)
    ,m_bNeedToReInitialize(sal_False)
    ,m_pReader(NULL)
    ,m_pRowMarker(NULL)
    ,m_bInInitialize(sal_False)
    ,m_bCheckOnly(sal_False)
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::ODatabaseImportExport" );
    DBG_CTOR(ODatabaseImportExport,NULL);
    m_eDestEnc = osl_getThreadTextEncoding();
    try
    {
        SvtSysLocale aSysLocale;
        m_aLocale = aSysLocale.GetLocaleData().getLocale();
    }
    catch(Exception&)
    {
    }
}
//-------------------------------------------------------------------
ODatabaseImportExport::~ODatabaseImportExport()
{
    DBG_DTOR(ODatabaseImportExport,NULL);
    acquire();

    dispose();

    if(m_pReader)
        m_pReader->release();
    delete m_pRowMarker;
}
// -----------------------------------------------------------------------------
void ODatabaseImportExport::dispose()
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::disposing" );
    DBG_CHKTHIS(ODatabaseImportExport,NULL);
    // remove me as listener
    Reference< XComponent >  xComponent(m_xConnection, UNO_QUERY);
    if (xComponent.is())
    {
        Reference< XEventListener> xEvt((::cppu::OWeakObject*)this,UNO_QUERY);
        xComponent->removeEventListener(xEvt);
    }
    m_xConnection.clear();

    ::comphelper::disposeComponent(m_xRow);

    m_xObject.clear();
    m_xResultSetMetaData.clear();
    m_xResultSet.clear();
    m_xRow.clear();
    m_xRowLocate.clear();
    m_xFormatter.clear();
}
// -----------------------------------------------------------------------------
void SAL_CALL ODatabaseImportExport::disposing( const EventObject& Source ) throw(::com::sun::star::uno::RuntimeException)
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::disposing" );
    DBG_CHKTHIS(ODatabaseImportExport,NULL);
    Reference<XConnection> xCon(Source.Source,UNO_QUERY);
    if(m_xConnection.is() && m_xConnection == xCon)
    {
        m_xConnection.clear();
        dispose();
        m_bNeedToReInitialize = true;
    }
}
// -----------------------------------------------------------------------------
void ODatabaseImportExport::initialize( const ODataAccessDescriptor& _aDataDescriptor )
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::initialize" );
    impl_initFromDescriptor( _aDataDescriptor, true );
}

// -----------------------------------------------------------------------------
void ODatabaseImportExport::impl_initFromDescriptor( const ODataAccessDescriptor& _aDataDescriptor, bool _bPlusDefaultInit)
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::impl_initFromDescriptor" );
    DBG_CHKTHIS(ODatabaseImportExport,NULL);
    if ( !_bPlusDefaultInit )
    {
        m_sDataSourceName = _aDataDescriptor.getDataSource();
        _aDataDescriptor[daCommandType] >>= m_nCommandType;
        _aDataDescriptor[daCommand]     >>= m_sName;
        // some additonal information
        if(_aDataDescriptor.has(daConnection))
        {
            Reference< XConnection > xPureConn( _aDataDescriptor[daConnection], UNO_QUERY );
            m_xConnection.reset( xPureConn, SharedConnection::NoTakeOwnership );
            Reference< XEventListener> xEvt((::cppu::OWeakObject*)this,UNO_QUERY);
            Reference< XComponent >  xComponent(m_xConnection, UNO_QUERY);
            if (xComponent.is() && xEvt.is())
                xComponent->addEventListener(xEvt);
        }

        if ( _aDataDescriptor.has( daSelection ) )
            _aDataDescriptor[ daSelection ] >>= m_aSelection;

        if ( _aDataDescriptor.has( daBookmarkSelection ) )
            _aDataDescriptor[ daBookmarkSelection ] >>= m_bBookmarkSelection;

        if ( _aDataDescriptor.has( daCursor ) )
        {
            _aDataDescriptor[ daCursor ] >>= m_xResultSet;
            m_xRowLocate.set( m_xResultSet, UNO_QUERY );
        }

        if ( m_aSelection.getLength() != 0 )
        {
            if ( !m_xResultSet.is() )
            {
                OSL_FAIL( "ODatabaseImportExport::impl_initFromDescriptor: selection without result set is nonsense!" );
                m_aSelection.realloc( 0 );
            }
        }

        if ( m_aSelection.getLength() != 0 )
        {
            if ( m_bBookmarkSelection && !m_xRowLocate.is() )
            {
                OSL_FAIL( "ODatabaseImportExport::impl_initFromDescriptor: no XRowLocate -> no bookmars!" );
                m_aSelection.realloc( 0 );
            }
        }
    }
    else
        initialize();

    try
    {
        SvtSysLocale aSysLocale;
        m_aLocale = aSysLocale.GetLocaleData().getLocale();
    }
    catch(Exception&)
    {
    }
}
// -----------------------------------------------------------------------------
void ODatabaseImportExport::initialize()
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::initialize" );
    DBG_CHKTHIS(ODatabaseImportExport,NULL);
    m_bInInitialize = sal_True;
    m_bNeedToReInitialize = false;

    if ( !m_xConnection.is() )
    {   // we need a connection
        OSL_ENSURE(m_sDataSourceName.getLength(),"There must be a datsource name!");
        Reference<XNameAccess> xDatabaseContext = Reference< XNameAccess >(m_xFactory->createInstance(SERVICE_SDB_DATABASECONTEXT), UNO_QUERY);
        Reference< XEventListener> xEvt((::cppu::OWeakObject*)this,UNO_QUERY);

        Reference< XConnection > xConnection;
        SQLExceptionInfo aInfo = ::dbaui::createConnection( m_sDataSourceName, xDatabaseContext, m_xFactory, xEvt, xConnection );
        m_xConnection.reset( xConnection );

        if(aInfo.isValid() && aInfo.getType() == SQLExceptionInfo::SQL_EXCEPTION)
            throw *static_cast<const SQLException*>(aInfo);
    }

    Reference<XNameAccess> xNameAccess;
    switch(m_nCommandType)
    {
        case CommandType::TABLE:
            {
                // only for tables
                Reference<XTablesSupplier> xSup(m_xConnection,UNO_QUERY);
                if(xSup.is())
                    xNameAccess = xSup->getTables();
            }
            break;
        case CommandType::QUERY:
            {
                Reference<XQueriesSupplier> xSup(m_xConnection,UNO_QUERY);
                if(xSup.is())
                    xNameAccess = xSup->getQueries();
            }
            break;
    }
    if(xNameAccess.is() && xNameAccess->hasByName(m_sName))
    {
        Reference<XPropertySet> xSourceObject;
        xNameAccess->getByName(m_sName) >>= m_xObject;
    }

    if(m_xObject.is())
    {
        try
        {
            if(m_xObject->getPropertySetInfo()->hasPropertyByName(PROPERTY_FONT))
                m_xObject->getPropertyValue(PROPERTY_FONT) >>= m_aFont;

            // the result set may be already set with the datadescriptor
            if ( !m_xResultSet.is() )
            {
                m_xResultSet.set( m_xFactory->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.RowSet")) ), UNO_QUERY );
                Reference< XPropertySet > xProp( m_xResultSet, UNO_QUERY_THROW );
                xProp->setPropertyValue( PROPERTY_ACTIVE_CONNECTION, makeAny( m_xConnection.getTyped() ) );
                xProp->setPropertyValue( PROPERTY_COMMAND_TYPE, makeAny( m_nCommandType ) );
                xProp->setPropertyValue( PROPERTY_COMMAND, makeAny( m_sName ) );
                Reference< XRowSet > xRowSet( xProp, UNO_QUERY );
                xRowSet->execute();
            }
            impl_initializeRowMember_throw();
        }
        catch(Exception& )
        {
            m_xRow = NULL;
            m_xResultSetMetaData = NULL;
            ::comphelper::disposeComponent(m_xResultSet);
            throw;
        }
    }
    if ( !m_aFont.Name.getLength() )
    {
        Font aApplicationFont = OutputDevice::GetDefaultFont(
            DEFAULTFONT_SANS_UNICODE,
            Application::GetSettings().GetUILanguage(),
            DEFAULTFONT_FLAGS_ONLYONE
        );
        m_aFont = VCLUnoHelper::CreateFontDescriptor( aApplicationFont );
    }

    m_bInInitialize = sal_False;
}
// -----------------------------------------------------------------------------
sal_Bool ODatabaseImportExport::Write()
{
    if ( m_bNeedToReInitialize )
    {
        if ( !m_bInInitialize )
            initialize();
    }
    return sal_True;
}
// -----------------------------------------------------------------------------
sal_Bool ODatabaseImportExport::Read()
{
    if ( m_bNeedToReInitialize )
    {
        if ( !m_bInInitialize )
            initialize();
    }
    return sal_True;
}
// -----------------------------------------------------------------------------
void ODatabaseImportExport::impl_initializeRowMember_throw()
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::impl_initializeRowMember_throw" );
    if ( !m_xRow.is() && m_xResultSet.is() )
    {
        m_xRow.set( m_xResultSet, UNO_QUERY );
        m_xRowLocate.set( m_xResultSet, UNO_QUERY );
        m_xResultSetMetaData = Reference<XResultSetMetaDataSupplier>(m_xRow,UNO_QUERY)->getMetaData();
        Reference<XColumnsSupplier> xSup(m_xResultSet,UNO_QUERY_THROW);
        m_xRowSetColumns.set(xSup->getColumns(),UNO_QUERY_THROW);
    }
}
//======================================================================
sal_Bool ORTFImportExport::Write()
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ORTFImportExport::Write" );
    ODatabaseImportExport::Write();
    (*m_pStream) << '{'     << OOO_STRING_SVTOOLS_RTF_RTF;
    (*m_pStream) << OOO_STRING_SVTOOLS_RTF_ANSI << ODatabaseImportExport::sNewLine;
    rtl_TextEncoding eDestEnc = RTL_TEXTENCODING_MS_1252;

    sal_Bool bBold          = ( ::com::sun::star::awt::FontWeight::BOLD     == m_aFont.Weight );
    sal_Bool bItalic        = ( ::com::sun::star::awt::FontSlant_ITALIC     == m_aFont.Slant );
    sal_Bool bUnderline     = ( ::com::sun::star::awt::FontUnderline::NONE  != m_aFont.Underline );
    sal_Bool bStrikeout     = ( ::com::sun::star::awt::FontStrikeout::NONE  != m_aFont.Strikeout );

    sal_Int32 nColor = 0;
    if(m_xObject.is())
        m_xObject->getPropertyValue(PROPERTY_TEXTCOLOR) >>= nColor;
    ::Color aColor(nColor);

    ByteString aFonts(String(m_aFont.Name),eDestEnc);
    if(!aFonts.Len())
    {
        String aName = Application::GetSettings().GetStyleSettings().GetAppFont().GetName();
        aFonts = ByteString (aName,eDestEnc);
    }
    ::rtl::OString aFormat("\\fcharset0\\fnil ");
    ByteString aFontNr;

    (*m_pStream)    << "{\\fonttbl";
    xub_StrLen nTokenCount = aFonts.GetTokenCount();
    for(xub_StrLen j=0;j<nTokenCount;++j)
    {
        (*m_pStream) << "\\f";
        m_pStream->WriteNumber(static_cast<sal_Int32>(j));
        (*m_pStream) << aFormat;
        (*m_pStream) << aFonts.GetToken(j).GetBuffer();
        (*m_pStream) << ';';
    }<commit_msg>fix tinderboxes<commit_after>/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org 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 version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_dbaccess.hxx"
#include "TokenWriter.hxx"
#include <tools/debug.hxx>
#include <tools/diagnose_ex.h>
#include <osl/diagnose.h>
#include "RtfReader.hxx"
#include "HtmlReader.hxx"
#include "dbustrings.hrc"
#include <connectivity/dbtools.hxx>
#include <comphelper/types.hxx>
#include <com/sun/star/sdbc/XConnection.hpp>
#include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
#include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
#include <com/sun/star/sdbc/XRowSet.hpp>
#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
#include <com/sun/star/sdb/XQueriesSupplier.hpp>
#include <com/sun/star/sdbc/XDataSource.hpp>
#include <com/sun/star/awt/FontWeight.hpp>
#include <com/sun/star/awt/FontStrikeout.hpp>
#include <com/sun/star/awt/FontSlant.hpp>
#include <com/sun/star/awt/FontUnderline.hpp>
#include <com/sun/star/document/XDocumentProperties.hpp>
#include <svtools/htmlkywd.hxx>
#include <svtools/rtfkeywd.hxx>
#include <tools/color.hxx>
#include <svtools/htmlout.hxx>
#include <sfx2/frmhtmlw.hxx>
#include <svl/numuno.hxx>
#include <vcl/svapp.hxx>
#include "UITools.hxx"
#include <toolkit/helper/vclunohelper.hxx>
#include <vcl/outdev.hxx>
#include <svtools/rtfout.hxx>
#include <svtools/htmlcfg.hxx>
#include <connectivity/formattedcolumnvalue.hxx>
#include <unotools/syslocale.hxx>
#include <comphelper/componentcontext.hxx>
#include <rtl/logfile.hxx>

using namespace dbaui;
using namespace dbtools;
using namespace svx;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::sdb;
using namespace ::com::sun::star::frame;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::sdbcx;
using namespace ::com::sun::star::awt;
using namespace ::com::sun::star::util;
using ::com::sun::star::frame::XModel;

#if defined(UNX)
const char ODatabaseImportExport::sNewLine = '\012';
#else
const char ODatabaseImportExport::sNewLine[] = "\015\012";
#endif

const static char sMyBegComment[]   = "<!-- ";
const static char sMyEndComment[]   = " -->";
const static char sFontFamily[]     = "font-family: ";
const static char sFontSize[]       = "font-size: ";

#define SBA_FORMAT_SELECTION_COUNT  4
#define CELL_X                      1437

DBG_NAME(ODatabaseImportExport)
//======================================================================
ODatabaseImportExport::ODatabaseImportExport(const ::svx::ODataAccessDescriptor& _aDataDescriptor,
                                             const Reference< XMultiServiceFactory >& _rM,
                                             const Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
                                             const String& rExchange)
    :m_bBookmarkSelection( sal_False )
    ,m_xFormatter(_rxNumberF)
    ,m_xFactory(_rM)
    ,m_nCommandType(CommandType::TABLE)
    ,m_bNeedToReInitialize(sal_False)
    ,m_pReader(NULL)
    ,m_pRowMarker(NULL)
    ,m_bInInitialize(sal_False)
    ,m_bCheckOnly(sal_False)
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::ODatabaseImportExport" );
    DBG_CTOR(ODatabaseImportExport,NULL);

    m_eDestEnc = osl_getThreadTextEncoding();

    osl_incrementInterlockedCount( &m_refCount );
    impl_initFromDescriptor( _aDataDescriptor, false );

    xub_StrLen nCount = rExchange.GetTokenCount(char(11));
    if( nCount > SBA_FORMAT_SELECTION_COUNT && rExchange.GetToken(4).Len())
    {
        m_pRowMarker = new sal_Int32[nCount-SBA_FORMAT_SELECTION_COUNT];
        for(xub_StrLen i=SBA_FORMAT_SELECTION_COUNT;i<nCount;++i)
            m_pRowMarker[i-SBA_FORMAT_SELECTION_COUNT] = rExchange.GetToken(i,char(11)).ToInt32();
    }
    osl_decrementInterlockedCount( &m_refCount );
}
// -----------------------------------------------------------------------------
// import data
ODatabaseImportExport::ODatabaseImportExport( const ::dbtools::SharedConnection& _rxConnection,
        const Reference< XNumberFormatter >& _rxNumberF, const Reference< XMultiServiceFactory >& _rM )
    :m_bBookmarkSelection( sal_False )
    ,m_xConnection(_rxConnection)
    ,m_xFormatter(_rxNumberF)
    ,m_xFactory(_rM)
    ,m_nCommandType(::com::sun::star::sdb::CommandType::TABLE)
    ,m_bNeedToReInitialize(sal_False)
    ,m_pReader(NULL)
    ,m_pRowMarker(NULL)
    ,m_bInInitialize(sal_False)
    ,m_bCheckOnly(sal_False)
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::ODatabaseImportExport" );
    DBG_CTOR(ODatabaseImportExport,NULL);
    m_eDestEnc = osl_getThreadTextEncoding();
    try
    {
        SvtSysLocale aSysLocale;
        m_aLocale = aSysLocale.GetLocaleData().getLocale();
    }
    catch(Exception&)
    {
    }
}
//-------------------------------------------------------------------
ODatabaseImportExport::~ODatabaseImportExport()
{
    DBG_DTOR(ODatabaseImportExport,NULL);
    acquire();

    dispose();

    if(m_pReader)
        m_pReader->release();
    delete m_pRowMarker;
}
// -----------------------------------------------------------------------------
void ODatabaseImportExport::dispose()
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::disposing" );
    DBG_CHKTHIS(ODatabaseImportExport,NULL);
    // remove me as listener
    Reference< XComponent >  xComponent(m_xConnection, UNO_QUERY);
    if (xComponent.is())
    {
        Reference< XEventListener> xEvt((::cppu::OWeakObject*)this,UNO_QUERY);
        xComponent->removeEventListener(xEvt);
    }
    m_xConnection.clear();

    ::comphelper::disposeComponent(m_xRow);

    m_xObject.clear();
    m_xResultSetMetaData.clear();
    m_xResultSet.clear();
    m_xRow.clear();
    m_xRowLocate.clear();
    m_xFormatter.clear();
}
// -----------------------------------------------------------------------------
void SAL_CALL ODatabaseImportExport::disposing( const EventObject& Source ) throw(::com::sun::star::uno::RuntimeException)
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::disposing" );
    DBG_CHKTHIS(ODatabaseImportExport,NULL);
    Reference<XConnection> xCon(Source.Source,UNO_QUERY);
    if(m_xConnection.is() && m_xConnection == xCon)
    {
        m_xConnection.clear();
        dispose();
        m_bNeedToReInitialize = true;
    }
}
// -----------------------------------------------------------------------------
void ODatabaseImportExport::initialize( const ODataAccessDescriptor& _aDataDescriptor )
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::initialize" );
    impl_initFromDescriptor( _aDataDescriptor, true );
}

// -----------------------------------------------------------------------------
void ODatabaseImportExport::impl_initFromDescriptor( const ODataAccessDescriptor& _aDataDescriptor, bool _bPlusDefaultInit)
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::impl_initFromDescriptor" );
    DBG_CHKTHIS(ODatabaseImportExport,NULL);
    if ( !_bPlusDefaultInit )
    {
        m_sDataSourceName = _aDataDescriptor.getDataSource();
        _aDataDescriptor[daCommandType] >>= m_nCommandType;
        _aDataDescriptor[daCommand]     >>= m_sName;
        // some additonal information
        if(_aDataDescriptor.has(daConnection))
        {
            Reference< XConnection > xPureConn( _aDataDescriptor[daConnection], UNO_QUERY );
            m_xConnection.reset( xPureConn, SharedConnection::NoTakeOwnership );
            Reference< XEventListener> xEvt((::cppu::OWeakObject*)this,UNO_QUERY);
            Reference< XComponent >  xComponent(m_xConnection, UNO_QUERY);
            if (xComponent.is() && xEvt.is())
                xComponent->addEventListener(xEvt);
        }

        if ( _aDataDescriptor.has( daSelection ) )
            _aDataDescriptor[ daSelection ] >>= m_aSelection;

        if ( _aDataDescriptor.has( daBookmarkSelection ) )
            _aDataDescriptor[ daBookmarkSelection ] >>= m_bBookmarkSelection;

        if ( _aDataDescriptor.has( daCursor ) )
        {
            _aDataDescriptor[ daCursor ] >>= m_xResultSet;
            m_xRowLocate.set( m_xResultSet, UNO_QUERY );
        }

        if ( m_aSelection.getLength() != 0 )
        {
            if ( !m_xResultSet.is() )
            {
                OSL_FAIL( "ODatabaseImportExport::impl_initFromDescriptor: selection without result set is nonsense!" );
                m_aSelection.realloc( 0 );
            }
        }

        if ( m_aSelection.getLength() != 0 )
        {
            if ( m_bBookmarkSelection && !m_xRowLocate.is() )
            {
                OSL_FAIL( "ODatabaseImportExport::impl_initFromDescriptor: no XRowLocate -> no bookmars!" );
                m_aSelection.realloc( 0 );
            }
        }
    }
    else
        initialize();

    try
    {
        SvtSysLocale aSysLocale;
        m_aLocale = aSysLocale.GetLocaleData().getLocale();
    }
    catch(Exception&)
    {
    }
}
// -----------------------------------------------------------------------------
void ODatabaseImportExport::initialize()
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::initialize" );
    DBG_CHKTHIS(ODatabaseImportExport,NULL);
    m_bInInitialize = sal_True;
    m_bNeedToReInitialize = false;

    if ( !m_xConnection.is() )
    {   // we need a connection
        OSL_ENSURE(m_sDataSourceName.getLength(),"There must be a datsource name!");
        Reference<XNameAccess> xDatabaseContext = Reference< XNameAccess >(m_xFactory->createInstance(SERVICE_SDB_DATABASECONTEXT), UNO_QUERY);
        Reference< XEventListener> xEvt((::cppu::OWeakObject*)this,UNO_QUERY);

        Reference< XConnection > xConnection;
        SQLExceptionInfo aInfo = ::dbaui::createConnection( m_sDataSourceName, xDatabaseContext, m_xFactory, xEvt, xConnection );
        m_xConnection.reset( xConnection );

        if(aInfo.isValid() && aInfo.getType() == SQLExceptionInfo::SQL_EXCEPTION)
            throw *static_cast<const SQLException*>(aInfo);
    }

    Reference<XNameAccess> xNameAccess;
    switch(m_nCommandType)
    {
        case CommandType::TABLE:
            {
                // only for tables
                Reference<XTablesSupplier> xSup(m_xConnection,UNO_QUERY);
                if(xSup.is())
                    xNameAccess = xSup->getTables();
            }
            break;
        case CommandType::QUERY:
            {
                Reference<XQueriesSupplier> xSup(m_xConnection,UNO_QUERY);
                if(xSup.is())
                    xNameAccess = xSup->getQueries();
            }
            break;
    }
    if(xNameAccess.is() && xNameAccess->hasByName(m_sName))
    {
        Reference<XPropertySet> xSourceObject;
        xNameAccess->getByName(m_sName) >>= m_xObject;
    }

    if(m_xObject.is())
    {
        try
        {
            if(m_xObject->getPropertySetInfo()->hasPropertyByName(PROPERTY_FONT))
                m_xObject->getPropertyValue(PROPERTY_FONT) >>= m_aFont;

            // the result set may be already set with the datadescriptor
            if ( !m_xResultSet.is() )
            {
                m_xResultSet.set( m_xFactory->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.RowSet")) ), UNO_QUERY );
                Reference< XPropertySet > xProp( m_xResultSet, UNO_QUERY_THROW );
                xProp->setPropertyValue( PROPERTY_ACTIVE_CONNECTION, makeAny( m_xConnection.getTyped() ) );
                xProp->setPropertyValue( PROPERTY_COMMAND_TYPE, makeAny( m_nCommandType ) );
                xProp->setPropertyValue( PROPERTY_COMMAND, makeAny( m_sName ) );
                Reference< XRowSet > xRowSet( xProp, UNO_QUERY );
                xRowSet->execute();
            }
            impl_initializeRowMember_throw();
        }
        catch(Exception& )
        {
            m_xRow = NULL;
            m_xResultSetMetaData = NULL;
            ::comphelper::disposeComponent(m_xResultSet);
            throw;
        }
    }
    if ( !m_aFont.Name.getLength() )
    {
        Font aApplicationFont = OutputDevice::GetDefaultFont(
            DEFAULTFONT_SANS_UNICODE,
            Application::GetSettings().GetUILanguage(),
            DEFAULTFONT_FLAGS_ONLYONE
        );
        m_aFont = VCLUnoHelper::CreateFontDescriptor( aApplicationFont );
    }

    m_bInInitialize = sal_False;
}
// -----------------------------------------------------------------------------
sal_Bool ODatabaseImportExport::Write()
{
    if ( m_bNeedToReInitialize )
    {
        if ( !m_bInInitialize )
            initialize();
    }
    return sal_True;
}
// -----------------------------------------------------------------------------
sal_Bool ODatabaseImportExport::Read()
{
    if ( m_bNeedToReInitialize )
    {
        if ( !m_bInInitialize )
            initialize();
    }
    return sal_True;
}
// -----------------------------------------------------------------------------
void ODatabaseImportExport::impl_initializeRowMember_throw()
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ODatabaseImportExport::impl_initializeRowMember_throw" );
    if ( !m_xRow.is() && m_xResultSet.is() )
    {
        m_xRow.set( m_xResultSet, UNO_QUERY );
        m_xRowLocate.set( m_xResultSet, UNO_QUERY );
        m_xResultSetMetaData = Reference<XResultSetMetaDataSupplier>(m_xRow,UNO_QUERY)->getMetaData();
        Reference<XColumnsSupplier> xSup(m_xResultSet,UNO_QUERY_THROW);
        m_xRowSetColumns.set(xSup->getColumns(),UNO_QUERY_THROW);
    }
}
//======================================================================
sal_Bool ORTFImportExport::Write()
{
    RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "<EMAIL>", "ORTFImportExport::Write" );
    ODatabaseImportExport::Write();
    (*m_pStream) << '{'     << OOO_STRING_SVTOOLS_RTF_RTF;
    (*m_pStream) << OOO_STRING_SVTOOLS_RTF_ANSI << ODatabaseImportExport::sNewLine;
    rtl_TextEncoding eDestEnc = RTL_TEXTENCODING_MS_1252;

    sal_Bool bBold          = ( ::com::sun::star::awt::FontWeight::BOLD     == m_aFont.Weight );
    sal_Bool bItalic        = ( ::com::sun::star::awt::FontSlant_ITALIC     == m_aFont.Slant );
    sal_Bool bUnderline     = ( ::com::sun::star::awt::FontUnderline::NONE  != m_aFont.Underline );
    sal_Bool bStrikeout     = ( ::com::sun::star::awt::FontStrikeout::NONE  != m_aFont.Strikeout );

    sal_Int32 nColor = 0;
    if(m_xObject.is())
        m_xObject->getPropertyValue(PROPERTY_TEXTCOLOR) >>= nColor;
    ::Color aColor(nColor);

    ByteString aFonts(String(m_aFont.Name),eDestEnc);
    if(!aFonts.Len())
    {
        String aName = Application::GetSettings().GetStyleSettings().GetAppFont().GetName();
        aFonts = ByteString (aName,eDestEnc);
    }
    ::rtl::OString aFormat("\\fcharset0\\fnil ");
    ByteString aFontNr;

    (*m_pStream)    << "{\\fonttbl";
    xub_StrLen nTokenCount = aFonts.GetTokenCount();
    for(sal_uInt32 j=0;j<nTokenCount;++j)
    {
        (*m_pStream) << "\\f";
        m_pStream->WriteNumber(static_cast<sal_Int32>(j));
        (*m_pStream) << aFormat;
        (*m_pStream) << aFonts.GetToken(j).GetBuffer();
        (*m_pStream) << ';';
    }
{'fraction_non_alphanumeric': 0.17847468849696022, 'fraction_numerical': 0.0032504665021368807, 'mean_word_length': 2.829760258183495, 'pattern_counts': {'":': 0, '<': 177, '<?xml version=': 0, '>': 213, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2478723', 'n_tokens_mistral': 10495, 'n_tokens_neox': 10304, 'n_words': 1669}