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