Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ui / misc / HtmlReader.cxx
blob436995b63cf32bf8adef211c4a615ee0f254c1da
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "HtmlReader.hxx"
21 #include <connectivity/dbconversion.hxx>
22 #include <connectivity/dbtools.hxx>
23 #include <toolkit/helper/vclunohelper.hxx>
24 #include <tools/tenccvt.hxx>
25 #include <comphelper/extract.hxx>
26 #include <comphelper/string.hxx>
27 #include "dbu_misc.hrc"
28 #include "dbustrings.hrc"
29 #include <sfx2/sfxhtml.hxx>
30 #include <osl/diagnose.h>
31 #include "moduledbu.hxx"
32 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
33 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
34 #include <com/sun/star/sdbcx/XAppend.hpp>
35 #include <com/sun/star/sdbc/DataType.hpp>
36 #include <com/sun/star/sdbc/ColumnValue.hpp>
37 #include <com/sun/star/awt/FontDescriptor.hpp>
38 #include <com/sun/star/awt/FontWeight.hpp>
39 #include <com/sun/star/awt/FontStrikeout.hpp>
40 #include <com/sun/star/awt/FontSlant.hpp>
41 #include <com/sun/star/awt/FontUnderline.hpp>
42 #include <com/sun/star/util/NumberFormat.hpp>
43 #include <com/sun/star/util/XNumberFormatTypes.hpp>
44 #include <svtools/htmltokn.h>
45 #include <svtools/htmlkywd.hxx>
46 #include <tools/color.hxx>
47 #include "WCopyTable.hxx"
48 #include "WExtendPages.hxx"
49 #include "WNameMatch.hxx"
50 #include "WColumnSelect.hxx"
51 #include "QEnumTypes.hxx"
52 #include "WCPage.hxx"
53 #include <tools/inetmime.hxx>
54 #include <svl/inettype.hxx>
55 #include <rtl/tencinfo.h>
56 #include "UITools.hxx"
57 #include <vcl/svapp.hxx>
58 #include <vcl/settings.hxx>
60 using namespace dbaui;
61 using namespace ::com::sun::star::uno;
62 using namespace ::com::sun::star::beans;
63 using namespace ::com::sun::star::container;
64 using namespace ::com::sun::star::sdbc;
65 using namespace ::com::sun::star::sdbcx;
66 using namespace ::com::sun::star::awt;
68 #define DBAUI_HTML_FONTSIZES 8 // like export, HTML-Options
70 // OHTMLReader
71 OHTMLReader::OHTMLReader(SvStream& rIn,const SharedConnection& _rxConnection,
72 const Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
73 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext,
74 const TColumnVector* pList,
75 const OTypeInfoMap* _pInfoMap)
76 : HTMLParser(rIn)
77 , ODatabaseExport( _rxConnection, _rxNumberF, _rxContext, pList, _pInfoMap, rIn )
78 , m_nTableCount(0)
79 , m_nWidth(0)
80 , m_nColumnWidth(87)
81 , m_bMetaOptions(false)
82 , m_bSDNum(false)
84 SetSrcEncoding( GetExtendedCompatibilityTextEncoding( RTL_TEXTENCODING_ISO_8859_1 ) );
85 // If the file starts with a BOM, switch to UCS2.
86 SetSwitchToUCS2( true );
89 OHTMLReader::OHTMLReader(SvStream& rIn,
90 sal_Int32 nRows,
91 const TPositions &_rColumnPositions,
92 const Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
93 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext,
94 const TColumnVector* pList,
95 const OTypeInfoMap* _pInfoMap,
96 bool _bAutoIncrementEnabled)
97 : HTMLParser(rIn)
98 , ODatabaseExport( nRows, _rColumnPositions, _rxNumberF, _rxContext, pList, _pInfoMap, _bAutoIncrementEnabled, rIn )
99 , m_nTableCount(0)
100 , m_nWidth(0)
101 , m_nColumnWidth(87)
102 , m_bMetaOptions(false)
103 , m_bSDNum(false)
105 SetSrcEncoding( GetExtendedCompatibilityTextEncoding( RTL_TEXTENCODING_ISO_8859_1 ) );
106 // If the file starts with a BOM, switch to UCS2.
107 SetSwitchToUCS2( true );
110 OHTMLReader::~OHTMLReader()
114 SvParserState OHTMLReader::CallParser()
116 rInput.Seek(STREAM_SEEK_TO_BEGIN);
117 rInput.ResetError();
118 SvParserState eParseState = HTMLParser::CallParser();
119 SetColumnTypes(m_pColumnList,m_pInfoMap);
120 return m_bFoundTable ? eParseState : SVPAR_ERROR;
123 void OHTMLReader::NextToken( int nToken )
125 if(m_bError || !m_nRows) // if there is an error or no more rows to check, return immediatelly
126 return;
127 if ( nToken == HTML_META )
128 setTextEncoding();
130 if(m_xConnection.is()) // names, which CTOR was called and hence, if a table should be created
132 switch(nToken)
134 case HTML_TABLE_ON:
135 ++m_nTableCount;
136 { // can also be TD or TH, if there was no TABLE before
137 const HTMLOptions& rHtmlOptions = GetOptions();
138 for (size_t i = 0, n = rHtmlOptions.size(); i < n; ++i)
140 const HTMLOption& rOption = rHtmlOptions[i];
141 switch( rOption.GetToken() )
143 case HTML_O_WIDTH:
144 { // percentage: of document width respectively outer cell
145 m_nColumnWidth = GetWidthPixel( rOption );
147 break;
151 //fall-through
152 case HTML_THEAD_ON:
153 case HTML_TBODY_ON:
155 sal_Size nTell = rInput.Tell(); // perhaps alters position of the stream
156 if ( !m_xTable.is() )
157 {// use first line as header
158 m_bError = !CreateTable(nToken);
159 if ( m_bAppendFirstLine )
160 rInput.Seek(nTell);
163 break;
164 case HTML_TABLE_OFF:
165 if(!--m_nTableCount)
167 m_xTable = NULL;
169 break;
170 case HTML_TABLEROW_ON:
171 if ( m_pUpdateHelper.get() )
175 m_pUpdateHelper->moveToInsertRow(); // otherwise append new line
177 catch(SQLException& e)
178 // handling update failure
180 showErrorDialog(e);
183 else
184 m_bError = true;
185 break;
186 case HTML_TEXTTOKEN:
187 case HTML_SINGLECHAR:
188 if ( m_bInTbl ) //&& !m_bSDNum ) // important, as otherwise we also get the names of the fonts
189 m_sTextToken += aToken;
190 break;
191 case HTML_PARABREAK_OFF:
192 m_sCurrent += m_sTextToken;
193 break;
194 case HTML_PARABREAK_ON:
195 m_sTextToken.clear();
196 break;
197 case HTML_TABLEDATA_ON:
198 fetchOptions();
199 break;
200 case HTML_TABLEDATA_OFF:
202 if ( !m_sCurrent.isEmpty() )
203 m_sTextToken = m_sCurrent;
206 insertValueIntoColumn();
208 catch(SQLException& e)
209 // handling update failure
211 showErrorDialog(e);
213 m_sCurrent.clear();
214 m_nColumnPos++;
215 eraseTokens();
216 m_bSDNum = m_bInTbl = false;
218 break;
219 case HTML_TABLEROW_OFF:
220 if ( !m_pUpdateHelper.get() )
222 m_bError = true;
223 break;
227 m_nRowCount++;
228 if (m_bIsAutoIncrement) // if bSetAutoIncrement then I have to set the autoincrement
229 m_pUpdateHelper->updateInt(1,m_nRowCount);
230 m_pUpdateHelper->insertRow();
232 catch(SQLException& e)
233 // handling update failure
235 showErrorDialog(e);
237 m_nColumnPos = 0;
238 break;
241 else // branch only valid for type checking
243 switch(nToken)
245 case HTML_THEAD_ON:
246 case HTML_TBODY_ON:
247 // The head of the column is not included
248 if(m_bHead)
252 while(GetNextToken() != HTML_TABLEROW_OFF);
253 m_bHead = false;
255 break;
256 case HTML_TABLEDATA_ON:
257 case HTML_TABLEHEADER_ON:
258 fetchOptions();
259 break;
260 case HTML_TEXTTOKEN:
261 case HTML_SINGLECHAR:
262 if ( m_bInTbl ) // && !m_bSDNum ) // important, as otherwise we also get the names of the fonts
263 m_sTextToken += aToken;
264 break;
265 case HTML_PARABREAK_OFF:
266 m_sCurrent += m_sTextToken;
267 break;
268 case HTML_PARABREAK_ON:
269 m_sTextToken.clear();
270 break;
271 case HTML_TABLEDATA_OFF:
272 if ( !m_sCurrent.isEmpty() )
273 m_sTextToken = m_sCurrent;
274 adjustFormat();
275 m_nColumnPos++;
276 m_bSDNum = m_bInTbl = false;
277 m_sCurrent.clear();
278 break;
279 case HTML_TABLEROW_OFF:
280 if ( !m_sCurrent.isEmpty() )
281 m_sTextToken = m_sCurrent;
282 adjustFormat();
283 m_nColumnPos = 0;
284 m_nRows--;
285 m_sCurrent.clear();
286 break;
291 void OHTMLReader::fetchOptions()
293 m_bInTbl = true;
294 const HTMLOptions& options = GetOptions();
295 for (size_t i = 0, n = options.size(); i < n; ++i)
297 const HTMLOption& rOption = options[i];
298 switch( rOption.GetToken() )
300 case HTML_O_SDVAL:
302 m_sValToken = rOption.GetString();
303 m_bSDNum = true;
305 break;
306 case HTML_O_SDNUM:
307 m_sNumToken = rOption.GetString();
308 break;
313 void OHTMLReader::TableDataOn(SvxCellHorJustify& eVal)
315 const HTMLOptions& rHtmlOptions = GetOptions();
316 for (size_t i = 0, n = rHtmlOptions.size(); i < n; ++i)
318 const HTMLOption& rOption = rHtmlOptions[i];
319 switch( rOption.GetToken() )
321 case HTML_O_ALIGN:
323 const OUString& rOptVal = rOption.GetString();
324 if (rOptVal.equalsIgnoreAsciiCase( OOO_STRING_SVTOOLS_HTML_AL_right ))
325 eVal = SVX_HOR_JUSTIFY_RIGHT;
326 else if (rOptVal.equalsIgnoreAsciiCase( OOO_STRING_SVTOOLS_HTML_AL_center ))
327 eVal = SVX_HOR_JUSTIFY_CENTER;
328 else if (rOptVal.equalsIgnoreAsciiCase( OOO_STRING_SVTOOLS_HTML_AL_left ))
329 eVal = SVX_HOR_JUSTIFY_LEFT;
330 else
331 eVal = SVX_HOR_JUSTIFY_STANDARD;
333 break;
334 case HTML_O_WIDTH:
335 m_nWidth = GetWidthPixel( rOption );
336 break;
341 void OHTMLReader::TableFontOn(FontDescriptor& _rFont,sal_Int32 &_rTextColor)
343 const HTMLOptions& rHtmlOptions = GetOptions();
344 for (size_t i = 0, n = rHtmlOptions.size(); i < n; ++i)
346 const HTMLOption& rOption = rHtmlOptions[i];
347 switch( rOption.GetToken() )
349 case HTML_O_COLOR:
351 Color aColor;
352 rOption.GetColor( aColor );
353 _rTextColor = aColor.GetRGBColor();
355 break;
356 case HTML_O_FACE :
358 const OUString& rFace = rOption.GetString();
359 OUString aFontName;
360 sal_Int32 nPos = 0;
361 while( nPos != -1 )
363 // list fo fonts, VCL: semicolon as separator, HTML: comma
364 OUString aFName = rFace.getToken( 0, ',', nPos );
365 aFName = comphelper::string::strip(aFName, ' ');
366 if( !aFontName.isEmpty() )
367 aFontName += ";";
368 aFontName += aFName;
370 if ( !aFontName.isEmpty() )
371 _rFont.Name = aFontName;
373 break;
374 case HTML_O_SIZE :
376 sal_Int16 nSize = (sal_Int16) rOption.GetNumber();
377 if ( nSize == 0 )
378 nSize = 1;
379 else if ( nSize < DBAUI_HTML_FONTSIZES )
380 nSize = DBAUI_HTML_FONTSIZES;
382 _rFont.Height = nSize;
384 break;
389 sal_Int16 OHTMLReader::GetWidthPixel( const HTMLOption& rOption )
391 const OUString& rOptVal = rOption.GetString();
392 if ( rOptVal.indexOf('%') != -1 )
393 { // percentage
394 OSL_ENSURE( m_nColumnWidth, "WIDTH Option: m_nColumnWidth==0 und Width%" );
395 return (sal_Int16)((rOption.GetNumber() * m_nColumnWidth) / 100);
397 else
399 if ( rOptVal.indexOf('*') != -1 )
400 { // relativ to what?!?
401 //TODO: collect ColArray of all relevant values and then MakeCol
402 return 0;
404 else
405 return (sal_Int16)rOption.GetNumber(); // pixel
409 bool OHTMLReader::CreateTable(int nToken)
411 OUString aTempName(ModuleRes(STR_TBL_TITLE));
412 aTempName = aTempName.getToken(0,' ');
413 aTempName = ::dbtools::createUniqueName(m_xTables, aTempName);
415 bool bCaption = false;
416 bool bTableHeader = false;
417 OUString aColumnName;
418 SvxCellHorJustify eVal;
420 OUString aTableName;
421 FontDescriptor aFont = VCLUnoHelper::CreateFontDescriptor(Application::GetSettings().GetStyleSettings().GetAppFont());
422 sal_Int32 nTextColor = 0;
425 switch (nToken)
427 case HTML_TEXTTOKEN:
428 case HTML_SINGLECHAR:
429 if(bTableHeader)
430 aColumnName += aToken;
431 if(bCaption)
432 aTableName += aToken;
433 break;
434 case HTML_PARABREAK_OFF:
435 m_sCurrent += aColumnName;
436 break;
437 case HTML_PARABREAK_ON:
438 m_sTextToken.clear();
439 break;
440 case HTML_TABLEDATA_ON:
441 case HTML_TABLEHEADER_ON:
442 TableDataOn(eVal);
443 bTableHeader = true;
444 break;
445 case HTML_TABLEDATA_OFF:
446 case HTML_TABLEHEADER_OFF:
448 aColumnName = comphelper::string::strip(aColumnName, ' ' );
449 if (aColumnName.isEmpty() || m_bAppendFirstLine )
450 aColumnName = ModuleRes(STR_COLUMN_NAME);
451 else if ( !m_sCurrent.isEmpty() )
452 aColumnName = m_sCurrent;
454 aColumnName = comphelper::string::strip(aColumnName, ' ');
455 CreateDefaultColumn(aColumnName);
456 aColumnName.clear();
457 m_sCurrent.clear();
459 eVal = SVX_HOR_JUSTIFY_STANDARD;
460 bTableHeader = false;
462 break;
464 case HTML_TITLE_ON:
465 case HTML_CAPTION_ON:
466 bCaption = true;
467 break;
468 case HTML_TITLE_OFF:
469 case HTML_CAPTION_OFF:
470 aTableName = comphelper::string::strip(aTableName, ' ');
471 if(aTableName.isEmpty())
472 aTableName = ::dbtools::createUniqueName(m_xTables, aTableName);
473 else
474 aTableName = aTempName;
475 bCaption = false;
476 break;
477 case HTML_FONT_ON:
478 TableFontOn(aFont,nTextColor);
479 break;
480 case HTML_BOLD_ON:
481 aFont.Weight = ::com::sun::star::awt::FontWeight::BOLD;
482 break;
483 case HTML_ITALIC_ON:
484 aFont.Slant = ::com::sun::star::awt::FontSlant_ITALIC;
485 break;
486 case HTML_UNDERLINE_ON:
487 aFont.Underline = ::com::sun::star::awt::FontUnderline::SINGLE;
488 break;
489 case HTML_STRIKE_ON:
490 aFont.Strikeout = ::com::sun::star::awt::FontStrikeout::SINGLE;
491 break;
493 nToken = GetNextToken();
495 while (nToken != HTML_TABLEROW_OFF);
497 if ( !m_sCurrent.isEmpty() )
498 aColumnName = m_sCurrent;
499 aColumnName = comphelper::string::strip(aColumnName, ' ');
500 if(!aColumnName.isEmpty())
501 CreateDefaultColumn(aColumnName);
503 if ( m_vDestVector.empty() )
504 return false;
506 if(aTableName.isEmpty())
507 aTableName = aTempName;
509 m_bInTbl = false;
510 m_bFoundTable = true;
512 if ( isCheckEnabled() )
513 return true;
515 return !executeWizard(aTableName,makeAny(nTextColor),aFont) && m_xTable.is();
518 void OHTMLReader::setTextEncoding()
520 m_bMetaOptions = true;
521 ParseMetaOptions(NULL, NULL);
524 void OHTMLReader::release()
526 ReleaseRef();
529 TypeSelectionPageFactory OHTMLReader::getTypeSelectionPageFactory()
531 return &OWizHTMLExtend::Create;
534 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */