Update ooo320-m1
[ooovba.git] / oox / source / xls / workbookhelper.cxx
blob39b292ae0c7b9e86787dcd0b22e61fd9ea6a50d1
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: workbookhelper.cxx,v $
10 * $Revision: 1.5.20.4 $
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 #include "oox/xls/workbookhelper.hxx"
32 #include <osl/thread.h>
33 #include <rtl/strbuf.hxx>
34 #include <com/sun/star/container/XIndexAccess.hpp>
35 #include <com/sun/star/container/XNameContainer.hpp>
36 #include <com/sun/star/awt/XDevice.hpp>
37 #include <com/sun/star/document/XActionLockable.hpp>
38 #include <com/sun/star/table/CellAddress.hpp>
39 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
40 #include <com/sun/star/sheet/XSpreadsheet.hpp>
41 #include <com/sun/star/sheet/XNamedRange.hpp>
42 #include <com/sun/star/sheet/XNamedRanges.hpp>
43 #include <com/sun/star/sheet/XDatabaseRanges.hpp>
44 #include <com/sun/star/sheet/XExternalDocLinks.hpp>
45 #include <com/sun/star/style/XStyle.hpp>
46 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
47 #include "properties.hxx"
48 #include "oox/helper/progressbar.hxx"
49 #include "oox/helper/propertyset.hxx"
50 #include "oox/core/binaryfilterbase.hxx"
51 #include "oox/core/xmlfilterbase.hxx"
52 #include "oox/drawingml/theme.hxx"
53 #include "oox/xls/addressconverter.hxx"
54 #include "oox/xls/biffinputstream.hxx"
55 #include "oox/xls/biffcodec.hxx"
56 #include "oox/xls/defnamesbuffer.hxx"
57 #include "oox/xls/excelchartconverter.hxx"
58 #include "oox/xls/externallinkbuffer.hxx"
59 #include "oox/xls/formulaparser.hxx"
60 #include "oox/xls/pagesettings.hxx"
61 #include "oox/xls/pivotcachebuffer.hxx"
62 #include "oox/xls/pivottablebuffer.hxx"
63 #include "oox/xls/scenariobuffer.hxx"
64 #include "oox/xls/sharedstringsbuffer.hxx"
65 #include "oox/xls/stylesbuffer.hxx"
66 #include "oox/xls/tablebuffer.hxx"
67 #include "oox/xls/themebuffer.hxx"
68 #include "oox/xls/unitconverter.hxx"
69 #include "oox/xls/viewsettings.hxx"
70 #include "oox/xls/webquerybuffer.hxx"
71 #include "oox/xls/workbooksettings.hxx"
72 #include "oox/xls/worksheetbuffer.hxx"
74 using ::rtl::OStringBuffer;
75 using ::rtl::OUString;
76 using ::com::sun::star::uno::Any;
77 using ::com::sun::star::uno::Reference;
78 using ::com::sun::star::uno::Exception;
79 using ::com::sun::star::uno::UNO_QUERY;
80 using ::com::sun::star::uno::UNO_QUERY_THROW;
81 using ::com::sun::star::uno::UNO_SET_THROW;
82 using ::com::sun::star::container::XIndexAccess;
83 using ::com::sun::star::container::XNameAccess;
84 using ::com::sun::star::container::XNameContainer;
85 using ::com::sun::star::lang::XMultiServiceFactory;
86 using ::com::sun::star::awt::XDevice;
87 using ::com::sun::star::document::XActionLockable;
88 using ::com::sun::star::table::CellAddress;
89 using ::com::sun::star::table::CellRangeAddress;
90 using ::com::sun::star::table::XCell;
91 using ::com::sun::star::table::XCellRange;
92 using ::com::sun::star::sheet::XSpreadsheetDocument;
93 using ::com::sun::star::sheet::XSpreadsheet;
94 using ::com::sun::star::sheet::XNamedRange;
95 using ::com::sun::star::sheet::XNamedRanges;
96 using ::com::sun::star::sheet::XDatabaseRanges;
97 using ::com::sun::star::sheet::XExternalDocLinks;
98 using ::com::sun::star::style::XStyle;
99 using ::com::sun::star::style::XStyleFamiliesSupplier;
100 using ::oox::core::BinaryFilterBase;
101 using ::oox::core::FilterBase;
102 using ::oox::core::FragmentHandler;
103 using ::oox::core::XmlFilterBase;
104 using ::oox::drawingml::Theme;
106 namespace oox {
107 namespace xls {
109 // DEBUG ======================================================================
111 #if OSL_DEBUG_LEVEL > 0
112 namespace dbg {
114 // ----------------------------------------------------------------------------
116 #if OOX_SHOW_LOADSAVE_TIME > 0
118 struct TimeCount
120 sal_Int64 mnTime;
121 sal_Int32 mnCount;
122 inline explicit TimeCount() : mnTime( 0 ), mnCount( 0 ) {}
125 Timer::Timer( TimeCount& rTimeCount ) :
126 mrTimeCount( rTimeCount )
128 ++mrTimeCount.mnCount;
129 osl_getSystemTime( &maStartTime );
132 Timer::~Timer()
134 TimeValue aEndTime;
135 osl_getSystemTime( &aEndTime );
136 mrTimeCount.mnTime += (SAL_CONST_INT64( 1000000000 ) * (aEndTime.Seconds - maStartTime.Seconds) + aEndTime.Nanosec - maStartTime.Nanosec);
139 #endif
141 // ----------------------------------------------------------------------------
143 struct WorkbookData
145 #if OOX_SHOW_LOADSAVE_TIME > 0
146 typedef ::std::vector< TimeCount > TimeCountVector;
147 typedef ::boost::shared_ptr< Timer > TimerRef;
148 TimeCountVector maTimeCounts;
149 TimerRef mxTotal;
150 #endif
151 sal_Int32 mnObjCount;
153 explicit WorkbookData();
154 ~WorkbookData();
155 #if OOX_SHOW_LOADSAVE_TIME > 0
156 TimeCount& getTimeCount( TimerType eType );
157 #endif
160 WorkbookData::WorkbookData() :
161 #if OOX_SHOW_LOADSAVE_TIME > 0
162 maTimeCounts( static_cast< size_t >( TIMER_TOTAL + 1 ) ),
163 mxTotal( new Timer( getTimeCount( TIMER_TOTAL ) ) ),
164 #endif
165 mnObjCount( 0 )
169 WorkbookData::~WorkbookData()
171 #if OOX_SHOW_LOADSAVE_TIME > 0
172 mxTotal.reset();
173 static const sal_Char* sppcNames[] =
175 "importFormula\t",
176 "importSheetFragment",
177 " onCreateSheetContext",
178 " importRow\t",
179 " convertRowFormat",
180 " convertColumnFormat",
181 " importCell\t",
182 " onEndSheetElement",
183 " setCell\t\t",
184 " setCellFormat\t",
185 " mergeCellFormats",
186 " writeCellProperties",
187 " finalizeSheetData",
188 " finalizeDrawing",
189 "finalizeBookData",
190 "total\t\t"
192 OStringBuffer aBuffer( "Call counts and load/save times:\n" );
193 sal_Int32 nIdx = 0;
194 for( TimeCountVector::iterator aIt = maTimeCounts.begin(), aEnd = maTimeCounts.end(); aIt != aEnd; ++aIt, ++nIdx )
196 if( aIt->mnCount > 0 )
198 aBuffer.append( nIdx ).append( ":\t" ).append( sppcNames[ nIdx ] ).
199 append( "\tn=" ).append( aIt->mnCount ).append( ',' ).
200 append( "\tt=" ).append( (aIt->mnTime / 100000000) / 10.0 ).append( 's' ).
201 append( "\t(" ).append( static_cast< sal_Int32 >( ((aIt->mnTime * 1000) / maTimeCounts.back().mnTime) / 10 ) ).append( "%)" );
202 if( aIt->mnCount > 1 )
203 aBuffer.append( "\tt/n=" ).append( static_cast< sal_Int32 >( aIt->mnTime / aIt->mnCount / 1000 ) ).append( "mys" );
204 aBuffer.append( '\n' );
207 OSL_ENSURE( false, aBuffer.getStr() );
208 #endif
209 OSL_ENSURE( mnObjCount == 0,
210 OStringBuffer( "WorkbookData::~WorkbookData - failed to delete " ).append( mnObjCount ).append( " objects" ).getStr() );
213 #if OOX_SHOW_LOADSAVE_TIME > 0
214 TimeCount& WorkbookData::getTimeCount( TimerType eType )
216 return maTimeCounts[ static_cast< size_t >( eType ) ];
218 #endif
220 // ----------------------------------------------------------------------------
222 WorkbookHelper::WorkbookHelper( WorkbookData& rBookData ) :
223 mrDbgBookData( rBookData )
225 ++mrDbgBookData.mnObjCount;
228 WorkbookHelper::WorkbookHelper( const WorkbookHelper& rCopy ) :
229 mrDbgBookData( rCopy.mrDbgBookData )
231 ++mrDbgBookData.mnObjCount;
234 WorkbookHelper::~WorkbookHelper()
236 --mrDbgBookData.mnObjCount;
239 #if OOX_SHOW_LOADSAVE_TIME > 0
240 TimeCount& WorkbookHelper::getTimeCount( TimerType eType ) const
242 return mrDbgBookData.getTimeCount( eType );
244 #endif
246 // ----------------------------------------------------------------------------
248 } // namespace dbg
249 #endif
251 // ============================================================================
253 bool IgnoreCaseCompare::operator()( const OUString& rName1, const OUString& rName2 ) const
255 // there is no wrapper in rtl::OUString, TODO: compare with collator
256 return ::rtl_ustr_compareIgnoreAsciiCase_WithLength(
257 rName1.getStr(), rName1.getLength(), rName2.getStr(), rName2.getLength() ) < 0;
260 // ============================================================================
262 class WorkbookData
263 #if OSL_DEBUG_LEVEL > 0
264 : public dbg::WorkbookData
265 #endif
267 public:
268 explicit WorkbookData( XmlFilterBase& rFilter );
269 explicit WorkbookData( BinaryFilterBase& rFilter, BiffType eBiff );
270 ~WorkbookData();
272 /** Returns true, if this helper refers to a valid document. */
273 inline bool isValid() const { return mxDoc.is(); }
275 // filter -----------------------------------------------------------------
277 /** Returns the base filter object (base class of all filters). */
278 inline FilterBase& getBaseFilter() const { return mrBaseFilter; }
279 /** Returns the filter progress bar. */
280 inline SegmentProgressBar& getProgressBar() const { return *mxProgressBar; }
281 /** Returns the file type of the current filter. */
282 inline FilterType getFilterType() const { return meFilterType; }
283 /** Returns true, if the file is a multi-sheet document, or false if single-sheet. */
284 inline bool isWorkbookFile() const { return mbWorkbook; }
285 /** Returns the index of the current sheet in the Calc document. */
286 inline sal_Int16 getCurrentSheetIndex() const { return mnCurrSheet; }
287 /** Sets the index of the current sheet in the Calc document. */
288 inline void setCurrentSheetIndex( sal_Int16 nSheet ) { mnCurrSheet = nSheet; }
290 // document model ---------------------------------------------------------
292 /** Returns a reference to the source/target spreadsheet document model. */
293 inline Reference< XSpreadsheetDocument > getDocument() const { return mxDoc; }
294 /** Returns the reference device of the document. */
295 Reference< XDevice > getReferenceDevice() const;
296 /** Returns the container for defined names from the Calc document. */
297 Reference< XNamedRanges > getNamedRanges() const;
298 /** Returns the container for database ranges from the Calc document. */
299 Reference< XDatabaseRanges > getDatabaseRanges() const;
300 /** Returns the container for external documents from the Calc document. */
301 Reference< XExternalDocLinks > getExternalDocLinks() const;
302 /** Returns the container for DDE links from the Calc document. */
303 Reference< XNameAccess > getDdeLinks() const;
304 /** Returns the cell or page styles container from the Calc document. */
305 Reference< XNameContainer > getStyleFamily( bool bPageStyles ) const;
306 /** Returns the specified cell or page style from the Calc document. */
307 Reference< XStyle > getStyleObject( const OUString& rStyleName, bool bPageStyle ) const;
308 /** Creates and returns a defined name on-the-fly in the Calc document. */
309 Reference< XNamedRange > createNamedRangeObject( OUString& orName, sal_Int32 nNameFlags ) const;
310 /** Creates and returns a com.sun.star.style.Style object for cells or pages. */
311 Reference< XStyle > createStyleObject( OUString& orStyleName, bool bPageStyle ) const;
313 // buffers ----------------------------------------------------------------
315 /** Returns the global workbook settings object. */
316 inline WorkbookSettings& getWorkbookSettings() const { return *mxWorkbookSettings; }
317 /** Returns the workbook and sheet view settings object. */
318 inline ViewSettings& getViewSettings() const { return *mxViewSettings; }
319 /** Returns the worksheet buffer containing sheet names and properties. */
320 inline WorksheetBuffer& getWorksheets() const { return *mxWorksheets; }
321 /** Returns the office theme object read from the theme substorage. */
322 inline ThemeBuffer& getTheme() const { return *mxTheme; }
323 /** Returns the office theme object reference read from the theme substorage. */
324 inline ::boost::shared_ptr< Theme > getThemeRef() const { return mxTheme; }
325 /** Returns all cell formatting objects read from the styles substream. */
326 inline StylesBuffer& getStyles() const { return *mxStyles; }
327 /** Returns the shared strings read from the shared strings substream. */
328 inline SharedStringsBuffer& getSharedStrings() const { return *mxSharedStrings; }
329 /** Returns the external links read from the external links substream. */
330 inline ExternalLinkBuffer& getExternalLinks() const { return *mxExtLinks; }
331 /** Returns the defined names read from the workbook globals. */
332 inline DefinedNamesBuffer& getDefinedNames() const { return *mxDefNames; }
333 /** Returns the tables collection (equivalent to Calc's database ranges). */
334 inline TableBuffer& getTables() const { return *mxTables; }
335 /** Returns the scenarios collection. */
336 inline ScenarioBuffer& getScenarios() const { return *mxScenarios; }
337 /** Returns the web queries. */
338 inline WebQueryBuffer& getWebQueries() const { return *mxWebQueries; }
339 /** Returns the collection of pivot caches. */
340 inline PivotCacheBuffer& getPivotCaches() const { return *mxPivotCaches; }
341 /** Returns the collection of pivot tables. */
342 inline PivotTableBuffer& getPivotTables() { return *mxPivotTables; }
344 // converters -------------------------------------------------------------
346 /** Returns the import formula parser. */
347 inline FormulaParser& getFormulaParser() const { return *mxFmlaParser; }
348 /** Returns the measurement unit converter. */
349 inline UnitConverter& getUnitConverter() const { return *mxUnitConverter; }
350 /** Returns the converter for string to cell address/range conversion. */
351 inline AddressConverter& getAddressConverter() const { return *mxAddrConverter; }
352 /** Returns the chart object converter. */
353 inline ExcelChartConverter& getChartConverter() const { return *mxChartConverter; }
354 /** Returns the page/print settings converter. */
355 inline PageSettingsConverter& getPageSettingsConverter() const { return *mxPageSettConverter; }
357 // OOX specific -----------------------------------------------------------
359 /** Returns the base OOX filter object. */
360 inline XmlFilterBase& getOoxFilter() const { return *mpOoxFilter; }
362 // BIFF specific ----------------------------------------------------------
364 /** Returns the base BIFF filter object. */
365 inline BinaryFilterBase& getBiffFilter() const { return *mpBiffFilter; }
366 /** Returns the BIFF type in binary filter. */
367 inline BiffType getBiff() const { return meBiff; }
368 /** Returns the text encoding used to import/export byte strings. */
369 inline rtl_TextEncoding getTextEncoding() const { return meTextEnc; }
370 /** Sets the text encoding to import/export byte strings. */
371 void setTextEncoding( rtl_TextEncoding eTextEnc );
372 /** Sets code page read from a CODEPAGE record for byte string import. */
373 void setCodePage( sal_uInt16 nCodePage );
374 /** Sets text encoding from the default application font, if CODEPAGE record is missing. */
375 void setAppFontEncoding( rtl_TextEncoding eAppFontEnc );
376 /** Enables workbook file mode, used for BIFF4 workspace files. */
377 void setIsWorkbookFile();
378 /** Recreates global buffers that are used per sheet in specific BIFF versions. */
379 void createBuffersPerSheet( sal_Int16 nSheet );
380 /** Returns the codec helper that stores the encoder/decoder object. */
381 inline BiffCodecHelper& getCodecHelper() { return *mxCodecHelper; }
383 private:
384 /** Initializes some basic members and sets needed document properties. */
385 void initialize( bool bWorkbookFile );
386 /** Finalizes the filter process (sets some needed document properties). */
387 void finalize();
389 private:
390 typedef ::std::auto_ptr< SegmentProgressBar > ProgressBarPtr;
391 typedef ::std::auto_ptr< WorkbookSettings > WorkbookSettPtr;
392 typedef ::std::auto_ptr< ViewSettings > ViewSettingsPtr;
393 typedef ::std::auto_ptr< WorksheetBuffer > WorksheetBfrPtr;
394 typedef ::boost::shared_ptr< ThemeBuffer > ThemeBfrRef;
395 typedef ::std::auto_ptr< StylesBuffer > StylesBfrPtr;
396 typedef ::std::auto_ptr< SharedStringsBuffer > SharedStrBfrPtr;
397 typedef ::std::auto_ptr< ExternalLinkBuffer > ExtLinkBfrPtr;
398 typedef ::std::auto_ptr< DefinedNamesBuffer > DefNamesBfrPtr;
399 typedef ::std::auto_ptr< TableBuffer > TableBfrPtr;
400 typedef ::std::auto_ptr< ScenarioBuffer > ScenarioBfrPtr;
401 typedef ::std::auto_ptr< WebQueryBuffer > WebQueryBfrPtr;
402 typedef ::std::auto_ptr< PivotCacheBuffer > PivotCacheBfrPtr;
403 typedef ::std::auto_ptr< PivotTableBuffer > PivotTableBfrPtr;
404 typedef ::std::auto_ptr< UnitConverter > UnitConvPtr;
405 typedef ::std::auto_ptr< AddressConverter > AddressConvPtr;
406 typedef ::std::auto_ptr< ExcelChartConverter > ExcelChartConvPtr;
407 typedef ::std::auto_ptr< PageSettingsConverter > PageSettConvPtr;
408 typedef ::std::auto_ptr< FormulaParser > FormulaParserPtr;
409 typedef ::std::auto_ptr< BiffCodecHelper > BiffCodecHelperPtr;
411 OUString maCellStyles; /// Style family name for cell styles.
412 OUString maPageStyles; /// Style family name for page styles.
413 OUString maCellStyleServ; /// Service name for a cell style.
414 OUString maPageStyleServ; /// Service name for a page style.
415 Reference< XSpreadsheetDocument > mxDoc; /// Document model.
416 FilterBase& mrBaseFilter; /// Base filter object.
417 FilterType meFilterType; /// File type of the filter.
418 ProgressBarPtr mxProgressBar; /// The progress bar.
419 sal_Int16 mnCurrSheet; /// Current sheet index in Calc dcument.
420 bool mbWorkbook; /// True = multi-sheet file.
422 // buffers
423 WorkbookSettPtr mxWorkbookSettings; /// Global workbook settings.
424 ViewSettingsPtr mxViewSettings; /// Workbook and sheet view settings.
425 WorksheetBfrPtr mxWorksheets; /// Sheet info buffer.
426 ThemeBfrRef mxTheme; /// Formatting theme from theme substream.
427 StylesBfrPtr mxStyles; /// All cell style objects from styles substream.
428 SharedStrBfrPtr mxSharedStrings; /// All strings from shared strings substream.
429 ExtLinkBfrPtr mxExtLinks; /// All external links.
430 DefNamesBfrPtr mxDefNames; /// All defined names.
431 TableBfrPtr mxTables; /// All tables (database ranges).
432 ScenarioBfrPtr mxScenarios; /// All scenarios.
433 WebQueryBfrPtr mxWebQueries; /// Web queries buffer.
434 PivotCacheBfrPtr mxPivotCaches; /// All pivot caches in the document.
435 PivotTableBfrPtr mxPivotTables; /// All pivot tables in the document.
437 // converters
438 FormulaParserPtr mxFmlaParser; /// Import formula parser.
439 UnitConvPtr mxUnitConverter; /// General unit converter.
440 AddressConvPtr mxAddrConverter; /// Cell address and cell range address converter.
441 ExcelChartConvPtr mxChartConverter; /// Chart object converter.
442 PageSettConvPtr mxPageSettConverter; /// Page/print settings converter.
444 // OOX specific
445 XmlFilterBase* mpOoxFilter; /// Base OOX filter object.
447 // BIFF specific
448 BinaryFilterBase* mpBiffFilter; /// Base BIFF filter object.
449 BiffCodecHelperPtr mxCodecHelper; /// Encoder/decoder helper.
450 BiffType meBiff; /// BIFF version for BIFF import/export.
451 rtl_TextEncoding meTextEnc; /// BIFF byte string text encoding.
452 bool mbHasCodePage; /// True = CODEPAGE record exists in imported stream.
455 // ----------------------------------------------------------------------------
457 WorkbookData::WorkbookData( XmlFilterBase& rFilter ) :
458 mrBaseFilter( rFilter ),
459 meFilterType( FILTER_OOX ),
460 mpOoxFilter( &rFilter ),
461 meBiff( BIFF_UNKNOWN )
463 initialize( true );
466 WorkbookData::WorkbookData( BinaryFilterBase& rFilter, BiffType eBiff ) :
467 mrBaseFilter( rFilter ),
468 meFilterType( FILTER_BIFF ),
469 mpBiffFilter( &rFilter ),
470 meBiff( eBiff )
472 initialize( eBiff >= BIFF5 );
475 WorkbookData::~WorkbookData()
477 finalize();
480 // document model -------------------------------------------------------------
482 Reference< XDevice > WorkbookData::getReferenceDevice() const
484 PropertySet aPropSet( mxDoc );
485 Reference< XDevice > xDevice;
486 aPropSet.getProperty( xDevice, PROP_ReferenceDevice );
487 return xDevice;
490 Reference< XNamedRanges > WorkbookData::getNamedRanges() const
492 PropertySet aPropSet( mxDoc );
493 Reference< XNamedRanges > xNamedRanges;
494 aPropSet.getProperty( xNamedRanges, PROP_NamedRanges );
495 return xNamedRanges;
498 Reference< XDatabaseRanges > WorkbookData::getDatabaseRanges() const
500 PropertySet aPropSet( mxDoc );
501 Reference< XDatabaseRanges > xDatabaseRanges;
502 aPropSet.getProperty( xDatabaseRanges, PROP_DatabaseRanges );
503 return xDatabaseRanges;
506 Reference< XExternalDocLinks > WorkbookData::getExternalDocLinks() const
508 PropertySet aPropSet( mxDoc );
509 Reference< XExternalDocLinks > xDocLinks;
510 aPropSet.getProperty( xDocLinks, PROP_ExternalDocLinks );
511 return xDocLinks;
514 Reference< XNameAccess > WorkbookData::getDdeLinks() const
516 PropertySet aPropSet( mxDoc );
517 Reference< XNameAccess > xDdeLinks;
518 aPropSet.getProperty( xDdeLinks, PROP_DDELinks );
519 return xDdeLinks;
522 Reference< XNameContainer > WorkbookData::getStyleFamily( bool bPageStyles ) const
524 Reference< XNameContainer > xStylesNC;
527 Reference< XStyleFamiliesSupplier > xFamiliesSup( mxDoc, UNO_QUERY_THROW );
528 Reference< XNameAccess > xFamiliesNA( xFamiliesSup->getStyleFamilies(), UNO_QUERY_THROW );
529 xStylesNC.set( xFamiliesNA->getByName( bPageStyles ? maPageStyles : maCellStyles ), UNO_QUERY );
531 catch( Exception& )
534 OSL_ENSURE( xStylesNC.is(), "WorkbookData::getStyleFamily - cannot access style family" );
535 return xStylesNC;
538 Reference< XStyle > WorkbookData::getStyleObject( const OUString& rStyleName, bool bPageStyle ) const
540 Reference< XStyle > xStyle;
543 Reference< XNameContainer > xStylesNC( getStyleFamily( bPageStyle ), UNO_SET_THROW );
544 xStyle.set( xStylesNC->getByName( rStyleName ), UNO_QUERY );
546 catch( Exception& )
549 OSL_ENSURE( xStyle.is(), "WorkbookData::getStyleObject - cannot access style object" );
550 return xStyle;
553 Reference< XNamedRange > WorkbookData::createNamedRangeObject( OUString& orName, sal_Int32 nNameFlags ) const
555 // find an unused name
556 Reference< XNamedRanges > xNamedRanges = getNamedRanges();
557 Reference< XNameAccess > xNameAccess( xNamedRanges, UNO_QUERY );
558 if( xNameAccess.is() )
559 orName = ContainerHelper::getUnusedName( xNameAccess, orName, '_' );
561 // create the name and insert it into the Calc document
562 Reference< XNamedRange > xNamedRange;
563 if( xNamedRanges.is() && (orName.getLength() > 0) ) try
565 xNamedRanges->addNewByName( orName, OUString(), CellAddress( 0, 0, 0 ), nNameFlags );
566 xNamedRange.set( xNamedRanges->getByName( orName ), UNO_QUERY );
568 catch( Exception& )
571 OSL_ENSURE( xNamedRange.is(), "WorkbookData::createNamedRangeObject - cannot create defined name" );
572 return xNamedRange;
575 Reference< XStyle > WorkbookData::createStyleObject( OUString& orStyleName, bool bPageStyle ) const
577 Reference< XStyle > xStyle;
580 Reference< XNameContainer > xStylesNC( getStyleFamily( bPageStyle ), UNO_SET_THROW );
581 xStyle.set( mrBaseFilter.getModelFactory()->createInstance( bPageStyle ? maPageStyleServ : maCellStyleServ ), UNO_QUERY_THROW );
582 orStyleName = ContainerHelper::insertByUnusedName( xStylesNC, orStyleName, ' ', Any( xStyle ), false );
584 catch( Exception& )
587 OSL_ENSURE( xStyle.is(), "WorkbookData::createStyleObject - cannot create style" );
588 return xStyle;
591 // BIFF specific --------------------------------------------------------------
593 void WorkbookData::setTextEncoding( rtl_TextEncoding eTextEnc )
595 if( eTextEnc != RTL_TEXTENCODING_DONTKNOW )
596 meTextEnc = eTextEnc;
599 void WorkbookData::setCodePage( sal_uInt16 nCodePage )
601 setTextEncoding( BiffHelper::calcTextEncodingFromCodePage( nCodePage ) );
602 mbHasCodePage = true;
605 void WorkbookData::setAppFontEncoding( rtl_TextEncoding eAppFontEnc )
607 if( !mbHasCodePage )
608 setTextEncoding( eAppFontEnc );
611 void WorkbookData::setIsWorkbookFile()
613 OSL_ENSURE( meBiff == BIFF4, "WorkbookData::setIsWorkbookFile - invalid call" );
614 mbWorkbook = true;
617 void WorkbookData::createBuffersPerSheet( sal_Int16 nSheet )
619 // set mnCurrSheet to enable usage of WorkbookHelper::getCurrentSheetIndex()
620 mnCurrSheet = nSheet;
621 switch( meBiff )
623 case BIFF2:
624 case BIFF3:
625 OSL_ENSURE( mnCurrSheet == 0, "WorkbookData::createBuffersPerSheet - unexpected sheet index" );
626 mxDefNames->setLocalCalcSheet( mnCurrSheet );
627 break;
629 case BIFF4:
630 OSL_ENSURE( mbWorkbook || (mnCurrSheet == 0), "WorkbookData::createBuffersPerSheet - unexpected sheet index" );
631 // #i11183# sheets in BIFF4W files have own styles and names
632 if( mbWorkbook && (mnCurrSheet > 0) )
634 mxStyles.reset( new StylesBuffer( *this ) );
635 mxDefNames.reset( new DefinedNamesBuffer( *this ) );
636 mxExtLinks.reset( new ExternalLinkBuffer( *this ) );
638 mxDefNames->setLocalCalcSheet( mnCurrSheet );
639 break;
641 case BIFF5:
642 // BIFF5 stores external references per sheet
643 mxExtLinks.reset( new ExternalLinkBuffer( *this ) );
644 break;
646 case BIFF8:
647 break;
649 case BIFF_UNKNOWN:
650 break;
652 mnCurrSheet = -1;
655 // private --------------------------------------------------------------------
657 void WorkbookData::initialize( bool bWorkbookFile )
659 maCellStyles = CREATE_OUSTRING( "CellStyles" );
660 maPageStyles = CREATE_OUSTRING( "PageStyles" );
661 maCellStyleServ = CREATE_OUSTRING( "com.sun.star.style.CellStyle" );
662 maPageStyleServ = CREATE_OUSTRING( "com.sun.star.style.PageStyle" );
663 mnCurrSheet = -1;
664 mbWorkbook = bWorkbookFile;
665 meTextEnc = osl_getThreadTextEncoding();
666 mbHasCodePage = false;
668 // the spreadsheet document
669 mxDoc.set( mrBaseFilter.getModel(), UNO_QUERY );
670 OSL_ENSURE( mxDoc.is(), "WorkbookData::initialize - no spreadsheet document" );
672 mxWorkbookSettings.reset( new WorkbookSettings( *this ) );
673 mxViewSettings.reset( new ViewSettings( *this ) );
674 mxWorksheets.reset( new WorksheetBuffer( *this ) );
675 mxTheme.reset( new ThemeBuffer( *this ) );
676 mxStyles.reset( new StylesBuffer( *this ) );
677 mxSharedStrings.reset( new SharedStringsBuffer( *this ) );
678 mxExtLinks.reset( new ExternalLinkBuffer( *this ) );
679 mxDefNames.reset( new DefinedNamesBuffer( *this ) );
680 mxTables.reset( new TableBuffer( *this ) );
681 mxScenarios.reset( new ScenarioBuffer( *this ) );
682 mxWebQueries.reset( new WebQueryBuffer( *this ) );
683 mxPivotCaches.reset( new PivotCacheBuffer( *this ) );
684 mxPivotTables.reset( new PivotTableBuffer( *this ) );
686 mxUnitConverter.reset( new UnitConverter( *this ) );
687 mxAddrConverter.reset( new AddressConverter( *this ) );
688 mxChartConverter.reset( new ExcelChartConverter( *this ) );
690 mxPageSettConverter.reset( new PageSettingsConverter( *this ) );
692 // set some document properties needed during import
693 if( mrBaseFilter.isImportFilter() )
695 PropertySet aPropSet( mxDoc );
696 // enable editing read-only documents (e.g. from read-only files)
697 aPropSet.setProperty( PROP_IsChangeReadOnlyEnabled, true );
698 // #i76026# disable Undo while loading the document
699 aPropSet.setProperty( PROP_IsUndoEnabled, false );
700 // #i79826# disable calculating automatic row height while loading the document
701 aPropSet.setProperty( PROP_IsAdjustHeightEnabled, false );
702 // disable automatic update of linked sheets and DDE links
703 aPropSet.setProperty( PROP_IsExecuteLinkEnabled, false );
704 // #i79890# disable automatic update of defined names
705 Reference< XActionLockable > xLockable( getNamedRanges(), UNO_QUERY );
706 if( xLockable.is() )
707 xLockable->addActionLock();
709 //! TODO: localize progress bar text
710 mxProgressBar.reset( new SegmentProgressBar( mrBaseFilter.getStatusIndicator(), CREATE_OUSTRING( "Loading..." ) ) );
711 mxFmlaParser.reset( new FormulaParser( *this ) );
713 else if( mrBaseFilter.isExportFilter() )
715 //! TODO: localize progress bar text
716 mxProgressBar.reset( new SegmentProgressBar( mrBaseFilter.getStatusIndicator(), CREATE_OUSTRING( "Saving..." ) ) );
719 // filter specific
720 switch( getFilterType() )
722 case FILTER_BIFF:
723 mxCodecHelper.reset( new BiffCodecHelper( * this ) );
724 break;
726 case FILTER_OOX:
727 break;
729 case FILTER_UNKNOWN:
730 break;
734 void WorkbookData::finalize()
736 OOX_LOADSAVE_TIMER( FINALIZEBOOKDATA );
737 // set some document properties needed after import
738 if( mrBaseFilter.isImportFilter() )
740 PropertySet aPropSet( mxDoc );
741 // #i74668# do not insert default sheets
742 aPropSet.setProperty( PROP_IsLoaded, true );
743 // #i79890# enable automatic update of defined names (before IsAdjustHeightEnabled!)
744 Reference< XActionLockable > xLockable( getNamedRanges(), UNO_QUERY );
745 if( xLockable.is() )
746 xLockable->removeActionLock();
747 // enable automatic update of linked sheets and DDE links
748 aPropSet.setProperty( PROP_IsExecuteLinkEnabled, true );
749 // #i79826# enable updating automatic row height after loading the document
750 aPropSet.setProperty( PROP_IsAdjustHeightEnabled, true );
751 // #i76026# enable Undo after loading the document
752 aPropSet.setProperty( PROP_IsUndoEnabled, true );
753 // disable editing read-only documents (e.g. from read-only files)
754 aPropSet.setProperty( PROP_IsChangeReadOnlyEnabled, false );
758 // ============================================================================
760 WorkbookHelper::WorkbookHelper( WorkbookData& rBookData ) :
761 #if OSL_DEBUG_LEVEL > 0
762 dbg::WorkbookHelper( rBookData ),
763 #endif
764 mrBookData( rBookData )
768 WorkbookHelper::~WorkbookHelper()
772 // filter ---------------------------------------------------------------------
774 FilterBase& WorkbookHelper::getBaseFilter() const
776 return mrBookData.getBaseFilter();
779 Reference< XMultiServiceFactory > WorkbookHelper::getGlobalFactory() const
781 return mrBookData.getBaseFilter().getGlobalFactory();
784 FilterType WorkbookHelper::getFilterType() const
786 return mrBookData.getFilterType();
789 SegmentProgressBar& WorkbookHelper::getProgressBar() const
791 return mrBookData.getProgressBar();
794 bool WorkbookHelper::isWorkbookFile() const
796 return mrBookData.isWorkbookFile();
799 sal_Int16 WorkbookHelper::getCurrentSheetIndex() const
801 return mrBookData.getCurrentSheetIndex();
804 void WorkbookHelper::setCurrentSheetIndex( sal_Int16 nSheet )
806 mrBookData.setCurrentSheetIndex( nSheet );
809 void WorkbookHelper::finalizeWorkbookImport()
811 // workbook settings, document and sheet view settings
812 mrBookData.getWorkbookSettings().finalizeImport();
813 mrBookData.getViewSettings().finalizeImport();
815 /* Insert all pivot tables. Must be done after loading all sheets, because
816 data pilots expect existing source data on creation. */
817 mrBookData.getPivotTables().finalizeImport();
819 /* Insert scenarios after all sheet processing is done, because new hidden
820 sheets are created for scenarios which would confuse code that relies
821 on certain sheet indexes. Must be done after pivot tables too. */
822 mrBookData.getScenarios().finalizeImport();
824 /* Set 'Default' page style to automatic page numbering (default is manual
825 number 1). Otherwise hidden sheets (e.g. for scenarios) which have
826 'Default' page style will break automatic page numbering for following
827 sheets. Automatic numbering is set by passing the value 0. */
828 PropertySet aDefPageStyle( getStyleObject( CREATE_OUSTRING( "Default" ), true ) );
829 aDefPageStyle.setProperty< sal_Int16 >( PROP_FirstPageNumber, 0 );
832 // document model -------------------------------------------------------------
834 Reference< XSpreadsheetDocument > WorkbookHelper::getDocument() const
836 return mrBookData.getDocument();
839 Reference< XMultiServiceFactory > WorkbookHelper::getDocumentFactory() const
841 return mrBookData.getBaseFilter().getModelFactory();
844 Reference< XDevice > WorkbookHelper::getReferenceDevice() const
846 return mrBookData.getReferenceDevice();
849 Reference< XNamedRanges > WorkbookHelper::getNamedRanges() const
851 return mrBookData.getNamedRanges();
854 Reference< XDatabaseRanges > WorkbookHelper::getDatabaseRanges() const
856 return mrBookData.getDatabaseRanges();
859 Reference< XExternalDocLinks > WorkbookHelper::getExternalDocLinks() const
861 return mrBookData.getExternalDocLinks();
864 Reference< XNameAccess > WorkbookHelper::getDdeLinks() const
866 return mrBookData.getDdeLinks();
869 Reference< XSpreadsheet > WorkbookHelper::getSheetFromDoc( sal_Int16 nSheet ) const
871 Reference< XSpreadsheet > xSheet;
874 Reference< XIndexAccess > xSheetsIA( getDocument()->getSheets(), UNO_QUERY_THROW );
875 xSheet.set( xSheetsIA->getByIndex( nSheet ), UNO_QUERY_THROW );
877 catch( Exception& )
880 return xSheet;
883 Reference< XSpreadsheet > WorkbookHelper::getSheetFromDoc( const OUString& rSheet ) const
885 Reference< XSpreadsheet > xSheet;
888 Reference< XNameAccess > xSheetsNA( getDocument()->getSheets(), UNO_QUERY_THROW );
889 xSheet.set( xSheetsNA->getByName( rSheet ), UNO_QUERY );
891 catch( Exception& )
894 return xSheet;
897 Reference< XCell > WorkbookHelper::getCellFromDoc( const CellAddress& rAddress ) const
899 Reference< XCell > xCell;
902 Reference< XSpreadsheet > xSheet( getSheetFromDoc( rAddress.Sheet ), UNO_SET_THROW );
903 xCell = xSheet->getCellByPosition( rAddress.Column, rAddress.Row );
905 catch( Exception& )
908 return xCell;
911 Reference< XCellRange > WorkbookHelper::getCellRangeFromDoc( const CellRangeAddress& rRange ) const
913 Reference< XCellRange > xRange;
916 Reference< XSpreadsheet > xSheet( getSheetFromDoc( rRange.Sheet ), UNO_SET_THROW );
917 xRange = xSheet->getCellRangeByPosition( rRange.StartColumn, rRange.StartRow, rRange.EndColumn, rRange.EndRow );
919 catch( Exception& )
922 return xRange;
925 Reference< XNameContainer > WorkbookHelper::getStyleFamily( bool bPageStyles ) const
927 return mrBookData.getStyleFamily( bPageStyles );
930 Reference< XStyle > WorkbookHelper::getStyleObject( const OUString& rStyleName, bool bPageStyle ) const
932 return mrBookData.getStyleObject( rStyleName, bPageStyle );
935 Reference< XNamedRange > WorkbookHelper::createNamedRangeObject( OUString& orName, sal_Int32 nNameFlags ) const
937 return mrBookData.createNamedRangeObject( orName, nNameFlags );
940 Reference< XStyle > WorkbookHelper::createStyleObject( OUString& orStyleName, bool bPageStyle ) const
942 return mrBookData.createStyleObject( orStyleName, bPageStyle );
945 // buffers --------------------------------------------------------------------
947 WorkbookSettings& WorkbookHelper::getWorkbookSettings() const
949 return mrBookData.getWorkbookSettings();
952 ViewSettings& WorkbookHelper::getViewSettings() const
954 return mrBookData.getViewSettings();
957 WorksheetBuffer& WorkbookHelper::getWorksheets() const
959 return mrBookData.getWorksheets();
962 ThemeBuffer& WorkbookHelper::getTheme() const
964 return mrBookData.getTheme();
967 ::boost::shared_ptr< Theme > WorkbookHelper::getThemeRef() const
969 return mrBookData.getThemeRef();
972 StylesBuffer& WorkbookHelper::getStyles() const
974 return mrBookData.getStyles();
977 SharedStringsBuffer& WorkbookHelper::getSharedStrings() const
979 return mrBookData.getSharedStrings();
982 ExternalLinkBuffer& WorkbookHelper::getExternalLinks() const
984 return mrBookData.getExternalLinks();
987 DefinedNamesBuffer& WorkbookHelper::getDefinedNames() const
989 return mrBookData.getDefinedNames();
992 TableBuffer& WorkbookHelper::getTables() const
994 return mrBookData.getTables();
997 ScenarioBuffer& WorkbookHelper::getScenarios() const
999 return mrBookData.getScenarios();
1002 WebQueryBuffer& WorkbookHelper::getWebQueries() const
1004 return mrBookData.getWebQueries();
1007 PivotCacheBuffer& WorkbookHelper::getPivotCaches() const
1009 return mrBookData.getPivotCaches();
1012 PivotTableBuffer& WorkbookHelper::getPivotTables() const
1014 return mrBookData.getPivotTables();
1017 // converters -----------------------------------------------------------------
1019 FormulaParser& WorkbookHelper::getFormulaParser() const
1021 return mrBookData.getFormulaParser();
1024 UnitConverter& WorkbookHelper::getUnitConverter() const
1026 return mrBookData.getUnitConverter();
1029 AddressConverter& WorkbookHelper::getAddressConverter() const
1031 return mrBookData.getAddressConverter();
1034 ExcelChartConverter& WorkbookHelper::getChartConverter() const
1036 return mrBookData.getChartConverter();
1039 // property helpers -----------------------------------------------------------
1041 PageSettingsConverter& WorkbookHelper::getPageSettingsConverter() const
1043 return mrBookData.getPageSettingsConverter();
1046 // OOX specific ---------------------------------------------------------------
1048 XmlFilterBase& WorkbookHelper::getOoxFilter() const
1050 OSL_ENSURE( mrBookData.getFilterType() == FILTER_OOX, "WorkbookHelper::getOoxFilter - invalid call" );
1051 return mrBookData.getOoxFilter();
1054 bool WorkbookHelper::importOoxFragment( const ::rtl::Reference< FragmentHandler >& rxHandler )
1056 return getOoxFilter().importFragment( rxHandler );
1059 // BIFF specific --------------------------------------------------------------
1061 BinaryFilterBase& WorkbookHelper::getBiffFilter() const
1063 OSL_ENSURE( mrBookData.getFilterType() == FILTER_BIFF, "WorkbookHelper::getBiffFilter - invalid call" );
1064 return mrBookData.getBiffFilter();
1067 BiffType WorkbookHelper::getBiff() const
1069 return mrBookData.getBiff();
1072 rtl_TextEncoding WorkbookHelper::getTextEncoding() const
1074 return mrBookData.getTextEncoding();
1077 void WorkbookHelper::setTextEncoding( rtl_TextEncoding eTextEnc )
1079 mrBookData.setTextEncoding( eTextEnc );
1082 void WorkbookHelper::setCodePage( sal_uInt16 nCodePage )
1084 mrBookData.setCodePage( nCodePage );
1087 void WorkbookHelper::setAppFontEncoding( rtl_TextEncoding eAppFontEnc )
1089 mrBookData.setAppFontEncoding( eAppFontEnc );
1092 void WorkbookHelper::setIsWorkbookFile()
1094 mrBookData.setIsWorkbookFile();
1097 void WorkbookHelper::createBuffersPerSheet( sal_Int16 nSheet )
1099 mrBookData.createBuffersPerSheet( nSheet );
1102 BiffCodecHelper& WorkbookHelper::getCodecHelper() const
1104 return mrBookData.getCodecHelper();
1107 // ============================================================================
1109 namespace prv {
1111 WorkbookDataOwner::WorkbookDataOwner( WorkbookDataRef xBookData ) :
1112 mxBookData( xBookData )
1116 WorkbookDataOwner::~WorkbookDataOwner()
1120 } // namespace prv
1122 // ----------------------------------------------------------------------------
1124 WorkbookHelperRoot::WorkbookHelperRoot( ::oox::core::XmlFilterBase& rFilter ) :
1125 prv::WorkbookDataOwner( prv::WorkbookDataRef( new WorkbookData( rFilter ) ) ),
1126 WorkbookHelper( *mxBookData )
1130 WorkbookHelperRoot::WorkbookHelperRoot( ::oox::core::BinaryFilterBase& rFilter, BiffType eBiff ) :
1131 prv::WorkbookDataOwner( prv::WorkbookDataRef( new WorkbookData( rFilter, eBiff ) ) ),
1132 WorkbookHelper( *mxBookData )
1136 bool WorkbookHelperRoot::isValid() const
1138 return mxBookData->isValid();
1141 // ============================================================================
1143 } // namespace xls
1144 } // namespace oox