Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
59 :
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;
67 :
68 : #define DBAUI_HTML_FONTSIZES 8 // like export, HTML-Options
69 :
70 : // OHTMLReader
71 0 : 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(sal_False)
82 0 : , m_bSDNum(sal_False)
83 : {
84 : SAL_INFO("dbaccess.ui", "OHTMLReader::OHTMLReader" );
85 0 : SetSrcEncoding( GetExtendedCompatibilityTextEncoding( RTL_TEXTENCODING_ISO_8859_1 ) );
86 : // If the file starts with a BOM, switch to UCS2.
87 0 : SetSwitchToUCS2( true );
88 0 : }
89 :
90 0 : OHTMLReader::OHTMLReader(SvStream& rIn,
91 : sal_Int32 nRows,
92 : const TPositions &_rColumnPositions,
93 : const Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
94 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext,
95 : const TColumnVector* pList,
96 : const OTypeInfoMap* _pInfoMap,
97 : sal_Bool _bAutoIncrementEnabled)
98 : : HTMLParser(rIn)
99 : , ODatabaseExport( nRows, _rColumnPositions, _rxNumberF, _rxContext, pList, _pInfoMap, _bAutoIncrementEnabled, rIn )
100 : , m_nTableCount(0)
101 : , m_nWidth(0)
102 : , m_nColumnWidth(87)
103 : , m_bMetaOptions(sal_False)
104 0 : , m_bSDNum(sal_False)
105 : {
106 : SAL_INFO("dbaccess.ui", "OHTMLReader::OHTMLReader" );
107 0 : SetSrcEncoding( GetExtendedCompatibilityTextEncoding( RTL_TEXTENCODING_ISO_8859_1 ) );
108 : // If the file starts with a BOM, switch to UCS2.
109 0 : SetSwitchToUCS2( true );
110 0 : }
111 :
112 0 : OHTMLReader::~OHTMLReader()
113 : {
114 0 : }
115 :
116 0 : SvParserState OHTMLReader::CallParser()
117 : {
118 : SAL_INFO("dbaccess.ui", "OHTMLReader::CallParser" );
119 0 : rInput.Seek(STREAM_SEEK_TO_BEGIN);
120 0 : rInput.ResetError();
121 0 : SvParserState eParseState = HTMLParser::CallParser();
122 0 : SetColumnTypes(m_pColumnList,m_pInfoMap);
123 0 : return m_bFoundTable ? eParseState : SVPAR_ERROR;
124 : }
125 :
126 0 : void OHTMLReader::NextToken( int nToken )
127 : {
128 : SAL_INFO("dbaccess.ui", "OHTMLReader::NextToken" );
129 0 : if(m_bError || !m_nRows) // if there is an error or no more rows to check, return immediatelly
130 0 : return;
131 0 : if ( nToken == HTML_META )
132 0 : setTextEncoding();
133 :
134 0 : if(m_xConnection.is()) // names, which CTOR was called and hence, if a table should be created
135 : {
136 0 : switch(nToken)
137 : {
138 : case HTML_TABLE_ON:
139 0 : ++m_nTableCount;
140 : { // can also be TD or TH, if there was no TABLE before
141 0 : const HTMLOptions& rHtmlOptions = GetOptions();
142 0 : for (size_t i = 0, n = rHtmlOptions.size(); i < n; ++i)
143 : {
144 0 : const HTMLOption& rOption = rHtmlOptions[i];
145 0 : switch( rOption.GetToken() )
146 : {
147 : case HTML_O_WIDTH:
148 : { // percentage: of document width respectively outer cell
149 0 : m_nColumnWidth = GetWidthPixel( rOption );
150 : }
151 0 : break;
152 : }
153 : }
154 : }
155 : case HTML_THEAD_ON:
156 : case HTML_TBODY_ON:
157 : {
158 0 : sal_Size nTell = rInput.Tell(); // perhaps alters position of the stream
159 0 : if ( !m_xTable.is() )
160 : {// use first line as header
161 0 : m_bError = !CreateTable(nToken);
162 0 : if ( m_bAppendFirstLine )
163 0 : rInput.Seek(nTell);
164 : }
165 : }
166 0 : break;
167 : case HTML_TABLE_OFF:
168 0 : if(!--m_nTableCount)
169 : {
170 0 : m_xTable = NULL;
171 : }
172 0 : break;
173 : case HTML_TABLEROW_ON:
174 0 : if ( m_pUpdateHelper.get() )
175 : {
176 : try
177 : {
178 0 : m_pUpdateHelper->moveToInsertRow(); // otherwise append new line
179 : }
180 0 : catch(SQLException& e)
181 : // handling update failure
182 : {
183 0 : showErrorDialog(e);
184 : }
185 : }
186 : else
187 0 : m_bError = sal_True;
188 0 : break;
189 : case HTML_TEXTTOKEN:
190 : case HTML_SINGLECHAR:
191 0 : if ( m_bInTbl ) //&& !m_bSDNum ) // important, as otherwise we also get the names of the fonts
192 0 : m_sTextToken += aToken;
193 0 : break;
194 : case HTML_PARABREAK_OFF:
195 0 : m_sCurrent += m_sTextToken;
196 0 : break;
197 : case HTML_PARABREAK_ON:
198 0 : m_sTextToken = "";
199 0 : break;
200 : case HTML_TABLEDATA_ON:
201 0 : fetchOptions();
202 0 : break;
203 : case HTML_TABLEDATA_OFF:
204 : {
205 0 : if ( !m_sCurrent.isEmpty() )
206 0 : m_sTextToken = m_sCurrent;
207 : try
208 : {
209 0 : insertValueIntoColumn();
210 : }
211 0 : catch(SQLException& e)
212 : // handling update failure
213 : {
214 0 : showErrorDialog(e);
215 : }
216 0 : m_sCurrent = "";
217 0 : m_nColumnPos++;
218 0 : eraseTokens();
219 0 : m_bSDNum = m_bInTbl = sal_False;
220 : }
221 0 : break;
222 : case HTML_TABLEROW_OFF:
223 0 : if ( !m_pUpdateHelper.get() )
224 : {
225 0 : m_bError = sal_True;
226 0 : break;
227 : }
228 : try
229 : {
230 0 : m_nRowCount++;
231 0 : if (m_bIsAutoIncrement) // if bSetAutoIncrement then I have to set the autoincrement
232 0 : m_pUpdateHelper->updateInt(1,m_nRowCount);
233 0 : m_pUpdateHelper->insertRow();
234 : }
235 0 : catch(SQLException& e)
236 : // handling update failure
237 : {
238 0 : showErrorDialog(e);
239 : }
240 0 : m_nColumnPos = 0;
241 0 : break;
242 : }
243 : }
244 : else // branch only valid for type checking
245 : {
246 0 : switch(nToken)
247 : {
248 : case HTML_THEAD_ON:
249 : case HTML_TBODY_ON:
250 : // The head of the column is not included
251 0 : if(m_bHead)
252 : {
253 0 : do
254 : {}
255 0 : while(GetNextToken() != HTML_TABLEROW_OFF);
256 0 : m_bHead = sal_False;
257 : }
258 0 : break;
259 : case HTML_TABLEDATA_ON:
260 : case HTML_TABLEHEADER_ON:
261 0 : fetchOptions();
262 0 : break;
263 : case HTML_TEXTTOKEN:
264 : case HTML_SINGLECHAR:
265 0 : if ( m_bInTbl ) // && !m_bSDNum ) // important, as otherwise we also get the names of the fonts
266 0 : m_sTextToken += aToken;
267 0 : break;
268 : case HTML_PARABREAK_OFF:
269 0 : m_sCurrent += m_sTextToken;
270 0 : break;
271 : case HTML_PARABREAK_ON:
272 0 : m_sTextToken = "";
273 0 : break;
274 : case HTML_TABLEDATA_OFF:
275 0 : if ( !m_sCurrent.isEmpty() )
276 0 : m_sTextToken = m_sCurrent;
277 0 : adjustFormat();
278 0 : m_nColumnPos++;
279 0 : m_bSDNum = m_bInTbl = sal_False;
280 0 : m_sCurrent = "";
281 0 : break;
282 : case HTML_TABLEROW_OFF:
283 0 : if ( !m_sCurrent.isEmpty() )
284 0 : m_sTextToken = m_sCurrent;
285 0 : adjustFormat();
286 0 : m_nColumnPos = 0;
287 0 : m_nRows--;
288 0 : m_sCurrent = "";
289 0 : break;
290 : }
291 : }
292 : }
293 :
294 0 : void OHTMLReader::fetchOptions()
295 : {
296 : SAL_INFO("dbaccess.ui", "OHTMLReader::fetchOptions" );
297 0 : m_bInTbl = sal_True;
298 0 : const HTMLOptions& options = GetOptions();
299 0 : for (size_t i = 0, n = options.size(); i < n; ++i)
300 : {
301 0 : const HTMLOption& rOption = options[i];
302 0 : switch( rOption.GetToken() )
303 : {
304 : case HTML_O_SDVAL:
305 : {
306 0 : m_sValToken = rOption.GetString();
307 0 : m_bSDNum = sal_True;
308 : }
309 0 : break;
310 : case HTML_O_SDNUM:
311 0 : m_sNumToken = rOption.GetString();
312 0 : break;
313 : }
314 : }
315 0 : }
316 :
317 0 : void OHTMLReader::TableDataOn(SvxCellHorJustify& eVal)
318 : {
319 : SAL_INFO("dbaccess.ui", "OHTMLReader::TableDataOn" );
320 0 : const HTMLOptions& rHtmlOptions = GetOptions();
321 0 : for (size_t i = 0, n = rHtmlOptions.size(); i < n; ++i)
322 : {
323 0 : const HTMLOption& rOption = rHtmlOptions[i];
324 0 : switch( rOption.GetToken() )
325 : {
326 : case HTML_O_ALIGN:
327 : {
328 0 : const OUString& rOptVal = rOption.GetString();
329 0 : if (rOptVal.equalsIgnoreAsciiCase( OOO_STRING_SVTOOLS_HTML_AL_right ))
330 0 : eVal = SVX_HOR_JUSTIFY_RIGHT;
331 0 : else if (rOptVal.equalsIgnoreAsciiCase( OOO_STRING_SVTOOLS_HTML_AL_center ))
332 0 : eVal = SVX_HOR_JUSTIFY_CENTER;
333 0 : else if (rOptVal.equalsIgnoreAsciiCase( OOO_STRING_SVTOOLS_HTML_AL_left ))
334 0 : eVal = SVX_HOR_JUSTIFY_LEFT;
335 : else
336 0 : eVal = SVX_HOR_JUSTIFY_STANDARD;
337 : }
338 0 : break;
339 : case HTML_O_WIDTH:
340 0 : m_nWidth = GetWidthPixel( rOption );
341 0 : break;
342 : }
343 : }
344 0 : }
345 :
346 0 : void OHTMLReader::TableFontOn(FontDescriptor& _rFont,sal_Int32 &_rTextColor)
347 : {
348 : SAL_INFO("dbaccess.ui", "OHTMLReader::TableFontOn" );
349 0 : const HTMLOptions& rHtmlOptions = GetOptions();
350 0 : for (size_t i = 0, n = rHtmlOptions.size(); i < n; ++i)
351 : {
352 0 : const HTMLOption& rOption = rHtmlOptions[i];
353 0 : switch( rOption.GetToken() )
354 : {
355 : case HTML_O_COLOR:
356 : {
357 0 : Color aColor;
358 0 : rOption.GetColor( aColor );
359 0 : _rTextColor = aColor.GetRGBColor();
360 : }
361 0 : break;
362 : case HTML_O_FACE :
363 : {
364 0 : const OUString& rFace = rOption.GetString();
365 0 : OUString aFontName;
366 0 : sal_Int32 nPos = 0;
367 0 : while( nPos != -1 )
368 : {
369 : // list fo fonts, VCL: semicolon as separator, HTML: comma
370 0 : OUString aFName = rFace.getToken( 0, ',', nPos );
371 0 : aFName = comphelper::string::strip(aFName, ' ');
372 0 : if( !aFontName.isEmpty() )
373 0 : aFontName += ";";
374 0 : aFontName += aFName;
375 0 : }
376 0 : if ( !aFontName.isEmpty() )
377 0 : _rFont.Name = aFontName;
378 : }
379 0 : break;
380 : case HTML_O_SIZE :
381 : {
382 0 : sal_Int16 nSize = (sal_Int16) rOption.GetNumber();
383 0 : if ( nSize == 0 )
384 0 : nSize = 1;
385 0 : else if ( nSize < DBAUI_HTML_FONTSIZES )
386 0 : nSize = DBAUI_HTML_FONTSIZES;
387 :
388 0 : _rFont.Height = nSize;
389 : }
390 0 : break;
391 : }
392 : }
393 0 : }
394 :
395 0 : sal_Int16 OHTMLReader::GetWidthPixel( const HTMLOption& rOption )
396 : {
397 : SAL_INFO("dbaccess.ui", "OHTMLReader::GetWidthPixel" );
398 0 : const OUString& rOptVal = rOption.GetString();
399 0 : if ( rOptVal.indexOf('%') != -1 )
400 : { // percentage
401 : OSL_ENSURE( m_nColumnWidth, "WIDTH Option: m_nColumnWidth==0 und Width%" );
402 0 : return (sal_Int16)((rOption.GetNumber() * m_nColumnWidth) / 100);
403 : }
404 : else
405 : {
406 0 : if ( rOptVal.indexOf('*') != -1 )
407 : { // relativ to what?!?
408 : //TODO: collect ColArray of all relevant values and then MakeCol
409 0 : return 0;
410 : }
411 : else
412 0 : return (sal_Int16)rOption.GetNumber(); // pixel
413 : }
414 : }
415 :
416 0 : sal_Bool OHTMLReader::CreateTable(int nToken)
417 : {
418 : SAL_INFO("dbaccess.ui", "OHTMLReader::CreateTable" );
419 0 : OUString aTempName(ModuleRes(STR_TBL_TITLE));
420 0 : aTempName = aTempName.getToken(0,' ');
421 0 : aTempName = ::dbtools::createUniqueName(m_xTables, aTempName);
422 :
423 0 : int nTmpToken2 = nToken;
424 0 : sal_Bool bCaption = sal_False;
425 0 : sal_Bool bTableHeader = sal_False;
426 0 : OUString aColumnName;
427 : SvxCellHorJustify eVal;
428 :
429 0 : OUString aTableName;
430 0 : FontDescriptor aFont = VCLUnoHelper::CreateFontDescriptor(Application::GetSettings().GetStyleSettings().GetAppFont());
431 0 : sal_Int32 nTextColor = 0;
432 0 : do
433 : {
434 0 : switch(nTmpToken2)
435 : {
436 : case HTML_TEXTTOKEN:
437 : case HTML_SINGLECHAR:
438 0 : if(bTableHeader)
439 0 : aColumnName += aToken;
440 0 : if(bCaption)
441 0 : aTableName += aToken;
442 0 : break;
443 : case HTML_PARABREAK_OFF:
444 0 : m_sCurrent += aColumnName;
445 0 : break;
446 : case HTML_PARABREAK_ON:
447 0 : m_sTextToken = "";
448 0 : break;
449 : case HTML_TABLEDATA_ON:
450 : case HTML_TABLEHEADER_ON:
451 0 : TableDataOn(eVal);
452 0 : bTableHeader = sal_True;
453 0 : break;
454 : case HTML_TABLEDATA_OFF:
455 : case HTML_TABLEHEADER_OFF:
456 : {
457 0 : aColumnName = comphelper::string::strip(aColumnName, ' ' );
458 0 : if (aColumnName.isEmpty() || m_bAppendFirstLine )
459 0 : aColumnName = ModuleRes(STR_COLUMN_NAME);
460 0 : else if ( !m_sCurrent.isEmpty() )
461 0 : aColumnName = m_sCurrent;
462 :
463 0 : aColumnName = comphelper::string::strip(aColumnName, ' ');
464 0 : CreateDefaultColumn(aColumnName);
465 0 : aColumnName = "";
466 0 : m_sCurrent = "";
467 :
468 0 : eVal = SVX_HOR_JUSTIFY_STANDARD;
469 0 : bTableHeader = sal_False;
470 : }
471 0 : break;
472 :
473 : case HTML_TITLE_ON:
474 : case HTML_CAPTION_ON:
475 0 : bCaption = sal_True;
476 0 : break;
477 : case HTML_TITLE_OFF:
478 : case HTML_CAPTION_OFF:
479 0 : aTableName = comphelper::string::strip(aTableName, ' ');
480 0 : if(aTableName.isEmpty())
481 0 : aTableName = ::dbtools::createUniqueName(m_xTables, aTableName);
482 : else
483 0 : aTableName = aTempName;
484 0 : bCaption = sal_False;
485 0 : break;
486 : case HTML_FONT_ON:
487 0 : TableFontOn(aFont,nTextColor);
488 0 : break;
489 : case HTML_BOLD_ON:
490 0 : aFont.Weight = ::com::sun::star::awt::FontWeight::BOLD;
491 0 : break;
492 : case HTML_ITALIC_ON:
493 0 : aFont.Slant = ::com::sun::star::awt::FontSlant_ITALIC;
494 0 : break;
495 : case HTML_UNDERLINE_ON:
496 0 : aFont.Underline = ::com::sun::star::awt::FontUnderline::SINGLE;
497 0 : break;
498 : case HTML_STRIKE_ON:
499 0 : aFont.Strikeout = ::com::sun::star::awt::FontStrikeout::SINGLE;
500 0 : break;
501 : }
502 : }
503 0 : while((nTmpToken2 = GetNextToken()) != HTML_TABLEROW_OFF);
504 :
505 0 : if ( !m_sCurrent.isEmpty() )
506 0 : aColumnName = m_sCurrent;
507 0 : aColumnName = comphelper::string::strip(aColumnName, ' ');
508 0 : if(!aColumnName.isEmpty())
509 0 : CreateDefaultColumn(aColumnName);
510 :
511 0 : if ( m_vDestVector.empty() )
512 0 : return sal_False;
513 :
514 0 : if(aTableName.isEmpty())
515 0 : aTableName = aTempName;
516 :
517 0 : m_bInTbl = sal_False;
518 0 : m_bFoundTable = sal_True;
519 :
520 0 : if ( isCheckEnabled() )
521 0 : return sal_True;
522 :
523 0 : return !executeWizard(aTableName,makeAny(nTextColor),aFont) && m_xTable.is();
524 : }
525 :
526 0 : void OHTMLReader::setTextEncoding()
527 : {
528 : SAL_INFO("dbaccess.ui", "OHTMLReader::setTextEncoding" );
529 0 : m_bMetaOptions = sal_True;
530 0 : ParseMetaOptions(NULL, NULL);
531 0 : }
532 :
533 0 : void OHTMLReader::release()
534 : {
535 : SAL_INFO("dbaccess.ui", "OHTMLReader::release" );
536 0 : ReleaseRef();
537 0 : }
538 :
539 0 : TypeSelectionPageFactory OHTMLReader::getTypeSelectionPageFactory()
540 : {
541 : SAL_INFO("dbaccess.ui", "OHTMLReader::getTypeSelectionPageFactory" );
542 0 : return &OWizHTMLExtend::Create;
543 : }
544 :
545 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|