merged tag ooo/OOO330_m14
[LibreOffice.git] / sc / source / filter / html / htmlimp.cxx
blob1c0b9fc8ec33957d6f386e7c47fd0a00a9d75571
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
33 //------------------------------------------------------------------------
35 #include "scitems.hxx"
36 #include <editeng/eeitem.hxx>
38 #include <editeng/lrspitem.hxx>
39 #include <editeng/paperinf.hxx>
40 #include <editeng/sizeitem.hxx>
41 #include <editeng/ulspitem.hxx>
42 #include <editeng/boxitem.hxx>
43 #include <vcl/svapp.hxx>
45 #include "htmlimp.hxx"
46 #include "htmlpars.hxx"
47 #include "filter.hxx"
48 #include "global.hxx"
49 #include "document.hxx"
50 #include "editutil.hxx"
51 #include "stlpool.hxx"
52 #include "stlsheet.hxx"
53 #include "compiler.hxx"
54 #include "rangenam.hxx"
55 #include "attrib.hxx"
56 #include "ftools.hxx"
57 #include "tokenarray.hxx"
60 //------------------------------------------------------------------------
62 FltError ScFormatFilterPluginImpl::ScImportHTML( SvStream &rStream, const String& rBaseURL, ScDocument *pDoc,
63 ScRange& rRange, double nOutputFactor, BOOL bCalcWidthHeight, SvNumberFormatter* pFormatter,
64 bool bConvertDate )
66 ScHTMLImport aImp( pDoc, rBaseURL, rRange, bCalcWidthHeight );
67 FltError nErr = (FltError) aImp.Read( rStream, rBaseURL );
68 ScRange aR = aImp.GetRange();
69 rRange.aEnd = aR.aEnd;
70 aImp.WriteToDocument( TRUE, nOutputFactor, pFormatter, bConvertDate );
71 return nErr;
74 ScEEAbsImport *ScFormatFilterPluginImpl::CreateHTMLImport( ScDocument* pDocP, const String& rBaseURL, const ScRange& rRange, BOOL bCalcWidthHeight )
76 return new ScHTMLImport( pDocP, rBaseURL, rRange, bCalcWidthHeight );
79 ScHTMLImport::ScHTMLImport( ScDocument* pDocP, const String& rBaseURL, const ScRange& rRange, BOOL bCalcWidthHeight ) :
80 ScEEImport( pDocP, rRange )
82 Size aPageSize;
83 OutputDevice* pDefaultDev = Application::GetDefaultDevice();
84 const String& aPageStyle = mpDoc->GetPageStyle( rRange.aStart.Tab() );
85 ScStyleSheet* pStyleSheet = (ScStyleSheet*)mpDoc->
86 GetStyleSheetPool()->Find( aPageStyle, SFX_STYLE_FAMILY_PAGE );
87 if ( pStyleSheet )
89 const SfxItemSet& rSet = pStyleSheet->GetItemSet();
90 const SvxLRSpaceItem* pLRItem = (const SvxLRSpaceItem*) &rSet.Get( ATTR_LRSPACE );
91 long nLeftMargin = pLRItem->GetLeft();
92 long nRightMargin = pLRItem->GetRight();
93 const SvxULSpaceItem* pULItem = (const SvxULSpaceItem*) &rSet.Get( ATTR_ULSPACE );
94 long nTopMargin = pULItem->GetUpper();
95 long nBottomMargin = pULItem->GetLower();
96 aPageSize = ((const SvxSizeItem&) rSet.Get(ATTR_PAGE_SIZE)).GetSize();
97 if ( !aPageSize.Width() || !aPageSize.Height() )
99 DBG_ERRORFILE("PageSize Null ?!?!?");
100 aPageSize = SvxPaperInfo::GetPaperSize( PAPER_A4 );
102 aPageSize.Width() -= nLeftMargin + nRightMargin;
103 aPageSize.Height() -= nTopMargin + nBottomMargin;
104 aPageSize = pDefaultDev->LogicToPixel( aPageSize, MapMode( MAP_TWIP ) );
106 else
108 DBG_ERRORFILE("kein StyleSheet?!?");
109 aPageSize = pDefaultDev->LogicToPixel(
110 SvxPaperInfo::GetPaperSize( PAPER_A4 ), MapMode( MAP_TWIP ) );
112 if( bCalcWidthHeight )
113 mpParser = new ScHTMLLayoutParser( mpEngine, rBaseURL, aPageSize, pDocP );
114 else
115 mpParser = new ScHTMLQueryParser( mpEngine, pDocP );
119 ScHTMLImport::~ScHTMLImport()
121 // Reihenfolge wichtig, sonst knallt's irgendwann irgendwo in irgendeinem Dtor!
122 // Ist gewaehrleistet, da ScEEImport Basisklasse ist
123 delete (ScHTMLParser*) mpParser; // vor EditEngine!
127 void ScHTMLImport::InsertRangeName( ScDocument* pDoc, const String& rName, const ScRange& rRange )
129 ScComplexRefData aRefData;
130 aRefData.InitRange( rRange );
131 ScTokenArray aTokArray;
132 aTokArray.AddDoubleReference( aRefData );
133 ScRangeData* pRangeData = new ScRangeData( pDoc, rName, aTokArray );
134 if( !pDoc->GetRangeName()->Insert( pRangeData ) )
135 delete pRangeData;
138 void ScHTMLImport::WriteToDocument(
139 BOOL bSizeColsRows, double nOutputFactor, SvNumberFormatter* pFormatter, bool bConvertDate )
141 ScEEImport::WriteToDocument( bSizeColsRows, nOutputFactor, pFormatter, bConvertDate );
143 const ScHTMLParser* pParser = GetParser();
144 const ScHTMLTable* pGlobTable = pParser->GetGlobalTable();
145 if( !pGlobTable )
146 return;
148 // set cell borders for HTML table cells
149 pGlobTable->ApplyCellBorders( mpDoc, maRange.aStart );
151 // correct cell borders for merged cells
152 for ( ScEEParseEntry* pEntry = pParser->First(); pEntry; pEntry = pParser->Next() )
154 if( (pEntry->nColOverlap > 1) || (pEntry->nRowOverlap > 1) )
156 SCTAB nTab = maRange.aStart.Tab();
157 const ScMergeAttr* pItem = (ScMergeAttr*) mpDoc->GetAttr( pEntry->nCol, pEntry->nRow, nTab, ATTR_MERGE );
158 if( pItem->IsMerged() )
160 SCCOL nColMerge = pItem->GetColMerge();
161 SCROW nRowMerge = pItem->GetRowMerge();
163 const SvxBoxItem* pToItem = (const SvxBoxItem*)
164 mpDoc->GetAttr( pEntry->nCol, pEntry->nRow, nTab, ATTR_BORDER );
165 SvxBoxItem aNewItem( *pToItem );
166 if( nColMerge > 1 )
168 const SvxBoxItem* pFromItem = (const SvxBoxItem*)
169 mpDoc->GetAttr( pEntry->nCol + nColMerge - 1, pEntry->nRow, nTab, ATTR_BORDER );
170 aNewItem.SetLine( pFromItem->GetLine( BOX_LINE_RIGHT ), BOX_LINE_RIGHT );
172 if( nRowMerge > 1 )
174 const SvxBoxItem* pFromItem = (const SvxBoxItem*)
175 mpDoc->GetAttr( pEntry->nCol, pEntry->nRow + nRowMerge - 1, nTab, ATTR_BORDER );
176 aNewItem.SetLine( pFromItem->GetLine( BOX_LINE_BOTTOM ), BOX_LINE_BOTTOM );
178 mpDoc->ApplyAttr( pEntry->nCol, pEntry->nRow, nTab, aNewItem );
183 // create ranges for HTML tables
184 // 1 - entire document
185 ScRange aNewRange( maRange.aStart );
186 aNewRange.aEnd.IncCol( static_cast<SCsCOL>(pGlobTable->GetDocSize( tdCol )) - 1 );
187 aNewRange.aEnd.IncRow( pGlobTable->GetDocSize( tdRow ) - 1 );
188 InsertRangeName( mpDoc, ScfTools::GetHTMLDocName(), aNewRange );
190 // 2 - all tables
191 InsertRangeName( mpDoc, ScfTools::GetHTMLTablesName(), ScRange( maRange.aStart ) );
193 // 3 - single tables
194 SCsCOL nColDiff = (SCsCOL)maRange.aStart.Col();
195 SCsROW nRowDiff = (SCsROW)maRange.aStart.Row();
196 SCsTAB nTabDiff = (SCsTAB)maRange.aStart.Tab();
198 ScHTMLTable* pTable = NULL;
199 ScHTMLTableId nTableId = SC_HTML_GLOBAL_TABLE;
200 while( (pTable = pGlobTable->FindNestedTable( ++nTableId )) != 0 )
202 pTable->GetDocRange( aNewRange );
203 aNewRange.Move( nColDiff, nRowDiff, nTabDiff );
204 // insert table number as name
205 InsertRangeName( mpDoc, ScfTools::GetNameFromHTMLIndex( nTableId ), aNewRange );
206 // insert table id as name
207 if( pTable->GetTableName().Len() )
209 String aName( ScfTools::GetNameFromHTMLName( pTable->GetTableName() ) );
210 USHORT nPos;
211 if( !mpDoc->GetRangeName()->SearchName( aName, nPos ) )
212 InsertRangeName( mpDoc, aName, aNewRange );
217 String ScFormatFilterPluginImpl::GetHTMLRangeNameList( ScDocument* pDoc, const String& rOrigName )
219 return ScHTMLImport::GetHTMLRangeNameList( pDoc, rOrigName );
222 String ScHTMLImport::GetHTMLRangeNameList( ScDocument* pDoc, const String& rOrigName )
224 DBG_ASSERT( pDoc, "ScHTMLImport::GetHTMLRangeNameList - missing document" );
226 String aNewName;
227 ScRangeName* pRangeNames = pDoc->GetRangeName();
228 ScRangeList aRangeList;
229 xub_StrLen nTokenCnt = rOrigName.GetTokenCount( ';' );
230 xub_StrLen nStringIx = 0;
231 for( xub_StrLen nToken = 0; nToken < nTokenCnt; nToken++ )
233 String aToken( rOrigName.GetToken( 0, ';', nStringIx ) );
234 if( pRangeNames && ScfTools::IsHTMLTablesName( aToken ) )
235 { // build list with all HTML tables
236 ULONG nIndex = 1;
237 USHORT nPos;
238 BOOL bLoop = TRUE;
239 while( bLoop )
241 aToken = ScfTools::GetNameFromHTMLIndex( nIndex++ );
242 bLoop = pRangeNames->SearchName( aToken, nPos );
243 if( bLoop )
245 const ScRangeData* pRangeData = (*pRangeNames)[ nPos ];
246 ScRange aRange;
247 if( pRangeData && pRangeData->IsReference( aRange ) && !aRangeList.In( aRange ) )
249 ScGlobal::AddToken( aNewName, aToken, ';' );
250 aRangeList.Append( aRange );
255 else
256 ScGlobal::AddToken( aNewName, aToken, ';' );
258 return aNewName;