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