LibreOffice Module xmloff (master)  1
SchXMLTableContext.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 <sax/tools/converter.hxx>
21 
22 #include "SchXMLTableContext.hxx"
25 #include <SchXMLImport.hxx>
26 #include "SchXMLTools.hxx"
27 #include "transporttypes.hxx"
29 #include <o3tl/safeint.hxx>
30 #include <rtl/math.hxx>
31 #include <sal/log.hxx>
32 #include <xmloff/xmlnamespace.hxx>
33 #include <xmloff/xmltoken.hxx>
34 #include <xmloff/namespacemap.hxx>
35 #include <comphelper/sequence.hxx>
36 #include <com/sun/star/chart2/XAnyDescriptionAccess.hpp>
37 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
38 #include <com/sun/star/chart2/XChartDocument.hpp>
39 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
40 #include <com/sun/star/chart2/XInternalDataProvider.hpp>
41 #include <com/sun/star/beans/XPropertySet.hpp>
42 
43 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
44 
45 #include <vector>
46 #include <algorithm>
47 #include <iterator>
48 #include <string_view>
49 
50 using namespace com::sun::star;
51 using namespace ::xmloff::token;
52 using ::com::sun::star::uno::Sequence;
53 using ::com::sun::star::uno::Reference;
54 
55 namespace
56 {
57 
58 constexpr OUStringLiteral aCategoriesRange = u"categories";
59 
60 typedef ::std::multimap< OUString, OUString >
61  lcl_tOriginalRangeToInternalRangeMap;
62 
63 struct lcl_ApplyCellToData
64 {
65  explicit lcl_ApplyCellToData( Sequence< double > & rOutData ) :
66  m_rData( rOutData ),
67  m_nIndex( 0 ),
68  m_nSize( rOutData.getLength()),
69  m_fNaN( 0.0 )
70  {
71  ::rtl::math::setNan( &m_fNaN );
72  }
73 
74  void operator() ( const SchXMLCell & rCell )
75  {
76  if( m_nIndex < m_nSize )
77  {
78  if( rCell.eType == SCH_CELL_TYPE_FLOAT )
79  m_rData[m_nIndex] = rCell.fValue;
80  else
81  m_rData[m_nIndex] = m_fNaN;
82  }
83  ++m_nIndex;
84  }
85 
86  sal_Int32 getCurrentIndex() const
87  {
88  return m_nIndex;
89  }
90 
91 private:
92  Sequence< double > & m_rData;
93  sal_Int32 m_nIndex;
94  sal_Int32 m_nSize;
95  double m_fNaN;
96 };
97 
98 void lcl_fillRangeMapping(
99  const SchXMLTable & rTable,
100  lcl_tOriginalRangeToInternalRangeMap & rOutRangeMap,
101  chart::ChartDataRowSource eDataRowSource )
102 {
103  sal_Int32 nRowOffset = ( rTable.bHasHeaderRow ? 1 : 0 );
104  sal_Int32 nColOffset = ( rTable.bHasHeaderColumn ? 1 : 0 );
105 
106  const OUString lcl_aCategoriesRange(aCategoriesRange);
107  const OUString lcl_aLabelPrefix("label ");
108 
109  // Fill range mapping
110  const size_t nTableRowCount( rTable.aData.size());
111  for( size_t nRow = 0; nRow < nTableRowCount; ++nRow )
112  {
113  const ::std::vector< SchXMLCell > & rRow( rTable.aData[nRow] );
114  const size_t nTableColCount( rRow.size());
115  for( size_t nCol = 0; nCol < nTableColCount; ++nCol )
116  {
117  const OUString aRangeId( rRow[nCol].aRangeId );
118  if( !aRangeId.isEmpty())
119  {
120  if( eDataRowSource == chart::ChartDataRowSource_COLUMNS )
121  {
122  if( nCol == 0 && rTable.bHasHeaderColumn )
123  {
124  SAL_WARN_IF( static_cast< sal_Int32 >( nRow ) != nRowOffset, "xmloff.chart", "nRow != nRowOffset" );
125  rOutRangeMap.emplace(aRangeId, lcl_aCategoriesRange);
126  }
127  else
128  {
129  OUString aColNumStr = OUString::number( nCol - nColOffset);
130  if( nRow == 0 && rTable.bHasHeaderRow )
131  rOutRangeMap.emplace( aRangeId, lcl_aLabelPrefix + aColNumStr );
132  else
133  rOutRangeMap.emplace( aRangeId, aColNumStr );
134  }
135  }
136  else // eDataRowSource == chart::ChartDataRowSource_ROWS
137  {
138  if( nRow == 0 && rTable.bHasHeaderRow )
139  {
140  SAL_WARN_IF( static_cast< sal_Int32 >( nCol ) != nColOffset, "xmloff.chart", "nCol != nColOffset" );
141  rOutRangeMap.emplace( aRangeId, lcl_aCategoriesRange );
142  }
143  else
144  {
145  OUString aRowNumStr = OUString::number( nRow - nRowOffset);
146  if( nCol == 0 && rTable.bHasHeaderColumn )
147  rOutRangeMap.emplace( aRangeId, lcl_aLabelPrefix + aRowNumStr );
148  else
149  rOutRangeMap.emplace( aRangeId, aRowNumStr );
150  }
151  }
152  }
153  }
154  }
155 }
156 
157 Reference< chart2::data::XDataSequence >
158  lcl_reassignDataSequence(
159  const Reference< chart2::data::XDataSequence > & xSequence,
160  const Reference< chart2::data::XDataProvider > & xDataProvider,
161  lcl_tOriginalRangeToInternalRangeMap & rRangeMap,
162  const OUString & rRange )
163 {
164  Reference< chart2::data::XDataSequence > xResult( xSequence );
165  lcl_tOriginalRangeToInternalRangeMap::iterator aIt( rRangeMap.find( rRange ));
166  if( aIt != rRangeMap.end())
167  {
168  // set sequence with correct data
169  xResult.set( xDataProvider->createDataSequenceByRangeRepresentation( aIt->second ));
170  // remove translation, because it was used
171  rRangeMap.erase( aIt );
172  }
173 
174  return xResult;
175 }
176 
177 bool lcl_mapContainsRange(
178  lcl_tOriginalRangeToInternalRangeMap & rRangeMap,
179  const OUString & rRange )
180 {
181  lcl_tOriginalRangeToInternalRangeMap::iterator aIt( rRangeMap.find( rRange ));
182  return ( aIt != rRangeMap.end());
183 }
184 
185 bool lcl_tableOfRangeMatches(
186  const OUString & rRange,
187  std::u16string_view rTableName )
188 {
189  // both strings are non-empty and the table name is part of the range
190  return ( !rRange.isEmpty() &&
191  !rTableName.empty() &&
192  (rRange.indexOf( rTableName ) != -1 ));
193 }
194 
195 } // anonymous namespace
196 
197 // class SchXMLTableContext
199  SchXMLTable& aTable ) :
200  SvXMLImportContext( rImport ),
201  mrTable( aTable ),
202  mbHasRowPermutation( false ),
203  mbHasColumnPermutation( false )
204 {
205  mrTable.nColumnIndex = -1;
207  mrTable.nRowIndex = -1;
208  mrTable.aData.clear();
209 }
210 
212 {
213 }
214 
215 css::uno::Reference< css::xml::sax::XFastContextHandler > SchXMLTableContext::createFastChildContext(
216  sal_Int32 nElement,
217  const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
218 {
219  SvXMLImportContext* pContext = nullptr;
220 
221  switch(nElement)
222  {
224  mrTable.bHasHeaderColumn = true;
225  [[fallthrough]];
227  pContext = new SchXMLTableColumnsContext( GetImport(), mrTable );
228  break;
229 
231  pContext = new SchXMLTableColumnContext( GetImport(), mrTable );
232  break;
233 
235  mrTable.bHasHeaderRow = true;
236  [[fallthrough]];
238  pContext = new SchXMLTableRowsContext( GetImport(), mrTable );
239  break;
240 
242  pContext = new SchXMLTableRowContext( GetImport(), mrTable );
243  break;
244  default:
245  XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
246  }
247 
248  return pContext;
249 }
250 
251 void SchXMLTableContext::startFastElement (sal_Int32 /*nElement*/,
252  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList)
253 {
254  // get table-name
255 
256  for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
257  {
258  switch(aIter.getToken())
259  {
260  case XML_ELEMENT(TABLE, XML_NAME):
261  mrTable.aTableNameOfFile = aIter.toString();
262  break;
264  if ( IsXMLToken( aIter, XML_TRUE ) )
265  {
266  mrTable.bProtected = true;
267  }
268  break;
269  default:
270  XMLOFF_WARN_UNKNOWN("xmloff", aIter);
271  }
272  }
273 }
274 
276 {
278  {
279  SAL_WARN_IF( mbHasRowPermutation, "xmloff.chart", "mbHasColumnPermutation is true" );
280  auto aPermutation( comphelper::sequenceToContainer<std::vector< sal_Int32 >>( maColumnPermutation ));
281  SAL_WARN_IF( aPermutation.empty(), "xmloff.chart", "aPermutation is NULL");
282  if( aPermutation.empty())
283  return;
284 
285  // permute the values of all rows according to aPermutation
286  for( auto& rRow : mrTable.aData )
287  {
288  bool bModified = false;
289  ::std::vector< SchXMLCell > aModifiedRow;
290  const size_t nPermSize = aPermutation.size();
291  SAL_WARN_IF( static_cast< sal_Int32 >( nPermSize ) - 1 != *(::std::max_element( aPermutation.begin(), aPermutation.end())), "xmloff.chart", "nPermSize - 1 != *(::std::max_element( aPermutation.begin(), aPermutation.end())");
292  const size_t nRowSize = rRow.size();
293  const size_t nDestSize = ::std::min( nPermSize, nRowSize );
294  for( size_t nDestinationIndex = 0; nDestinationIndex < nDestSize; ++nDestinationIndex )
295  {
296  const size_t nSourceIndex = static_cast< size_t >( aPermutation[ nDestinationIndex ] );
297  if( nSourceIndex != nDestinationIndex &&
298  nSourceIndex < nRowSize )
299  {
300  // copy original on first real permutation
301  if( !bModified )
302  {
303  SAL_WARN_IF( !aModifiedRow.empty(), "xmloff.chart", "aModifiedRow is NOT NULL");
304  aModifiedRow.insert( aModifiedRow.end(), rRow.begin(), rRow.end() );
305  SAL_WARN_IF( aModifiedRow.empty(), "xmloff.chart", "aModifiedRow is NULL");
306  }
307  SAL_WARN_IF( nDestinationIndex >= aModifiedRow.size(), "xmloff.chart", "nDestinationIndex >= aModifiedRow.size()");
308  aModifiedRow[ nDestinationIndex ] = rRow[ nSourceIndex ];
309  bModified = true;
310  }
311  }
312  // copy back
313  if( bModified )
314  ::std::copy( aModifiedRow.begin(), aModifiedRow.end(), rRow.begin());
315  }
316  }
317  else if( mbHasRowPermutation )
318  {
319  auto aPermutation( comphelper::sequenceToContainer<std::vector< sal_Int32 >>( maRowPermutation ));
320  SAL_WARN_IF( aPermutation.empty(), "xmloff.chart", "aPermutation is NULL");
321  if( aPermutation.empty())
322  return;
323 
324  bool bModified = false;
325  const size_t nPermSize = aPermutation.size();
326  SAL_WARN_IF( static_cast< sal_Int32 >( nPermSize ) - 1 != *(::std::max_element( aPermutation.begin(), aPermutation.end())), "xmloff.chart", "nPermSize - 1 != *(::std::max_element( aPermutation.begin(), aPermutation.end())");
327  const size_t nTableRowCount = mrTable.aData.size();
328  const size_t nDestSize = ::std::min( nPermSize, nTableRowCount );
329  ::std::vector< ::std::vector< SchXMLCell > > aDestination;
330  for( size_t nDestinationIndex = 0; nDestinationIndex < nDestSize; ++nDestinationIndex )
331  {
332  const size_t nSourceIndex = static_cast< size_t >( aPermutation[ nDestinationIndex ] );
333  if( nSourceIndex != nDestinationIndex &&
334  nSourceIndex < nTableRowCount )
335  {
336  // copy original on first real permutation
337  if( !bModified )
338  {
339  SAL_WARN_IF( !aDestination.empty(), "xmloff.chart", "aDestination is NOT NULL");
340  aDestination.insert( aDestination.end(), mrTable.aData.begin(), mrTable.aData.end());
341  SAL_WARN_IF( aDestination.empty(), "xmloff.chart", "aDestination is NULL");
342  }
343  SAL_WARN_IF( nDestinationIndex >= aDestination.size(), "xmloff.chart", "nDestinationIndex >= aDestination.size()");
344  aDestination[ nDestinationIndex ] = mrTable.aData[ nSourceIndex ];
345  bModified = true;
346  }
347  }
348  if( bModified )
349  {
350  // copy back
351  ::std::copy( aDestination.begin(), aDestination.end(), mrTable.aData.begin());
352  }
353  }
354 }
355 
356 void SchXMLTableContext::setRowPermutation( const uno::Sequence< sal_Int32 > & rPermutation )
357 {
358  maRowPermutation = rPermutation;
359  mbHasRowPermutation = rPermutation.hasElements();
360 
362  {
363  mbHasColumnPermutation = false;
364  maColumnPermutation.realloc( 0 );
365  }
366 }
367 
368 void SchXMLTableContext::setColumnPermutation( const uno::Sequence< sal_Int32 > & rPermutation )
369 {
370  maColumnPermutation = rPermutation;
371  mbHasColumnPermutation = rPermutation.hasElements();
372 
374  {
375  mbHasRowPermutation = false;
376  maRowPermutation.realloc( 0 );
377  }
378 }
379 
380 // classes for columns
381 // class SchXMLTableColumnsContext
383  SvXMLImport& rImport,
384  SchXMLTable& aTable ) :
385  SvXMLImportContext( rImport ),
386  mrTable( aTable )
387 {
388 }
389 
391 {
392 }
393 
394 css::uno::Reference< css::xml::sax::XFastContextHandler > SchXMLTableColumnsContext::createFastChildContext(
395  sal_Int32 nElement,
396  const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
397 {
398  SvXMLImportContext* pContext = nullptr;
399 
400  if( nElement == XML_ELEMENT(TABLE, XML_TABLE_COLUMN) )
401  pContext = new SchXMLTableColumnContext( GetImport(), mrTable );
402  else
403  XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
404 
405  return pContext;
406 }
407 
408 // class SchXMLTableColumnContext
410  SvXMLImport& rImport,
411  SchXMLTable& aTable ) :
412  SvXMLImportContext( rImport ),
413  mrTable( aTable )
414 {
415 }
416 
417 void SchXMLTableColumnContext::startFastElement (sal_Int32 /*nElement*/,
418  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList)
419 {
420  // get number-columns-repeated attribute
421  sal_Int32 nRepeated = 1;
422  bool bHidden = false;
423 
424  for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
425  {
426  switch(aIter.getToken())
427  {
429  {
430  if( !aIter.isEmpty())
431  nRepeated = aIter.toInt32();
432  break;
433  }
435  {
436  OUString aVisibility = aIter.toString();
437  bHidden = aVisibility == GetXMLToken( XML_COLLAPSE );
438  break;
439  }
440  default:
441  XMLOFF_WARN_UNKNOWN("xmloff", aIter);
442  }
443  }
444 
445  sal_Int32 nOldCount = mrTable.nNumberOfColsEstimate;
446  sal_Int32 nNewCount = nOldCount + nRepeated;
447  mrTable.nNumberOfColsEstimate = nNewCount;
448 
449  if( bHidden )
450  {
451  //i91578 display of hidden values (copy paste scenario; use hidden flag during migration to locale table upon paste )
452  sal_Int32 nColOffset = ( mrTable.bHasHeaderColumn ? 1 : 0 );
453  for( sal_Int32 nN = nOldCount; nN<nNewCount; nN++ )
454  {
455  sal_Int32 nHiddenColumnIndex = nN-nColOffset;
456  if( nHiddenColumnIndex>=0 )
457  mrTable.aHiddenColumns.push_back(nHiddenColumnIndex);
458  }
459  }
460 }
461 
463 {
464 }
465 
466 // classes for rows
467 // class SchXMLTableRowsContext
469  SvXMLImport& rImport,
470  SchXMLTable& aTable ) :
471  SvXMLImportContext( rImport ),
472  mrTable( aTable )
473 {
474 }
475 
477 {
478 }
479 
480 css::uno::Reference< css::xml::sax::XFastContextHandler > SchXMLTableRowsContext::createFastChildContext(
481  sal_Int32 nElement,
482  const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
483 {
484  SvXMLImportContext* pContext = nullptr;
485 
486  if( nElement == XML_ELEMENT(TABLE, XML_TABLE_ROW) )
487  pContext = new SchXMLTableRowContext( GetImport(), mrTable );
488  else
489  XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
490 
491  return pContext;
492 }
493 
494 // class SchXMLTableRowContext
496  SvXMLImport& rImport,
497  SchXMLTable& aTable ) :
498  SvXMLImportContext( rImport ),
499  mrTable( aTable )
500 {
501  mrTable.nColumnIndex = -1;
502  mrTable.nRowIndex++;
503 
504  std::vector< SchXMLCell > aNewRow;
505  aNewRow.reserve( mrTable.nNumberOfColsEstimate );
507  mrTable.aData.push_back( aNewRow );
508 }
509 
511 {
512 }
513 
514 css::uno::Reference< css::xml::sax::XFastContextHandler > SchXMLTableRowContext::createFastChildContext(
515  sal_Int32 nElement,
516  const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
517 {
518  SvXMLImportContext* pContext = nullptr;
519 
520  // <table:table-cell> element
521  if( nElement == XML_ELEMENT(TABLE, XML_TABLE_CELL) )
522  {
523  pContext = new SchXMLTableCellContext( GetImport(), mrTable );
524  }
525  else
526  {
527  XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
528  assert(false);
529  }
530 
531  return pContext;
532 }
533 
534 namespace {
535 
536 class SchXMLRangeSomewhereContext : public SvXMLImportContext
537 {
538 //#i113950# previously the range was exported to attribute text:id,
539 //but that attribute does not allow arbitrary strings anymore
540 //so we need to find an alternative to save that range info for copy/paste scenario ...
541 //-> use description at an empty group element for now
542 
543 private:
544  OUString& mrRangeString;
545  OUStringBuffer maRangeStringBuffer;
546 
547 public:
548  SchXMLRangeSomewhereContext( SvXMLImport& rImport,
549  OUString& rRangeString );
550 
551  virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
552  sal_Int32 nElement,
553  const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
554  virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
555 };
556 
557 }
558 
559 // classes for cells and their content
560 // class SchXMLTableCellContext
562  SvXMLImport& rImport,
563  SchXMLTable& aTable)
564  : SvXMLImportContext(rImport)
565  , mrTable(aTable)
566  , mbReadText(false)
567 {
568 }
569 
571 {
572 }
573 
574 void SchXMLTableCellContext::startFastElement (sal_Int32 /*nElement*/,
575  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList)
576 {
577  OUString aCellContent;
579 
580  for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
581  {
582  switch(aIter.getToken())
583  {
585  if( IsXMLToken( aIter, XML_FLOAT ) )
586  eValueType = SCH_CELL_TYPE_FLOAT;
587  else if( IsXMLToken( aIter, XML_STRING ) )
588  eValueType = SCH_CELL_TYPE_STRING;
589  break;
590 
592  aCellContent = aIter.toString();
593  break;
594 
595  default:
596  XMLOFF_WARN_UNKNOWN("xmloff", aIter);
597  }
598  }
599 
600  mbReadText = true;
601  SchXMLCell aCell;
602  aCell.eType = eValueType;
603 
604  if( eValueType == SCH_CELL_TYPE_FLOAT )
605  {
606  double fData;
607  // the result may be false if a NaN is read, but that's ok
608  ::sax::Converter::convertDouble( fData, aCellContent );
609 
610  aCell.fValue = fData;
611  // don't read text from following <text:p> or <text:list> element
612  mbReadText = false;
613  }
614 
615  mrTable.aData[ mrTable.nRowIndex ].push_back( aCell );
619 }
620 
621 css::uno::Reference< css::xml::sax::XFastContextHandler > SchXMLTableCellContext::createFastChildContext(
622  sal_Int32 nElement,
623  const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
624 {
625  SvXMLImportContext* pContext = nullptr;
626 
627  // <text:list> element
628  if( nElement == XML_ELEMENT(TEXT, XML_LIST ) && mbReadText )
629  {
633  pContext = new SchXMLTextListContext( GetImport(), rCell.aComplexString );
634  mbReadText = false;//don't apply text from <text:p>
635  }
636  // <text:p> element - read text (and range from text:id old version)
637  else if( nElement == XML_ELEMENT(TEXT, XML_P) ||
638  nElement == XML_ELEMENT(LO_EXT, XML_P) )
639  {
641  }
642  // <draw:g> element - read range
643  else if( nElement == XML_ELEMENT(DRAW, XML_G) )
644  {
645  //#i113950# previously the range was exported to attribute text:id, but that attribute does not allow arbitrary strings anymore
646  //so we need to find an alternative to save that range info for copy/paste scenario ... -> use description at an empty group element for now
647  pContext = new SchXMLRangeSomewhereContext( GetImport(), maRangeId );
648  }
649  else
650  XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
651 
652  return pContext;
653 }
654 
656 {
657  if( mbReadText && !maCellContent.isEmpty() ) //apply text from <text:p> element
659  if( !maRangeId.isEmpty())
661 }
662 
663 static void lcl_ApplyCellToComplexLabel( const SchXMLCell& rCell, Sequence< uno::Any >& rComplexLabel )
664 {
665  if( rCell.eType == SCH_CELL_TYPE_STRING )
666  {
667  rComplexLabel.realloc(1);
668  rComplexLabel[0] <<= rCell.aString;
669  }
670  else if( rCell.aComplexString.hasElements() && rCell.eType == SCH_CELL_TYPE_COMPLEX_STRING )
671  {
672  sal_Int32 nCount = rCell.aComplexString.getLength();
673  rComplexLabel.realloc( nCount );
674  for( sal_Int32 nN=0; nN<nCount; nN++)
675  rComplexLabel[nN] <<= (rCell.aComplexString)[nN];
676  }
677  else if( rCell.eType == SCH_CELL_TYPE_FLOAT )
678  {
679  rComplexLabel.realloc(1);
680  rComplexLabel[0] <<= rCell.fValue;
681  }
682 }
683 
685  const SchXMLTable& rTable,
686  const uno::Reference< chart2::XChartDocument >& xChartDoc )
687 {
688  // apply all data read from the local table to the internal data provider
689  if( !xChartDoc.is() || !xChartDoc->hasInternalDataProvider() )
690  return;
691  Reference< chart2::data::XDataProvider > xDataProv( xChartDoc->getDataProvider() );
692  if( !xDataProv.is() )
693  return;
694 
695  //prepare the read local table data
696  sal_Int32 nNumRows( static_cast< sal_Int32 >( rTable.aData.size()));
697  sal_Int32 nRowOffset = 0;
698  if( rTable.bHasHeaderRow )
699  {
700  --nNumRows;
701  nRowOffset = 1;
702  }
703  sal_Int32 nNumColumns( rTable.nMaxColumnIndex + 1 );
704  sal_Int32 nColOffset = 0;
705  if( rTable.bHasHeaderColumn )
706  {
707  --nNumColumns;
708  nColOffset = 1;
709  }
710 
711  Sequence< Sequence< double > > aDataInRows( nNumRows );
712  Sequence< Sequence< uno::Any > > aComplexRowDescriptions( nNumRows );
713  Sequence< Sequence< uno::Any > > aComplexColumnDescriptions( nNumColumns );
714  for( sal_Int32 i=0; i<nNumRows; ++i )
715  aDataInRows[i].realloc( nNumColumns );
716 
717  if( !rTable.aData.empty() )
718  {
719  //apply column labels
720  if( rTable.bHasHeaderRow )
721  {
722  const ::std::vector< SchXMLCell >& rFirstRow = rTable.aData.front();
723  const sal_Int32 nColumnLabelsSize = aComplexColumnDescriptions.getLength();
724  const sal_Int32 nMax = ::std::min< sal_Int32 >( nColumnLabelsSize, static_cast< sal_Int32 >( rFirstRow.size()) - nColOffset );
725  SAL_WARN_IF( nMax != nColumnLabelsSize, "xmloff.chart", "nMax != nColumnLabelsSize");
726  for( sal_Int32 i=0; i<nMax; ++i )
727  lcl_ApplyCellToComplexLabel( rFirstRow[i+nColOffset], aComplexColumnDescriptions[i] );
728  }
729 
730  std::vector< ::std::vector< SchXMLCell > >::const_iterator aRowIter( rTable.aData.begin() + nRowOffset );
731  std::vector< ::std::vector< SchXMLCell > >::const_iterator aEnd( rTable.aData.end() );
732  for( sal_Int32 nRow = 0; aRowIter != aEnd && nRow < nNumRows; ++aRowIter, ++nRow )
733  {
734  const ::std::vector< SchXMLCell >& rRow = *aRowIter;
735  if( !rRow.empty() )
736  {
737  // row label
738  if( rTable.bHasHeaderColumn )
739  lcl_ApplyCellToComplexLabel( rRow.front(), aComplexRowDescriptions[nRow] );
740 
741  // values
742  Sequence< double >& rTargetRow = aDataInRows[nRow];
743  lcl_ApplyCellToData aApplyCellToData = ::std::for_each( rRow.begin() + nColOffset, rRow.end(), lcl_ApplyCellToData( rTargetRow ) );
744  double fNaN = 0.0;
745  ::rtl::math::setNan( &fNaN );
746  for( sal_Int32 nCurrentIndex = aApplyCellToData.getCurrentIndex(); nCurrentIndex<nNumColumns; nCurrentIndex++ )
747  rTargetRow[nCurrentIndex] = fNaN;//#i110615#
748  }
749  }
750  }
751 
752  //apply the collected data to the chart
753  Reference< chart2::XAnyDescriptionAccess > xDataAccess( xDataProv, uno::UNO_QUERY );
754  if( !xDataAccess.is() )
755  return;
756 
757  xDataAccess->setData( aDataInRows );
758  if( rTable.bHasHeaderColumn )
759  xDataAccess->setAnyRowDescriptions( aComplexRowDescriptions );
760  if( rTable.bHasHeaderRow )
761  xDataAccess->setAnyColumnDescriptions( aComplexColumnDescriptions );
762 
763  if ( rTable.bProtected )
764  {
765  try
766  {
767  Reference< beans::XPropertySet > xProps( xChartDoc, uno::UNO_QUERY_THROW );
768  xProps->setPropertyValue( "DisableDataTableDialog", uno::makeAny( true ) );
769  xProps->setPropertyValue( "DisableComplexChartTypes", uno::makeAny( true ) );
770  }
771  catch ( uno::Exception& )
772  {
773  }
774  }
775 }
776 
778  const SchXMLTable& rTable,
779  const tSchXMLLSequencesPerIndex & rLSequencesPerIndex,
780  const uno::Reference< chart2::XChartDocument >& xChartDoc,
781  chart::ChartDataRowSource eDataRowSource )
782 {
783  if( ! (xChartDoc.is() && xChartDoc->hasInternalDataProvider()))
784  return;
785 
786  // If the range-strings are valid (starting with "local-table") they should
787  // be interpreted like given, otherwise (when the ranges refer to Calc- or
788  // Writer-ranges, but the container is not available like when pasting a
789  // chart from Calc to Impress) the range is ignored, and every object gets
790  // one table column in the order of appearance, which is: 1. categories,
791  // 2. data series: 2.a) domains, 2.b) values (main-role, usually y-values)
792 
793  Reference< chart2::data::XDataProvider > xDataProv( xChartDoc->getDataProvider());
794 
795  // create a mapping from original ranges to new ranges
796  lcl_tOriginalRangeToInternalRangeMap aRangeMap;
797 
798  lcl_fillRangeMapping( rTable, aRangeMap, eDataRowSource );
799 
800  const OUString lcl_aCategoriesRange(aCategoriesRange);
801 
802  bool bCategoriesApplied = false;
803  // translate ranges (using the map created before)
804  for( const auto& rLSeq : rLSequencesPerIndex )
805  {
806  if( rLSeq.second.is())
807  {
808  // values/error bars/categories
809  if( rLSeq.first.second == SCH_XML_PART_VALUES ||
810  rLSeq.first.second == SCH_XML_PART_ERROR_BARS )
811  {
812  Reference< chart2::data::XDataSequence > xSeq( rLSeq.second->getValues());
813 
814  OUString aRange;
815  if( xSeq.is() &&
816  SchXMLTools::getXMLRangePropertyFromDataSequence( xSeq, aRange, /* bClearProp = */ true ) &&
817  lcl_mapContainsRange( aRangeMap, aRange ))
818  {
819  Reference< chart2::data::XDataSequence > xNewSeq(
820  lcl_reassignDataSequence( xSeq, xDataProv, aRangeMap, aRange ));
821  if( xNewSeq != xSeq )
822  {
824  Reference< beans::XPropertySet >( xNewSeq, uno::UNO_QUERY ));
825  rLSeq.second->setValues( xNewSeq );
826  }
827  }
828  else
829  {
830  if( lcl_tableOfRangeMatches( aRange, rTable.aTableNameOfFile ))
831  {
832  if( rLSeq.first.first == SCH_XML_CATEGORIES_INDEX )
833  bCategoriesApplied = true;
834  }
835  else
836  {
837  if( rLSeq.first.first == SCH_XML_CATEGORIES_INDEX )
838  {
839  Reference< beans::XPropertySet > xOldSequenceProp( rLSeq.second->getValues(), uno::UNO_QUERY );
840  Reference< chart2::data::XDataSequence > xNewSequence(
841  xDataProv->createDataSequenceByRangeRepresentation("categories"));
843  xOldSequenceProp, Reference< beans::XPropertySet >( xNewSequence, uno::UNO_QUERY ));
844  rLSeq.second->setValues( xNewSequence );
845  bCategoriesApplied = true;
846  }
847  else
848  {
849  Reference< beans::XPropertySet > xOldSequenceProp( rLSeq.second->getValues(), uno::UNO_QUERY );
850  OUString aRep( OUString::number( rLSeq.first.first ));
851  Reference< chart2::data::XDataSequence > xNewSequence(
852  xDataProv->createDataSequenceByRangeRepresentation( aRep ));
854  xOldSequenceProp, Reference< beans::XPropertySet >( xNewSequence, uno::UNO_QUERY ));
855  rLSeq.second->setValues( xNewSequence );
856  }
857  }
858  }
859  }
860  else // labels
861  {
862  SAL_WARN_IF( rLSeq.first.second != SCH_XML_PART_LABEL, "xmloff.chart", "rLSeq.first.second != SCH_XML_PART_LABEL" );
863  // labels
864  Reference< chart2::data::XDataSequence > xSeq( rLSeq.second->getLabel());
865  OUString aRange;
866  if( xSeq.is() &&
867  SchXMLTools::getXMLRangePropertyFromDataSequence( xSeq, aRange, /* bClearProp = */ true ) &&
868  lcl_mapContainsRange( aRangeMap, aRange ))
869  {
870  Reference< chart2::data::XDataSequence > xNewSeq(
871  lcl_reassignDataSequence( xSeq, xDataProv, aRangeMap, aRange ));
872  if( xNewSeq != xSeq )
873  {
875  Reference< beans::XPropertySet >( xNewSeq, uno::UNO_QUERY ));
876  rLSeq.second->setLabel( xNewSeq );
877  }
878  }
879  else if( ! lcl_tableOfRangeMatches( aRange, rTable.aTableNameOfFile ))
880  {
881  OUString aRep = "label " + OUString::number( rLSeq.first.first );
882 
883  Reference< chart2::data::XDataSequence > xNewSeq(
884  xDataProv->createDataSequenceByRangeRepresentation( aRep ));
886  Reference< beans::XPropertySet >( xNewSeq, uno::UNO_QUERY ));
887  rLSeq.second->setLabel( xNewSeq );
888  }
889  }
890  }
891  }
892 
893  // there exist files with own data without a categories element but with row
894  // descriptions. The row descriptions were used as categories even without
895  // the categories element
896  if( ! bCategoriesApplied )
897  {
899  xDataProv, xChartDoc, "categories",
900  0 /* nCooSysIndex */, 0 /* nDimension */ );
901  }
902 
903  //i91578 display of hidden values (copy paste scenario; use hidden flag during migration to locale table upon paste )
904  //remove series that consist only of hidden columns
905  Reference< chart2::XInternalDataProvider > xInternalDataProvider( xDataProv, uno::UNO_QUERY );
906  if( !xInternalDataProvider.is() || rTable.aHiddenColumns.empty() )
907  return;
908 
909  try
910  {
911  Reference< chart2::XCoordinateSystemContainer > xCooSysCnt( xChartDoc->getFirstDiagram(), uno::UNO_QUERY_THROW );
912  const Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems() );
913  for( const auto& rCooSys : aCooSysSeq )
914  {
915  Reference< chart2::XChartTypeContainer > xCooSysContainer( rCooSys, uno::UNO_QUERY_THROW );
916  const Sequence< Reference< chart2::XChartType > > aChartTypeSeq( xCooSysContainer->getChartTypes());
917  for( const auto& rChartType : aChartTypeSeq )
918  {
919  Reference< chart2::XDataSeriesContainer > xSeriesContainer( rChartType, uno::UNO_QUERY );
920  if(!xSeriesContainer.is())
921  continue;
922  const Sequence< Reference< chart2::XDataSeries > > aSeriesSeq( xSeriesContainer->getDataSeries() );
923  std::vector< Reference< chart2::XDataSeries > > aRemainingSeries;
924 
925  for( const auto& rSeries : aSeriesSeq )
926  {
927  Reference< chart2::data::XDataSource > xDataSource( rSeries, uno::UNO_QUERY );
928  if( xDataSource.is() )
929  {
930  bool bHasUnhiddenColumns = false;
931  OUString aRange;
932  const uno::Sequence< Reference< chart2::data::XLabeledDataSequence > > aSequences( xDataSource->getDataSequences() );
933  for( const auto& xLabeledSequence : aSequences )
934  {
935  if(!xLabeledSequence.is())
936  continue;
937  Reference< chart2::data::XDataSequence > xValues( xLabeledSequence->getValues() );
938  if( xValues.is() )
939  {
940  aRange = xValues->getSourceRangeRepresentation();
941  if( ::std::find( rTable.aHiddenColumns.begin(), rTable.aHiddenColumns.end(), aRange.toInt32() ) == rTable.aHiddenColumns.end() )
942  bHasUnhiddenColumns = true;
943  }
944  if( !bHasUnhiddenColumns )
945  {
946  Reference< chart2::data::XDataSequence > xLabel( xLabeledSequence->getLabel() );
947  if( xLabel.is() )
948  {
949  aRange = xLabel->getSourceRangeRepresentation();
950  const sal_Int32 nId {aRange.getToken(1, ' ').toInt32()};
951  if( ::std::find( rTable.aHiddenColumns.begin(), rTable.aHiddenColumns.end(), nId ) == rTable.aHiddenColumns.end() )
952  bHasUnhiddenColumns = true;
953  }
954  }
955  }
956  if( bHasUnhiddenColumns )
957  aRemainingSeries.push_back( rSeries );
958  }
959  }
960 
961  if( static_cast<sal_Int32>(aRemainingSeries.size()) != aSeriesSeq.getLength() )
962  {
963  //remove the series that have only hidden data
964  xSeriesContainer->setDataSeries( comphelper::containerToSequence(aRemainingSeries) );
965 
966  //remove unused sequences
967  Reference< chart2::data::XDataSource > xDataSource( xChartDoc, uno::UNO_QUERY );
968  if( xDataSource.is() )
969  {
970  //first detect which columns are really used
971  std::map< sal_Int32, bool > aUsageMap;
972  OUString aRange;
973  const Sequence< Reference< chart2::data::XLabeledDataSequence > > aUsedSequences( xDataSource->getDataSequences() );
974  for( const auto& xLabeledSequence : aUsedSequences )
975  {
976  if(!xLabeledSequence.is())
977  continue;
978  Reference< chart2::data::XDataSequence > xValues( xLabeledSequence->getValues() );
979  if( xValues.is() )
980  {
981  aRange = xValues->getSourceRangeRepresentation();
982  sal_Int32 nIndex = aRange.toInt32();
983  if( nIndex!=0 || aRange != lcl_aCategoriesRange )
984  aUsageMap[nIndex] = true;
985  }
986  Reference< chart2::data::XDataSequence > xLabel( xLabeledSequence->getLabel() );
987  if( xLabel.is() )
988  {
989  aRange = xLabel->getSourceRangeRepresentation();
990  OUString aSecondToken = aRange.getToken(1, ' ');
991  if( !aSecondToken.isEmpty() )
992  aUsageMap[aSecondToken.toInt32()] = true;
993  }
994  }
995 
996  ::std::vector< sal_Int32 > aSequenceIndexesToDelete;
997  std::copy_if(rTable.aHiddenColumns.begin(), rTable.aHiddenColumns.end(),
998  std::back_inserter(aSequenceIndexesToDelete),
999  [&aUsageMap](sal_Int32 nSequenceIndex) { return aUsageMap.find(nSequenceIndex) == aUsageMap.end(); });
1000 
1001  // delete unnecessary sequences of the internal data
1002  // iterate using greatest index first, so that deletion does not
1003  // shift other sequences that will be deleted later
1004  ::std::sort( aSequenceIndexesToDelete.begin(), aSequenceIndexesToDelete.end());
1005  for( ::std::vector< sal_Int32 >::reverse_iterator aIt(
1006  aSequenceIndexesToDelete.rbegin()); aIt != aSequenceIndexesToDelete.rend(); ++aIt )
1007  {
1008  if( *aIt != -1 )
1009  xInternalDataProvider->deleteSequence( *aIt );
1010  }
1011  }
1012  }
1013  }
1014  }
1015  }
1016  catch( const uno::Exception & )
1017  {
1018  }
1019 }
1020 
1021 SchXMLRangeSomewhereContext::SchXMLRangeSomewhereContext( SvXMLImport& rImport,
1022  OUString& rRangeString ) :
1023  SvXMLImportContext( rImport ),
1024  mrRangeString( rRangeString )
1025 {
1026 }
1027 
1028 css::uno::Reference< css::xml::sax::XFastContextHandler > SchXMLRangeSomewhereContext::createFastChildContext(
1029  sal_Int32 nElement,
1030  const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
1031 {
1032  if( nElement == XML_ELEMENT(SVG, XML_DESC) )
1033  {
1034  return new XMLStringBufferImportContext( GetImport(), maRangeStringBuffer );
1035  }
1036  else
1037  XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
1038  return nullptr;
1039 }
1040 
1041 void SchXMLRangeSomewhereContext::endFastElement(sal_Int32 )
1042 {
1043  mrRangeString = maRangeStringBuffer.makeStringAndClear();
1044 }
1045 
1046 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SchXMLCellType
SchXMLTableRowContext(SvXMLImport &rImport, SchXMLTable &aTable)
SchXMLTableContext(SvXMLImport &rImport, SchXMLTable &aTable)
SchXMLTableRowsContext(SvXMLImport &rImport, SchXMLTable &aTable)
sal_Int32 nIndex
OUString aString
virtual ~SchXMLTableColumnContext() override
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
SchXMLTableColumnsContext(SvXMLImport &rImport, SchXMLTable &aTable)
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:56
sal_Int16 nId
void setRowPermutation(const css::uno::Sequence< sal_Int32 > &rPermutation)
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3462
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
SchXMLTableColumnContext(SvXMLImport &rImport, SchXMLTable &aTable)
css::uno::Sequence< OUString > aComplexString
static void convertDouble(OUStringBuffer &rBuffer, double fNumber, bool bWriteUnits, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit)
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
DstType sequenceToContainer(const css::uno::Sequence< SrcType > &i_Sequence)
css::uno::Sequence< sal_Int32 > maRowPermutation
const Sequence< Sequence< double > > & m_rData
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
DRAW
int nCount
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:110
css::uno::Sequence< sal_Int32 > maColumnPermutation
virtual ~SchXMLTableContext() override
sal_Int32 nNumberOfColsEstimate
the greatest number of columns detected
static void lcl_ApplyCellToComplexLabel(const SchXMLCell &rCell, Sequence< uno::Any > &rComplexLabel)
SchXMLTableCellContext(SvXMLImport &rImport, SchXMLTable &aTable)
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
virtual ~SchXMLTableRowsContext() override
int i
#define SCH_XML_CATEGORIES_INDEX
virtual ~SchXMLTableCellContext() override
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
float u
sal_Int32 nColumnIndex
reflects the index of the row currently parsed
std::vector< std::vector< SchXMLCell > > aData
Import all text into a string buffer.
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:45
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
void CreateCategories(const uno::Reference< chart2::data::XDataProvider > &xDataProvider, const uno::Reference< chart2::XChartDocument > &xNewDoc, const OUString &rRangeAddress, sal_Int32 nCooSysIndex, sal_Int32 nDimensionIndex, tSchXMLLSequencesPerIndex *pLSequencesPerIndex)
bool bHasHeaderRow
parsing column-elements may yield an estimate
static void switchRangesFromOuterToInternalIfNecessary(const SchXMLTable &rTable, const tSchXMLLSequencesPerIndex &rLSequencesPerIndex, const css::uno::Reference< css::chart2::XChartDocument > &xChartDoc, css::chart::ChartDataRowSource eDataRowSource)
This function reorders local data to fit the correct data structure.
OUString aTableNameOfFile
#define SAL_WARN_IF(condition, area, stream)
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3406
virtual void SAL_CALL endFastElement(sal_Int32 Element) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
Definition: xmlictxt.cxx:40
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Handling of tokens in XML:
virtual css::uno::Reference< XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: xmlictxt.cxx:59
bool getXMLRangePropertyFromDataSequence(const Reference< chart2::data::XDataSequence > &xDataSequence, OUString &rOutXMLRange, bool bClearProp)
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:96
double getLength(const B2DPolygon &rCandidate)
TABLE
sal_Int32 nMaxColumnIndex
reflects the index of the column currently parsed
virtual ~SchXMLTableRowContext() override
SchXMLCellType eType
#define XMLOFF_WARN_UNKNOWN_ELEMENT(area, token)
Definition: xmlictxt.hxx:116
::std::vector< sal_Int32 > aHiddenColumns
the table name read at the table:table element
With this context all column elements are parsed to determine the index of the column containing the ...
::std::multimap< tSchXMLIndexWithPart, css::uno::Reference< css::chart2::data::XLabeledDataSequence > > tSchXMLLSequencesPerIndex
void copyProperties(const Reference< beans::XPropertySet > &xSource, const Reference< beans::XPropertySet > &xDestination)
void setColumnPermutation(const css::uno::Sequence< sal_Int32 > &rPermutation)
TEXT
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
static void applyTableToInternalDataProvider(const SchXMLTable &rTable, const css::uno::Reference< css::chart2::XChartDocument > &xChartDoc)
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
virtual ~SchXMLTableColumnsContext() override
sal_uInt32 m_nSize
sal_Int32 nRowIndex
an array of rows containing the table contents