1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: RtfReader.cxx,v $
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
;
71 // ==========================================================================
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
)
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
,
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
)
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
);
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
127 if(m_xConnection
.is()) // gibt an welcher CTOR gerufen wurde und damit, ob eine Tabelle erstellt werden soll
134 int nTmpToken2
= GetNextToken();
135 sal_Bool bNext
= TRUE
;
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;
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
);
161 case RTF_LANG
: // Sprache abfragen
162 m_nDefToken
= (rtl_TextEncoding
)nTokenValue
;
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
)
179 if ( bInsertRow
&& !m_bError
)
183 m_pUpdateHelper
->moveToInsertRow(); // sonst neue Zeile anh"angen
185 catch(SQLException
& e
)
186 // UpdateFehlerbehandlung
199 m_bInTbl
= TRUE
; // jetzt befinden wir uns in einer Tabellenbeschreibung
203 if(m_bInTbl
) // wichtig, da wir sonst auch die Namen der Fonts bekommen
204 m_sTextToken
+= aToken
;
210 insertValueIntoColumn();
212 catch(SQLException
& e
)
213 // UpdateFehlerbehandlung
222 // es kann vorkommen, daß die letzte Celle nicht mit \cell abgeschlossen ist
225 insertValueIntoColumn();
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
241 else // Zweig nur f"ur Typpr"ufung g"ultig
246 // Der Spalten Kopf z"ahlt nicht mit
251 while(GetNextToken() != RTF_ROW
&& eState
!= SVPAR_ERROR
&& eState
!= SVPAR_ACCEPTED
);
261 m_sTextToken
+= aToken
;
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
;
287 FontDescriptor aFont
= ::dbaui::CreateFontDescriptor(Application::GetSettings().GetStyleSettings().GetAppFont());
292 case RTF_UNKNOWNCONTROL
:
293 case RTF_UNKNOWNDATA
:
294 m_bInTbl
= sal_False
;
306 aColumnName
+= aToken
;
310 aColumnName
.EraseLeadingChars();
311 aColumnName
.EraseTrailingChars();
312 if (!aColumnName
.Len() || m_bAppendFirstLine
)
313 aColumnName
= String(ModuleRes(STR_COLUMN_NAME
));
315 CreateDefaultColumn(aColumnName
);
320 // if(nTokenValue < m_vecColor.size())
321 // m_xTable->setPropertyValue(PROPERTY_TEXTCOLOR,makeAny(m_vecColor[nTokenValue]));
324 aFont
.Weight
= ::com::sun::star::awt::FontWeight::BOLD
;
327 aFont
.Slant
= ::com::sun::star::awt::FontSlant_ITALIC
;
330 aFont
.Underline
= ::com::sun::star::awt::FontUnderline::SINGLE
;
333 aFont
.Strikeout
= ::com::sun::star::awt::FontStrikeout::SINGLE
;
337 while((nTmpToken2
= GetNextToken()) != RTF_TROWD
&& eState
!= SVPAR_ERROR
&& eState
!= SVPAR_ACCEPTED
);
339 sal_Bool bOk
= !m_vDestVector
.empty();
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() )
355 if(!m_vecColor
.empty())
356 aTextColor
<<= m_vecColor
[0];
358 bOk
= !executeWizard(aTableName
,aTextColor
,aFont
) && m_xTable
.is();
362 // -----------------------------------------------------------------------------
363 void ORTFReader::release()
365 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "misc", "Ocke.Janssen@sun.com", "ORTFReader::release" );
366 DBG_CHKTHIS(ORTFReader
,NULL
);
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 // -----------------------------------------------------------------------------