1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 #include "RtfReader.hxx"
27 #include <tools/debug.hxx>
28 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
29 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
30 #include <com/sun/star/sdbcx/XAppend.hpp>
31 #include <com/sun/star/sdbc/DataType.hpp>
32 #include <com/sun/star/sdbc/ColumnValue.hpp>
33 #include <com/sun/star/awt/FontDescriptor.hpp>
34 #include <com/sun/star/awt/FontWeight.hpp>
35 #include <com/sun/star/awt/FontStrikeout.hpp>
36 #include <com/sun/star/awt/FontSlant.hpp>
37 #include <com/sun/star/awt/FontUnderline.hpp>
38 #include <com/sun/star/util/NumberFormat.hpp>
39 #include <com/sun/star/util/XNumberFormatTypes.hpp>
40 #include "dbustrings.hrc"
41 #include <svtools/rtftoken.h>
42 #include "dbu_misc.hrc"
43 #include <vcl/msgbox.hxx>
44 #include <connectivity/dbconversion.hxx>
45 #include <connectivity/dbtools.hxx>
46 #include <comphelper/extract.hxx>
47 #include <tools/color.hxx>
48 #include "WExtendPages.hxx"
49 #include "moduledbu.hxx"
50 #include "QEnumTypes.hxx"
51 #include "UITools.hxx"
52 #include <vcl/svapp.hxx>
53 #include <rtl/logfile.hxx>
54 
55 using namespace dbaui;
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::beans;
58 using namespace ::com::sun::star::container;
59 using namespace ::com::sun::star::sdbc;
60 using namespace ::com::sun::star::sdbcx;
61 using namespace ::com::sun::star::awt;
62 
DBG_NAME(ORTFReader)63 DBG_NAME(ORTFReader)
64 // ==========================================================================
65 // ORTFReader
66 // ==========================================================================
67 ORTFReader::ORTFReader(	SvStream& rIn,
68 						const SharedConnection& _rxConnection,
69 						const Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
70 						const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
71 						const TColumnVector* pList,
72 						const OTypeInfoMap* _pInfoMap)
73 	:SvRTFParser(rIn)
74 	,ODatabaseExport( _rxConnection, _rxNumberF, _rM, pList, _pInfoMap, rIn )
75 {
76     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::ORTFReader" );
77 	DBG_CTOR(ORTFReader,NULL);
78     m_bAppendFirstLine = false;
79 }
80 // ---------------------------------------------------------------------------
ORTFReader(SvStream & rIn,sal_Int32 nRows,const TPositions & _rColumnPositions,const Reference<::com::sun::star::util::XNumberFormatter> & _rxNumberF,const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & _rM,const TColumnVector * pList,const OTypeInfoMap * _pInfoMap,sal_Bool _bAutoIncrementEnabled)81 ORTFReader::ORTFReader(SvStream& rIn,
82 					   sal_Int32 nRows,
83 					   const TPositions &_rColumnPositions,
84 					   const Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
85 					   const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
86 					   const TColumnVector* pList,
87 					   const OTypeInfoMap* _pInfoMap,
88                        sal_Bool _bAutoIncrementEnabled)
89    :SvRTFParser(rIn)
90    ,ODatabaseExport( nRows, _rColumnPositions, _rxNumberF, _rM, pList, _pInfoMap, _bAutoIncrementEnabled, rIn )
91 {
92     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::ORTFReader" );
93 	DBG_CTOR(ORTFReader,NULL);
94     m_bAppendFirstLine = false;
95 }
96 // ---------------------------------------------------------------------------
~ORTFReader()97 ORTFReader::~ORTFReader()
98 {
99 	DBG_DTOR(ORTFReader,NULL);
100 }
101 // ---------------------------------------------------------------------------
CallParser()102 SvParserState ORTFReader::CallParser()
103 {
104     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::CallParser" );
105 	DBG_CHKTHIS(ORTFReader,NULL);
106 	rInput.Seek(STREAM_SEEK_TO_BEGIN);
107 	rInput.ResetError();
108 	SvParserState  eParseState = SvRTFParser::CallParser();
109 	SetColumnTypes(m_pColumnList,m_pInfoMap);
110 	return m_bFoundTable ? eParseState : SVPAR_ERROR;
111 }
112 // ---------------------------------------------------------------------------
NextToken(int nToken)113 void ORTFReader::NextToken( int nToken )
114 {
115     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::NextToken" );
116 	DBG_CHKTHIS(ORTFReader,NULL);
117 	if(m_bError || !m_nRows) // falls Fehler oder keine Rows mehr zur "Uberpr"ufung dann gleich zur"uck
118 		return;
119 
120 	if(m_xConnection.is())	  // gibt an welcher CTOR gerufen wurde und damit, ob eine Tabelle erstellt werden soll
121 	{
122 		switch(nToken)
123 		{
124 			case RTF_COLORTBL:
125 				{
126 
127 					int nTmpToken2 = GetNextToken();
128 					sal_Bool bNext = sal_True;
129 					do
130 					{
131 						Color aColor;
132 						do
133 						{
134 							switch(nTmpToken2)
135 							{
136 								case RTF_RED:	aColor.SetRed((sal_uInt8)nTokenValue); break;
137 								case RTF_BLUE:	aColor.SetBlue((sal_uInt8)nTokenValue); break;
138 								case RTF_GREEN:	aColor.SetGreen((sal_uInt8)nTokenValue); break;
139 								default:
140 									bNext = sal_False;
141 							}
142 							nTmpToken2 = GetNextToken();
143 						}
144 						while(aToken.GetChar(0) != ';' && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
145 						m_vecColor.push_back(aColor.GetRGBColor());
146 						nTmpToken2 = GetNextToken();
147 					}
148 					while(nTmpToken2 == RTF_RED && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
149 					SkipToken();
150 				}
151 				break;
152 
153 			case RTF_DEFLANG:
154 			case RTF_LANG: // Sprache abfragen
155 				m_nDefToken = (rtl_TextEncoding)nTokenValue;
156 				break;
157 			case RTF_TROWD:
158                 {
159                     bool bInsertRow = true;
160 				    if ( !m_xTable.is() ) // erste Zeile als Header verwenden
161                     {
162                         sal_uInt32 nTell = rInput.Tell(); // ver�ndert vielleicht die Position des Streams
163 
164 					    m_bError = !CreateTable(nToken);
165                         bInsertRow = m_bAppendFirstLine;
166                         if ( m_bAppendFirstLine )
167                         {
168                             rInput.Seek(nTell);
169                             rInput.ResetError();
170                         }
171                     }
172 				    if ( bInsertRow && !m_bError)
173 				    {
174 					    try
175 					    {
176 						    m_pUpdateHelper->moveToInsertRow(); // sonst neue Zeile anh"angen
177 					    }
178 					    catch(SQLException& e)
179 					    // UpdateFehlerbehandlung
180 					    {
181 						    showErrorDialog(e);
182 					    }
183 				    }
184                 }
185 				break;
186 			case RTF_INTBL:
187 				if(m_bInTbl)
188                 {
189                     eraseTokens();
190                 }
191 
192 				m_bInTbl = sal_True; // jetzt befinden wir uns in einer Tabellenbeschreibung
193 				break;
194 			case RTF_TEXTTOKEN:
195 			case RTF_SINGLECHAR:
196 				if(m_bInTbl) // wichtig, da wir sonst auch die Namen der Fonts bekommen
197 					m_sTextToken += aToken;
198 				break;
199 			case RTF_CELL:
200 				{
201 					try
202 					{
203 						insertValueIntoColumn();
204 					}
205 					catch(SQLException& e)
206 					// UpdateFehlerbehandlung
207 					{
208 						showErrorDialog(e);
209 					}
210 					m_nColumnPos++;
211                     eraseTokens();
212 				}
213 				break;
214 			case RTF_ROW:
215 				// es kann vorkommen, da� die letzte Celle nicht mit \cell abgeschlossen ist
216 				try
217 				{
218 					insertValueIntoColumn();
219 					m_nRowCount++;
220 					if(m_bIsAutoIncrement) // if bSetAutoIncrement then I have to set the autoincrement
221 						m_pUpdateHelper->updateInt(1,m_nRowCount);
222 					m_pUpdateHelper->insertRow();
223 				}
224 				catch(SQLException& e)
225 				//////////////////////////////////////////////////////////////////////
226 				// UpdateFehlerbehandlung
227 				{
228 					showErrorDialog(e);
229 				}
230 				m_nColumnPos = 0;
231 				break;
232 		}
233 	}
234 	else // Zweig nur f"ur Typpr"ufung g"ultig
235 	{
236 		switch(nToken)
237 		{
238 			case RTF_TROWD:
239 				// Der Spalten Kopf z"ahlt nicht mit
240 				if(m_bHead)
241 				{
242 					do
243 					{}
244 					while(GetNextToken() != RTF_ROW && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
245 					m_bHead = sal_False;
246 				}
247 				break;
248 			case RTF_INTBL:
249 				m_bInTbl = sal_True;
250 				break;
251 			case RTF_TEXTTOKEN:
252 			case RTF_SINGLECHAR:
253 				if(m_bInTbl)
254 					m_sTextToken += aToken;
255 				break;
256 			case RTF_CELL:
257                 adjustFormat();
258 				m_nColumnPos++;
259 				break;
260 			case RTF_ROW:
261                 adjustFormat();
262 				m_nColumnPos = 0;
263 				m_nRows--;
264 				break;
265 		}
266 	}
267 }
268 // ---------------------------------------------------------------------------
CreateTable(int nToken)269 sal_Bool ORTFReader::CreateTable(int nToken)
270 {
271     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::CreateTable" );
272 	DBG_CHKTHIS(ORTFReader,NULL);
273 	String aTableName(ModuleRes(STR_TBL_TITLE));
274 	aTableName = aTableName.GetToken(0,' ');
275 	aTableName = String(::dbtools::createUniqueName(m_xTables,::rtl::OUString(aTableName)));
276 
277 	int nTmpToken2 = nToken;
278 	String aColumnName;
279 
280 	FontDescriptor aFont = ::dbaui::CreateFontDescriptor(Application::GetSettings().GetStyleSettings().GetAppFont());
281 	do
282 	{
283 		switch(nTmpToken2)
284 		{
285 			case RTF_UNKNOWNCONTROL:
286 			case RTF_UNKNOWNDATA:
287 				m_bInTbl = sal_False;
288 				aColumnName.Erase();
289 				break;
290 			case RTF_INTBL:
291 				if(m_bInTbl)
292 					aColumnName.Erase();
293 
294 				m_bInTbl = sal_True;
295 				break;
296 			case RTF_TEXTTOKEN:
297 			case RTF_SINGLECHAR:
298 				if(m_bInTbl)
299 					aColumnName += aToken;
300 				break;
301 			case RTF_CELL:
302 				{
303 					aColumnName.EraseLeadingChars();
304 					aColumnName.EraseTrailingChars();
305 					if (!aColumnName.Len() || m_bAppendFirstLine )
306 						aColumnName = String(ModuleRes(STR_COLUMN_NAME));
307 
308 					CreateDefaultColumn(aColumnName);
309 					aColumnName.Erase();
310 				}
311 				break;
312 			case RTF_CF:
313 				//	if(nTokenValue < m_vecColor.size())
314 					//	m_xTable->setPropertyValue(PROPERTY_TEXTCOLOR,makeAny(m_vecColor[nTokenValue]));
315 				break;
316 			case RTF_B:
317 				aFont.Weight = ::com::sun::star::awt::FontWeight::BOLD;
318 				break;
319 			case RTF_I:
320 				aFont.Slant = ::com::sun::star::awt::FontSlant_ITALIC;
321 				break;
322 			case RTF_UL:
323 				aFont.Underline = ::com::sun::star::awt::FontUnderline::SINGLE;
324 				break;
325 			case RTF_STRIKE:
326 				aFont.Strikeout = ::com::sun::star::awt::FontStrikeout::SINGLE;
327 				break;
328 		}
329 	}
330 	while((nTmpToken2 = GetNextToken()) != RTF_TROWD && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
331 
332 	sal_Bool bOk = !m_vDestVector.empty();
333 	if(bOk)
334 	{
335 		if ( aColumnName.Len() )
336         {
337             if ( m_bAppendFirstLine )
338 				aColumnName = String(ModuleRes(STR_COLUMN_NAME));
339 			CreateDefaultColumn(aColumnName);
340         }
341 
342 		m_bInTbl		= sal_False;
343 		m_bFoundTable	= sal_True;
344 
345 		if ( isCheckEnabled() )
346 			return sal_True;
347 		Any aTextColor;
348 		if(!m_vecColor.empty())
349 			aTextColor <<= m_vecColor[0];
350 
351 		bOk = !executeWizard(aTableName,aTextColor,aFont) && m_xTable.is();
352 	}
353 	return bOk;
354 }
355 // -----------------------------------------------------------------------------
release()356 void ORTFReader::release()
357 {
358     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::release" );
359 	DBG_CHKTHIS(ORTFReader,NULL);
360 	ReleaseRef();
361 }
362 
363 // -----------------------------------------------------------------------------
getTypeSelectionPageFactory()364 TypeSelectionPageFactory ORTFReader::getTypeSelectionPageFactory()
365 {
366     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::getTypeSelectionPageFactory" );
367 	DBG_CHKTHIS(ORTFReader,NULL);
368     return &OWizRTFExtend::Create;
369 }
370 // -----------------------------------------------------------------------------
371 
372 
373 
374