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