LibreOffice Module sc (master) 1
xeroot.cxx
Go to the documentation of this file.
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 <officecfg/Office/Common.hxx>
21#include <rtl/random.h>
22#include <sal/log.hxx>
23#include <sfx2/docfile.hxx>
24#include <sfx2/frame.hxx>
25#include <sfx2/sfxsids.hrc>
26#include <sot/storage.hxx>
27#include <svl/itemset.hxx>
28#include <svl/stritem.hxx>
29#include <xecontent.hxx>
30#include <xeescher.hxx>
31#include <xeformula.hxx>
32#include <xehelper.hxx>
33#include <xelink.hxx>
34#include <xename.hxx>
35#include <xepivot.hxx>
36#include <xestyle.hxx>
37#include <xeroot.hxx>
38#include <xepivotxml.hxx>
39#include <xedbdata.hxx>
40#include <xlcontent.hxx>
41#include <xlname.hxx>
42#include <xllink.hxx>
43
44#include <excrecds.hxx>
45#include <tabprotection.hxx>
46#include <document.hxx>
47
48#include <formulabase.hxx>
49#include <com/sun/star/sheet/FormulaOpCodeMapEntry.hpp>
50#include <com/sun/star/lang/XMultiServiceFactory.hpp>
51
52using namespace ::com::sun::star;
53
54// Global data ================================================================
55
57 const tools::SvRef<SotStorage>& xRootStrg, ScDocument& rDoc, rtl_TextEncoding eTextEnc ) :
58 XclRootData( eBiff, rMedium, xRootStrg, rDoc, eTextEnc, true )
59{
61 ? officecfg::Office::Common::Save::URL::Internet::get()
62 : officecfg::Office::Common::Save::URL::FileSystem::get();
63 maStringBuf.setLength(0);
64}
65
67{
68}
69
71 XclRoot( rExpRootData ),
72 mrExpData( rExpRootData )
73{
74}
75
77{
78 OSL_ENSURE( mrExpData.mxTabInfo, "XclExpRoot::GetTabInfo - missing object (wrong BIFF?)" );
79 return *mrExpData.mxTabInfo;
80}
81
83{
84 OSL_ENSURE( mrExpData.mxAddrConv, "XclExpRoot::GetAddressConverter - missing object (wrong BIFF?)" );
85 return *mrExpData.mxAddrConv;
86}
87
89{
90 OSL_ENSURE( mrExpData.mxFmlaComp, "XclExpRoot::GetFormulaCompiler - missing object (wrong BIFF?)" );
91 return *mrExpData.mxFmlaComp;
92}
93
95{
96 OSL_ENSURE( mrExpData.mxProgress, "XclExpRoot::GetProgressBar - missing object (wrong BIFF?)" );
97 return *mrExpData.mxProgress;
98}
99
101{
102 OSL_ENSURE( mrExpData.mxSst, "XclExpRoot::GetSst - missing object (wrong BIFF?)" );
103 return *mrExpData.mxSst;
104}
105
107{
108 OSL_ENSURE( mrExpData.mxPalette, "XclExpRoot::GetPalette - missing object (wrong BIFF?)" );
109 return *mrExpData.mxPalette;
110}
111
113{
114 OSL_ENSURE( mrExpData.mxFontBfr, "XclExpRoot::GetFontBuffer - missing object (wrong BIFF?)" );
115 return *mrExpData.mxFontBfr;
116}
117
119{
120 OSL_ENSURE( mrExpData.mxNumFmtBfr, "XclExpRoot::GetNumFmtBuffer - missing object (wrong BIFF?)" );
121 return *mrExpData.mxNumFmtBfr;
122}
123
125{
126 OSL_ENSURE( mrExpData.mxXFBfr, "XclExpRoot::GetXFBuffer - missing object (wrong BIFF?)" );
127 return *mrExpData.mxXFBfr;
128}
129
131{
132 OSL_ENSURE( mrExpData.mxGlobLinkMgr, "XclExpRoot::GetGlobalLinkManager - missing object (wrong BIFF?)" );
133 return *mrExpData.mxGlobLinkMgr;
134}
135
137{
138 OSL_ENSURE( GetLocalLinkMgrRef(), "XclExpRoot::GetLocalLinkManager - missing object (wrong BIFF?)" );
139 return *GetLocalLinkMgrRef();
140}
141
143{
144 OSL_ENSURE( mrExpData.mxNameMgr, "XclExpRoot::GetNameManager - missing object (wrong BIFF?)" );
145 return *mrExpData.mxNameMgr;
146}
147
149{
150 OSL_ENSURE( mrExpData.mxObjMgr, "XclExpRoot::GetObjectManager - missing object (wrong BIFF?)" );
151 return *mrExpData.mxObjMgr;
152}
153
155{
156 OSL_ENSURE( mrExpData.mxFilterMgr, "XclExpRoot::GetFilterManager - missing object (wrong BIFF?)" );
157 return *mrExpData.mxFilterMgr;
158}
159
161{
162 OSL_ENSURE( mrExpData.mxDxfs, "XclExpRoot::GetDxfs - missing object ( wrong BIFF?)" );
163 return *mrExpData.mxDxfs;
164}
165
167{
168 OSL_ENSURE( mrExpData.mxPTableMgr, "XclExpRoot::GetPivotTableManager - missing object (wrong BIFF?)" );
169 return *mrExpData.mxPTableMgr;
170}
171
173{
176}
177
179{
180 assert(mrExpData.mxTablesMgr);
181 return *mrExpData.mxTablesMgr;
182}
183
185{
186 mrExpData.mxTabInfo = std::make_shared<XclExpTabInfo>( GetRoot() );
187 mrExpData.mxAddrConv = std::make_shared<XclExpAddressConverter>( GetRoot() );
188 mrExpData.mxFmlaComp = std::make_shared<XclExpFormulaCompiler>( GetRoot() );
189 mrExpData.mxProgress = std::make_shared<XclExpProgressBar>( GetRoot() );
190
192}
193
195{
197
198 if( GetBiff() >= EXC_BIFF5 )
199 {
206 }
207
208 if( GetBiff() == EXC_BIFF8 )
209 {
210 mrExpData.mxSst = new XclExpSst();
211 mrExpData.mxObjMgr = std::make_shared<XclExpObjectManager>( GetRoot() );
212 mrExpData.mxFilterMgr = std::make_shared<XclExpFilterManager>( GetRoot() );
213 mrExpData.mxPTableMgr = std::make_shared<XclExpPivotTableManager>( GetRoot() );
214 // BIFF8: only one link manager for all sheets
217 }
218
220 {
221 mrExpData.mxXmlPTableMgr = std::make_shared<XclExpXmlPivotTableManager>(GetRoot());
222 mrExpData.mxTablesMgr = std::make_shared<XclExpTablesManager>(GetRoot());
223
224 do
225 {
226 ScDocument& rDoc = GetDoc();
227 // Pass the model factory to OpCodeProvider, not the process
228 // service factory, otherwise a FormulaOpCodeMapperObj would be
229 // instantiated instead of a ScFormulaOpCodeMapperObj and the
230 // ScCompiler virtuals not be called! Which would be the case with
231 // the current (2013-01-24) rDoc.GetServiceManager()
232 const SfxObjectShell* pShell = rDoc.GetDocumentShell();
233 if (!pShell)
234 {
235 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no object shell");
236 break;
237 }
238 uno::Reference< lang::XComponent > xComponent = pShell->GetModel();
239 if (!xComponent.is())
240 {
241 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no component");
242 break;
243 }
244 uno::Reference< lang::XMultiServiceFactory > xModelFactory( xComponent, uno::UNO_QUERY);
245 oox::xls::OpCodeProvider aOpCodeProvider(xModelFactory, false);
246 // Compiler mocks about non-matching ctor or conversion from
247 // Sequence<...> to Sequence<const ...> if directly created or passed,
248 // conversion through Any works around.
249 uno::Any aAny( aOpCodeProvider.getOoxParserMap());
250 uno::Sequence< const sheet::FormulaOpCodeMapEntry > aOpCodeMapping;
251 if (!(aAny >>= aOpCodeMapping))
252 {
253 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no OpCodeMap");
254 break;
255 }
256 ScCompiler aCompiler( rDoc, ScAddress(), rDoc.GetGrammar());
258 } while(false);
259 }
260
263}
264
266{
267 SetCurrScTab( nScTab );
268 if( GetBiff() == EXC_BIFF5 )
269 {
270 // local link manager per sheet
272 }
273}
274
276{
279 GetDxfs().Finalize();
280}
281
283{
284 XclExpRecordRef xRec;
285 switch( nRecId )
286 {
287 case EXC_ID_PALETTE: xRec = mrExpData.mxPalette; break;
288 case EXC_ID_FONTLIST: xRec = mrExpData.mxFontBfr; break;
289 case EXC_ID_FORMATLIST: xRec = mrExpData.mxNumFmtBfr; break;
290 case EXC_ID_XFLIST: xRec = mrExpData.mxXFBfr; break;
291 case EXC_ID_SST: xRec = mrExpData.mxSst; break;
292 case EXC_ID_EXTERNSHEET: xRec = GetLocalLinkMgrRef(); break;
293 case EXC_ID_NAME: xRec = mrExpData.mxNameMgr; break;
294 case EXC_ID_DXFS: xRec = mrExpData.mxDxfs; break;
295 }
296 OSL_ENSURE( xRec, "XclExpRoot::CreateRecord - unknown record ID or missing object" );
297 return xRec;
298}
299
301{
302 // We need to encrypt the content when the document structure is protected.
303 const ScDocProtection* pDocProt = GetDoc().GetDocProtection();
304 if (pDocProt && pDocProt->isProtected() && pDocProt->isOptionEnabled(ScDocProtection::STRUCTURE))
305 return true;
306
307 // Whether password is entered directly into the save dialog.
308 return GetEncryptionData().hasElements();
309}
310
311uno::Sequence< beans::NamedValue > XclExpRoot::GenerateEncryptionData( std::u16string_view aPass )
312{
313 uno::Sequence< beans::NamedValue > aEncryptionData;
314
315 if ( !aPass.empty() && aPass.size() < 16 )
316 {
317 rtlRandomPool aRandomPool = rtl_random_createPool ();
318 sal_uInt8 pnDocId[16];
319 rtl_random_getBytes( aRandomPool, pnDocId, 16 );
320
321 rtl_random_destroyPool( aRandomPool );
322
323 sal_uInt16 pnPasswd[16] = {};
324 for( size_t nChar = 0; nChar < aPass.size(); ++nChar )
325 pnPasswd[nChar] = aPass[nChar];
326
328 aCodec.InitKey( pnPasswd, pnDocId );
329 aEncryptionData = aCodec.GetEncryptionData();
330 }
331
332 return aEncryptionData;
333}
334
335uno::Sequence< beans::NamedValue > XclExpRoot::GetEncryptionData() const
336{
337 uno::Sequence< beans::NamedValue > aEncryptionData;
338 const SfxUnoAnyItem* pEncryptionDataItem = GetMedium().GetItemSet().GetItem(SID_ENCRYPTIONDATA, false);
339 if ( pEncryptionDataItem )
340 pEncryptionDataItem->GetValue() >>= aEncryptionData;
341 else
342 {
343 // try to get the encryption data from the password
344 const SfxStringItem* pPasswordItem = GetMedium().GetItemSet().GetItem(SID_PASSWORD, false);
345 if ( pPasswordItem && !pPasswordItem->GetValue().isEmpty() )
346 aEncryptionData = GenerateEncryptionData( pPasswordItem->GetValue() );
347 }
348
349 return aEncryptionData;
350}
351
352uno::Sequence< beans::NamedValue > XclExpRoot::GenerateDefaultEncryptionData()
353{
355}
356
358{
360}
361
362/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void * rtlRandomPool
const OUString & GetValue() const
virtual bool isProtected() const override
bool isOptionEnabled(Option eOption) const
SC_DLLPUBLIC formula::FormulaGrammar::Grammar GetGrammar() const
Definition: document.hxx:1010
SC_DLLPUBLIC ScDocProtection * GetDocProtection() const
Definition: documen3.cxx:1881
SfxObjectShell * GetDocumentShell() const
Definition: document.hxx:1083
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
SfxItemSet & GetItemSet() const
bool IsRemote() const
css::uno::Reference< css::frame::XModel3 > GetModel() const
const css::uno::Any & GetValue() const
Provides functions to convert Calc cell addresses to Excel cell addresses.
Definition: xehelper.hxx:82
void Finalize()
Definition: xestyle.cxx:3288
Sheet filter manager.
Definition: excrecds.hxx:436
Stores the data of all fonts used in the document.
Definition: xestyle.hxx:209
The formula compiler to create Excel token arrays from Calc token arrays.
Definition: xeformula.hxx:52
Stores all data for internal/external references (the link table).
Definition: xelink.hxx:146
Manager that stores all internal defined names (NAME records) of the document.
Definition: xename.hxx:32
void Initialize()
Creates NAME records for built-in and user defined names.
Definition: xename.cxx:804
Stores all number formats used in the document.
Definition: xestyle.hxx:268
Stores all used colors in the document.
Definition: xestyle.hxx:77
void Finalize()
Reduces the color list to the maximum count of the current BIFF version.
Definition: xestyle.cxx:778
The main class for pivot table export.
Definition: xepivot.hxx:406
The main progress bar for the export filter.
Definition: xehelper.hxx:44
void Initialize()
Initializes all segments and sub progress bars.
Definition: xehelper.cxx:83
XclExpTablesManager & GetTablesManager()
Definition: xeroot.cxx:178
XclExpLinkManager & GetGlobalLinkManager() const
Returns the global link manager for defined names.
Definition: xeroot.cxx:130
XclExpFontBuffer & GetFontBuffer() const
Returns the font buffer.
Definition: xeroot.cxx:112
XclExpPivotTableManager & GetPivotTableManager() const
Returns the pivot table manager.
Definition: xeroot.cxx:166
void InitializeConvert()
Is called when export filter starts to create the Excel document (all BIFF versions).
Definition: xeroot.cxx:184
XclExpNameManager & GetNameManager() const
Returns the buffer that contains internal defined names.
Definition: xeroot.cxx:142
XclExpFormulaCompiler & GetFormulaCompiler() const
Returns the formula compiler to produce formula token arrays.
Definition: xeroot.cxx:88
void InitializeTable(SCTAB nScTab)
Is called when export filter starts to create data for a single sheet (all BIFF versions).
Definition: xeroot.cxx:265
XclExpObjectManager & GetObjectManager() const
Returns the drawing object manager.
Definition: xeroot.cxx:148
XclExpDxfs & GetDxfs() const
Returns the differential formatting list.
Definition: xeroot.cxx:160
XclExpAddressConverter & GetAddressConverter() const
Returns the address converter.
Definition: xeroot.cxx:82
css::uno::Sequence< css::beans::NamedValue > GetEncryptionData() const
Definition: xeroot.cxx:335
void InitializeGlobals()
Is called when export filter starts to create the workbook global data (>=BIFF5).
Definition: xeroot.cxx:194
XclExpNumFmtBuffer & GetNumFmtBuffer() const
Returns the number format buffer.
Definition: xeroot.cxx:118
static css::uno::Sequence< css::beans::NamedValue > GenerateDefaultEncryptionData()
Definition: xeroot.cxx:352
XclExpSst & GetSst() const
Returns the shared string table.
Definition: xeroot.cxx:100
bool IsDocumentEncrypted() const
Definition: xeroot.cxx:300
static css::uno::Sequence< css::beans::NamedValue > GenerateEncryptionData(std::u16string_view aPass)
Definition: xeroot.cxx:311
XclExpRootData & mrExpData
Definition: xeroot.hxx:188
XclExpLinkManager & GetLocalLinkManager() const
Returns the local link manager for the current sheet.
Definition: xeroot.cxx:136
XclExpXmlPivotTableManager & GetXmlPivotTableManager()
Definition: xeroot.cxx:172
XclExpTabInfo & GetTabInfo() const
Returns the buffer for Calc->Excel sheet index conversion.
Definition: xeroot.cxx:76
XclExpRecordRef CreateRecord(sal_uInt16 nRecId) const
Returns the reference to a record (or record list) representing a root object.
Definition: xeroot.cxx:282
XclExpPalette & GetPalette() const
Returns the color buffer.
Definition: xeroot.cxx:106
XclExpRoot(XclExpRootData &rExpRootData)
Definition: xeroot.cxx:70
XclExpXFBuffer & GetXFBuffer() const
Returns the cell formatting attributes buffer.
Definition: xeroot.cxx:124
void InitializeSave()
Is called before export filter starts to write the records to the stream.
Definition: xeroot.cxx:275
XclExpProgressBar & GetProgressBar() const
Returns the export progress bar.
Definition: xeroot.cxx:94
XclExpRootData::XclExpLinkMgrRef const & GetLocalLinkMgrRef() const
Returns the local or global link manager, depending on current context.
Definition: xeroot.cxx:357
const XclExpRoot & GetRoot() const
Returns this root instance - for code readability in derived classes.
Definition: xeroot.hxx:118
XclExpFilterManager & GetFilterManager() const
Returns the filter manager.
Definition: xeroot.cxx:154
Provides export of the SST (shared string table) record.
Definition: xecontent.hxx:52
Stores the correct Excel sheet index for each Calc sheet.
Definition: xelink.hxx:62
Stores all data for database ranges (tables in Excel speak).
Definition: xedbdata.hxx:53
Stores all XF records (cell formats and cell styles) in the document.
Definition: xestyle.hxx:586
void Finalize()
Reduces the XF record list to the maximum allowed number of records.
Definition: xestyle.cxx:2569
void Initialize()
Inserts predefined built-in styles and user-defined styles.
Definition: xestyle.cxx:2527
Access to global data for a filter object (imported or exported document) from other classes.
Definition: xlroot.hxx:128
XclOutput GetOutput() const
Returns the current output format of the importer/exporter.
Definition: xlroot.hxx:143
static OUString GetDefaultPassword()
Returns the default password used for stream encryption.
Definition: xlroot.hxx:179
SfxMedium & GetMedium() const
Returns the medium to import from.
Definition: xlroot.hxx:170
bool IsInGlobals() const
Returns the current Calc sheet index.
Definition: xlroot.hxx:160
XclBiff GetBiff() const
Returns the current BIFF version of the importer/exporter.
Definition: xlroot.hxx:141
void SetCurrScTab(SCTAB nScTab)
Sets the current Calc sheet index.
Definition: xlroot.hxx:261
ScDocument & GetDoc() const
Returns reference to the destination document (import) or source document (export).
Definition: xlroot.cxx:285
static OpCodeMapPtr CreateOpCodeMap(const css::uno::Sequence< const css::sheet::FormulaOpCodeMapEntry > &rMapping, bool bEnglish)
virtual css::uno::Sequence< css::beans::NamedValue > GetEncryptionData()
virtual void InitKey(const sal_uInt16 pPassData[16], const sal_uInt8 pDocId[16]) override
Provides access to API op-codes for all available formula tokens and to function info structs for all...
css::uno::Sequence< css::sheet::FormulaOpCodeMapEntry > getOoxParserMap() const
Returns the op-code map that is used by the OOXML formula parser.
#define SAL_WARN(area, stream)
Stores global buffers and data needed for Excel export filter.
Definition: xeroot.hxx:60
XclExpTabInfoRef mxTabInfo
Definition: xeroot.hxx:78
ScCompiler::OpCodeMapPtr mxOpCodeMap
Definition: xeroot.hxx:100
XclExpDxfsRef mxDxfs
All pivot tables and pivot caches.
Definition: xeroot.hxx:94
XclExpLinkMgrRef mxLocLinkMgr
Global link manager for defined names.
Definition: xeroot.hxx:90
OStringBuffer maStringBuf
true = Store URLs relative.
Definition: xeroot.hxx:104
XclExpLinkMgrRef mxGlobLinkMgr
Internal defined names.
Definition: xeroot.hxx:89
bool mbRelUrl
mapping between op-codes and names
Definition: xeroot.hxx:102
XclExpProgressRef mxProgress
The formula compiler.
Definition: xeroot.hxx:81
XclExpObjectMgrRef mxObjMgr
Local link manager for a sheet.
Definition: xeroot.hxx:91
XclExpFilterMgrRef mxFilterMgr
All drawing objects.
Definition: xeroot.hxx:92
XclExpNumFmtBfrRef mxNumFmtBfr
All fonts in the file.
Definition: xeroot.hxx:86
XclExpRootData(XclBiff eBiff, SfxMedium &rMedium, const tools::SvRef< SotStorage > &xRootStrg, ScDocument &rDoc, rtl_TextEncoding eTextEnc)
buffer to avoid massive OUString allocations
Definition: xeroot.cxx:56
XclExpFmlaCompRef mxFmlaComp
The address converter.
Definition: xeroot.hxx:80
XclExpNameMgrRef mxNameMgr
All XF records in the file.
Definition: xeroot.hxx:88
XclExpXFBfrRef mxXFBfr
All number formats in the file.
Definition: xeroot.hxx:87
std::shared_ptr< XclExpTablesManager > mxTablesMgr
Definition: xeroot.hxx:97
XclExpSstRef mxSst
The export progress bar.
Definition: xeroot.hxx:83
XclExpPaletteRef mxPalette
The shared string table.
Definition: xeroot.hxx:84
XclExpPTableMgrRef mxPTableMgr
Manager for filtered areas in all sheets.
Definition: xeroot.hxx:93
virtual ~XclExpRootData() override
Definition: xeroot.cxx:66
std::shared_ptr< XclExpXmlPivotTableManager > mxXmlPTableMgr
All delta formatting entries.
Definition: xeroot.hxx:96
XclExpAddrConvRef mxAddrConv
Calc->Excel sheet index conversion.
Definition: xeroot.hxx:79
XclExpFontBfrRef mxFontBfr
The color buffer.
Definition: xeroot.hxx:85
Stores global buffers and data needed elsewhere in the Excel filters.
Definition: xlroot.hxx:65
SfxMedium & mrMedium
Current Output format.
Definition: xlroot.hxx:77
unsigned char sal_uInt8
sal_Int16 SCTAB
Definition: types.hxx:22
const sal_uInt16 EXC_ID_FONTLIST
Definition: xestyle.hxx:39
const sal_uInt16 EXC_ID_XFLIST
For internal use only.
Definition: xestyle.hxx:41
const sal_uInt16 EXC_ID_FORMATLIST
For internal use only.
Definition: xestyle.hxx:40
const sal_uInt16 EXC_ID_DXFS
For internal use only.
Definition: xestyle.hxx:42
const SCTAB SCTAB_GLOBAL
An invalid Calc sheet index, for common use.
Definition: xlconst.hxx:74
XclBiff
An enumeration for all Excel file format types (BIFF types).
Definition: xlconst.hxx:30
@ EXC_BIFF5
MS Excel 4.0.
Definition: xlconst.hxx:34
@ EXC_BIFF8
MS Excel 5.0, MS Excel 7.0 (95)
Definition: xlconst.hxx:35
@ EXC_OUTPUT_XML_2007
MS Excel binary .xls.
Definition: xlconst.hxx:43
const sal_uInt16 EXC_ID_SST
Definition: xlcontent.hxx:45
const sal_uInt16 EXC_ID_NAME
Definition: xlname.hxx:28
const sal_uInt16 EXC_ID_PALETTE
Definition: xlstyle.hxx:200