Line data Source code
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/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>
50 :
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;
58 :
59 : // ORTFReader
60 0 : 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)
66 : :SvRTFParser(rIn)
67 0 : ,ODatabaseExport( _rxConnection, _rxNumberF, _rxContext, pList, _pInfoMap, rIn )
68 : {
69 0 : m_bAppendFirstLine = false;
70 0 : }
71 :
72 0 : ORTFReader::ORTFReader(SvStream& rIn,
73 : sal_Int32 nRows,
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)
80 : :SvRTFParser(rIn)
81 0 : ,ODatabaseExport( nRows, _rColumnPositions, _rxNumberF, _rxContext, pList, _pInfoMap, _bAutoIncrementEnabled, rIn )
82 : {
83 0 : m_bAppendFirstLine = false;
84 0 : }
85 :
86 0 : ORTFReader::~ORTFReader()
87 : {
88 0 : }
89 :
90 0 : SvParserState ORTFReader::CallParser()
91 : {
92 0 : rInput.Seek(STREAM_SEEK_TO_BEGIN);
93 0 : rInput.ResetError();
94 0 : SvParserState eParseState = SvRTFParser::CallParser();
95 0 : SetColumnTypes(m_pColumnList,m_pInfoMap);
96 0 : return m_bFoundTable ? eParseState : SVPAR_ERROR;
97 : }
98 :
99 0 : void ORTFReader::NextToken( int nToken )
100 : {
101 0 : if(m_bError || !m_nRows) // if there is an error or no more rows to check, return immediatelly
102 0 : return;
103 :
104 0 : if(m_xConnection.is()) // names, which CTOR was called and hence, if a table should be created
105 : {
106 0 : switch(nToken)
107 : {
108 : case RTF_COLORTBL:
109 : {
110 :
111 0 : int nTmpToken2 = GetNextToken();
112 0 : do
113 : {
114 0 : Color aColor;
115 0 : do
116 : {
117 0 : switch(nTmpToken2)
118 : {
119 0 : case RTF_RED: aColor.SetRed((sal_uInt8)nTokenValue); break;
120 0 : case RTF_BLUE: aColor.SetBlue((sal_uInt8)nTokenValue); break;
121 0 : case RTF_GREEN: aColor.SetGreen((sal_uInt8)nTokenValue); break;
122 0 : default: break;
123 : }
124 0 : nTmpToken2 = GetNextToken();
125 : }
126 0 : while(aToken[0] != ';' && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
127 0 : m_vecColor.push_back(aColor.GetRGBColor());
128 0 : nTmpToken2 = GetNextToken();
129 : }
130 0 : while(nTmpToken2 == RTF_RED && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
131 0 : SkipToken();
132 : }
133 0 : break;
134 :
135 : case RTF_DEFLANG:
136 : case RTF_LANG: // inquire language
137 0 : m_nDefToken = (rtl_TextEncoding)nTokenValue;
138 0 : break;
139 : case RTF_TROWD:
140 : {
141 0 : bool bInsertRow = true;
142 0 : if ( !m_xTable.is() ) // use first line as header
143 : {
144 0 : sal_Size nTell = rInput.Tell(); // perhaps alters position of the stream
145 :
146 0 : m_bError = !CreateTable(nToken);
147 0 : bInsertRow = m_bAppendFirstLine;
148 0 : if ( m_bAppendFirstLine )
149 : {
150 0 : rInput.Seek(nTell);
151 0 : rInput.ResetError();
152 : }
153 : }
154 0 : if ( bInsertRow && !m_bError)
155 : {
156 : try
157 : {
158 0 : m_pUpdateHelper->moveToInsertRow(); // otherwise append new line
159 : }
160 0 : catch(SQLException& e)
161 : // handling update failure
162 : {
163 0 : showErrorDialog(e);
164 : }
165 : }
166 : }
167 0 : break;
168 : case RTF_INTBL:
169 0 : if(m_bInTbl)
170 : {
171 0 : eraseTokens();
172 : }
173 :
174 0 : m_bInTbl = true; // Now we are in a table description
175 0 : break;
176 : case RTF_TEXTTOKEN:
177 : case RTF_SINGLECHAR:
178 0 : if(m_bInTbl) // important, as otherwise we also get the names of the fonts
179 0 : m_sTextToken += aToken;
180 0 : break;
181 : case RTF_CELL:
182 : {
183 : try
184 : {
185 0 : insertValueIntoColumn();
186 : }
187 0 : catch(SQLException& e)
188 : // handling update failure
189 : {
190 0 : showErrorDialog(e);
191 : }
192 0 : m_nColumnPos++;
193 0 : eraseTokens();
194 : }
195 0 : break;
196 : case RTF_ROW:
197 : // it can happen that the last cell is not concluded with \cell
198 : try
199 : {
200 0 : insertValueIntoColumn();
201 0 : m_nRowCount++;
202 0 : if(m_bIsAutoIncrement) // if bSetAutoIncrement then I have to set the autoincrement
203 0 : m_pUpdateHelper->updateInt(1,m_nRowCount);
204 0 : m_pUpdateHelper->insertRow();
205 : }
206 0 : catch(SQLException& e)
207 : // handling update failure
208 : {
209 0 : showErrorDialog(e);
210 : }
211 0 : m_nColumnPos = 0;
212 0 : break;
213 : }
214 : }
215 : else // branch only valid for type checking
216 : {
217 0 : switch(nToken)
218 : {
219 : case RTF_TROWD:
220 : // The head of the column is not included
221 0 : if(m_bHead)
222 : {
223 0 : do
224 : {}
225 0 : while(GetNextToken() != RTF_ROW && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
226 0 : m_bHead = false;
227 : }
228 0 : break;
229 : case RTF_INTBL:
230 0 : m_bInTbl = true;
231 0 : break;
232 : case RTF_TEXTTOKEN:
233 : case RTF_SINGLECHAR:
234 0 : if(m_bInTbl)
235 0 : m_sTextToken += aToken;
236 0 : break;
237 : case RTF_CELL:
238 0 : adjustFormat();
239 0 : m_nColumnPos++;
240 0 : break;
241 : case RTF_ROW:
242 0 : adjustFormat();
243 0 : m_nColumnPos = 0;
244 0 : m_nRows--;
245 0 : break;
246 : }
247 : }
248 : }
249 :
250 0 : bool ORTFReader::CreateTable(int nToken)
251 : {
252 0 : OUString aTableName(ModuleRes(STR_TBL_TITLE));
253 0 : aTableName = aTableName.getToken(0,' ');
254 0 : aTableName = ::dbtools::createUniqueName(m_xTables, aTableName);
255 :
256 0 : int nTmpToken2 = nToken;
257 0 : OUString aColumnName;
258 :
259 0 : FontDescriptor aFont = VCLUnoHelper::CreateFontDescriptor(Application::GetSettings().GetStyleSettings().GetAppFont());
260 0 : do
261 : {
262 0 : switch(nTmpToken2)
263 : {
264 : case RTF_UNKNOWNCONTROL:
265 : case RTF_UNKNOWNDATA:
266 0 : m_bInTbl = false;
267 0 : aColumnName = "";
268 0 : break;
269 : case RTF_INTBL:
270 0 : if(m_bInTbl)
271 0 : aColumnName = "";
272 :
273 0 : m_bInTbl = true;
274 0 : break;
275 : case RTF_TEXTTOKEN:
276 : case RTF_SINGLECHAR:
277 0 : if(m_bInTbl)
278 0 : aColumnName += aToken;
279 0 : break;
280 : case RTF_CELL:
281 : {
282 0 : aColumnName = comphelper::string::strip(aColumnName, ' ');
283 0 : if (aColumnName.isEmpty() || m_bAppendFirstLine )
284 0 : aColumnName = ModuleRes(STR_COLUMN_NAME);
285 :
286 0 : CreateDefaultColumn(aColumnName);
287 0 : aColumnName = "";
288 : }
289 0 : break;
290 : case RTF_CF:
291 0 : break;
292 : case RTF_B:
293 0 : aFont.Weight = ::com::sun::star::awt::FontWeight::BOLD;
294 0 : break;
295 : case RTF_I:
296 0 : aFont.Slant = ::com::sun::star::awt::FontSlant_ITALIC;
297 0 : break;
298 : case RTF_UL:
299 0 : aFont.Underline = ::com::sun::star::awt::FontUnderline::SINGLE;
300 0 : break;
301 : case RTF_STRIKE:
302 0 : aFont.Strikeout = ::com::sun::star::awt::FontStrikeout::SINGLE;
303 0 : break;
304 : }
305 : }
306 0 : while((nTmpToken2 = GetNextToken()) != RTF_TROWD && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
307 :
308 0 : bool bOk = !m_vDestVector.empty();
309 0 : if(bOk)
310 : {
311 0 : if ( !aColumnName.isEmpty() )
312 : {
313 0 : if ( m_bAppendFirstLine )
314 0 : aColumnName = ModuleRes(STR_COLUMN_NAME);
315 0 : CreateDefaultColumn(aColumnName);
316 : }
317 :
318 0 : m_bInTbl = false;
319 0 : m_bFoundTable = true;
320 :
321 0 : if ( isCheckEnabled() )
322 0 : return true;
323 0 : Any aTextColor;
324 0 : if(!m_vecColor.empty())
325 0 : aTextColor <<= m_vecColor[0];
326 :
327 0 : bOk = !executeWizard(aTableName,aTextColor,aFont) && m_xTable.is();
328 : }
329 0 : return bOk;
330 : }
331 :
332 0 : void ORTFReader::release()
333 : {
334 0 : ReleaseRef();
335 0 : }
336 :
337 0 : TypeSelectionPageFactory ORTFReader::getTypeSelectionPageFactory()
338 : {
339 0 : return &OWizRTFExtend::Create;
340 72 : }
341 :
342 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|