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/debug.hxx>
22 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
23 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
24 #include <com/sun/star/sdbcx/XAppend.hpp>
25 #include <com/sun/star/sdbc/DataType.hpp>
26 #include <com/sun/star/sdbc/ColumnValue.hpp>
27 #include <com/sun/star/awt/FontDescriptor.hpp>
28 #include <com/sun/star/awt/FontWeight.hpp>
29 #include <com/sun/star/awt/FontStrikeout.hpp>
30 #include <com/sun/star/awt/FontSlant.hpp>
31 #include <com/sun/star/awt/FontUnderline.hpp>
32 #include <com/sun/star/util/NumberFormat.hpp>
33 #include <com/sun/star/util/XNumberFormatTypes.hpp>
34 #include "dbustrings.hrc"
35 #include <svtools/rtftoken.h>
36 #include <toolkit/helper/vclunohelper.hxx>
37 #include "dbu_misc.hrc"
38 #include <vcl/msgbox.hxx>
39 #include <connectivity/dbconversion.hxx>
40 #include <connectivity/dbtools.hxx>
41 #include <comphelper/extract.hxx>
42 #include <comphelper/string.hxx>
43 #include <tools/color.hxx>
44 #include "WExtendPages.hxx"
45 #include "moduledbu.hxx"
46 #include "QEnumTypes.hxx"
47 #include "UITools.hxx"
48 #include <vcl/svapp.hxx>
49 #include <vcl/settings.hxx>
51 using namespace dbaui
;
52 using namespace ::com::sun::star::uno
;
53 using namespace ::com::sun::star::beans
;
54 using namespace ::com::sun::star::container
;
55 using namespace ::com::sun::star::sdbc
;
56 using namespace ::com::sun::star::sdbcx
;
57 using namespace ::com::sun::star::awt
;
60 ORTFReader::ORTFReader( SvStream
& rIn
,
61 const SharedConnection
& _rxConnection
,
62 const Reference
< ::com::sun::star::util::XNumberFormatter
>& _rxNumberF
,
63 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& _rxContext
,
64 const TColumnVector
* pList
,
65 const OTypeInfoMap
* _pInfoMap
)
67 ,ODatabaseExport( _rxConnection
, _rxNumberF
, _rxContext
, pList
, _pInfoMap
, rIn
)
69 m_bAppendFirstLine
= false;
72 ORTFReader::ORTFReader(SvStream
& rIn
,
74 const TPositions
&_rColumnPositions
,
75 const Reference
< ::com::sun::star::util::XNumberFormatter
>& _rxNumberF
,
76 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& _rxContext
,
77 const TColumnVector
* pList
,
78 const OTypeInfoMap
* _pInfoMap
,
79 bool _bAutoIncrementEnabled
)
81 ,ODatabaseExport( nRows
, _rColumnPositions
, _rxNumberF
, _rxContext
, pList
, _pInfoMap
, _bAutoIncrementEnabled
, rIn
)
83 m_bAppendFirstLine
= false;
86 ORTFReader::~ORTFReader()
90 SvParserState
ORTFReader::CallParser()
92 rInput
.Seek(STREAM_SEEK_TO_BEGIN
);
94 SvParserState eParseState
= SvRTFParser::CallParser();
95 SetColumnTypes(m_pColumnList
,m_pInfoMap
);
96 return m_bFoundTable
? eParseState
: SVPAR_ERROR
;
99 void ORTFReader::NextToken( int nToken
)
101 if(m_bError
|| !m_nRows
) // if there is an error or no more rows to check, return immediatelly
104 if(m_xConnection
.is()) // names, which CTOR was called and hence, if a table should be created
111 int nTmpToken2
= GetNextToken();
119 case RTF_RED
: aColor
.SetRed((sal_uInt8
)nTokenValue
); break;
120 case RTF_BLUE
: aColor
.SetBlue((sal_uInt8
)nTokenValue
); break;
121 case RTF_GREEN
: aColor
.SetGreen((sal_uInt8
)nTokenValue
); break;
124 nTmpToken2
= GetNextToken();
126 while(aToken
[0] != ';' && eState
!= SVPAR_ERROR
&& eState
!= SVPAR_ACCEPTED
);
127 m_vecColor
.push_back(aColor
.GetRGBColor());
128 nTmpToken2
= GetNextToken();
130 while(nTmpToken2
== RTF_RED
&& eState
!= SVPAR_ERROR
&& eState
!= SVPAR_ACCEPTED
);
136 case RTF_LANG
: // inquire language
137 m_nDefToken
= (rtl_TextEncoding
)nTokenValue
;
141 bool bInsertRow
= true;
142 if ( !m_xTable
.is() ) // use first line as header
144 sal_Size nTell
= rInput
.Tell(); // perhaps alters position of the stream
146 m_bError
= !CreateTable(nToken
);
147 bInsertRow
= m_bAppendFirstLine
;
148 if ( m_bAppendFirstLine
)
154 if ( bInsertRow
&& !m_bError
)
158 m_pUpdateHelper
->moveToInsertRow(); // otherwise append new line
160 catch(SQLException
& e
)
161 // handling update failure
174 m_bInTbl
= true; // Now we are in a table description
178 if(m_bInTbl
) // important, as otherwise we also get the names of the fonts
179 m_sTextToken
+= aToken
;
185 insertValueIntoColumn();
187 catch(SQLException
& e
)
188 // handling update failure
197 // it can happen that the last cell is not concluded with \cell
200 insertValueIntoColumn();
202 if(m_bIsAutoIncrement
) // if bSetAutoIncrement then I have to set the autoincrement
203 m_pUpdateHelper
->updateInt(1,m_nRowCount
);
204 m_pUpdateHelper
->insertRow();
206 catch(SQLException
& e
)
207 // handling update failure
215 else // branch only valid for type checking
220 // The head of the column is not included
225 while(GetNextToken() != RTF_ROW
&& eState
!= SVPAR_ERROR
&& eState
!= SVPAR_ACCEPTED
);
235 m_sTextToken
+= aToken
;
250 bool ORTFReader::CreateTable(int nToken
)
252 OUString
aTableName(ModuleRes(STR_TBL_TITLE
));
253 aTableName
= aTableName
.getToken(0,' ');
254 aTableName
= ::dbtools::createUniqueName(m_xTables
, aTableName
);
256 OUString aColumnName
;
258 FontDescriptor aFont
= VCLUnoHelper::CreateFontDescriptor(Application::GetSettings().GetStyleSettings().GetAppFont());
263 case RTF_UNKNOWNCONTROL
:
264 case RTF_UNKNOWNDATA
:
277 aColumnName
+= aToken
;
281 aColumnName
= comphelper::string::strip(aColumnName
, ' ');
282 if (aColumnName
.isEmpty() || m_bAppendFirstLine
)
283 aColumnName
= ModuleRes(STR_COLUMN_NAME
);
285 CreateDefaultColumn(aColumnName
);
292 aFont
.Weight
= ::com::sun::star::awt::FontWeight::BOLD
;
295 aFont
.Slant
= ::com::sun::star::awt::FontSlant_ITALIC
;
298 aFont
.Underline
= ::com::sun::star::awt::FontUnderline::SINGLE
;
301 aFont
.Strikeout
= ::com::sun::star::awt::FontStrikeout::SINGLE
;
304 nToken
= GetNextToken();
306 while(nToken
!= RTF_TROWD
&& eState
!= SVPAR_ERROR
&& eState
!= SVPAR_ACCEPTED
);
308 bool bOk
= !m_vDestVector
.empty();
311 if ( !aColumnName
.isEmpty() )
313 if ( m_bAppendFirstLine
)
314 aColumnName
= ModuleRes(STR_COLUMN_NAME
);
315 CreateDefaultColumn(aColumnName
);
319 m_bFoundTable
= true;
321 if ( isCheckEnabled() )
324 if(!m_vecColor
.empty())
325 aTextColor
<<= m_vecColor
[0];
327 bOk
= !executeWizard(aTableName
,aTextColor
,aFont
) && m_xTable
.is();
332 void ORTFReader::release()
337 TypeSelectionPageFactory
ORTFReader::getTypeSelectionPageFactory()
339 return &OWizRTFExtend::Create
;
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */