1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
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
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
)
77 , ODatabaseExport( _rxConnection
, _rxNumberF
, _rxContext
, pList
, _pInfoMap
, rIn
)
81 , m_bMetaOptions(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
,
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
)
98 , ODatabaseExport( nRows
, _rColumnPositions
, _rxNumberF
, _rxContext
, pList
, _pInfoMap
, _bAutoIncrementEnabled
, rIn
)
102 , m_bMetaOptions(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
);
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
127 if ( nToken
== HTML_META
)
130 if(m_xConnection
.is()) // names, which CTOR was called and hence, if a table should be created
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() )
144 { // percentage: of document width respectively outer cell
145 m_nColumnWidth
= GetWidthPixel( rOption
);
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
)
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
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
;
191 case HTML_PARABREAK_OFF
:
192 m_sCurrent
+= m_sTextToken
;
194 case HTML_PARABREAK_ON
:
195 m_sTextToken
.clear();
197 case HTML_TABLEDATA_ON
:
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
216 m_bSDNum
= m_bInTbl
= false;
219 case HTML_TABLEROW_OFF
:
220 if ( !m_pUpdateHelper
.get() )
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
241 else // branch only valid for type checking
247 // The head of the column is not included
252 while(GetNextToken() != HTML_TABLEROW_OFF
);
256 case HTML_TABLEDATA_ON
:
257 case HTML_TABLEHEADER_ON
:
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
;
265 case HTML_PARABREAK_OFF
:
266 m_sCurrent
+= m_sTextToken
;
268 case HTML_PARABREAK_ON
:
269 m_sTextToken
.clear();
271 case HTML_TABLEDATA_OFF
:
272 if ( !m_sCurrent
.isEmpty() )
273 m_sTextToken
= m_sCurrent
;
276 m_bSDNum
= m_bInTbl
= false;
279 case HTML_TABLEROW_OFF
:
280 if ( !m_sCurrent
.isEmpty() )
281 m_sTextToken
= m_sCurrent
;
291 void OHTMLReader::fetchOptions()
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() )
302 m_sValToken
= rOption
.GetString();
307 m_sNumToken
= rOption
.GetString();
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() )
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
;
331 eVal
= SVX_HOR_JUSTIFY_STANDARD
;
335 m_nWidth
= GetWidthPixel( rOption
);
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() )
352 rOption
.GetColor( aColor
);
353 _rTextColor
= aColor
.GetRGBColor();
358 const OUString
& rFace
= rOption
.GetString();
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() )
370 if ( !aFontName
.isEmpty() )
371 _rFont
.Name
= aFontName
;
376 sal_Int16 nSize
= (sal_Int16
) rOption
.GetNumber();
379 else if ( nSize
< DBAUI_HTML_FONTSIZES
)
380 nSize
= DBAUI_HTML_FONTSIZES
;
382 _rFont
.Height
= nSize
;
389 sal_Int16
OHTMLReader::GetWidthPixel( const HTMLOption
& rOption
)
391 const OUString
& rOptVal
= rOption
.GetString();
392 if ( rOptVal
.indexOf('%') != -1 )
394 OSL_ENSURE( m_nColumnWidth
, "WIDTH Option: m_nColumnWidth==0 und Width%" );
395 return (sal_Int16
)((rOption
.GetNumber() * m_nColumnWidth
) / 100);
399 if ( rOptVal
.indexOf('*') != -1 )
400 { // relativ to what?!?
401 //TODO: collect ColArray of all relevant values and then MakeCol
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
;
421 FontDescriptor aFont
= VCLUnoHelper::CreateFontDescriptor(Application::GetSettings().GetStyleSettings().GetAppFont());
422 sal_Int32 nTextColor
= 0;
428 case HTML_SINGLECHAR
:
430 aColumnName
+= aToken
;
432 aTableName
+= aToken
;
434 case HTML_PARABREAK_OFF
:
435 m_sCurrent
+= aColumnName
;
437 case HTML_PARABREAK_ON
:
438 m_sTextToken
.clear();
440 case HTML_TABLEDATA_ON
:
441 case HTML_TABLEHEADER_ON
:
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
);
459 eVal
= SVX_HOR_JUSTIFY_STANDARD
;
460 bTableHeader
= false;
465 case HTML_CAPTION_ON
:
469 case HTML_CAPTION_OFF
:
470 aTableName
= comphelper::string::strip(aTableName
, ' ');
471 if(aTableName
.isEmpty())
472 aTableName
= ::dbtools::createUniqueName(m_xTables
, aTableName
);
474 aTableName
= aTempName
;
478 TableFontOn(aFont
,nTextColor
);
481 aFont
.Weight
= ::com::sun::star::awt::FontWeight::BOLD
;
484 aFont
.Slant
= ::com::sun::star::awt::FontSlant_ITALIC
;
486 case HTML_UNDERLINE_ON
:
487 aFont
.Underline
= ::com::sun::star::awt::FontUnderline::SINGLE
;
490 aFont
.Strikeout
= ::com::sun::star::awt::FontStrikeout::SINGLE
;
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() )
506 if(aTableName
.isEmpty())
507 aTableName
= aTempName
;
510 m_bFoundTable
= true;
512 if ( isCheckEnabled() )
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()
529 TypeSelectionPageFactory
OHTMLReader::getTypeSelectionPageFactory()
531 return &OWizHTMLExtend::Create
;
534 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */