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