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 "scitems.hxx"
21 : #include <comphelper/string.hxx>
22 : #include <editeng/eeitem.hxx>
23 :
24 : #include <editeng/lrspitem.hxx>
25 : #include <editeng/paperinf.hxx>
26 : #include <editeng/sizeitem.hxx>
27 : #include <editeng/ulspitem.hxx>
28 : #include <editeng/boxitem.hxx>
29 : #include <vcl/svapp.hxx>
30 :
31 : #include "htmlimp.hxx"
32 : #include "htmlpars.hxx"
33 : #include "filter.hxx"
34 : #include "global.hxx"
35 : #include "document.hxx"
36 : #include "editutil.hxx"
37 : #include "stlpool.hxx"
38 : #include "stlsheet.hxx"
39 : #include "compiler.hxx"
40 : #include "rangenam.hxx"
41 : #include "attrib.hxx"
42 : #include "ftools.hxx"
43 : #include "tokenarray.hxx"
44 :
45 1 : FltError ScFormatFilterPluginImpl::ScImportHTML( SvStream &rStream, const OUString& rBaseURL, ScDocument *pDoc,
46 : ScRange& rRange, double nOutputFactor, bool bCalcWidthHeight, SvNumberFormatter* pFormatter,
47 : bool bConvertDate )
48 : {
49 1 : ScHTMLImport aImp( pDoc, rBaseURL, rRange, bCalcWidthHeight );
50 1 : FltError nErr = (FltError) aImp.Read( rStream, rBaseURL );
51 1 : ScRange aR = aImp.GetRange();
52 1 : rRange.aEnd = aR.aEnd;
53 1 : aImp.WriteToDocument( true, nOutputFactor, pFormatter, bConvertDate );
54 1 : return nErr;
55 : }
56 :
57 0 : ScEEAbsImport *ScFormatFilterPluginImpl::CreateHTMLImport( ScDocument* pDocP, const OUString& rBaseURL, const ScRange& rRange, bool bCalcWidthHeight )
58 : {
59 0 : return new ScHTMLImport( pDocP, rBaseURL, rRange, bCalcWidthHeight );
60 : }
61 :
62 1 : ScHTMLImport::ScHTMLImport( ScDocument* pDocP, const OUString& rBaseURL, const ScRange& rRange, bool bCalcWidthHeight ) :
63 1 : ScEEImport( pDocP, rRange )
64 : {
65 1 : Size aPageSize;
66 1 : OutputDevice* pDefaultDev = Application::GetDefaultDevice();
67 1 : const OUString& aPageStyle = mpDoc->GetPageStyle( rRange.aStart.Tab() );
68 : ScStyleSheet* pStyleSheet = static_cast<ScStyleSheet*>(mpDoc->
69 1 : GetStyleSheetPool()->Find( aPageStyle, SFX_STYLE_FAMILY_PAGE ));
70 1 : if ( pStyleSheet )
71 : {
72 1 : const SfxItemSet& rSet = pStyleSheet->GetItemSet();
73 1 : const SvxLRSpaceItem* pLRItem = static_cast<const SvxLRSpaceItem*>( &rSet.Get( ATTR_LRSPACE ) );
74 1 : long nLeftMargin = pLRItem->GetLeft();
75 1 : long nRightMargin = pLRItem->GetRight();
76 1 : const SvxULSpaceItem* pULItem = static_cast<const SvxULSpaceItem*>( &rSet.Get( ATTR_ULSPACE ) );
77 1 : long nTopMargin = pULItem->GetUpper();
78 1 : long nBottomMargin = pULItem->GetLower();
79 1 : aPageSize = static_cast<const SvxSizeItem&>(rSet.Get(ATTR_PAGE_SIZE)).GetSize();
80 1 : if ( !aPageSize.Width() || !aPageSize.Height() )
81 : {
82 : OSL_FAIL("PageSize Null ?!?!?");
83 0 : aPageSize = SvxPaperInfo::GetPaperSize( PAPER_A4 );
84 : }
85 1 : aPageSize.Width() -= nLeftMargin + nRightMargin;
86 1 : aPageSize.Height() -= nTopMargin + nBottomMargin;
87 1 : aPageSize = pDefaultDev->LogicToPixel( aPageSize, MapMode( MAP_TWIP ) );
88 : }
89 : else
90 : {
91 : OSL_FAIL("no StyleSheet?!?");
92 : aPageSize = pDefaultDev->LogicToPixel(
93 0 : SvxPaperInfo::GetPaperSize( PAPER_A4 ), MapMode( MAP_TWIP ) );
94 : }
95 1 : if( bCalcWidthHeight )
96 0 : mpParser = new ScHTMLLayoutParser( mpEngine, rBaseURL, aPageSize, pDocP );
97 : else
98 1 : mpParser = new ScHTMLQueryParser( mpEngine, pDocP );
99 1 : }
100 :
101 2 : ScHTMLImport::~ScHTMLImport()
102 : {
103 : // Ordering is important, otherwise we get an error in some other Dtor!
104 : // OK, as ScEEImport is the Base Class
105 1 : delete static_cast<ScHTMLParser*>(mpParser); // before EditEngine!
106 1 : }
107 :
108 3 : void ScHTMLImport::InsertRangeName( ScDocument* pDoc, const OUString& rName, const ScRange& rRange )
109 : {
110 : ScComplexRefData aRefData;
111 3 : aRefData.InitRange( rRange );
112 3 : ScTokenArray aTokArray;
113 3 : aTokArray.AddDoubleReference( aRefData );
114 3 : ScRangeData* pRangeData = new ScRangeData( pDoc, rName, aTokArray );
115 3 : pDoc->GetRangeName()->insert( pRangeData );
116 3 : }
117 :
118 1 : void ScHTMLImport::WriteToDocument(
119 : bool bSizeColsRows, double nOutputFactor, SvNumberFormatter* pFormatter, bool bConvertDate )
120 : {
121 1 : ScEEImport::WriteToDocument( bSizeColsRows, nOutputFactor, pFormatter, bConvertDate );
122 :
123 1 : const ScHTMLParser* pParser = GetParser();
124 1 : const ScHTMLTable* pGlobTable = pParser->GetGlobalTable();
125 1 : if( !pGlobTable )
126 1 : return;
127 :
128 : // set cell borders for HTML table cells
129 1 : pGlobTable->ApplyCellBorders( mpDoc, maRange.aStart );
130 :
131 : // correct cell borders for merged cells
132 8 : for ( size_t i = 0, n = pParser->ListSize(); i < n; ++i )
133 : {
134 7 : const ScEEParseEntry* pEntry = pParser->ListEntry( i );
135 7 : if( (pEntry->nColOverlap > 1) || (pEntry->nRowOverlap > 1) )
136 : {
137 0 : SCTAB nTab = maRange.aStart.Tab();
138 0 : const ScMergeAttr* pItem = static_cast<const ScMergeAttr*>( mpDoc->GetAttr( pEntry->nCol, pEntry->nRow, nTab, ATTR_MERGE ) );
139 0 : if( pItem->IsMerged() )
140 : {
141 0 : SCCOL nColMerge = pItem->GetColMerge();
142 0 : SCROW nRowMerge = pItem->GetRowMerge();
143 :
144 : const SvxBoxItem* pToItem = static_cast<const SvxBoxItem*>(
145 0 : mpDoc->GetAttr( pEntry->nCol, pEntry->nRow, nTab, ATTR_BORDER ) );
146 0 : SvxBoxItem aNewItem( *pToItem );
147 0 : if( nColMerge > 1 )
148 : {
149 : const SvxBoxItem* pFromItem = static_cast<const SvxBoxItem*>(
150 0 : mpDoc->GetAttr( pEntry->nCol + nColMerge - 1, pEntry->nRow, nTab, ATTR_BORDER ) );
151 0 : aNewItem.SetLine( pFromItem->GetLine( SvxBoxItemLine::RIGHT ), SvxBoxItemLine::RIGHT );
152 : }
153 0 : if( nRowMerge > 1 )
154 : {
155 : const SvxBoxItem* pFromItem = static_cast<const SvxBoxItem*>(
156 0 : mpDoc->GetAttr( pEntry->nCol, pEntry->nRow + nRowMerge - 1, nTab, ATTR_BORDER ) );
157 0 : aNewItem.SetLine( pFromItem->GetLine( SvxBoxItemLine::BOTTOM ), SvxBoxItemLine::BOTTOM );
158 : }
159 0 : mpDoc->ApplyAttr( pEntry->nCol, pEntry->nRow, nTab, aNewItem );
160 : }
161 : }
162 : }
163 :
164 : // create ranges for HTML tables
165 : // 1 - entire document
166 1 : ScRange aNewRange( maRange.aStart );
167 1 : aNewRange.aEnd.IncCol( static_cast<SCsCOL>(pGlobTable->GetDocSize( tdCol )) - 1 );
168 1 : aNewRange.aEnd.IncRow( pGlobTable->GetDocSize( tdRow ) - 1 );
169 1 : InsertRangeName( mpDoc, ScfTools::GetHTMLDocName(), aNewRange );
170 :
171 : // 2 - all tables
172 1 : InsertRangeName( mpDoc, ScfTools::GetHTMLTablesName(), ScRange( maRange.aStart ) );
173 :
174 : // 3 - single tables
175 1 : SCsCOL nColDiff = (SCsCOL)maRange.aStart.Col();
176 1 : SCsROW nRowDiff = (SCsROW)maRange.aStart.Row();
177 1 : SCsTAB nTabDiff = (SCsTAB)maRange.aStart.Tab();
178 :
179 1 : ScHTMLTable* pTable = NULL;
180 1 : ScHTMLTableId nTableId = SC_HTML_GLOBAL_TABLE;
181 3 : while( (pTable = pGlobTable->FindNestedTable( ++nTableId )) != 0 )
182 : {
183 1 : pTable->GetDocRange( aNewRange );
184 1 : aNewRange.Move( nColDiff, nRowDiff, nTabDiff );
185 : // insert table number as name
186 1 : InsertRangeName( mpDoc, ScfTools::GetNameFromHTMLIndex( nTableId ), aNewRange );
187 : // insert table id as name
188 1 : if (!pTable->GetTableName().isEmpty())
189 : {
190 0 : OUString aName( ScfTools::GetNameFromHTMLName( pTable->GetTableName() ) );
191 0 : if (!mpDoc->GetRangeName()->findByUpperName(ScGlobal::pCharClass->uppercase(aName)))
192 0 : InsertRangeName( mpDoc, aName, aNewRange );
193 : }
194 : }
195 : }
196 :
197 0 : OUString ScFormatFilterPluginImpl::GetHTMLRangeNameList( ScDocument* pDoc, const OUString& rOrigName )
198 : {
199 0 : return ScHTMLImport::GetHTMLRangeNameList( pDoc, rOrigName );
200 : }
201 :
202 0 : OUString ScHTMLImport::GetHTMLRangeNameList( ScDocument* pDoc, const OUString& rOrigName )
203 : {
204 : OSL_ENSURE( pDoc, "ScHTMLImport::GetHTMLRangeNameList - missing document" );
205 :
206 0 : OUString aNewName;
207 0 : ScRangeName* pRangeNames = pDoc->GetRangeName();
208 0 : ScRangeList aRangeList;
209 0 : sal_Int32 nTokenCnt = comphelper::string::getTokenCount(rOrigName, ';');
210 0 : sal_Int32 nStringIx = 0;
211 0 : for( sal_Int32 nToken = 0; nToken < nTokenCnt; nToken++ )
212 : {
213 0 : OUString aToken( rOrigName.getToken( 0, ';', nStringIx ) );
214 0 : if( pRangeNames && ScfTools::IsHTMLTablesName( aToken ) )
215 : { // build list with all HTML tables
216 0 : sal_uLong nIndex = 1;
217 0 : bool bLoop = true;
218 0 : while( bLoop )
219 : {
220 0 : aToken = ScfTools::GetNameFromHTMLIndex( nIndex++ );
221 0 : const ScRangeData* pRangeData = pRangeNames->findByUpperName(ScGlobal::pCharClass->uppercase(aToken));
222 0 : if (pRangeData)
223 : {
224 0 : ScRange aRange;
225 0 : if( pRangeData->IsReference( aRange ) && !aRangeList.In( aRange ) )
226 : {
227 0 : aNewName = ScGlobal::addToken(aNewName, aToken, ';');
228 0 : aRangeList.Append( aRange );
229 : }
230 : }
231 : else
232 0 : bLoop = false;
233 : }
234 : }
235 : else
236 0 : aNewName = ScGlobal::addToken(aNewName, aToken, ';');
237 0 : }
238 0 : return aNewName;
239 30 : }
240 :
241 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|