LibreOffice Module sc (master)  1
extlstcontext.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 
10 #include <memory>
11 #include <extlstcontext.hxx>
12 #include <worksheethelper.hxx>
15 #include <oox/token/namespaces.hxx>
16 #include <oox/token/tokens.hxx>
17 #include <colorscale.hxx>
18 #include <condformatbuffer.hxx>
19 #include <condformatcontext.hxx>
20 #include <document.hxx>
21 #include <worksheetfragment.hxx>
22 #include <workbookfragment.hxx>
23 #include <stylesbuffer.hxx>
24 #include <stylesfragment.hxx>
25 
26 #include <rangeutl.hxx>
27 #include <sal/log.hxx>
28 
29 using ::oox::core::ContextHandlerRef;
30 using ::oox::xls::CondFormatBuffer;
31 
32 sal_Int32 rStyleIdx = 0;
33 
34 namespace oox::xls {
35 
37  WorksheetContextBase( rFragment ),
38  mpTarget( pTarget ),
39  mbFirstEntry(true)
40 {
41 }
42 
44 {
45  return this;
46 }
47 
49 {
50  switch( getCurrentElement() )
51  {
52  case XLS14_TOKEN( dataBar ):
53  {
55  xRule->importDataBar( rAttribs );
56  break;
57  }
58  case XLS14_TOKEN( negativeFillColor ):
59  {
61  xRule->importNegativeFillColor( rAttribs );
62  break;
63  }
64  case XLS14_TOKEN( axisColor ):
65  {
67  xRule->importAxisColor( rAttribs );
68  break;
69  }
70  case XLS14_TOKEN( cfvo ):
71  {
73  xRule->importCfvo( rAttribs );
74  xRule->getModel().mbIsLower = mbFirstEntry;
75  mbFirstEntry = false;
76  break;
77  }
78  default:
79  break;
80  }
81 }
82 
83 namespace {
84  bool IsSpecificTextCondMode(ScConditionMode eMode)
85  {
86  switch (eMode)
87  {
92  return true;
93  default:
94  break;
95  }
96  return false;
97  }
98 }
99 
101  : WorksheetContextBase(rFragment)
102  , nFormulaCount(0)
103  , nPriority(-1)
104  , eOperator(ScConditionMode::NONE)
105  , isPreviousElementF(false)
106 {
107 }
108 
110 {
111  if (mpCurrentRule)
112  {
113  ScFormatEntry& rFormat = **maEntries.rbegin();
114  assert(rFormat.GetType() == ScFormatEntry::Type::Iconset);
115  ScIconSetFormat& rIconSet = static_cast<ScIconSetFormat&>(rFormat);
116  ScDocument& rDoc = getScDocument();
117  SCTAB nTab = getSheetIndex();
118  ScAddress aPos(0, 0, nTab);
119  mpCurrentRule->SetData(&rIconSet, &rDoc, aPos);
120  mpCurrentRule.reset();
121  }
122  if (nElement == XLS14_TOKEN(cfRule))
123  {
124  OUString aType = rAttribs.getString(XML_type, OUString());
125  OUString aId = rAttribs.getString(XML_id, OUString());
126  nPriority = rAttribs.getInteger( XML_priority, -1 );
127  maPriorities.push_back(nPriority);
129 
130  if (aType == "dataBar")
131  {
132  // an ext entry does not need to have an existing corresponding entry
133  ExtLst::const_iterator aExt = getExtLst().find( aId );
134  if(aExt == getExtLst().end())
135  return nullptr;
136 
137  ScDataBarFormatData* pInfo = aExt->second;
138  if (!pInfo)
139  {
140  return nullptr;
141  }
142  return new ExtCfRuleContext( *this, pInfo );
143  }
144  else if (aType == "iconSet")
145  {
146  ScDocument& rDoc = getScDocument();
147  mpCurrentRule.reset(new IconSetRule(*this));
148  maEntries.push_back(std::make_unique<ScIconSetFormat>(&rDoc));
149  return new IconSetContext(*this, mpCurrentRule.get());
150  }
151  else if (aType == "cellIs")
152  {
153  sal_Int32 aToken = rAttribs.getToken( XML_operator, XML_TOKEN_INVALID );
156  return this;
157  }
158  else if (aType == "containsText")
159  {
162  return this;
163  }
164  else if (aType == "notContainsText")
165  {
168  return this;
169  }
170  else if (aType == "beginsWith")
171  {
174  return this;
175  }
176  else if (aType == "endsWith")
177  {
180  return this;
181  }
182  else if (aType == "expression")
183  {
186  return this;
187  }
188  else
189  {
190  SAL_WARN("sc", "unhandled XLS14_TOKEN(cfRule) with type: " << aType);
191  }
192  }
193  else if (nElement == XLS14_TOKEN( dxf ))
194  {
195  return new DxfContext( *this, getStyles().createExtDxf() );
196  }
197  else if (nElement == XM_TOKEN( sqref ) || nElement == XM_TOKEN( f ))
198  {
199  if(nElement == XM_TOKEN( f ))
200  nFormulaCount++;
201  return this;
202  }
203 
204  return nullptr;
205 }
206 
208 {
209 }
210 
211 void ExtConditionalFormattingContext::onCharacters(const OUString& rCharacters)
212 {
213  switch (getCurrentElement())
214  {
215  case XM_TOKEN(f):
216  {
217  aChars = rCharacters;
218  isPreviousElementF = true;
219  }
220  break;
221  case XM_TOKEN(sqref):
222  {
223  aChars = rCharacters;
224  }
225  break;
226  }
227 
228 }
229 
231 {
232  switch (getCurrentElement())
233  {
234  case XM_TOKEN(f):
235  {
236  if(!IsSpecificTextCondMode(eOperator) || nFormulaCount == 2)
238  }
239  break;
240  case XLS14_TOKEN( cfRule ):
241  {
244  rStyleIdx++;
245  nFormulaCount = 0;
246  maModels.push_back(maModel);
247  }
248  break;
249  case XM_TOKEN(sqref):
250  {
251  ScRangeList aRange;
252  ScDocument& rDoc = getScDocument();
254  if (!bSuccess || aRange.empty())
255  break;
256 
257  SCTAB nTab = getSheetIndex();
258  for (size_t i = 0; i < aRange.size(); ++i)
259  {
260  aRange[i].aStart.SetTab(nTab);
261  aRange[i].aEnd.SetTab(nTab);
262  }
263 
264  if (maModels.size() > 1)
265  {
266  std::sort(maModels.begin(), maModels.end(),
267  [](const ExtCondFormatRuleModel& lhs, const ExtCondFormatRuleModel& rhs) {
268  return lhs.nPriority < rhs.nPriority;
269  });
270  }
271 
272  if (isPreviousElementF) // sqref can be alone in some cases.
273  {
274  for (size_t i = 0; i < maModels.size(); ++i)
275  {
276  ScAddress rPos = aRange.GetTopLeftCorner();
277  ScCondFormatEntry* pEntry = new ScCondFormatEntry(maModels[i].eOperator, maModels[i].aFormula, "", rDoc,
278  rPos, maModels[i].aStyle, "", "",
282  maEntries.push_back(std::unique_ptr<ScFormatEntry>(pEntry));
283  }
284 
285  assert(maModels.size() == maPriorities.size());
286  maModels.clear();
287  }
288 
289  std::vector< std::unique_ptr<ExtCfCondFormat> >& rExtFormats = getCondFormats().importExtCondFormat();
290  rExtFormats.push_back(std::make_unique<ExtCfCondFormat>(aRange, maEntries, &maPriorities));
291 
292  maPriorities.clear();
293  isPreviousElementF = false;
294  }
295  break;
296  default:
297  break;
298  }
299 }
300 
302  WorksheetContextBase(rFragment),
303  mpTarget(pTarget)
304 {
305 }
306 
308 {
309  switch( getCurrentElement() )
310  {
311  case XLS_TOKEN( extLst ):
312  if(nElement == XLS_TOKEN( ext ))
313  return this;
314  else
315  return nullptr;
316  case XLS_TOKEN( ext ):
317  if (nElement == XLS14_TOKEN( id ))
318  return this;
319  else
320  return nullptr;
321  }
322  return nullptr;
323 }
324 
326 {
327  switch( getCurrentElement() )
328  {
329  case XLS14_TOKEN( id ):
330  break;
331  }
332 }
333 
334 void ExtLstLocalContext::onCharacters( const OUString& rChars )
335 {
336  if (getCurrentElement() == XLS14_TOKEN( id ))
337  {
338  getExtLst().insert( std::pair< OUString, ScDataBarFormatData*>(rChars, mpTarget) );
339  }
340 }
341 
343  WorksheetContextBase(rFragment)
344 {
345 }
346 
347 ContextHandlerRef ExtGlobalContext::onCreateContext( sal_Int32 nElement, const AttributeList& /*rAttribs*/ )
348 {
349  switch (nElement)
350  {
351  case XLS14_TOKEN(conditionalFormatting): return new ExtConditionalFormattingContext(*this);
352  case XLS14_TOKEN(dataValidations): return new ExtDataValidationsContext(*this);
353  }
354  return this;
355 }
356 
358 {
359 }
360 
362  WorksheetContextBase(rFragment)
363 {
364 }
365 
367 {
368  if (nElement == XLS_TOKEN( ext ))
369  return new ExtGlobalContext( *this );
370 
371  return this;
372 }
373 
375  WorkbookContextBase(rFragment)
376 {
377 }
378 
380 {
381  if (nElement == LOEXT_TOKEN(extCalcPr))
382  {
383  ScDocument& rDoc = getScDocument();
384  sal_Int32 nToken = rAttribs.getToken( XML_stringRefSyntax, XML_CalcA1 );
385  ScCalcConfig aCalcConfig = rDoc.GetCalcConfig();
386 
387  switch( nToken )
388  {
389  case XML_CalcA1:
391  break;
392  case XML_ExcelA1:
394  break;
395  case XML_ExcelR1C1:
397  break;
398  case XML_CalcA1ExcelA1:
400  break;
401  default:
403  break;
404  }
405  rDoc.SetCalcConfig(aCalcConfig);
406  }
407 
408  return this;
409 }
410 
412 {
413 }
414 
416  WorkbookContextBase(rFragment)
417 {
418 }
419 
421 {
422  if (nElement == XLS_TOKEN( ext ))
423  return new ExtGlobalWorkbookContext( *this );
424 
425  return this;
426 }
427 
428 } //namespace oox::xls
429 
430 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void onCharacters(const OUString &rChars) override
OptValue< sal_Int32 > getInteger(sal_Int32 nAttrToken) const
std::vector< ExtCondFormatRuleModel > maModels
std::vector< std::unique_ptr< ScFormatEntry > > maEntries
virtual void onStartElement(const AttributeList &rAttribs) override
OptValue< OUString > getString(sal_Int32 nAttrToken) const
ScDataBarFormatData * mpTarget
ScDataBarFormatData * mpTarget
StylesBuffer & getStyles() const
Returns all cell formatting objects read from the styles substream.
A single ext entry.
std::vector< std::unique_ptr< ExtCfCondFormat > > & importExtCondFormat()
NONE
virtual void onStartElement(const AttributeList &rAttribs) override
virtual ::oox::core::ContextHandlerRef onCreateContext(sal_Int32 nElement, const AttributeList &rAttribs) override
void forEachMem(FuncType pFunc) const
CondFormatBuffer & getCondFormats() const
Returns the conditional formatting in this sheet.
virtual ::oox::core::ContextHandlerRef onCreateContext(sal_Int32 nElement, const AttributeList &rAttribs) override
const RefVector< Dxf > & getExtDxfs() const
ScConditionMode
Definition: conditio.hxx:60
ExtCfRuleContext(WorksheetContextBase &rFragment, ScDataBarFormatData *pDataBar)
T * mpTarget
ExtLstGlobalContext(WorksheetFragment &rFragment)
XML_TOKEN_INVALID
ScAddress GetTopLeftCorner() const
Definition: rangelst.cxx:1154
bool empty() const
Definition: rangelst.hxx:88
int i
virtual ::oox::core::ContextHandlerRef onCreateContext(sal_Int32 nElement, const AttributeList &rAttribs) override
OUString aId
virtual void onCharacters(const OUString &rCharacters) override
virtual void onStartElement(const AttributeList &rAttribs) override
ExtLstGlobalWorkbookContext(WorkbookFragment &rFragment)
virtual ::oox::core::ContextHandlerRef onCreateContext(sal_Int32 nElement, const AttributeList &rAttribs) override
virtual ::oox::core::ContextHandlerRef onCreateContext(sal_Int32 nElement, const AttributeList &rAttribs) override
sal_Int32 getCurrentElement() const
size_t size() const
Definition: rangelst.hxx:89
ExtCfDataBarRuleRef createExtCfDataBarRule(ScDataBarFormatData *pTarget)
sal_Int32 rStyleIdx
virtual ::oox::core::ContextHandlerRef onCreateContext(sal_Int32 nElement, const AttributeList &rAttribs) override
enumrange< T >::Iterator end(enumrange< T >)
static ScConditionMode convertToInternalOperator(sal_Int32 nToken)
Configuration options for formula interpreter.
Definition: calcconfig.hxx:43
Context handler derived from the WorkbookHelper helper class.
void finalizeImport()
Final processing after import of all style settings.
ExtGlobalContext(WorksheetContextBase &rFragment)
DefTokenId nToken
Definition: qproform.cxx:397
ExtConditionalFormattingContext(WorksheetContextBase &rFragment)
virtual ::oox::core::ContextHandlerRef onCreateContext(sal_Int32 nElement, const AttributeList &rAttribs) override
Context handler derived from the WorksheetHelper helper class.
const ScCalcConfig & GetCalcConfig() const
Definition: document.hxx:2532
SCTAB getSheetIndex() const
Returns the index of the current sheet.
ExtLstLocalContext(WorksheetContextBase &rFragment, ScDataBarFormatData *pTarget)
std::unique_ptr< IconSetRule > mpCurrentRule
static bool GetRangeListFromString(ScRangeList &rRangeList, const OUString &rRangeListStr, const ScDocument &rDocument, formula::FormulaGrammar::AddressConvention eConv, sal_Unicode cSeparator= ' ', sal_Unicode cQuote= '\'')
Definition: rangeutl.cxx:544
std::shared_ptr< ExtCfDataBarRule > ExtCfDataBarRuleRef
OUString createExtDxfStyle(sal_Int32 nDxfId) const
virtual void onStartElement(const AttributeList &rAttribs) override
virtual Type GetType() const =0
#define SAL_WARN(area, stream)
void(* f)(TrueTypeTable *)
SC_DLLPUBLIC void SetCalcConfig(const ScCalcConfig &rConfig)
Definition: document10.cxx:199
ExtGlobalWorkbookContext(WorkbookContextBase &rFragment)
virtual void onStartElement(const AttributeList &rAttribs) override
OptValue< sal_Int32 > getToken(sal_Int32 nAttrToken) const
sal_Int16 SCTAB
Definition: types.hxx:22
void SetStringRefSyntax(formula::FormulaGrammar::AddressConvention eConv)
Definition: calcconfig.cxx:165