LibreOffice Module sc (master)  1
xmlrowi.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 "xmlrowi.hxx"
21 #include "xmlimprt.hxx"
22 #include "xmlcelli.hxx"
23 #include "xmlstyli.hxx"
24 #include <document.hxx>
25 #include <docuno.hxx>
26 #include <olinetab.hxx>
27 #include <sheetdata.hxx>
28 #include <documentimport.hxx>
29 #include <unonames.hxx>
30 
31 #include <unotools/configmgr.hxx>
32 #include <xmloff/xmlnamespace.hxx>
33 #include <xmloff/families.hxx>
34 #include <xmloff/xmltoken.hxx>
35 #include <sax/fastattribs.hxx>
36 #include <com/sun/star/sheet/XSpreadsheet.hpp>
37 #include <com/sun/star/table/XColumnRowRange.hpp>
38 #include <com/sun/star/sheet/XPrintAreas.hpp>
40 #include <osl/diagnose.h>
41 
42 constexpr OUStringLiteral SC_ISFILTERED = u"IsFiltered";
43 
44 using namespace com::sun::star;
45 using namespace xmloff::token;
46 
49  ScXMLImportContext( rImport ),
50  sVisibility(GetXMLToken(XML_VISIBLE)),
51  nRepeatedRows(1),
52  bHasCell(false)
53 {
54  OUString sCellStyleName;
55  if ( rAttrList.is() )
56  {
57  for (auto &it : *rAttrList)
58  {
59  switch (it.getToken())
60  {
62  {
63  sStyleName = it.toString();
64  }
65  break;
67  {
68  sVisibility = it.toString();
69  }
70  break;
72  {
73  nRepeatedRows = std::max( it.toInt32(), sal_Int32(1) );
74  nRepeatedRows = std::min( nRepeatedRows, rImport.GetDocument()->GetSheetLimits().GetMaxRowCount() );
76  nRepeatedRows = std::min(nRepeatedRows, sal_Int32(1024));
77  }
78  break;
80  {
81  sCellStyleName = it.toString();
82  }
83  break;
84  /*case XML_ELEMENT( TABLE, XML_USE_OPTIMAL_HEIGHT ):
85  {
86  sOptimalHeight = it.toString();
87  }
88  break;*/
89  }
90  }
91  }
92 
94  GetScImport().GetTables().SetRowStyle(sCellStyleName);
95 }
96 
98 {
99 }
100 
101 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
103  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
104 {
105  SvXMLImportContext *pContext(nullptr);
106  sax_fastparser::FastAttributeList *pAttribList =
108 
109  switch( nElement )
110  {
112 // if( IsInsertCellPossible() )
113  {
114  bHasCell = true;
115  pContext = new ScXMLTableRowCellContext( GetScImport(),
116  pAttribList, false, static_cast<SCROW>(nRepeatedRows)
117  //this
118  );
119  }
120  break;
122 // if( IsInsertCellPossible() )
123  {
124  bHasCell = true;
125  pContext = new ScXMLTableRowCellContext( GetScImport(),
126  pAttribList, true, static_cast<SCROW>(nRepeatedRows)
127  //this
128  );
129  }
130  break;
131  }
132 
133  return pContext;
134 }
135 
136 void SAL_CALL ScXMLTableRowContext::endFastElement(sal_Int32 /*nElement*/)
137 {
138  ScXMLImport& rXMLImport(GetScImport());
139  ScDocument* pDoc(rXMLImport.GetDocument());
140  if (!bHasCell && nRepeatedRows > 1)
141  {
142  for (sal_Int32 i = 0; i < nRepeatedRows - 1; ++i) //one row is always added
144  OSL_FAIL("it seems here is a nonvalid file; possible missing of table:table-cell element");
145  }
146  SCTAB nSheet = rXMLImport.GetTables().GetCurrentSheet();
147  sal_Int32 nCurrentRow(rXMLImport.GetTables().GetCurrentRow());
148  uno::Reference<sheet::XSpreadsheet> xSheet(rXMLImport.GetTables().GetCurrentXSheet());
149  if(!xSheet.is())
150  return;
151 
152  sal_Int32 nFirstRow(nCurrentRow - nRepeatedRows + 1);
153  if (nFirstRow > pDoc->MaxRow())
154  nFirstRow = pDoc->MaxRow();
155  if (nCurrentRow > pDoc->MaxRow())
156  nCurrentRow = pDoc->MaxRow();
157  uno::Reference <table::XCellRange> xCellRange(xSheet->getCellRangeByPosition(0, nFirstRow, 0, nCurrentRow));
158  if (!xCellRange.is())
159  return;
160 
161  uno::Reference<table::XColumnRowRange> xColumnRowRange (xCellRange, uno::UNO_QUERY);
162  if (!xColumnRowRange.is())
163  return;
164 
165  uno::Reference <beans::XPropertySet> xRowProperties(xColumnRowRange->getRows(), uno::UNO_QUERY);
166  if (!xRowProperties.is())
167  return;
168 
169  if (!sStyleName.isEmpty())
170  {
171  XMLTableStylesContext *pStyles(static_cast<XMLTableStylesContext *>(rXMLImport.GetAutoStyles()));
172  if ( pStyles )
173  {
174  XMLTableStyleContext* pStyle(const_cast<XMLTableStyleContext*>(static_cast<const XMLTableStyleContext *>(pStyles->FindStyleChildContext(
175  XmlStyleFamily::TABLE_ROW, sStyleName, true))));
176  if (pStyle)
177  {
178  pStyle->FillPropertySet(xRowProperties);
179 
180  if ( nSheet != pStyle->GetLastSheet() )
181  {
182  ScSheetSaveData* pSheetData = comphelper::getFromUnoTunnel<ScModelObj>(rXMLImport.GetModel())->GetSheetSaveData();
183  pSheetData->AddRowStyle( sStyleName, ScAddress( 0, static_cast<SCROW>(nFirstRow), nSheet ) );
184  pStyle->SetLastSheet(nSheet);
185  }
186  }
187  }
188  }
189  bool bVisible (true);
190  bool bFiltered (false);
192  {
193  bVisible = false;
194  }
195  else if (IsXMLToken(sVisibility, XML_FILTER))
196  {
197  bVisible = false;
198  bFiltered = true;
199  }
200  if (!bVisible)
201  {
202  rXMLImport.GetDoc().setRowsVisible(nSheet, nFirstRow, nCurrentRow, false);
203  }
204  if (bFiltered)
205  xRowProperties->setPropertyValue(SC_ISFILTERED, uno::makeAny(bFiltered));
206 
207  uno::Any any = xRowProperties->getPropertyValue(SC_UNONAME_OHEIGHT);
208  bool bOptionalHeight = false;
209  any >>= bOptionalHeight;
210  if (bOptionalHeight)
211  {
212  // Save this row for later height update
213  std::vector<ScDocRowHeightUpdater::TabRanges>& rRecalcRanges = rXMLImport.GetRecalcRowRanges();
214  while (static_cast<SCTAB>(rRecalcRanges.size()) <= nSheet)
215  {
216  rRecalcRanges.emplace_back(0, pDoc->MaxRow());
217  }
218  rRecalcRanges.at(nSheet).mnTab = nSheet;
219  rRecalcRanges.at(nSheet).maRanges.setTrue(nFirstRow, nCurrentRow);
220  }
221 }
222 
225  const bool bTempHeader,
226  const bool bTempGroup ) :
227  ScXMLImportContext( rImport ),
228  nHeaderStartRow(0),
229  nGroupStartRow(0),
230  bHeader(bTempHeader),
231  bGroup(bTempGroup),
232  bGroupDisplay(true)
233 {
234  // don't have any attributes
235  if (bHeader)
236  {
237  ScAddress aAddr = rImport.GetTables().GetCurrentCellPos();
238  nHeaderStartRow = aAddr.Row();
239  ++nHeaderStartRow;
240  }
241  else if (bGroup)
242  {
243  nGroupStartRow = rImport.GetTables().GetCurrentRow();
244  ++nGroupStartRow;
245  if ( rAttrList.is() )
246  {
247  auto aIter( rAttrList->find( XML_ELEMENT( TABLE, XML_DISPLAY ) ) );
248  if (aIter != rAttrList->end())
249  bGroupDisplay = IsXMLToken( aIter, XML_TRUE );
250  }
251  }
252 }
253 
255 {
256 }
257 
258 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
260  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
261 {
262  SvXMLImportContext *pContext(nullptr);
263  sax_fastparser::FastAttributeList *pAttribList =
265 
266  switch( nElement )
267  {
269  pContext = new ScXMLTableRowsContext( GetScImport(), pAttribList,
270  false, true );
271  break;
273  pContext = new ScXMLTableRowsContext( GetScImport(), pAttribList,
274  true, false );
275  break;
277  pContext = new ScXMLTableRowsContext( GetScImport(), pAttribList,
278  false, false );
279  break;
281  pContext = new ScXMLTableRowContext( GetScImport(), pAttribList );
282  break;
283  }
284 
285  return pContext;
286 }
287 
288 void SAL_CALL ScXMLTableRowsContext::endFastElement(sal_Int32 /*nElement*/)
289 {
290  ScXMLImport& rXMLImport(GetScImport());
291  if (bHeader)
292  {
293  SCROW nHeaderEndRow = rXMLImport.GetTables().GetCurrentRow();
294  if (nHeaderStartRow <= nHeaderEndRow)
295  {
296  uno::Reference <sheet::XPrintAreas> xPrintAreas (rXMLImport.GetTables().GetCurrentXSheet(), uno::UNO_QUERY);
297  if (xPrintAreas.is())
298  {
299  if (!xPrintAreas->getPrintTitleRows())
300  {
301  xPrintAreas->setPrintTitleRows(true);
302  table::CellRangeAddress aRowHeaderRange;
303  aRowHeaderRange.StartRow = nHeaderStartRow;
304  aRowHeaderRange.EndRow = nHeaderEndRow;
305  xPrintAreas->setTitleRows(aRowHeaderRange);
306  }
307  else
308  {
309  table::CellRangeAddress aRowHeaderRange(xPrintAreas->getTitleRows());
310  aRowHeaderRange.EndRow = nHeaderEndRow;
311  xPrintAreas->setTitleRows(aRowHeaderRange);
312  }
313  }
314  }
315  }
316  else if (bGroup)
317  {
318  SCROW nGroupEndRow = rXMLImport.GetTables().GetCurrentRow();
319  SCTAB nSheet(rXMLImport.GetTables().GetCurrentSheet());
320  if (nGroupStartRow <= nGroupEndRow)
321  {
322  ScDocument* pDoc(GetScImport().GetDocument());
323  if (pDoc)
324  {
326  ScOutlineTable* pOutlineTable(pDoc->GetOutlineTable(nSheet, true));
327  ScOutlineArray& rRowArray(pOutlineTable->GetRowArray());
328  bool bResized;
329  rRowArray.Insert(nGroupStartRow, nGroupEndRow, bResized, !bGroupDisplay);
330  }
331  }
332  }
333 }
334 
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual ~ScXMLTableRowContext() override
Definition: xmlrowi.cxx:97
bool bVisible
SCROW Row() const
Definition: address.hxx:261
void SetRowStyle(const OUString &rCellStyleName)
Definition: xmlsubti.cxx:170
std::vector< ScDocRowHeightUpdater::TabRanges > & GetRecalcRowRanges()
Definition: xmlimprt.hxx:210
SCTAB GetCurrentSheet() const
Definition: xmlsubti.hxx:84
ScDocument * GetDocument()
Definition: xmlimprt.hxx:205
constexpr OUStringLiteral SC_ISFILTERED
Definition: xmlrowi.cxx:42
ScMyTables & GetTables()
Definition: xmlimprt.hxx:208
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
OUString sVisibility
Definition: xmlrowi.hxx:30
XML_TABLE_CELL
SCROW GetMaxRowCount() const
Definition: sheetlimits.hxx:61
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet) override
Definition: xmlstyli.cxx:510
static bool IsFuzzing()
XML_TABLE_ROWS
ScXMLTableRowsContext(ScXMLImport &rImport, const rtl::Reference< sax_fastparser::FastAttributeList > &rAttrList, const bool bHeader, const bool bGroup)
Definition: xmlrowi.cxx:223
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
Definition: xmlrowi.cxx:259
virtual ~ScXMLTableRowsContext() override
Definition: xmlrowi.cxx:254
This class exists only to provide GetScImport() to its derived classes.
XML_TABLE_ROW
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
Definition: xmlrowi.cxx:136
#define SC_UNONAME_OHEIGHT
Definition: unonames.hxx:169
int i
XML_TABLE_ROW_GROUP
ScSheetLimits & GetSheetLimits() const
Definition: document.hxx:873
const Any & any
float u
OUString sStyleName
Definition: xmlrowi.hxx:29
XML_DISPLAY
Use this class to manage solar mutex locking instead of calling LockSolarMutex() and UnlockSolarMutex...
Definition: xmlimprt.hxx:288
ScXMLImport & GetScImport()
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
Definition: xmlrowi.cxx:102
SCROW GetCurrentRow() const
Definition: xmlsubti.hxx:86
XML_COVERED_TABLE_CELL
XML_TRUE
const ScAddress & GetCurrentCellPos() const
Definition: xmlsubti.hxx:80
TABLE
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
Definition: xmlrowi.cxx:288
sal_Int32 SCROW
Definition: types.hxx:17
XML_DEFAULT_CELL_STYLE_NAME
XML_TABLE_HEADER_ROWS
void setRowsVisible(SCTAB nTab, SCROW nRowStart, SCROW nRowEnd, bool bVisible)
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
XML_NUMBER_ROWS_REPEATED
ScXMLTableRowContext(ScXMLImport &rImport, const rtl::Reference< sax_fastparser::FastAttributeList > &rAttrList)
Definition: xmlrowi.cxx:47
#define XML_ELEMENT(prefix, name)
SC_DLLPUBLIC ScOutlineTable * GetOutlineTable(SCTAB nTab, bool bCreate=false)
Definition: documen3.cxx:736
const css::uno::Reference< css::frame::XModel > & GetModel() const
XML_STYLE_NAME
const css::uno::Reference< css::sheet::XSpreadsheet > & GetCurrentXSheet() const
Definition: xmlsubti.hxx:88
const SvXMLStyleContext * FindStyleChildContext(XmlStyleFamily nFamily, const OUString &rName, bool bCreateIndex=false) const
XML_VISIBILITY
XML_FILTER
sal_Int32 nRepeatedRows
Definition: xmlrowi.hxx:31
void AddRowStyle(const OUString &rName, const ScAddress &rCellPos)
Definition: sheetdata.cxx:51
XML_COLLAPSE
XML_VISIBLE
SvXMLStylesContext * GetAutoStyles()
bool Insert(SCCOLROW nStartPos, SCCOLROW nEndPos, bool &rSizeChanged, bool bHidden=false)
Definition: olinetab.cxx:197
void AddRow()
Definition: xmlsubti.cxx:164
ScDocumentImport & GetDoc()
Definition: xmlimprt.cxx:508
sal_Int16 SCTAB
Definition: types.hxx:22