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