LibreOffice Module dbaccess (master) 1
RtfReader.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
31#include <strings.hrc>
33#include <comphelper/string.hxx>
34#include <tools/color.hxx>
35#include <WExtendPages.hxx>
36#include <vcl/svapp.hxx>
37#include <vcl/settings.hxx>
38
39using namespace dbaui;
40using namespace ::com::sun::star::uno;
41using namespace ::com::sun::star::beans;
42using namespace ::com::sun::star::container;
43using namespace ::com::sun::star::sdbc;
44using namespace ::com::sun::star::sdbcx;
45using namespace ::com::sun::star::awt;
46
47// ORTFReader
48ORTFReader::ORTFReader( SvStream& rIn,
49 const SharedConnection& _rxConnection,
51 const css::uno::Reference< css::uno::XComponentContext >& _rxContext)
52 :SvRTFParser(rIn)
53 ,ODatabaseExport( _rxConnection, _rxNumberF, _rxContext, rIn )
54{
55 m_bAppendFirstLine = false;
56}
57
58ORTFReader::ORTFReader(SvStream& rIn,
59 sal_Int32 nRows,
60 TPositions&& _rColumnPositions,
62 const css::uno::Reference< css::uno::XComponentContext >& _rxContext,
63 const TColumnVector* pList,
64 const OTypeInfoMap* _pInfoMap,
65 bool _bAutoIncrementEnabled)
66 :SvRTFParser(rIn)
67 ,ODatabaseExport( nRows, std::move(_rColumnPositions), _rxNumberF, _rxContext, pList, _pInfoMap, _bAutoIncrementEnabled, rIn )
68{
69 m_bAppendFirstLine = false;
70}
71
72ORTFReader::~ORTFReader()
73{
74}
75
77{
82 return m_bFoundTable ? eParseState : SvParserState::Error;
83}
84
85#if defined _MSC_VER
86#pragma warning(disable: 4702) // unreachable code, bug in MSVC2015
87#endif
88
89void ORTFReader::NextToken( int nToken )
90{
91 if(m_bError || !m_nRows) // if there is an error or no more rows to check, return immediately
92 return;
93
94 if(m_xConnection.is()) // names, which CTOR was called and hence, if a table should be created
95 {
96 switch(nToken)
97 {
98 case RTF_COLORTBL:
99 {
100
101 int nTmpToken2 = GetNextToken();
102 do
103 {
104 Color aColor;
105 do
106 {
107 switch(nTmpToken2)
108 {
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;
112 default: break;
113 }
114 nTmpToken2 = GetNextToken();
115 }
116 while(aToken[0] != ';' && eState != SvParserState::Error && eState != SvParserState::Accepted);
117 m_vecColor.push_back(aColor.GetRGBColor());
118 nTmpToken2 = GetNextToken();
119 }
120 while(nTmpToken2 == RTF_RED && eState != SvParserState::Error && eState != SvParserState::Accepted);
121 SkipToken();
122 }
123 break;
124
125 case RTF_TROWD:
126 {
127 if ( !m_xTable.is() ) // use first line as header
128 {
129 sal_uInt64 const nTell = rInput.Tell(); // perhaps alters position of the stream
130
132 if ( m_bAppendFirstLine )
133 {
134 rInput.Seek(nTell);
136 }
137 }
138 }
139 break;
140 case RTF_INTBL:
141 if(m_bInTbl)
142 {
143 eraseTokens();
144 }
145
146 m_bInTbl = true; // Now we are in a table description
147 break;
148 case RTF_TEXTTOKEN:
149 case RTF_SINGLECHAR:
150 if(m_bInTbl) // important, as otherwise we also get the names of the fonts
152 break;
153 case RTF_CELL:
154 {
155 try
156 {
158 }
159 catch(SQLException& e)
160 // handling update failure
161 {
163 }
164 m_nColumnPos++;
165 eraseTokens();
166 }
167 break;
168 case RTF_ROW:
169 // it can happen that the last cell is not concluded with \cell
170 try
171 {
173 m_nRowCount++;
174 if(m_bIsAutoIncrement) // if bSetAutoIncrement then I have to set the autoincrement
175 m_pUpdateHelper->updateInt(1,m_nRowCount);
176 m_pUpdateHelper->insertRow();
177 }
178 catch(SQLException& e)
179 // handling update failure
180 {
182 }
183 m_nColumnPos = 0;
184 break;
185 }
186 }
187 else // branch only valid for type checking
188 {
189 switch(nToken)
190 {
191 case RTF_TROWD:
192 // The head of the column is not included
193 if(m_bHead)
194 {
195 do
196 {}
197 while(GetNextToken() != RTF_ROW && eState != SvParserState::Error && eState != SvParserState::Accepted);
198 m_bHead = false;
199 }
200 break;
201 case RTF_INTBL:
202 m_bInTbl = true;
203 break;
204 case RTF_TEXTTOKEN:
205 case RTF_SINGLECHAR:
206 if(m_bInTbl)
208 break;
209 case RTF_CELL:
210 adjustFormat();
211 m_nColumnPos++;
212 break;
213 case RTF_ROW:
214 adjustFormat();
215 m_nColumnPos = 0;
216 m_nRows--;
217 break;
218 }
219 }
220}
221
223{
224 OUString aTableName(DBA_RES(STR_TBL_TITLE));
225 aTableName = aTableName.getToken(0,' ');
226 aTableName = ::dbtools::createUniqueName(m_xTables, aTableName);
227
228 OUString aColumnName;
229
230 FontDescriptor aFont = VCLUnoHelper::CreateFontDescriptor(Application::GetSettings().GetStyleSettings().GetAppFont());
231 do
232 {
233 switch (nToken)
234 {
236 case RTF_UNKNOWNDATA:
237 m_bInTbl = false;
238 aColumnName.clear();
239 break;
240 case RTF_INTBL:
241 if(m_bInTbl)
242 aColumnName.clear();
243
244 m_bInTbl = true;
245 break;
246 case RTF_TEXTTOKEN:
247 case RTF_SINGLECHAR:
248 if(m_bInTbl)
249 aColumnName += aToken;
250 break;
251 case RTF_CELL:
252 {
253 aColumnName = comphelper::string::strip(aColumnName, ' ');
254 if (aColumnName.isEmpty() || m_bAppendFirstLine )
255 aColumnName = DBA_RES(STR_COLUMN_NAME);
256
257 CreateDefaultColumn(aColumnName);
258 aColumnName.clear();
259 }
260 break;
261 case RTF_CF:
262 break;
263 case RTF_B:
264 aFont.Weight = css::awt::FontWeight::BOLD;
265 break;
266 case RTF_I:
267 aFont.Slant = css::awt::FontSlant_ITALIC;
268 break;
269 case RTF_UL:
270 aFont.Underline = css::awt::FontUnderline::SINGLE;
271 break;
272 case RTF_STRIKE:
273 aFont.Strikeout = css::awt::FontStrikeout::SINGLE;
274 break;
275 }
277 }
278 while(nToken != RTF_TROWD && eState != SvParserState::Error && eState != SvParserState::Accepted);
279
280 bool bOk = !m_vDestVector.empty();
281 if(bOk)
282 {
283 if ( !aColumnName.isEmpty() )
284 {
285 if ( m_bAppendFirstLine )
286 aColumnName = DBA_RES(STR_COLUMN_NAME);
287 CreateDefaultColumn(aColumnName);
288 }
289
290 m_bInTbl = false;
291 m_bFoundTable = true;
292
293 if ( isCheckEnabled() )
294 return true;
295 Any aTextColor;
296 if(!m_vecColor.empty())
297 aTextColor <<= m_vecColor[0];
298
299 bOk = !executeWizard(aTableName,aTextColor,aFont) && m_xTable.is();
300 }
301 return bOk;
302}
303
305{
306 return &OWizRTFExtend::Create;
307}
308
309/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static const AllSettings & GetSettings()
Color GetRGBColor() const
void SetGreen(sal_uInt8 nGreen)
void SetRed(sal_uInt8 nRed)
void SetBlue(sal_uInt8 nBlue)
SvStream & rInput
tools::Long nTokenValue
OUStringBuffer aToken
T SkipToken(short nCnt=-1)
SvParserState eState
virtual SvParserState CallParser() override
virtual void ResetError()
sal_uInt64 Tell() const
sal_uInt64 Seek(sal_uInt64 nPos)
static css::awt::FontDescriptor CreateFontDescriptor(const vcl::Font &rFont)
sal_Int32 m_nRows
number of rows to be searched
Definition: DExport.hxx:91
bool isCheckEnabled() const
Definition: DExport.hxx:154
OUString m_sTextToken
cell content
Definition: DExport.hxx:85
void CreateDefaultColumn(const OUString &_rColumnName)
Definition: DExport.cxx:605
css::uno::Reference< css::container::XNameAccess > m_xTables
container
Definition: DExport.hxx:72
TColumnVector m_vDestVector
Definition: DExport.hxx:69
bool m_bError
error and termination code
Definition: DExport.hxx:93
std::shared_ptr< IUpdateHelper > m_pUpdateHelper
Definition: DExport.hxx:75
sal_Int32 m_nColumnPos
current column position
Definition: DExport.hxx:90
const OTypeInfoMap * m_pInfoMap
Definition: DExport.hxx:89
bool m_bInTbl
true, if parser is in RTF table
Definition: DExport.hxx:94
sal_Int32 m_nRowCount
current count of rows
Definition: DExport.hxx:92
css::uno::Reference< css::beans::XPropertySet > m_xTable
dest table
Definition: DExport.hxx:71
bool executeWizard(const OUString &_sTableName, const css::uno::Any &_aTextColor, const css::awt::FontDescriptor &_rFont)
executeWizard calls a wizard to create/append data
Definition: DExport.cxx:661
bool m_bFoundTable
set to true when a table was found
Definition: DExport.hxx:98
bool m_bIsAutoIncrement
if PKey is set by user
Definition: DExport.hxx:97
void showErrorDialog(const css::sdbc::SQLException &e)
Definition: DExport.cxx:725
void SetColumnTypes(const TColumnVector *rList, const OTypeInfoMap *_pInfoMap)
Definition: DExport.cxx:530
void insertValueIntoColumn()
Definition: DExport.cxx:272
bool m_bHead
true, if the header hasn't been read yet
Definition: DExport.hxx:95
const TColumnVector * m_pColumnList
Definition: DExport.hxx:88
SharedConnection m_xConnection
dest conn
Definition: DExport.hxx:73
virtual TypeSelectionPageFactory getTypeSelectionPageFactory() override
Definition: RtfReader.cxx:304
virtual SvParserState CallParser() override
Definition: RtfReader.cxx:76
std::vector< Color > m_vecColor
Definition: RtfReader.hxx:31
virtual void NextToken(int nToken) override
Definition: RtfReader.cxx:89
bool CreateTable(int nToken)
Definition: RtfReader.cxx:222
static std::unique_ptr< OWizTypeSelect > Create(weld::Container *pPage, OCopyTableWizard *pWizard, SvStream &rInput)
#define DBA_RES(id)
OString strip(const OString &rIn, char c)
std::multimap< DataTypeEnum, OExtendedTypeInfo * > OTypeInfoMap
std::unique_ptr< OWizTypeSelect >(* TypeSelectionPageFactory)(weld::Container *, OCopyTableWizard *, SvStream &)
DefTokenId nToken
RTF_UNKNOWNDATA
RTF_TROWD
RTF_ROW
RTF_INTBL
RTF_B
RTF_I
RTF_UNKNOWNCONTROL
RTF_GREEN
RTF_CELL
RTF_UL
RTF_TEXTTOKEN
RTF_COLORTBL
RTF_STRIKE
RTF_SINGLECHAR
RTF_BLUE
RTF_CF
RTF_RED
#define STREAM_SEEK_TO_BEGIN
SvParserState
unsigned char sal_uInt8