Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / dbaccess / source / ui / misc / RtfReader.cxx
blob2ef4ccb86c36bac8c88855a1e9a58eea859dc870
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "RtfReader.hxx"
30 #include <tools/debug.hxx>
31 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
32 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
33 #include <com/sun/star/sdbcx/XAppend.hpp>
34 #include <com/sun/star/sdbc/DataType.hpp>
35 #include <com/sun/star/sdbc/ColumnValue.hpp>
36 #include <com/sun/star/awt/FontDescriptor.hpp>
37 #include <com/sun/star/awt/FontWeight.hpp>
38 #include <com/sun/star/awt/FontStrikeout.hpp>
39 #include <com/sun/star/awt/FontSlant.hpp>
40 #include <com/sun/star/awt/FontUnderline.hpp>
41 #include <com/sun/star/util/NumberFormat.hpp>
42 #include <com/sun/star/util/XNumberFormatTypes.hpp>
43 #include "dbustrings.hrc"
44 #include <svtools/rtftoken.h>
45 #include "dbu_misc.hrc"
46 #include <vcl/msgbox.hxx>
47 #include <connectivity/dbconversion.hxx>
48 #include <connectivity/dbtools.hxx>
49 #include <comphelper/extract.hxx>
50 #include <tools/color.hxx>
51 #include "WExtendPages.hxx"
52 #include "moduledbu.hxx"
53 #include "QEnumTypes.hxx"
54 #include "UITools.hxx"
55 #include <vcl/svapp.hxx>
56 #include <rtl/logfile.hxx>
58 using namespace dbaui;
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::beans;
61 using namespace ::com::sun::star::container;
62 using namespace ::com::sun::star::sdbc;
63 using namespace ::com::sun::star::sdbcx;
64 using namespace ::com::sun::star::awt;
66 DBG_NAME(ORTFReader)
67 // ==========================================================================
68 // ORTFReader
69 // ==========================================================================
70 ORTFReader::ORTFReader( SvStream& rIn,
71 const SharedConnection& _rxConnection,
72 const Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
73 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
74 const TColumnVector* pList,
75 const OTypeInfoMap* _pInfoMap)
76 :SvRTFParser(rIn)
77 ,ODatabaseExport( _rxConnection, _rxNumberF, _rM, pList, _pInfoMap, rIn )
79 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::ORTFReader" );
80 DBG_CTOR(ORTFReader,NULL);
81 m_bAppendFirstLine = false;
83 // ---------------------------------------------------------------------------
84 ORTFReader::ORTFReader(SvStream& rIn,
85 sal_Int32 nRows,
86 const TPositions &_rColumnPositions,
87 const Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
88 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
89 const TColumnVector* pList,
90 const OTypeInfoMap* _pInfoMap,
91 sal_Bool _bAutoIncrementEnabled)
92 :SvRTFParser(rIn)
93 ,ODatabaseExport( nRows, _rColumnPositions, _rxNumberF, _rM, pList, _pInfoMap, _bAutoIncrementEnabled, rIn )
95 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::ORTFReader" );
96 DBG_CTOR(ORTFReader,NULL);
97 m_bAppendFirstLine = false;
99 // ---------------------------------------------------------------------------
100 ORTFReader::~ORTFReader()
102 DBG_DTOR(ORTFReader,NULL);
104 // ---------------------------------------------------------------------------
105 SvParserState ORTFReader::CallParser()
107 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::CallParser" );
108 DBG_CHKTHIS(ORTFReader,NULL);
109 rInput.Seek(STREAM_SEEK_TO_BEGIN);
110 rInput.ResetError();
111 SvParserState eParseState = SvRTFParser::CallParser();
112 SetColumnTypes(m_pColumnList,m_pInfoMap);
113 return m_bFoundTable ? eParseState : SVPAR_ERROR;
115 // ---------------------------------------------------------------------------
116 void ORTFReader::NextToken( int nToken )
118 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::NextToken" );
119 DBG_CHKTHIS(ORTFReader,NULL);
120 if(m_bError || !m_nRows) // falls Fehler oder keine Rows mehr zur "Uberpr"ufung dann gleich zur"uck
121 return;
123 if(m_xConnection.is()) // gibt an welcher CTOR gerufen wurde und damit, ob eine Tabelle erstellt werden soll
125 switch(nToken)
127 case RTF_COLORTBL:
130 int nTmpToken2 = GetNextToken();
133 Color aColor;
136 switch(nTmpToken2)
138 case RTF_RED: aColor.SetRed((sal_uInt8)nTokenValue); break;
139 case RTF_BLUE: aColor.SetBlue((sal_uInt8)nTokenValue); break;
140 case RTF_GREEN: aColor.SetGreen((sal_uInt8)nTokenValue); break;
141 default: break;
143 nTmpToken2 = GetNextToken();
145 while(aToken.GetChar(0) != ';' && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
146 m_vecColor.push_back(aColor.GetRGBColor());
147 nTmpToken2 = GetNextToken();
149 while(nTmpToken2 == RTF_RED && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
150 SkipToken();
152 break;
154 case RTF_DEFLANG:
155 case RTF_LANG: // Sprache abfragen
156 m_nDefToken = (rtl_TextEncoding)nTokenValue;
157 break;
158 case RTF_TROWD:
160 bool bInsertRow = true;
161 if ( !m_xTable.is() ) // erste Zeile als Header verwenden
163 sal_uInt32 nTell = rInput.Tell(); // ver�ndert vielleicht die Position des Streams
165 m_bError = !CreateTable(nToken);
166 bInsertRow = m_bAppendFirstLine;
167 if ( m_bAppendFirstLine )
169 rInput.Seek(nTell);
170 rInput.ResetError();
173 if ( bInsertRow && !m_bError)
177 m_pUpdateHelper->moveToInsertRow(); // sonst neue Zeile anh"angen
179 catch(SQLException& e)
180 // UpdateFehlerbehandlung
182 showErrorDialog(e);
186 break;
187 case RTF_INTBL:
188 if(m_bInTbl)
190 eraseTokens();
193 m_bInTbl = sal_True; // jetzt befinden wir uns in einer Tabellenbeschreibung
194 break;
195 case RTF_TEXTTOKEN:
196 case RTF_SINGLECHAR:
197 if(m_bInTbl) // wichtig, da wir sonst auch die Namen der Fonts bekommen
198 m_sTextToken += aToken;
199 break;
200 case RTF_CELL:
204 insertValueIntoColumn();
206 catch(SQLException& e)
207 // UpdateFehlerbehandlung
209 showErrorDialog(e);
211 m_nColumnPos++;
212 eraseTokens();
214 break;
215 case RTF_ROW:
216 // es kann vorkommen, da� die letzte Celle nicht mit \cell abgeschlossen ist
219 insertValueIntoColumn();
220 m_nRowCount++;
221 if(m_bIsAutoIncrement) // if bSetAutoIncrement then I have to set the autoincrement
222 m_pUpdateHelper->updateInt(1,m_nRowCount);
223 m_pUpdateHelper->insertRow();
225 catch(SQLException& e)
226 //////////////////////////////////////////////////////////////////////
227 // UpdateFehlerbehandlung
229 showErrorDialog(e);
231 m_nColumnPos = 0;
232 break;
235 else // Zweig nur f"ur Typpr"ufung g"ultig
237 switch(nToken)
239 case RTF_TROWD:
240 // Der Spalten Kopf z"ahlt nicht mit
241 if(m_bHead)
245 while(GetNextToken() != RTF_ROW && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
246 m_bHead = sal_False;
248 break;
249 case RTF_INTBL:
250 m_bInTbl = sal_True;
251 break;
252 case RTF_TEXTTOKEN:
253 case RTF_SINGLECHAR:
254 if(m_bInTbl)
255 m_sTextToken += aToken;
256 break;
257 case RTF_CELL:
258 adjustFormat();
259 m_nColumnPos++;
260 break;
261 case RTF_ROW:
262 adjustFormat();
263 m_nColumnPos = 0;
264 m_nRows--;
265 break;
269 // ---------------------------------------------------------------------------
270 sal_Bool ORTFReader::CreateTable(int nToken)
272 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::CreateTable" );
273 DBG_CHKTHIS(ORTFReader,NULL);
274 String aTableName(ModuleRes(STR_TBL_TITLE));
275 aTableName = aTableName.GetToken(0,' ');
276 aTableName = String(::dbtools::createUniqueName(m_xTables,::rtl::OUString(aTableName)));
278 int nTmpToken2 = nToken;
279 String aColumnName;
281 FontDescriptor aFont = ::dbaui::CreateFontDescriptor(Application::GetSettings().GetStyleSettings().GetAppFont());
284 switch(nTmpToken2)
286 case RTF_UNKNOWNCONTROL:
287 case RTF_UNKNOWNDATA:
288 m_bInTbl = sal_False;
289 aColumnName.Erase();
290 break;
291 case RTF_INTBL:
292 if(m_bInTbl)
293 aColumnName.Erase();
295 m_bInTbl = sal_True;
296 break;
297 case RTF_TEXTTOKEN:
298 case RTF_SINGLECHAR:
299 if(m_bInTbl)
300 aColumnName += aToken;
301 break;
302 case RTF_CELL:
304 aColumnName.EraseLeadingChars();
305 aColumnName.EraseTrailingChars();
306 if (!aColumnName.Len() || m_bAppendFirstLine )
307 aColumnName = String(ModuleRes(STR_COLUMN_NAME));
309 CreateDefaultColumn(aColumnName);
310 aColumnName.Erase();
312 break;
313 case RTF_CF:
314 break;
315 case RTF_B:
316 aFont.Weight = ::com::sun::star::awt::FontWeight::BOLD;
317 break;
318 case RTF_I:
319 aFont.Slant = ::com::sun::star::awt::FontSlant_ITALIC;
320 break;
321 case RTF_UL:
322 aFont.Underline = ::com::sun::star::awt::FontUnderline::SINGLE;
323 break;
324 case RTF_STRIKE:
325 aFont.Strikeout = ::com::sun::star::awt::FontStrikeout::SINGLE;
326 break;
329 while((nTmpToken2 = GetNextToken()) != RTF_TROWD && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
331 sal_Bool bOk = !m_vDestVector.empty();
332 if(bOk)
334 if ( aColumnName.Len() )
336 if ( m_bAppendFirstLine )
337 aColumnName = String(ModuleRes(STR_COLUMN_NAME));
338 CreateDefaultColumn(aColumnName);
341 m_bInTbl = sal_False;
342 m_bFoundTable = sal_True;
344 if ( isCheckEnabled() )
345 return sal_True;
346 Any aTextColor;
347 if(!m_vecColor.empty())
348 aTextColor <<= m_vecColor[0];
350 bOk = !executeWizard(aTableName,aTextColor,aFont) && m_xTable.is();
352 return bOk;
354 // -----------------------------------------------------------------------------
355 void ORTFReader::release()
357 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::release" );
358 DBG_CHKTHIS(ORTFReader,NULL);
359 ReleaseRef();
362 // -----------------------------------------------------------------------------
363 TypeSelectionPageFactory ORTFReader::getTypeSelectionPageFactory()
365 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::getTypeSelectionPageFactory" );
366 DBG_CHKTHIS(ORTFReader,NULL);
367 return &OWizRTFExtend::Create;
369 // -----------------------------------------------------------------------------
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */