nss: upgrade to release 3.73
[LibreOffice.git] / dbaccess / source / ui / misc / TokenWriter.cxx
blob5b1be584b18feb54d934b25e1d1f79fd9dd58646
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <TokenWriter.hxx>
21 #include <tools/diagnose_ex.h>
22 #include <tools/stream.hxx>
23 #include <osl/diagnose.h>
24 #include <rtl/tencinfo.h>
25 #include <sal/log.hxx>
26 #include <i18nlangtag/languagetag.hxx>
27 #include <RtfReader.hxx>
28 #include <HtmlReader.hxx>
29 #include <strings.hxx>
30 #include <comphelper/types.hxx>
31 #include <connectivity/dbtools.hxx>
32 #include <com/sun/star/sdb/CommandType.hpp>
33 #include <com/sun/star/sdb/DatabaseContext.hpp>
34 #include <com/sun/star/sdbc/XConnection.hpp>
35 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
36 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
37 #include <com/sun/star/sdbc/XRowSet.hpp>
38 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
39 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
40 #include <com/sun/star/awt/FontWeight.hpp>
41 #include <com/sun/star/awt/FontStrikeout.hpp>
42 #include <com/sun/star/awt/FontSlant.hpp>
43 #include <com/sun/star/awt/FontUnderline.hpp>
44 #include <com/sun/star/document/DocumentProperties.hpp>
45 #include <svtools/htmlkywd.hxx>
46 #include <svtools/rtfkeywd.hxx>
47 #include <tools/color.hxx>
48 #include <svtools/htmlout.hxx>
49 #include <sfx2/frmhtmlw.hxx>
50 #include <svl/numuno.hxx>
51 #include <vcl/svapp.hxx>
52 #include <UITools.hxx>
53 #include <toolkit/helper/vclunohelper.hxx>
54 #include <vcl/outdev.hxx>
55 #include <vcl/settings.hxx>
56 #include <svtools/rtfout.hxx>
57 #include <svtools/htmlcfg.hxx>
58 #include <connectivity/formattedcolumnvalue.hxx>
59 #include <memory>
61 using namespace dbaui;
62 using namespace dbtools;
63 using namespace svx;
64 using namespace ::com::sun::star;
65 using namespace ::com::sun::star::uno;
66 using namespace ::com::sun::star::beans;
67 using namespace ::com::sun::star::container;
68 using namespace ::com::sun::star::sdbc;
69 using namespace ::com::sun::star::sdb;
70 using namespace ::com::sun::star::lang;
71 using namespace ::com::sun::star::sdbcx;
72 using namespace ::com::sun::star::awt;
73 using namespace ::com::sun::star::util;
75 #define CELL_X 1437
77 ODatabaseImportExport::ODatabaseImportExport(const svx::ODataAccessDescriptor& _aDataDescriptor,
78 const Reference< XComponentContext >& _rM,
79 const Reference< css::util::XNumberFormatter >& _rxNumberF)
80 :m_bBookmarkSelection( false )
81 ,m_xFormatter(_rxNumberF)
82 ,m_xContext(_rM)
83 ,m_nCommandType(CommandType::TABLE)
84 ,m_bNeedToReInitialize(false)
85 ,m_bInInitialize(false)
86 ,m_bCheckOnly(false)
88 m_eDestEnc = osl_getThreadTextEncoding();
90 osl_atomic_increment( &m_refCount );
91 impl_initFromDescriptor( _aDataDescriptor, false );
92 osl_atomic_decrement( &m_refCount );
95 // import data
96 ODatabaseImportExport::ODatabaseImportExport( const ::dbtools::SharedConnection& _rxConnection,
97 const Reference< XNumberFormatter >& _rxNumberF, const Reference< XComponentContext >& _rM )
98 :m_bBookmarkSelection( false )
99 ,m_pStream(nullptr)
100 ,m_xConnection(_rxConnection)
101 ,m_xFormatter(_rxNumberF)
102 ,m_xContext(_rM)
103 ,m_nCommandType(css::sdb::CommandType::TABLE)
104 ,m_bNeedToReInitialize(false)
105 ,m_bInInitialize(false)
106 ,m_bCheckOnly(false)
108 m_eDestEnc = osl_getThreadTextEncoding();
111 ODatabaseImportExport::~ODatabaseImportExport()
113 acquire();
114 dispose();
117 void ODatabaseImportExport::dispose()
119 // remove me as listener
120 Reference< XComponent > xComponent(m_xConnection, UNO_QUERY);
121 if (xComponent.is())
123 Reference< XEventListener> xEvt(static_cast<cppu::OWeakObject*>(this),UNO_QUERY);
124 xComponent->removeEventListener(xEvt);
126 m_xConnection.clear();
128 ::comphelper::disposeComponent(m_xRow);
130 m_xObject.clear();
131 m_xResultSetMetaData.clear();
132 m_xResultSet.clear();
133 m_xRow.clear();
134 m_xRowLocate.clear();
135 m_xFormatter.clear();
138 void SAL_CALL ODatabaseImportExport::disposing( const EventObject& Source )
140 Reference<XConnection> xCon(Source.Source,UNO_QUERY);
141 if(m_xConnection.is() && m_xConnection == xCon)
143 m_xConnection.clear();
144 dispose();
145 m_bNeedToReInitialize = true;
149 void ODatabaseImportExport::initialize( const ODataAccessDescriptor& _aDataDescriptor )
151 impl_initFromDescriptor( _aDataDescriptor, true );
154 void ODatabaseImportExport::impl_initFromDescriptor( const ODataAccessDescriptor& _aDataDescriptor, bool _bPlusDefaultInit)
156 if ( !_bPlusDefaultInit )
158 m_sDataSourceName = _aDataDescriptor.getDataSource();
159 _aDataDescriptor[DataAccessDescriptorProperty::CommandType] >>= m_nCommandType;
160 _aDataDescriptor[DataAccessDescriptorProperty::Command] >>= m_sName;
161 // some additional information
162 if(_aDataDescriptor.has(DataAccessDescriptorProperty::Connection))
164 Reference< XConnection > xPureConn( _aDataDescriptor[DataAccessDescriptorProperty::Connection], UNO_QUERY );
165 m_xConnection.reset( xPureConn, SharedConnection::NoTakeOwnership );
166 Reference< XEventListener> xEvt(static_cast<cppu::OWeakObject*>(this),UNO_QUERY);
167 Reference< XComponent > xComponent(m_xConnection, UNO_QUERY);
168 if (xComponent.is() && xEvt.is())
169 xComponent->addEventListener(xEvt);
172 if ( _aDataDescriptor.has( DataAccessDescriptorProperty::Selection ) )
173 _aDataDescriptor[ DataAccessDescriptorProperty::Selection ] >>= m_aSelection;
175 if ( _aDataDescriptor.has( DataAccessDescriptorProperty::BookmarkSelection ) )
176 _aDataDescriptor[ DataAccessDescriptorProperty::BookmarkSelection ] >>= m_bBookmarkSelection;
178 if ( _aDataDescriptor.has( DataAccessDescriptorProperty::Cursor ) )
180 _aDataDescriptor[ DataAccessDescriptorProperty::Cursor ] >>= m_xResultSet;
181 m_xRowLocate.set( m_xResultSet, UNO_QUERY );
184 if ( m_aSelection.hasElements() )
186 if ( !m_xResultSet.is() )
188 SAL_WARN("dbaccess.ui", "ODatabaseImportExport::impl_initFromDescriptor: selection without result set is nonsense!" );
189 m_aSelection.realloc( 0 );
193 if ( m_aSelection.hasElements() )
195 if ( m_bBookmarkSelection && !m_xRowLocate.is() )
197 SAL_WARN("dbaccess.ui", "ODatabaseImportExport::impl_initFromDescriptor: no XRowLocate -> no bookmarks!" );
198 m_aSelection.realloc( 0 );
202 else
203 initialize();
206 void ODatabaseImportExport::initialize()
208 m_bInInitialize = true;
209 m_bNeedToReInitialize = false;
211 if ( !m_xConnection.is() )
212 { // we need a connection
213 OSL_ENSURE(!m_sDataSourceName.isEmpty(),"There must be a datsource name!");
214 Reference<XNameAccess> xDatabaseContext( DatabaseContext::create(m_xContext), UNO_QUERY_THROW);
215 Reference< XEventListener> xEvt(static_cast<cppu::OWeakObject*>(this),UNO_QUERY);
217 Reference< XConnection > xConnection;
218 SQLExceptionInfo aInfo = ::dbaui::createConnection( m_sDataSourceName, xDatabaseContext, m_xContext, xEvt, xConnection );
219 m_xConnection.reset( xConnection );
221 if(aInfo.isValid() && aInfo.getType() == SQLExceptionInfo::TYPE::SQLException)
222 throw *static_cast<const SQLException*>(aInfo);
225 Reference<XNameAccess> xNameAccess;
226 switch(m_nCommandType)
228 case CommandType::TABLE:
230 // only for tables
231 Reference<XTablesSupplier> xSup(m_xConnection,UNO_QUERY);
232 if(xSup.is())
233 xNameAccess = xSup->getTables();
235 break;
236 case CommandType::QUERY:
238 Reference<XQueriesSupplier> xSup(m_xConnection,UNO_QUERY);
239 if(xSup.is())
240 xNameAccess = xSup->getQueries();
242 break;
244 if(xNameAccess.is() && xNameAccess->hasByName(m_sName))
246 xNameAccess->getByName(m_sName) >>= m_xObject;
249 if(m_xObject.is())
253 if(m_xObject->getPropertySetInfo()->hasPropertyByName(PROPERTY_FONT))
254 m_xObject->getPropertyValue(PROPERTY_FONT) >>= m_aFont;
256 // the result set may be already set with the datadescriptor
257 if ( !m_xResultSet.is() )
259 m_xResultSet.set( m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.sdb.RowSet", m_xContext), UNO_QUERY );
260 Reference< XPropertySet > xProp( m_xResultSet, UNO_QUERY_THROW );
261 xProp->setPropertyValue( PROPERTY_ACTIVE_CONNECTION, makeAny( m_xConnection.getTyped() ) );
262 xProp->setPropertyValue( PROPERTY_COMMAND_TYPE, makeAny( m_nCommandType ) );
263 xProp->setPropertyValue( PROPERTY_COMMAND, makeAny( m_sName ) );
264 Reference< XRowSet > xRowSet( xProp, UNO_QUERY );
265 xRowSet->execute();
267 if ( !m_xRow.is() && m_xResultSet.is() )
269 m_xRow.set( m_xResultSet, UNO_QUERY );
270 m_xRowLocate.set( m_xResultSet, UNO_QUERY );
271 m_xResultSetMetaData = Reference<XResultSetMetaDataSupplier>(m_xRow,UNO_QUERY_THROW)->getMetaData();
272 Reference<XColumnsSupplier> xSup(m_xResultSet,UNO_QUERY_THROW);
273 m_xRowSetColumns.set(xSup->getColumns(),UNO_QUERY_THROW);
276 catch(Exception& )
278 m_xRow = nullptr;
279 m_xResultSetMetaData = nullptr;
280 ::comphelper::disposeComponent(m_xResultSet);
281 throw;
284 if ( m_aFont.Name.isEmpty() )
286 vcl::Font aApplicationFont = OutputDevice::GetDefaultFont(
287 DefaultFontType::SANS_UNICODE,
288 Application::GetSettings().GetUILanguageTag().getLanguageType(),
289 GetDefaultFontFlags::OnlyOne
291 m_aFont = VCLUnoHelper::CreateFontDescriptor( aApplicationFont );
294 m_bInInitialize = false;
297 bool ODatabaseImportExport::Write()
299 if ( m_bNeedToReInitialize )
301 if ( !m_bInInitialize )
302 initialize();
304 return true;
307 bool ODatabaseImportExport::Read()
309 if ( m_bNeedToReInitialize )
311 if ( !m_bInInitialize )
312 initialize();
314 return true;
317 bool ORTFImportExport::Write()
319 ODatabaseImportExport::Write();
320 m_pStream->WriteChar( '{' ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_RTF );
321 m_pStream->WriteCharPtr(OOO_STRING_SVTOOLS_RTF_ANSI);
322 if (sal_uInt32 nCpg = rtl_getWindowsCodePageFromTextEncoding(m_eDestEnc); nCpg && nCpg != 65001)
324 m_pStream->WriteCharPtr(OOO_STRING_SVTOOLS_RTF_ANSICPG).WriteUInt32AsString(nCpg);
326 m_pStream->WriteCharPtr(SAL_NEWLINE_STRING);
328 bool bBold = ( css::awt::FontWeight::BOLD == m_aFont.Weight );
329 bool bItalic = ( css::awt::FontSlant_ITALIC == m_aFont.Slant );
330 bool bUnderline = ( css::awt::FontUnderline::NONE != m_aFont.Underline );
331 bool bStrikeout = ( css::awt::FontStrikeout::NONE != m_aFont.Strikeout );
333 sal_Int32 nColor = 0;
334 if(m_xObject.is())
335 m_xObject->getPropertyValue(PROPERTY_TEXTCOLOR) >>= nColor;
336 ::Color aColor(nColor);
338 OString aFonts(OUStringToOString(m_aFont.Name, RTL_TEXTENCODING_MS_1252));
339 if (aFonts.isEmpty())
341 OUString aName = Application::GetSettings().GetStyleSettings().GetAppFont().GetFamilyName();
342 aFonts = OUStringToOString(aName, RTL_TEXTENCODING_MS_1252);
345 m_pStream->WriteCharPtr( "{\\fonttbl" );
346 if (!aFonts.isEmpty())
348 sal_Int32 nIdx{0};
349 sal_Int32 nTok{-1}; // to compensate pre-increment
350 do {
351 m_pStream->WriteCharPtr( "\\f" );
352 m_pStream->WriteInt32AsString(++nTok);
353 m_pStream->WriteCharPtr( "\\fcharset0\\fnil " );
354 m_pStream->WriteOString( aFonts.getToken(0, ';', nIdx) );
355 m_pStream->WriteChar( ';' );
356 } while (nIdx>=0);
358 m_pStream->WriteChar( '}' ) ;
359 m_pStream->WriteCharPtr( SAL_NEWLINE_STRING );
360 // write the rtf color table
361 m_pStream->WriteChar( '{' ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_COLORTBL ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_RED );
362 m_pStream->WriteUInt32AsString(aColor.GetRed());
363 m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_GREEN );
364 m_pStream->WriteUInt32AsString(aColor.GetGreen());
365 m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_BLUE );
366 m_pStream->WriteUInt32AsString(aColor.GetBlue());
368 m_pStream->WriteCharPtr( ";\\red255\\green255\\blue255;\\red192\\green192\\blue192;}" )
369 .WriteCharPtr( SAL_NEWLINE_STRING );
371 static char const aCell1[] = "\\clbrdrl\\brdrs\\brdrcf0\\clbrdrt\\brdrs\\brdrcf0\\clbrdrb\\brdrs\\brdrcf0\\clbrdrr\\brdrs\\brdrcf0\\clshdng10000\\clcfpat2\\cellx";
373 m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TROWD ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRGAPH );
374 m_pStream->WriteInt32AsString(40);
375 m_pStream->WriteCharPtr( SAL_NEWLINE_STRING );
377 if(m_xObject.is())
379 Reference<XColumnsSupplier> xColSup(m_xObject,UNO_QUERY);
380 Reference<XNameAccess> xColumns = xColSup->getColumns();
381 Sequence< OUString> aNames(xColumns->getElementNames());
382 const OUString* pIter = aNames.getConstArray();
384 sal_Int32 nCount = aNames.getLength();
385 bool bUseResultMetaData = false;
386 if ( !nCount )
388 nCount = m_xResultSetMetaData->getColumnCount();
389 bUseResultMetaData = true;
392 for( sal_Int32 i=1; i<=nCount; ++i )
394 m_pStream->WriteCharPtr( aCell1 );
395 m_pStream->WriteInt32AsString(i*CELL_X);
396 m_pStream->WriteCharPtr( SAL_NEWLINE_STRING );
399 // column description
400 m_pStream->WriteChar( '{' ).WriteCharPtr( SAL_NEWLINE_STRING );
401 m_pStream->WriteCharPtr( "\\trrh-270\\pard\\intbl" );
403 std::unique_ptr<OString[]> pHorzChar(new OString[nCount]);
405 for ( sal_Int32 i=1; i <= nCount; ++i )
407 sal_Int32 nAlign = 0;
408 OUString sColumnName;
409 if ( bUseResultMetaData )
410 sColumnName = m_xResultSetMetaData->getColumnName(i);
411 else
413 sColumnName = *pIter;
414 Reference<XPropertySet> xColumn;
415 xColumns->getByName(sColumnName) >>= xColumn;
416 xColumn->getPropertyValue(PROPERTY_ALIGN) >>= nAlign;
417 ++pIter;
420 const char* pChar;
421 switch( nAlign )
423 case 1: pChar = OOO_STRING_SVTOOLS_RTF_QC; break;
424 case 2: pChar = OOO_STRING_SVTOOLS_RTF_QR; break;
425 case 0:
426 default:pChar = OOO_STRING_SVTOOLS_RTF_QL; break;
429 pHorzChar[i-1] = pChar; // to avoid to always rummage in the ITEMSET later on
431 m_pStream->WriteCharPtr( SAL_NEWLINE_STRING );
432 m_pStream->WriteChar( '{' );
433 m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_QC ); // column header always centered
435 if ( bBold ) m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_B );
436 if ( bItalic ) m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_I );
437 if ( bUnderline ) m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_UL );
438 if ( bStrikeout ) m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_STRIKE );
440 m_pStream->WriteCharPtr( "\\fs20\\f0\\cf0\\cb2" );
441 m_pStream->WriteChar( ' ' );
442 RTFOutFuncs::Out_String(*m_pStream, sColumnName, m_eDestEnc);
444 m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL );
445 m_pStream->WriteChar( '}' );
446 m_pStream->WriteCharPtr( SAL_NEWLINE_STRING );
447 m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PARD ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_INTBL );
450 m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ROW );
451 m_pStream->WriteCharPtr( SAL_NEWLINE_STRING ).WriteChar( '}' );
452 m_pStream->WriteCharPtr( SAL_NEWLINE_STRING );
454 sal_Int32 k=1;
455 sal_Int32 kk=0;
456 if ( m_aSelection.hasElements() )
458 const Any* pSelIter = m_aSelection.getConstArray();
459 const Any* pEnd = pSelIter + m_aSelection.getLength();
461 bool bContinue = true;
462 for( ; pSelIter != pEnd && bContinue; ++pSelIter )
464 if ( m_bBookmarkSelection )
466 bContinue = m_xRowLocate->moveToBookmark( *pSelIter );
468 else
470 sal_Int32 nPos = -1;
471 OSL_VERIFY( *pSelIter >>= nPos );
472 bContinue = ( m_xResultSet->absolute( nPos ) );
475 if ( bContinue )
476 appendRow( pHorzChar.get(), nCount, k, kk );
479 else
481 m_xResultSet->beforeFirst(); // set back before the first row
482 while(m_xResultSet->next())
484 appendRow(pHorzChar.get(),nCount,k,kk);
489 m_pStream->WriteChar( '}' ).WriteCharPtr( SAL_NEWLINE_STRING );
490 m_pStream->WriteUChar( 0 );
491 return ((*m_pStream).GetError() == ERRCODE_NONE);
494 void ORTFImportExport::appendRow(OString const * pHorzChar,sal_Int32 _nColumnCount,sal_Int32& k,sal_Int32& kk)
496 ++kk;
497 m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TROWD ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRGAPH );
498 m_pStream->WriteInt32AsString(40);
499 m_pStream->WriteCharPtr( SAL_NEWLINE_STRING );
501 static char const aCell2[] = "\\clbrdrl\\brdrs\\brdrcf2\\clbrdrt\\brdrs\\brdrcf2\\clbrdrb\\brdrs\\brdrcf2\\clbrdrr\\brdrs\\brdrcf2\\clshdng10000\\clcfpat1\\cellx";
503 for ( sal_Int32 i=1; i<=_nColumnCount; ++i )
505 m_pStream->WriteCharPtr( aCell2 );
506 m_pStream->WriteInt32AsString(i*CELL_X);
507 m_pStream->WriteCharPtr( SAL_NEWLINE_STRING );
510 const bool bBold = ( css::awt::FontWeight::BOLD == m_aFont.Weight );
511 const bool bItalic = ( css::awt::FontSlant_ITALIC == m_aFont.Slant );
512 const bool bUnderline = ( css::awt::FontUnderline::NONE != m_aFont.Underline );
513 const bool bStrikeout = ( css::awt::FontStrikeout::NONE != m_aFont.Strikeout );
514 Reference< XRowSet > xRowSet(m_xRow,UNO_QUERY);
516 m_pStream->WriteChar( '{' );
517 m_pStream->WriteCharPtr( "\\trrh-270\\pard\\intbl" );
518 for ( sal_Int32 i=1; i <= _nColumnCount; ++i )
520 m_pStream->WriteCharPtr( SAL_NEWLINE_STRING );
521 m_pStream->WriteChar( '{' );
522 m_pStream->WriteOString( pHorzChar[i-1] );
524 if ( bBold ) m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_B );
525 if ( bItalic ) m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_I );
526 if ( bUnderline ) m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_UL );
527 if ( bStrikeout ) m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_STRIKE );
529 m_pStream->WriteCharPtr( "\\fs20\\f1\\cf0\\cb1 " );
533 Reference<XPropertySet> xColumn(m_xRowSetColumns->getByIndex(i-1),UNO_QUERY_THROW);
534 dbtools::FormattedColumnValue aFormatedValue(m_xContext,xRowSet,xColumn);
535 OUString sValue = aFormatedValue.getFormattedValue();
536 if ( !sValue.isEmpty() )
537 RTFOutFuncs::Out_String(*m_pStream,sValue,m_eDestEnc);
539 catch (Exception&)
541 SAL_WARN("dbaccess.ui","RTF WRITE!");
544 m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL );
545 m_pStream->WriteChar( '}' );
546 m_pStream->WriteCharPtr( SAL_NEWLINE_STRING );
547 m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PARD ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_INTBL );
549 m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ROW ).WriteCharPtr( SAL_NEWLINE_STRING );
550 m_pStream->WriteChar( '}' );
551 ++k;
554 bool ORTFImportExport::Read()
556 ODatabaseImportExport::Read();
557 SvParserState eState = SvParserState::Error;
558 if ( m_pStream )
560 tools::SvRef<ORTFReader> xReader(new ORTFReader((*m_pStream),m_xConnection,m_xFormatter,m_xContext));
561 if ( isCheckEnabled() )
562 xReader->enableCheckOnly();
563 eState = xReader->CallParser();
566 return eState != SvParserState::Error;
569 const sal_Int16 OHTMLImportExport::nCellSpacing = 0;
570 const char OHTMLImportExport::sIndentSource[nIndentMax+1] = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
572 // Macros for HTML-Export
573 #define TAG_ON( tag ) HTMLOutFuncs::Out_AsciiTag( (*m_pStream), tag )
574 #define TAG_OFF( tag ) HTMLOutFuncs::Out_AsciiTag( (*m_pStream), tag, false )
575 #define OUT_LF() m_pStream->WriteCharPtr( SAL_NEWLINE_STRING ).WriteCharPtr( GetIndentStr() )
576 #define TAG_ON_LF( tag ) (TAG_ON( tag ).WriteCharPtr( SAL_NEWLINE_STRING ).WriteCharPtr( GetIndentStr() ))
577 #define TAG_OFF_LF( tag ) (TAG_OFF( tag ).WriteCharPtr( SAL_NEWLINE_STRING ).WriteCharPtr( GetIndentStr() ))
579 OHTMLImportExport::OHTMLImportExport(const svx::ODataAccessDescriptor& _aDataDescriptor,
580 const Reference< XComponentContext >& _rM,
581 const Reference< css::util::XNumberFormatter >& _rxNumberF)
582 : ODatabaseImportExport(_aDataDescriptor,_rM,_rxNumberF)
583 ,m_nIndent(0)
584 #if OSL_DEBUG_LEVEL > 0
585 ,m_bCheckFont(false)
586 #endif
588 // set HTML configuration
589 SvxHtmlOptions& rHtmlOptions = SvxHtmlOptions::Get();
590 m_eDestEnc = rHtmlOptions.GetTextEncoding();
591 strncpy( sIndent, sIndentSource ,std::min(sizeof(sIndent),sizeof(sIndentSource)));
592 sIndent[0] = 0;
595 bool OHTMLImportExport::Write()
597 ODatabaseImportExport::Write();
598 if(m_xObject.is())
600 m_pStream->WriteChar( '<' ).WriteCharPtr( OOO_STRING_SVTOOLS_HTML_doctype ).WriteChar( ' ' ).WriteCharPtr( OOO_STRING_SVTOOLS_HTML_doctype40 ).WriteChar( '>' ).WriteCharPtr( SAL_NEWLINE_STRING ).WriteCharPtr( SAL_NEWLINE_STRING );
601 TAG_ON_LF( OOO_STRING_SVTOOLS_HTML_html );
602 WriteHeader();
603 OUT_LF();
604 WriteBody();
605 OUT_LF();
606 TAG_OFF_LF( OOO_STRING_SVTOOLS_HTML_html );
608 return ((*m_pStream).GetError() == ERRCODE_NONE);
610 return false;
613 bool OHTMLImportExport::Read()
615 ODatabaseImportExport::Read();
616 SvParserState eState = SvParserState::Error;
617 if ( m_pStream )
619 tools::SvRef<OHTMLReader> xReader(new OHTMLReader((*m_pStream),m_xConnection,m_xFormatter,m_xContext));
620 if ( isCheckEnabled() )
621 xReader->enableCheckOnly();
622 xReader->SetTableName(m_sDefaultTableName);
623 eState = xReader->CallParser();
626 return eState != SvParserState::Error;
629 void OHTMLImportExport::WriteHeader()
631 uno::Reference<document::XDocumentProperties> xDocProps(
632 document::DocumentProperties::create( m_xContext ) );
633 if (xDocProps.is()) {
634 xDocProps->setTitle(m_sName);
637 IncIndent(1); TAG_ON_LF( OOO_STRING_SVTOOLS_HTML_head );
639 SfxFrameHTMLWriter::Out_DocInfo( (*m_pStream), OUString(),
640 xDocProps, sIndent, osl_getThreadTextEncoding() );
641 OUT_LF();
642 IncIndent(-1); OUT_LF(); TAG_OFF_LF( OOO_STRING_SVTOOLS_HTML_head );
645 void OHTMLImportExport::WriteBody()
647 IncIndent(1);
648 m_pStream->WriteCharPtr( "<" ).WriteCharPtr( OOO_STRING_SVTOOLS_HTML_style ).WriteCharPtr( " " ).WriteCharPtr( OOO_STRING_SVTOOLS_HTML_O_type ).WriteCharPtr( "=\"text/css\">" );
650 m_pStream->WriteCharPtr( "<!-- " ); OUT_LF();
651 m_pStream->WriteCharPtr( OOO_STRING_SVTOOLS_HTML_body ).WriteCharPtr( " { " ).WriteCharPtr( "font-family: " ).WriteChar( '"' ).WriteOString( OUStringToOString(m_aFont.Name, osl_getThreadTextEncoding()) ).WriteChar( '\"' );
652 // TODO : think about the encoding of the font name
653 m_pStream->WriteCharPtr( "; " ).WriteCharPtr( "font-size: " );
654 m_pStream->WriteInt32AsString(m_aFont.Height);
655 m_pStream->WriteChar( '}' );
657 OUT_LF();
658 m_pStream->WriteCharPtr( " -->" );
659 IncIndent(-1); OUT_LF(); TAG_OFF_LF( OOO_STRING_SVTOOLS_HTML_style );
660 OUT_LF();
662 // default Textcolour black
663 m_pStream->WriteChar( '<' ).WriteCharPtr( OOO_STRING_SVTOOLS_HTML_body ).WriteChar( ' ' ).WriteCharPtr( OOO_STRING_SVTOOLS_HTML_O_text ).WriteChar( '=' );
664 sal_Int32 nColor = 0;
665 if(m_xObject.is())
666 m_xObject->getPropertyValue(PROPERTY_TEXTCOLOR) >>= nColor;
667 ::Color aColor(nColor);
668 HTMLOutFuncs::Out_Color( (*m_pStream), aColor );
670 m_pStream->WriteCharPtr( " " OOO_STRING_SVTOOLS_HTML_O_bgcolor "=" );
671 HTMLOutFuncs::Out_Color( (*m_pStream), aColor );
673 m_pStream->WriteChar( '>' ); OUT_LF();
675 WriteTables();
677 TAG_OFF_LF( OOO_STRING_SVTOOLS_HTML_body );
680 void OHTMLImportExport::WriteTables()
682 OString aStrOut = OOO_STRING_SVTOOLS_HTML_table
684 OOO_STRING_SVTOOLS_HTML_frame
686 OOO_STRING_SVTOOLS_HTML_TF_void;
688 Sequence< OUString> aNames;
689 Reference<XNameAccess> xColumns;
690 bool bUseResultMetaData = false;
691 if(m_xObject.is())
693 Reference<XColumnsSupplier> xColSup(m_xObject,UNO_QUERY);
694 xColumns = xColSup->getColumns();
695 aNames = xColumns->getElementNames();
696 if ( !aNames.hasElements() )
698 sal_Int32 nCount = m_xResultSetMetaData->getColumnCount();
699 aNames.realloc(nCount);
700 for (sal_Int32 i= 0; i < nCount; ++i)
701 aNames[i] = m_xResultSetMetaData->getColumnName(i+1);
702 bUseResultMetaData = true;
706 aStrOut += " "
707 OOO_STRING_SVTOOLS_HTML_O_align
709 OOO_STRING_SVTOOLS_HTML_AL_left
711 OOO_STRING_SVTOOLS_HTML_O_cellspacing
712 "=" +
713 OString::number(nCellSpacing) +
715 OOO_STRING_SVTOOLS_HTML_O_cols
716 "=" +
717 OString::number(aNames.getLength()) +
719 OOO_STRING_SVTOOLS_HTML_O_border
720 "=1";
722 IncIndent(1);
723 TAG_ON( aStrOut.getStr() );
725 FontOn();
727 TAG_ON( OOO_STRING_SVTOOLS_HTML_caption );
728 TAG_ON( OOO_STRING_SVTOOLS_HTML_bold );
730 m_pStream->WriteOString( OUStringToOString(m_sName, osl_getThreadTextEncoding()) );
731 // TODO : think about the encoding of the name
732 TAG_OFF( OOO_STRING_SVTOOLS_HTML_bold );
733 TAG_OFF( OOO_STRING_SVTOOLS_HTML_caption );
735 FontOff();
736 OUT_LF();
737 // </FONT>
739 IncIndent(1);
740 TAG_ON_LF( OOO_STRING_SVTOOLS_HTML_thead );
742 IncIndent(1);
743 TAG_ON_LF( OOO_STRING_SVTOOLS_HTML_tablerow );
745 if(m_xObject.is())
747 std::unique_ptr<sal_Int32[]> pFormat(new sal_Int32[aNames.getLength()]);
749 std::unique_ptr<const char *[]> pHorJustify(new const char*[aNames.getLength()]);
750 std::unique_ptr<sal_Int32[]> pColWidth(new sal_Int32[aNames.getLength()]);
752 sal_Int32 nHeight = 0;
753 m_xObject->getPropertyValue(PROPERTY_ROW_HEIGHT) >>= nHeight;
755 // 1. writing the column description
756 const OUString* pIter = aNames.getConstArray();
757 const OUString* pEnd = pIter + aNames.getLength();
759 for( sal_Int32 i=0;pIter != pEnd; ++pIter,++i )
761 sal_Int32 nAlign = 0;
762 pFormat[i] = 0;
763 pColWidth[i] = 100;
764 if ( !bUseResultMetaData )
766 Reference<XPropertySet> xColumn;
767 xColumns->getByName(*pIter) >>= xColumn;
768 xColumn->getPropertyValue(PROPERTY_ALIGN) >>= nAlign;
769 pFormat[i] = ::comphelper::getINT32(xColumn->getPropertyValue(PROPERTY_FORMATKEY));
770 pColWidth[i] = ::comphelper::getINT32(xColumn->getPropertyValue(PROPERTY_WIDTH));
773 switch( nAlign )
775 case 1: pHorJustify[i] = OOO_STRING_SVTOOLS_HTML_AL_center; break;
776 case 2: pHorJustify[i] = OOO_STRING_SVTOOLS_HTML_AL_right; break;
777 default: pHorJustify[i] = OOO_STRING_SVTOOLS_HTML_AL_left; break;
780 if(i == aNames.getLength()-1)
781 IncIndent(-1);
783 WriteCell(pFormat[i],pColWidth[i],nHeight,pHorJustify[i],*pIter,OOO_STRING_SVTOOLS_HTML_tableheader);
786 IncIndent(-1);
787 TAG_OFF_LF( OOO_STRING_SVTOOLS_HTML_tablerow );
788 TAG_OFF_LF( OOO_STRING_SVTOOLS_HTML_thead );
790 IncIndent(1);
791 TAG_ON_LF( OOO_STRING_SVTOOLS_HTML_tbody );
793 // 2. and now the data
794 Reference< XRowSet > xRowSet(m_xRow,UNO_QUERY);
795 sal_Int32 kk=0;
796 m_xResultSet->beforeFirst(); // set back before the first row
797 while(m_xResultSet->next())
799 IncIndent(1);
800 TAG_ON_LF( OOO_STRING_SVTOOLS_HTML_tablerow );
802 ++kk;
803 for(sal_Int32 i=1;i<=aNames.getLength();++i)
805 if(i == aNames.getLength())
806 IncIndent(-1);
808 OUString aValue;
811 Reference<XPropertySet> xColumn(m_xRowSetColumns->getByIndex(i-1),UNO_QUERY_THROW);
812 dbtools::FormattedColumnValue aFormatedValue(m_xContext,xRowSet,xColumn);
813 OUString sValue = aFormatedValue.getFormattedValue();
814 if (!sValue.isEmpty())
816 aValue = sValue;
819 catch( const Exception& )
821 DBG_UNHANDLED_EXCEPTION("dbaccess");
823 WriteCell(pFormat[i-1],pColWidth[i-1],nHeight,pHorJustify[i-1],aValue,OOO_STRING_SVTOOLS_HTML_tabledata);
825 TAG_OFF_LF( OOO_STRING_SVTOOLS_HTML_tablerow );
828 else
830 IncIndent(-1);
831 TAG_OFF_LF( OOO_STRING_SVTOOLS_HTML_tablerow );
832 TAG_OFF_LF( OOO_STRING_SVTOOLS_HTML_thead );
834 IncIndent(1);
835 TAG_ON_LF( OOO_STRING_SVTOOLS_HTML_tbody );
838 IncIndent(-1); OUT_LF(); TAG_OFF_LF( OOO_STRING_SVTOOLS_HTML_tbody );
839 IncIndent(-1); TAG_OFF_LF( OOO_STRING_SVTOOLS_HTML_table );
842 void OHTMLImportExport::WriteCell( sal_Int32 nFormat, sal_Int32 nWidthPixel, sal_Int32 nHeightPixel, const char* pChar,
843 const OUString& rValue, const char* pHtmlTag)
845 OString aStrTD = pHtmlTag;
847 nWidthPixel = nWidthPixel ? nWidthPixel : 86;
848 nHeightPixel = nHeightPixel ? nHeightPixel : 17;
850 // despite the <TABLE COLS=n> and <COL WIDTH=x> designation necessary,
851 // as Netscape is not paying attention to them.
852 // column width
853 aStrTD += " "
854 OOO_STRING_SVTOOLS_HTML_O_width
855 "=" +
856 OString::number(nWidthPixel) +
857 // line height
859 OOO_STRING_SVTOOLS_HTML_O_height
860 "=" +
861 OString::number(nHeightPixel) +
863 OOO_STRING_SVTOOLS_HTML_O_align
864 "=" +
865 pChar;
867 SvNumberFormatsSupplierObj* pSupplierImpl = m_xFormatter.is() ? comphelper::getUnoTunnelImplementation<SvNumberFormatsSupplierObj>(m_xFormatter->getNumberFormatsSupplier()) : nullptr;
868 SvNumberFormatter* pFormatter = pSupplierImpl ? pSupplierImpl->GetNumberFormatter() : nullptr;
869 if(pFormatter)
871 double fVal = 0.0;
875 fVal = m_xFormatter->convertStringToNumber(nFormat,rValue);
876 HTMLOutFuncs::CreateTableDataOptionsValNum(false, fVal,nFormat, *pFormatter);
878 catch(const Exception&)
880 HTMLOutFuncs::CreateTableDataOptionsValNum(false, fVal,nFormat, *pFormatter);
884 TAG_ON( aStrTD.getStr() );
886 FontOn();
888 bool bBold = ( css::awt::FontWeight::BOLD == m_aFont.Weight );
889 bool bItalic = ( css::awt::FontSlant_ITALIC == m_aFont.Slant );
890 bool bUnderline = ( css::awt::FontUnderline::NONE != m_aFont.Underline );
891 bool bStrikeout = ( css::awt::FontStrikeout::NONE != m_aFont.Strikeout );
893 if ( bBold ) TAG_ON( OOO_STRING_SVTOOLS_HTML_bold );
894 if ( bItalic ) TAG_ON( OOO_STRING_SVTOOLS_HTML_italic );
895 if ( bUnderline ) TAG_ON( OOO_STRING_SVTOOLS_HTML_underline );
896 if ( bStrikeout ) TAG_ON( OOO_STRING_SVTOOLS_HTML_strike );
898 if ( rValue.isEmpty() )
899 TAG_ON( OOO_STRING_SVTOOLS_HTML_linebreak ); // no completely empty cell
900 else
901 HTMLOutFuncs::Out_String( (*m_pStream), rValue ,m_eDestEnc);
903 if ( bStrikeout ) TAG_OFF( OOO_STRING_SVTOOLS_HTML_strike );
904 if ( bUnderline ) TAG_OFF( OOO_STRING_SVTOOLS_HTML_underline );
905 if ( bItalic ) TAG_OFF( OOO_STRING_SVTOOLS_HTML_italic );
906 if ( bBold ) TAG_OFF( OOO_STRING_SVTOOLS_HTML_bold );
908 FontOff();
910 TAG_OFF_LF( pHtmlTag );
913 void OHTMLImportExport::FontOn()
915 #if OSL_DEBUG_LEVEL > 0
916 m_bCheckFont = true;
917 #endif
919 // <FONT FACE="xxx">
920 OString aStrOut = "<"
921 OOO_STRING_SVTOOLS_HTML_font
923 OOO_STRING_SVTOOLS_HTML_O_face
925 "\"" +
926 OUStringToOString(m_aFont.Name,osl_getThreadTextEncoding()) +
927 // TODO : think about the encoding of the font name
928 "\""
930 OOO_STRING_SVTOOLS_HTML_O_color
931 "=";
932 m_pStream->WriteOString( aStrOut );
934 sal_Int32 nColor = 0;
935 if(m_xObject.is())
936 m_xObject->getPropertyValue(PROPERTY_TEXTCOLOR) >>= nColor;
937 ::Color aColor(nColor);
939 HTMLOutFuncs::Out_Color( (*m_pStream), aColor );
940 m_pStream->WriteCharPtr( ">" );
943 inline void OHTMLImportExport::FontOff()
945 #if OSL_DEBUG_LEVEL > 0
946 OSL_ENSURE(m_bCheckFont,"No FontOn() called");
947 #endif
948 TAG_OFF( OOO_STRING_SVTOOLS_HTML_font );
949 #if OSL_DEBUG_LEVEL > 0
950 m_bCheckFont = false;
951 #endif
954 void OHTMLImportExport::IncIndent( sal_Int16 nVal )
956 sIndent[m_nIndent] = '\t';
957 m_nIndent = m_nIndent + nVal;
958 if ( m_nIndent < 0 )
959 m_nIndent = 0;
960 else if ( m_nIndent > nIndentMax )
961 m_nIndent = nIndentMax;
962 sIndent[m_nIndent] = 0;
965 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */