1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <RtfReader.hxx>
21 #include <tools/stream.hxx>
22 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
23 #include <com/sun/star/awt/FontDescriptor.hpp>
24 #include <com/sun/star/awt/FontWeight.hpp>
25 #include <com/sun/star/awt/FontStrikeout.hpp>
26 #include <com/sun/star/awt/FontSlant.hpp>
27 #include <com/sun/star/awt/FontUnderline.hpp>
28 #include <core_resource.hxx>
29 #include <svtools/rtftoken.h>
30 #include <toolkit/helper/vclunohelper.hxx>
31 #include <strings.hrc>
32 #include <connectivity/dbtools.hxx>
33 #include <comphelper/string.hxx>
34 #include <tools/color.hxx>
35 #include <WExtendPages.hxx>
36 #include <vcl/svapp.hxx>
37 #include <vcl/settings.hxx>
39 using namespace dbaui
;
40 using namespace ::com::sun::star::uno
;
41 using namespace ::com::sun::star::beans
;
42 using namespace ::com::sun::star::container
;
43 using namespace ::com::sun::star::sdbc
;
44 using namespace ::com::sun::star::sdbcx
;
45 using namespace ::com::sun::star::awt
;
48 ORTFReader::ORTFReader( SvStream
& rIn
,
49 const SharedConnection
& _rxConnection
,
50 const Reference
< css::util::XNumberFormatter
>& _rxNumberF
,
51 const css::uno::Reference
< css::uno::XComponentContext
>& _rxContext
)
53 ,ODatabaseExport( _rxConnection
, _rxNumberF
, _rxContext
, rIn
)
55 m_bAppendFirstLine
= false;
58 ORTFReader::ORTFReader(SvStream
& rIn
,
60 TPositions
&& _rColumnPositions
,
61 const Reference
< css::util::XNumberFormatter
>& _rxNumberF
,
62 const css::uno::Reference
< css::uno::XComponentContext
>& _rxContext
,
63 const TColumnVector
* pList
,
64 const OTypeInfoMap
* _pInfoMap
,
65 bool _bAutoIncrementEnabled
)
67 ,ODatabaseExport( nRows
, std::move(_rColumnPositions
), _rxNumberF
, _rxContext
, pList
, _pInfoMap
, _bAutoIncrementEnabled
, rIn
)
69 m_bAppendFirstLine
= false;
72 ORTFReader::~ORTFReader()
76 SvParserState
ORTFReader::CallParser()
78 rInput
.Seek(STREAM_SEEK_TO_BEGIN
);
80 SvParserState eParseState
= SvRTFParser::CallParser();
81 SetColumnTypes(m_pColumnList
,m_pInfoMap
);
82 return m_bFoundTable
? eParseState
: SvParserState::Error
;
86 #pragma warning(disable: 4702) // unreachable code, bug in MSVC2015
89 void ORTFReader::NextToken( int nToken
)
91 if(m_bError
|| !m_nRows
) // if there is an error or no more rows to check, return immediately
94 if(m_xConnection
.is()) // names, which CTOR was called and hence, if a table should be created
101 int nTmpToken2
= GetNextToken();
109 case RTF_RED
: aColor
.SetRed(static_cast<sal_uInt8
>(nTokenValue
)); break;
110 case RTF_BLUE
: aColor
.SetBlue(static_cast<sal_uInt8
>(nTokenValue
)); break;
111 case RTF_GREEN
: aColor
.SetGreen(static_cast<sal_uInt8
>(nTokenValue
)); break;
114 nTmpToken2
= GetNextToken();
116 while(aToken
[0] != ';' && eState
!= SvParserState::Error
&& eState
!= SvParserState::Accepted
);
117 m_vecColor
.push_back(aColor
.GetRGBColor());
118 nTmpToken2
= GetNextToken();
120 while(nTmpToken2
== RTF_RED
&& eState
!= SvParserState::Error
&& eState
!= SvParserState::Accepted
);
127 if ( !m_xTable
.is() ) // use first line as header
129 sal_uInt64
const nTell
= rInput
.Tell(); // perhaps alters position of the stream
131 m_bError
= !CreateTable(nToken
);
132 if ( m_bAppendFirstLine
)
146 m_bInTbl
= true; // Now we are in a table description
150 if(m_bInTbl
) // important, as otherwise we also get the names of the fonts
151 m_sTextToken
+= aToken
;
157 insertValueIntoColumn();
159 catch(SQLException
& e
)
160 // handling update failure
169 // it can happen that the last cell is not concluded with \cell
172 insertValueIntoColumn();
174 if(m_bIsAutoIncrement
) // if bSetAutoIncrement then I have to set the autoincrement
175 m_pUpdateHelper
->updateInt(1,m_nRowCount
);
176 m_pUpdateHelper
->insertRow();
178 catch(SQLException
& e
)
179 // handling update failure
187 else // branch only valid for type checking
192 // The head of the column is not included
197 while(GetNextToken() != RTF_ROW
&& eState
!= SvParserState::Error
&& eState
!= SvParserState::Accepted
);
207 m_sTextToken
+= aToken
;
222 bool ORTFReader::CreateTable(int nToken
)
224 OUString
aTableName(DBA_RES(STR_TBL_TITLE
));
225 aTableName
= aTableName
.getToken(0,' ');
226 aTableName
= ::dbtools::createUniqueName(m_xTables
, aTableName
);
228 OUString aColumnName
;
230 FontDescriptor aFont
= VCLUnoHelper::CreateFontDescriptor(Application::GetSettings().GetStyleSettings().GetAppFont());
235 case RTF_UNKNOWNCONTROL
:
236 case RTF_UNKNOWNDATA
:
249 aColumnName
+= aToken
;
253 aColumnName
= comphelper::string::strip(aColumnName
, ' ');
254 if (aColumnName
.isEmpty() || m_bAppendFirstLine
)
255 aColumnName
= DBA_RES(STR_COLUMN_NAME
);
257 CreateDefaultColumn(aColumnName
);
264 aFont
.Weight
= css::awt::FontWeight::BOLD
;
267 aFont
.Slant
= css::awt::FontSlant_ITALIC
;
270 aFont
.Underline
= css::awt::FontUnderline::SINGLE
;
273 aFont
.Strikeout
= css::awt::FontStrikeout::SINGLE
;
276 nToken
= GetNextToken();
278 while(nToken
!= RTF_TROWD
&& eState
!= SvParserState::Error
&& eState
!= SvParserState::Accepted
);
280 bool bOk
= !m_vDestVector
.empty();
283 if ( !aColumnName
.isEmpty() )
285 if ( m_bAppendFirstLine
)
286 aColumnName
= DBA_RES(STR_COLUMN_NAME
);
287 CreateDefaultColumn(aColumnName
);
291 m_bFoundTable
= true;
293 if ( isCheckEnabled() )
296 if(!m_vecColor
.empty())
297 aTextColor
<<= m_vecColor
[0];
299 bOk
= !executeWizard(aTableName
,aTextColor
,aFont
) && m_xTable
.is();
304 TypeSelectionPageFactory
ORTFReader::getTypeSelectionPageFactory()
306 return &OWizRTFExtend::Create
;
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */