update dev300-m58
[ooovba.git] / dbaccess / source / ui / misc / RtfReader.cxx
blob94af6b9b538767055dbf3f36adccd607e81a7f54
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: RtfReader.cxx,v $
10 * $Revision: 1.26 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
33 #include "RtfReader.hxx"
34 #include <tools/debug.hxx>
35 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
36 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
37 #include <com/sun/star/sdbcx/XAppend.hpp>
38 #include <com/sun/star/sdbc/DataType.hpp>
39 #include <com/sun/star/sdbc/ColumnValue.hpp>
40 #include <com/sun/star/awt/FontDescriptor.hpp>
41 #include <com/sun/star/awt/FontWeight.hpp>
42 #include <com/sun/star/awt/FontStrikeout.hpp>
43 #include <com/sun/star/awt/FontSlant.hpp>
44 #include <com/sun/star/awt/FontUnderline.hpp>
45 #include <com/sun/star/util/NumberFormat.hpp>
46 #include <com/sun/star/util/XNumberFormatTypes.hpp>
47 #include "dbustrings.hrc"
48 #include <svtools/rtftoken.h>
49 #include "dbu_misc.hrc"
50 #include <vcl/msgbox.hxx>
51 #include <connectivity/dbconversion.hxx>
52 #include <connectivity/dbtools.hxx>
53 #include <comphelper/extract.hxx>
54 #include <tools/color.hxx>
55 #include "WExtendPages.hxx"
56 #include "moduledbu.hxx"
57 #include "QEnumTypes.hxx"
58 #include "UITools.hxx"
59 #include <vcl/svapp.hxx>
60 #include <rtl/logfile.hxx>
62 using namespace dbaui;
63 using namespace ::com::sun::star::uno;
64 using namespace ::com::sun::star::beans;
65 using namespace ::com::sun::star::container;
66 using namespace ::com::sun::star::sdbc;
67 using namespace ::com::sun::star::sdbcx;
68 using namespace ::com::sun::star::awt;
70 DBG_NAME(ORTFReader)
71 // ==========================================================================
72 // ORTFReader
73 // ==========================================================================
74 ORTFReader::ORTFReader( SvStream& rIn,
75 const SharedConnection& _rxConnection,
76 const Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
77 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
78 const TColumnVector* pList,
79 const OTypeInfoMap* _pInfoMap)
80 :SvRTFParser(rIn)
81 ,ODatabaseExport( _rxConnection, _rxNumberF, _rM, pList, _pInfoMap, rIn )
83 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::ORTFReader" );
84 DBG_CTOR(ORTFReader,NULL);
85 m_bAppendFirstLine = false;
87 // ---------------------------------------------------------------------------
88 ORTFReader::ORTFReader(SvStream& rIn,
89 sal_Int32 nRows,
90 const TPositions &_rColumnPositions,
91 const Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
92 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
93 const TColumnVector* pList,
94 const OTypeInfoMap* _pInfoMap,
95 sal_Bool _bAutoIncrementEnabled)
96 :SvRTFParser(rIn)
97 ,ODatabaseExport( nRows, _rColumnPositions, _rxNumberF, _rM, pList, _pInfoMap, _bAutoIncrementEnabled, rIn )
99 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::ORTFReader" );
100 DBG_CTOR(ORTFReader,NULL);
101 m_bAppendFirstLine = false;
103 // ---------------------------------------------------------------------------
104 ORTFReader::~ORTFReader()
106 DBG_DTOR(ORTFReader,NULL);
108 // ---------------------------------------------------------------------------
109 SvParserState ORTFReader::CallParser()
111 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::CallParser" );
112 DBG_CHKTHIS(ORTFReader,NULL);
113 rInput.Seek(STREAM_SEEK_TO_BEGIN);
114 rInput.ResetError();
115 SvParserState eParseState = SvRTFParser::CallParser();
116 SetColumnTypes(m_pColumnList,m_pInfoMap);
117 return m_bFoundTable ? eParseState : SVPAR_ERROR;
119 // ---------------------------------------------------------------------------
120 void ORTFReader::NextToken( int nToken )
122 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::NextToken" );
123 DBG_CHKTHIS(ORTFReader,NULL);
124 if(m_bError || !m_nRows) // falls Fehler oder keine Rows mehr zur "Uberpr"ufung dann gleich zur"uck
125 return;
127 if(m_xConnection.is()) // gibt an welcher CTOR gerufen wurde und damit, ob eine Tabelle erstellt werden soll
129 switch(nToken)
131 case RTF_COLORTBL:
134 int nTmpToken2 = GetNextToken();
135 sal_Bool bNext = TRUE;
138 Color aColor;
141 switch(nTmpToken2)
143 case RTF_RED: aColor.SetRed((sal_uInt8)nTokenValue); break;
144 case RTF_BLUE: aColor.SetBlue((sal_uInt8)nTokenValue); break;
145 case RTF_GREEN: aColor.SetGreen((sal_uInt8)nTokenValue); break;
146 default:
147 bNext = sal_False;
149 nTmpToken2 = GetNextToken();
151 while(aToken.GetChar(0) != ';' && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
152 m_vecColor.push_back(aColor.GetRGBColor());
153 nTmpToken2 = GetNextToken();
155 while(nTmpToken2 == RTF_RED && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
156 SkipToken();
158 break;
160 case RTF_DEFLANG:
161 case RTF_LANG: // Sprache abfragen
162 m_nDefToken = (rtl_TextEncoding)nTokenValue;
163 break;
164 case RTF_TROWD:
166 bool bInsertRow = true;
167 if ( !m_xTable.is() ) // erste Zeile als Header verwenden
169 sal_uInt32 nTell = rInput.Tell(); // verändert vielleicht die Position des Streams
171 m_bError = !CreateTable(nToken);
172 bInsertRow = m_bAppendFirstLine;
173 if ( m_bAppendFirstLine )
175 rInput.Seek(nTell);
176 rInput.ResetError();
179 if ( bInsertRow && !m_bError)
183 m_pUpdateHelper->moveToInsertRow(); // sonst neue Zeile anh"angen
185 catch(SQLException& e)
186 // UpdateFehlerbehandlung
188 showErrorDialog(e);
192 break;
193 case RTF_INTBL:
194 if(m_bInTbl)
196 eraseTokens();
199 m_bInTbl = TRUE; // jetzt befinden wir uns in einer Tabellenbeschreibung
200 break;
201 case RTF_TEXTTOKEN:
202 case RTF_SINGLECHAR:
203 if(m_bInTbl) // wichtig, da wir sonst auch die Namen der Fonts bekommen
204 m_sTextToken += aToken;
205 break;
206 case RTF_CELL:
210 insertValueIntoColumn();
212 catch(SQLException& e)
213 // UpdateFehlerbehandlung
215 showErrorDialog(e);
217 m_nColumnPos++;
218 eraseTokens();
220 break;
221 case RTF_ROW:
222 // es kann vorkommen, daß die letzte Celle nicht mit \cell abgeschlossen ist
225 insertValueIntoColumn();
226 m_nRowCount++;
227 if(m_bIsAutoIncrement) // if bSetAutoIncrement then I have to set the autoincrement
228 m_pUpdateHelper->updateInt(1,m_nRowCount);
229 m_pUpdateHelper->insertRow();
231 catch(SQLException& e)
232 //////////////////////////////////////////////////////////////////////
233 // UpdateFehlerbehandlung
235 showErrorDialog(e);
237 m_nColumnPos = 0;
238 break;
241 else // Zweig nur f"ur Typpr"ufung g"ultig
243 switch(nToken)
245 case RTF_TROWD:
246 // Der Spalten Kopf z"ahlt nicht mit
247 if(m_bHead)
251 while(GetNextToken() != RTF_ROW && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
252 m_bHead = sal_False;
254 break;
255 case RTF_INTBL:
256 m_bInTbl = TRUE;
257 break;
258 case RTF_TEXTTOKEN:
259 case RTF_SINGLECHAR:
260 if(m_bInTbl)
261 m_sTextToken += aToken;
262 break;
263 case RTF_CELL:
264 adjustFormat();
265 m_nColumnPos++;
266 break;
267 case RTF_ROW:
268 adjustFormat();
269 m_nColumnPos = 0;
270 m_nRows--;
271 break;
275 // ---------------------------------------------------------------------------
276 sal_Bool ORTFReader::CreateTable(int nToken)
278 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::CreateTable" );
279 DBG_CHKTHIS(ORTFReader,NULL);
280 String aTableName(ModuleRes(STR_TBL_TITLE));
281 aTableName = aTableName.GetToken(0,' ');
282 aTableName = String(::dbtools::createUniqueName(m_xTables,::rtl::OUString(aTableName)));
284 int nTmpToken2 = nToken;
285 String aColumnName;
287 FontDescriptor aFont = ::dbaui::CreateFontDescriptor(Application::GetSettings().GetStyleSettings().GetAppFont());
290 switch(nTmpToken2)
292 case RTF_UNKNOWNCONTROL:
293 case RTF_UNKNOWNDATA:
294 m_bInTbl = sal_False;
295 aColumnName.Erase();
296 break;
297 case RTF_INTBL:
298 if(m_bInTbl)
299 aColumnName.Erase();
301 m_bInTbl = TRUE;
302 break;
303 case RTF_TEXTTOKEN:
304 case RTF_SINGLECHAR:
305 if(m_bInTbl)
306 aColumnName += aToken;
307 break;
308 case RTF_CELL:
310 aColumnName.EraseLeadingChars();
311 aColumnName.EraseTrailingChars();
312 if (!aColumnName.Len() || m_bAppendFirstLine )
313 aColumnName = String(ModuleRes(STR_COLUMN_NAME));
315 CreateDefaultColumn(aColumnName);
316 aColumnName.Erase();
318 break;
319 case RTF_CF:
320 // if(nTokenValue < m_vecColor.size())
321 // m_xTable->setPropertyValue(PROPERTY_TEXTCOLOR,makeAny(m_vecColor[nTokenValue]));
322 break;
323 case RTF_B:
324 aFont.Weight = ::com::sun::star::awt::FontWeight::BOLD;
325 break;
326 case RTF_I:
327 aFont.Slant = ::com::sun::star::awt::FontSlant_ITALIC;
328 break;
329 case RTF_UL:
330 aFont.Underline = ::com::sun::star::awt::FontUnderline::SINGLE;
331 break;
332 case RTF_STRIKE:
333 aFont.Strikeout = ::com::sun::star::awt::FontStrikeout::SINGLE;
334 break;
337 while((nTmpToken2 = GetNextToken()) != RTF_TROWD && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
339 sal_Bool bOk = !m_vDestVector.empty();
340 if(bOk)
342 if ( aColumnName.Len() )
344 if ( m_bAppendFirstLine )
345 aColumnName = String(ModuleRes(STR_COLUMN_NAME));
346 CreateDefaultColumn(aColumnName);
349 m_bInTbl = sal_False;
350 m_bFoundTable = sal_True;
352 if ( isCheckEnabled() )
353 return sal_True;
354 Any aTextColor;
355 if(!m_vecColor.empty())
356 aTextColor <<= m_vecColor[0];
358 bOk = !executeWizard(aTableName,aTextColor,aFont) && m_xTable.is();
360 return bOk;
362 // -----------------------------------------------------------------------------
363 void ORTFReader::release()
365 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::release" );
366 DBG_CHKTHIS(ORTFReader,NULL);
367 ReleaseRef();
370 // -----------------------------------------------------------------------------
371 TypeSelectionPageFactory ORTFReader::getTypeSelectionPageFactory()
373 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::getTypeSelectionPageFactory" );
374 DBG_CHKTHIS(ORTFReader,NULL);
375 return &OWizRTFExtend::Create;
377 // -----------------------------------------------------------------------------