1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: htmlimp.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
36 //------------------------------------------------------------------------
38 #include "scitems.hxx"
39 #include <svx/eeitem.hxx>
41 #include <svx/lrspitem.hxx>
42 #include <svx/paperinf.hxx>
43 #include <svx/sizeitem.hxx>
44 #include <svx/ulspitem.hxx>
45 #include <svx/boxitem.hxx>
46 #include <vcl/svapp.hxx>
48 #include "htmlimp.hxx"
49 #include "htmlpars.hxx"
52 #include "document.hxx"
53 #include "editutil.hxx"
54 #include "stlpool.hxx"
55 #include "stlsheet.hxx"
56 #include "compiler.hxx"
57 #include "rangenam.hxx"
60 #include "tokenarray.hxx"
63 //------------------------------------------------------------------------
65 FltError
ScFormatFilterPluginImpl::ScImportHTML( SvStream
&rStream
, const String
& rBaseURL
, ScDocument
*pDoc
,
66 ScRange
& rRange
, double nOutputFactor
, BOOL bCalcWidthHeight
, SvNumberFormatter
* pFormatter
,
69 ScHTMLImport
aImp( pDoc
, rBaseURL
, rRange
, bCalcWidthHeight
);
70 FltError nErr
= (FltError
) aImp
.Read( rStream
, rBaseURL
);
71 ScRange aR
= aImp
.GetRange();
72 rRange
.aEnd
= aR
.aEnd
;
73 aImp
.WriteToDocument( TRUE
, nOutputFactor
, pFormatter
, bConvertDate
);
77 ScEEAbsImport
*ScFormatFilterPluginImpl::CreateHTMLImport( ScDocument
* pDocP
, const String
& rBaseURL
, const ScRange
& rRange
, BOOL bCalcWidthHeight
)
79 return new ScHTMLImport( pDocP
, rBaseURL
, rRange
, bCalcWidthHeight
);
82 ScHTMLImport::ScHTMLImport( ScDocument
* pDocP
, const String
& rBaseURL
, const ScRange
& rRange
, BOOL bCalcWidthHeight
) :
83 ScEEImport( pDocP
, rRange
)
86 OutputDevice
* pDefaultDev
= Application::GetDefaultDevice();
87 const String
& aPageStyle
= mpDoc
->GetPageStyle( rRange
.aStart
.Tab() );
88 ScStyleSheet
* pStyleSheet
= (ScStyleSheet
*)mpDoc
->
89 GetStyleSheetPool()->Find( aPageStyle
, SFX_STYLE_FAMILY_PAGE
);
92 const SfxItemSet
& rSet
= pStyleSheet
->GetItemSet();
93 const SvxLRSpaceItem
* pLRItem
= (const SvxLRSpaceItem
*) &rSet
.Get( ATTR_LRSPACE
);
94 long nLeftMargin
= pLRItem
->GetLeft();
95 long nRightMargin
= pLRItem
->GetRight();
96 const SvxULSpaceItem
* pULItem
= (const SvxULSpaceItem
*) &rSet
.Get( ATTR_ULSPACE
);
97 long nTopMargin
= pULItem
->GetUpper();
98 long nBottomMargin
= pULItem
->GetLower();
99 aPageSize
= ((const SvxSizeItem
&) rSet
.Get(ATTR_PAGE_SIZE
)).GetSize();
100 if ( !aPageSize
.Width() || !aPageSize
.Height() )
102 DBG_ERRORFILE("PageSize Null ?!?!?");
103 aPageSize
= SvxPaperInfo::GetPaperSize( PAPER_A4
);
105 aPageSize
.Width() -= nLeftMargin
+ nRightMargin
;
106 aPageSize
.Height() -= nTopMargin
+ nBottomMargin
;
107 aPageSize
= pDefaultDev
->LogicToPixel( aPageSize
, MapMode( MAP_TWIP
) );
111 DBG_ERRORFILE("kein StyleSheet?!?");
112 aPageSize
= pDefaultDev
->LogicToPixel(
113 SvxPaperInfo::GetPaperSize( PAPER_A4
), MapMode( MAP_TWIP
) );
115 if( bCalcWidthHeight
)
116 mpParser
= new ScHTMLLayoutParser( mpEngine
, rBaseURL
, aPageSize
, pDocP
);
118 mpParser
= new ScHTMLQueryParser( mpEngine
, pDocP
);
122 ScHTMLImport::~ScHTMLImport()
124 // Reihenfolge wichtig, sonst knallt's irgendwann irgendwo in irgendeinem Dtor!
125 // Ist gewaehrleistet, da ScEEImport Basisklasse ist
126 delete (ScHTMLParser
*) mpParser
; // vor EditEngine!
130 void ScHTMLImport::InsertRangeName( ScDocument
* pDoc
, const String
& rName
, const ScRange
& rRange
)
132 ScComplexRefData aRefData
;
133 aRefData
.InitRange( rRange
);
134 ScTokenArray aTokArray
;
135 aTokArray
.AddDoubleReference( aRefData
);
136 ScRangeData
* pRangeData
= new ScRangeData( pDoc
, rName
, aTokArray
);
137 if( !pDoc
->GetRangeName()->Insert( pRangeData
) )
141 void ScHTMLImport::WriteToDocument(
142 BOOL bSizeColsRows
, double nOutputFactor
, SvNumberFormatter
* pFormatter
, bool bConvertDate
)
144 ScEEImport::WriteToDocument( bSizeColsRows
, nOutputFactor
, pFormatter
, bConvertDate
);
146 const ScHTMLParser
* pParser
= GetParser();
147 const ScHTMLTable
* pGlobTable
= pParser
->GetGlobalTable();
151 // set cell borders for HTML table cells
152 pGlobTable
->ApplyCellBorders( mpDoc
, maRange
.aStart
);
154 // correct cell borders for merged cells
155 for ( ScEEParseEntry
* pEntry
= pParser
->First(); pEntry
; pEntry
= pParser
->Next() )
157 if( (pEntry
->nColOverlap
> 1) || (pEntry
->nRowOverlap
> 1) )
159 SCTAB nTab
= maRange
.aStart
.Tab();
160 const ScMergeAttr
* pItem
= (ScMergeAttr
*) mpDoc
->GetAttr( pEntry
->nCol
, pEntry
->nRow
, nTab
, ATTR_MERGE
);
161 if( pItem
->IsMerged() )
163 SCCOL nColMerge
= pItem
->GetColMerge();
164 SCROW nRowMerge
= pItem
->GetRowMerge();
166 const SvxBoxItem
* pToItem
= (const SvxBoxItem
*)
167 mpDoc
->GetAttr( pEntry
->nCol
, pEntry
->nRow
, nTab
, ATTR_BORDER
);
168 SvxBoxItem
aNewItem( *pToItem
);
171 const SvxBoxItem
* pFromItem
= (const SvxBoxItem
*)
172 mpDoc
->GetAttr( pEntry
->nCol
+ nColMerge
- 1, pEntry
->nRow
, nTab
, ATTR_BORDER
);
173 aNewItem
.SetLine( pFromItem
->GetLine( BOX_LINE_RIGHT
), BOX_LINE_RIGHT
);
177 const SvxBoxItem
* pFromItem
= (const SvxBoxItem
*)
178 mpDoc
->GetAttr( pEntry
->nCol
, pEntry
->nRow
+ nRowMerge
- 1, nTab
, ATTR_BORDER
);
179 aNewItem
.SetLine( pFromItem
->GetLine( BOX_LINE_BOTTOM
), BOX_LINE_BOTTOM
);
181 mpDoc
->ApplyAttr( pEntry
->nCol
, pEntry
->nRow
, nTab
, aNewItem
);
186 // create ranges for HTML tables
187 // 1 - entire document
188 ScRange
aNewRange( maRange
.aStart
);
189 aNewRange
.aEnd
.IncCol( static_cast<SCsCOL
>(pGlobTable
->GetDocSize( tdCol
)) - 1 );
190 aNewRange
.aEnd
.IncRow( pGlobTable
->GetDocSize( tdRow
) - 1 );
191 InsertRangeName( mpDoc
, ScfTools::GetHTMLDocName(), aNewRange
);
194 InsertRangeName( mpDoc
, ScfTools::GetHTMLTablesName(), ScRange( maRange
.aStart
) );
197 SCsCOL nColDiff
= (SCsCOL
)maRange
.aStart
.Col();
198 SCsROW nRowDiff
= (SCsROW
)maRange
.aStart
.Row();
199 SCsTAB nTabDiff
= (SCsTAB
)maRange
.aStart
.Tab();
201 ScHTMLTable
* pTable
= NULL
;
202 ScHTMLTableId nTableId
= SC_HTML_GLOBAL_TABLE
;
203 while( (pTable
= pGlobTable
->FindNestedTable( ++nTableId
)) != 0 )
205 pTable
->GetDocRange( aNewRange
);
206 aNewRange
.Move( nColDiff
, nRowDiff
, nTabDiff
);
207 // insert table number as name
208 InsertRangeName( mpDoc
, ScfTools::GetNameFromHTMLIndex( nTableId
), aNewRange
);
209 // insert table id as name
210 if( pTable
->GetTableName().Len() )
212 String
aName( ScfTools::GetNameFromHTMLName( pTable
->GetTableName() ) );
214 if( !mpDoc
->GetRangeName()->SearchName( aName
, nPos
) )
215 InsertRangeName( mpDoc
, aName
, aNewRange
);
220 String
ScFormatFilterPluginImpl::GetHTMLRangeNameList( ScDocument
* pDoc
, const String
& rOrigName
)
222 return ScHTMLImport::GetHTMLRangeNameList( pDoc
, rOrigName
);
225 String
ScHTMLImport::GetHTMLRangeNameList( ScDocument
* pDoc
, const String
& rOrigName
)
227 DBG_ASSERT( pDoc
, "ScHTMLImport::GetHTMLRangeNameList - missing document" );
230 ScRangeName
* pRangeNames
= pDoc
->GetRangeName();
231 ScRangeList aRangeList
;
232 xub_StrLen nTokenCnt
= rOrigName
.GetTokenCount( ';' );
233 xub_StrLen nStringIx
= 0;
234 for( xub_StrLen nToken
= 0; nToken
< nTokenCnt
; nToken
++ )
236 String
aToken( rOrigName
.GetToken( 0, ';', nStringIx
) );
237 if( pRangeNames
&& ScfTools::IsHTMLTablesName( aToken
) )
238 { // build list with all HTML tables
244 aToken
= ScfTools::GetNameFromHTMLIndex( nIndex
++ );
245 bLoop
= pRangeNames
->SearchName( aToken
, nPos
);
248 const ScRangeData
* pRangeData
= (*pRangeNames
)[ nPos
];
250 if( pRangeData
&& pRangeData
->IsReference( aRange
) && !aRangeList
.In( aRange
) )
252 ScGlobal::AddToken( aNewName
, aToken
, ';' );
253 aRangeList
.Append( aRange
);
259 ScGlobal::AddToken( aNewName
, aToken
, ';' );