LibreOffice Module sc (master)  1
pivotcachefragment.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 <pivotcachefragment.hxx>
21 
22 #include <osl/diagnose.h>
23 #include <oox/token/namespaces.hxx>
24 #include <biffhelper.hxx>
25 #include <formulabuffer.hxx>
26 #include <pivotcachebuffer.hxx>
27 #include <worksheetbuffer.hxx>
28 
29 namespace oox::xls {
30 
31 using namespace ::com::sun::star::uno;
32 using namespace ::oox::core;
33 
35  WorkbookContextBase( rFragment ),
36  mrCacheField( rCacheField )
37 {
38 }
39 
41 {
42  switch( getCurrentElement() )
43  {
44  case XLS_TOKEN( cacheField ):
45  if( nElement == XLS_TOKEN( sharedItems ) ) { mrCacheField.importSharedItems( rAttribs ); return this; }
46  if( nElement == XLS_TOKEN( fieldGroup ) ) { mrCacheField.importFieldGroup( rAttribs ); return this; }
47  break;
48 
49  case XLS_TOKEN( fieldGroup ):
50  switch( nElement )
51  {
52  case XLS_TOKEN( rangePr ): mrCacheField.importRangePr( rAttribs ); break;
53  case XLS_TOKEN( discretePr ): return this;
54  case XLS_TOKEN( groupItems ): return this;
55  }
56  break;
57 
58  case XLS_TOKEN( sharedItems ): mrCacheField.importSharedItem( nElement, rAttribs ); break;
59  case XLS_TOKEN( discretePr ): mrCacheField.importDiscretePrItem( nElement, rAttribs ); break;
60  case XLS_TOKEN( groupItems ): mrCacheField.importGroupItem( nElement, rAttribs ); break;
61  }
62  return nullptr;
63 }
64 
66 {
67  if( isRootElement() )
68  mrCacheField.importCacheField( rAttribs );
69 }
70 
72 {
73  switch( getCurrentElement() )
74  {
75  case BIFF12_ID_PCDFIELD:
76  switch( nRecId )
77  {
79  case BIFF12_ID_PCDFIELDGROUP: mrCacheField.importPCDFieldGroup( rStrm ); return this;
80  }
81  break;
82 
84  switch( nRecId )
85  {
87  case BIFF12_ID_PCDFDISCRETEPR: return this;
88  case BIFF12_ID_PCDFGROUPITEMS: return this;
89  }
90  break;
91 
92  case BIFF12_ID_PCDFSHAREDITEMS: mrCacheField.importPCDFSharedItem( nRecId, rStrm ); break;
94  case BIFF12_ID_PCDFGROUPITEMS: mrCacheField.importPCDFGroupItem( nRecId, rStrm ); break;
95  }
96  return nullptr;
97 }
98 
100 {
101  if( isRootElement() )
102  mrCacheField.importPCDField( rStrm );
103 }
104 
106  const WorkbookHelper& rHelper, const OUString& rFragmentPath, PivotCache& rPivotCache ) :
107  WorkbookFragmentBase( rHelper, rFragmentPath ),
108  mrPivotCache( rPivotCache )
109 {
110 }
111 
113 {
114  switch( getCurrentElement() )
115  {
116  case XML_ROOT_CONTEXT:
117  if( nElement == XLS_TOKEN( pivotCacheDefinition ) ) { mrPivotCache.importPivotCacheDefinition( rAttribs ); return this; }
118  break;
119 
120  case XLS_TOKEN( pivotCacheDefinition ):
121  switch( nElement )
122  {
123  case XLS_TOKEN( cacheSource ): mrPivotCache.importCacheSource( rAttribs ); return this;
124  case XLS_TOKEN( cacheFields ): return this;
125  }
126  break;
127 
128  case XLS_TOKEN( cacheSource ):
129  if( nElement == XLS_TOKEN( worksheetSource ) ) mrPivotCache.importWorksheetSource( rAttribs, getRelations() );
130  break;
131 
132  case XLS_TOKEN( cacheFields ):
133  if( nElement == XLS_TOKEN( cacheField ) ) return new PivotCacheFieldContext( *this, mrPivotCache.createCacheField() );
134  break;
135  }
136  return nullptr;
137 }
138 
140 {
141  switch( getCurrentElement() )
142  {
143  case XML_ROOT_CONTEXT:
144  if( nRecId == BIFF12_ID_PCDEFINITION ) { mrPivotCache.importPCDefinition( rStrm ); return this; }
145  break;
146 
148  switch( nRecId )
149  {
150  case BIFF12_ID_PCDSOURCE: mrPivotCache.importPCDSource( rStrm ); return this;
151  case BIFF12_ID_PCDFIELDS: return this;
152  }
153  break;
154 
155  case BIFF12_ID_PCDSOURCE:
156  if( nRecId == BIFF12_ID_PCDSHEETSOURCE ) mrPivotCache.importPCDSheetSource( rStrm, getRelations() );
157  break;
158 
159  case BIFF12_ID_PCDFIELDS:
160  if( nRecId == BIFF12_ID_PCDFIELD ) return new PivotCacheFieldContext( *this, mrPivotCache.createCacheField() );
161  break;
162  }
163  return nullptr;
164 }
165 
167 {
168  static const RecordInfo spRecInfos[] =
169  {
181  { -1, -1 }
182  };
183  return spRecInfos;
184 }
185 
187 {
188  // finalize the cache (check source range etc.)
190 
191  // load the cache records, if the cache is based on a deleted or an external worksheet
193  {
194  OUString aRecFragmentPath = getRelations().getFragmentPathFromRelId( mrPivotCache.getRecordsRelId() );
195  if( !aRecFragmentPath.isEmpty() )
196  {
199  if( xSheetGlob )
200  importOoxFragment( new PivotCacheRecordsFragment( *xSheetGlob, aRecFragmentPath, mrPivotCache ) );
201  }
202  }
203 }
204 
206  const OUString& rFragmentPath, const PivotCache& rPivotCache ) :
207  WorksheetFragmentBase( rHelper, rFragmentPath ),
208  mrPivotCache( rPivotCache ),
209  mnColIdx( 0 ),
210  mnRowIdx( 0 ),
211  mbInRecord( false )
212 {
213  sal_Int32 nSheetCount = rPivotCache.getWorksheets().getAllSheetCount();
214 
215  // prepare sheet: insert column header names into top row
216  rPivotCache.writeSourceHeaderCells( *this );
217  // resize formula buffers since we've added a new dummy sheet
218  rHelper.getFormulaBuffer().SetSheetCount( nSheetCount );
219 }
220 
222 {
223  switch( getCurrentElement() )
224  {
225  case XML_ROOT_CONTEXT:
226  if( nElement == XLS_TOKEN( pivotCacheRecords ) ) return this;
227  break;
228 
229  case XLS_TOKEN( pivotCacheRecords ):
230  if( nElement == XLS_TOKEN( r ) ) { startCacheRecord(); return this; }
231  break;
232 
233  case XLS_TOKEN( r ):
234  {
235  PivotCacheItem aItem;
236  switch( nElement )
237  {
238  case XLS_TOKEN( m ): break;
239  case XLS_TOKEN( s ): aItem.readString( rAttribs ); break;
240  case XLS_TOKEN( n ): aItem.readNumeric( rAttribs ); break;
241  case XLS_TOKEN( d ): aItem.readDate( rAttribs ); break;
242  case XLS_TOKEN( b ): aItem.readBool( rAttribs ); break;
243  case XLS_TOKEN( e ): aItem.readError( rAttribs ); break;
244  case XLS_TOKEN( x ): aItem.readIndex( rAttribs ); break;
245  default: OSL_FAIL( "OoxPivotCacheRecordsFragment::onCreateContext - unexpected element" );
246  }
248  ++mnColIdx;
249  }
250  break;
251  }
252  return nullptr;
253 }
254 
256 {
257  switch( getCurrentElement() )
258  {
259  case XML_ROOT_CONTEXT:
260  if( nRecId == BIFF12_ID_PCRECORDS ) return this;
261  break;
262 
263  case BIFF12_ID_PCRECORDS:
264  switch( nRecId )
265  {
266  case BIFF12_ID_PCRECORD: importPCRecord( rStrm ); break;
267  case BIFF12_ID_PCRECORDDT: startCacheRecord(); break;
268  default: importPCRecordItem( nRecId, rStrm ); break;
269  }
270  break;
271  }
272  return nullptr;
273 }
274 
276 {
277  static const RecordInfo spRecInfos[] =
278  {
280  { -1, -1 }
281  };
282  return spRecInfos;
283 }
284 
285 // private --------------------------------------------------------------------
286 
288 {
289  mnColIdx = 0;
290  ++mnRowIdx;
291  mbInRecord = true;
292 }
293 
295 {
297  mrPivotCache.importPCRecord( rStrm, *this, mnRowIdx );
298  mbInRecord = false;
299 }
300 
302 {
303  if( !mbInRecord )
304  return;
305 
306  PivotCacheItem aItem;
307  switch( nRecId )
308  {
309  case BIFF12_ID_PCITEM_MISSING: break;
310  case BIFF12_ID_PCITEM_STRING: aItem.readString( rStrm ); break;
311  case BIFF12_ID_PCITEM_DOUBLE: aItem.readDouble( rStrm ); break;
312  case BIFF12_ID_PCITEM_DATE: aItem.readDate( rStrm ); break;
313  case BIFF12_ID_PCITEM_BOOL: aItem.readBool( rStrm ); break;
314  case BIFF12_ID_PCITEM_ERROR: aItem.readError( rStrm ); break;
315  case BIFF12_ID_PCITEM_INDEX: aItem.readIndex( rStrm ); break;
316  default: OSL_FAIL( "OoxPivotCacheRecordsFragment::importPCRecordItem - unexpected record" );
317  }
319  ++mnColIdx;
320 }
321 
322 
323 } // namespace oox::xls
324 
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Helper class to provide access to global workbook data.
void importDiscretePrItem(sal_Int32 nElement, const AttributeList &rAttribs)
Imports an item of the mapping between group items and base items from the passed element...
static WorksheetGlobalsRef constructGlobals(const WorkbookHelper &rHelper, const ISegmentProgressBarRef &rxProgressBar, WorksheetType eSheetType, SCTAB nSheet)
void importPCDFGroupItem(sal_Int32 nRecId, SequenceInputStream &rStrm)
Imports one or more group items from the passed record.
sal_Int32 getAllSheetCount() const
Returns the number of all sheets, workbook + dummy ones (pivot table cache records ) ...
ScAddress aStart
Definition: address.hxx:500
void importSharedItems(const AttributeList &rAttribs)
Imports shared items settings from the sharedItems element.
void importPCDFSharedItems(SequenceInputStream &rStrm)
Imports shared items settings from the PCDFSHAREDITEMS record.
const sal_Int32 BIFF12_ID_PCDSOURCE
Definition: biffhelper.hxx:165
PivotCacheFieldContext(WorkbookFragmentBase &rFragment, PivotCacheField &rCacheField)
void importPCDefinition(SequenceInputStream &rStrm)
Reads pivot cache global settings from the PCDEFINITION record.
virtual const ::oox::core::RecordInfo * getRecordInfos() const override
const OUString & getRecordsRelId() const
Returns the relation identifier of the pivot cache records fragment.
virtual ::oox::core::ContextHandlerRef onCreateContext(sal_Int32 nElement, const AttributeList &rAttribs) override
PivotCacheField & createCacheField()
Creates and returns a new pivot cache field.
sal_Int64 n
void importPCDSource(SequenceInputStream &rStrm)
Reads cache source settings from the PCDSOURCE record.
const sal_Int32 BIFF12_ID_PCITEM_STRING
Definition: biffhelper.hxx:173
PivotCacheDefinitionFragment(const WorkbookHelper &rHelper, const OUString &rFragmentPath, PivotCache &rPivotCache)
void writeSourceDataCell(const WorksheetHelper &rSheetHelper, sal_Int32 nColIdx, sal_Int32 nRowIdx, const PivotCacheItem &rItem) const
Writes a source field item value into the passed sheet.
const ScRange & getSourceRange() const
Returns the internal cell range the cache is based on.
float x
virtual ::oox::core::ContextHandlerRef onCreateContext(sal_Int32 nElement, const AttributeList &rAttribs) override
bool importOoxFragment(const rtl::Reference< oox::core::FragmentHandler > &rxHandler)
Imports a fragment using the passed fragment handler, which contains the full path to the fragment st...
void importWorksheetSource(const AttributeList &rAttribs, const ::oox::core::Relations &rRelations)
Reads sheet source settings from the worksheetSource element.
void importGroupItem(sal_Int32 nElement, const AttributeList &rAttribs)
Imports a group item from the passed element.
virtual ::oox::core::ContextHandlerRef onCreateRecordContext(sal_Int32 nRecId, SequenceInputStream &rStrm) override
void importPivotCacheDefinition(const AttributeList &rAttribs)
Reads pivot cache global settings from the pivotCacheDefinition element.
Fragment handler derived from the WorkbookHelper helper class.
virtual void onStartElement(const AttributeList &rAttribs) override
const sal_Int32 BIFF12_ID_PCDFIELD
Definition: biffhelper.hxx:159
const sal_Int32 BIFF12_ID_PCDFIELDGROUP
Definition: biffhelper.hxx:160
std::shared_ptr< WorksheetGlobals > WorksheetGlobalsRef
void readNumeric(const AttributeList &rAttribs)
Reads the double value from a pivot cache item.
const sal_Int32 BIFF12_ID_PCDFGROUPITEMS
Definition: biffhelper.hxx:158
const sal_Int32 BIFF12_ID_PCDFIELDS
Definition: biffhelper.hxx:161
SCTAB Tab() const
Definition: address.hxx:271
virtual ::oox::core::ContextHandlerRef onCreateRecordContext(sal_Int32 nRecId, SequenceInputStream &rStrm) override
void importPCRecordItem(sal_Int32 nRecId, SequenceInputStream &rStrm)
const sal_Int32 BIFF12_ID_PCITEM_ARRAY
Definition: biffhelper.hxx:166
void importFieldGroup(const AttributeList &rAttribs)
Imports grouping settings from the fieldGroup element.
bool mbInRecord
Relative row index in source data.
double d
virtual const ::oox::core::RecordInfo * getRecordInfos() const override
const sal_Int32 BIFF12_ID_PCDFSHAREDITEMS
Definition: biffhelper.hxx:163
void importPCRecord(SequenceInputStream &rStrm)
void importRangePr(const AttributeList &rAttribs)
Imports numeric grouping settings from the rangePr element.
void readDouble(SequenceInputStream &rStrm)
Reads the double value from a pivot cache item.
void importPCDFieldGroup(SequenceInputStream &rStrm)
Imports grouping settings from the PCDFIELDGROUP record.
const sal_Int32 BIFF12_ID_PCITEM_DOUBLE
Definition: biffhelper.hxx:169
std::shared_ptr< ISegmentProgressBar > ISegmentProgressBarRef
sal_Int32 getCurrentElement() const
void importSharedItem(sal_Int32 nElement, const AttributeList &rAttribs)
Imports a shared item from the passed element.
virtual void onStartRecord(SequenceInputStream &rStrm) override
bool isBasedOnDummySheet() const
Returns true, if the pivot cache is based on a dummy sheet created in finalizeImport.
const sal_Int32 BIFF12_ID_PCRECORDS
Definition: biffhelper.hxx:182
void importCacheSource(const AttributeList &rAttribs)
Reads cache source settings from the cacheSource element.
const sal_Int32 BIFF12_ID_PCITEM_DATE
Definition: biffhelper.hxx:168
const sal_Int32 BIFF12_ID_PCITEM_BOOL
Definition: biffhelper.hxx:167
void readBool(const AttributeList &rAttribs)
Reads the boolean value from a pivot cache item.
PivotCacheRecordsFragment(const WorksheetHelper &rHelper, const OUString &rFragmentPath, const PivotCache &rPivotCache)
const sal_Int32 BIFF12_ID_PCDSHEETSOURCE
Definition: biffhelper.hxx:164
const sal_Int32 BIFF12_ID_PCRECORDDT
Definition: biffhelper.hxx:181
const sal_Int32 BIFF12_ID_PCITEM_ERROR
Definition: biffhelper.hxx:170
void SetSheetCount(SCTAB nSheets)
ensure sizes of vectors matches the number of sheets
void writeSourceHeaderCells(const WorksheetHelper &rSheetHelper) const
Writes the titles of all source fields into the passed sheet.
Fragment handler derived from the WorksheetHelper helper class.
Context handler derived from the WorkbookHelper helper class.
const sal_Int32 BIFF12_ID_PCDFRANGEPR
Definition: biffhelper.hxx:162
void readError(const AttributeList &rAttribs)
Reads the error code value from a pivot cache item.
void importPCDField(SequenceInputStream &rStrm)
Imports pivot cache field settings from the PCDFIELD record.
sal_Int32 mnRowIdx
Relative column index in source data.
void importPCDFRangePr(SequenceInputStream &rStrm)
Imports numeric grouping settings from the PCDFRANGEPR record.
void readIndex(const AttributeList &rAttribs)
Reads the index of a shared item.
void finalizeImport()
Checks validity of source data and creates a dummy data sheet for external sheet sources.
const sal_Int32 BIFF12_ID_PCRECORD
Definition: biffhelper.hxx:180
bool isValidDataSource() const
Returns true, if the pivot cache is based on a valid data source, so that pivot tables can be created...
const sal_Int32 BIFF12_ID_PCITEM_MISSING
Definition: biffhelper.hxx:172
const sal_Int32 BIFF12_ID_PCDFDISCRETEPR
Definition: biffhelper.hxx:157
const sal_Int32 BIFF12_ID_PCITEM_INDEX
Definition: biffhelper.hxx:171
void importPCRecord(SequenceInputStream &rStrm, const WorksheetHelper &rSheetHelper, sal_Int32 nRowIdx) const
Reads a PCRECORD record and writes all item values to the passed sheet.
const sal_Int32 XML_ROOT_CONTEXT
virtual ::oox::core::ContextHandlerRef onCreateRecordContext(sal_Int32 nRecId, SequenceInputStream &rStrm) override
void readDate(const AttributeList &rAttribs)
Reads the date/time value from a pivot cache item.
void importCacheField(const AttributeList &rAttribs)
Imports pivot cache field settings from the cacheField element.
tuple m
void importPCDFSharedItem(sal_Int32 nRecId, SequenceInputStream &rStrm)
Imports one or more shared items from the passed record.
virtual ::oox::core::ContextHandlerRef onCreateContext(sal_Int32 nElement, const AttributeList &rAttribs) override
void importPCDFDiscretePrItem(sal_Int32 nRecId, SequenceInputStream &rStrm)
Imports an item of the mapping between group items and base items from the passed record...
sal_Int16 SCTAB
Definition: types.hxx:23
FormulaBuffer & getFormulaBuffer() const
void importPCDSheetSource(SequenceInputStream &rStrm, const ::oox::core::Relations &rRelations)
Reads sheet source settings from the PCDSHEETSOURCE record.
WorksheetBuffer & getWorksheets() const
Returns the worksheet buffer containing sheet names and properties.
const sal_Int32 BIFF12_ID_PCDEFINITION
Definition: biffhelper.hxx:156
void readString(const AttributeList &rAttribs)
Reads the string value from a pivot cache item.