Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / oox / extlstcontext.cxx
blobcf003d0bb19137afbb3fbbc510b25f8c23ad0f95
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <memory>
11 #include <extlstcontext.hxx>
12 #include <worksheethelper.hxx>
13 #include <oox/core/contexthandler.hxx>
14 #include <oox/helper/attributelist.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>
26 #include <rangeutl.hxx>
27 #include <sal/log.hxx>
29 using ::oox::core::ContextHandlerRef;
30 using ::oox::xls::CondFormatBuffer;
32 sal_Int32 rStyleIdx = 0;
34 namespace oox {
35 namespace xls {
37 ExtCfRuleContext::ExtCfRuleContext( WorksheetContextBase& rFragment, ScDataBarFormatData* pTarget ):
38 WorksheetContextBase( rFragment ),
39 mpTarget( pTarget ),
40 mbFirstEntry(true)
44 ContextHandlerRef ExtCfRuleContext::onCreateContext( sal_Int32 , const AttributeList& )
46 return this;
49 void ExtCfRuleContext::onStartElement( const AttributeList& rAttribs )
51 switch( getCurrentElement() )
53 case XLS14_TOKEN( dataBar ):
55 ExtCfDataBarRuleRef xRule = getCondFormats().createExtCfDataBarRule(mpTarget);
56 xRule->importDataBar( rAttribs );
57 break;
59 case XLS14_TOKEN( negativeFillColor ):
61 ExtCfDataBarRuleRef xRule = getCondFormats().createExtCfDataBarRule(mpTarget);
62 xRule->importNegativeFillColor( rAttribs );
63 break;
65 case XLS14_TOKEN( axisColor ):
67 ExtCfDataBarRuleRef xRule = getCondFormats().createExtCfDataBarRule(mpTarget);
68 xRule->importAxisColor( rAttribs );
69 break;
71 case XLS14_TOKEN( cfvo ):
73 ExtCfDataBarRuleRef xRule = getCondFormats().createExtCfDataBarRule(mpTarget);
74 xRule->importCfvo( rAttribs );
75 xRule->getModel().mbIsLower = mbFirstEntry;
76 mbFirstEntry = false;
77 break;
79 default:
80 break;
84 ExtConditionalFormattingContext::ExtConditionalFormattingContext(WorksheetContextBase& rFragment)
85 : WorksheetContextBase(rFragment)
86 , nPriority(-1)
87 , eOperator(ScConditionMode::NONE)
88 , isPreviousElementF(false)
92 ContextHandlerRef ExtConditionalFormattingContext::onCreateContext(sal_Int32 nElement, const AttributeList& rAttribs)
94 if (mpCurrentRule)
96 ScFormatEntry& rFormat = **maEntries.rbegin();
97 assert(rFormat.GetType() == ScFormatEntry::Type::Iconset);
98 ScIconSetFormat& rIconSet = static_cast<ScIconSetFormat&>(rFormat);
99 ScDocument* pDoc = &getScDocument();
100 SCTAB nTab = getSheetIndex();
101 ScAddress aPos(0, 0, nTab);
102 mpCurrentRule->SetData(&rIconSet, pDoc, aPos);
103 mpCurrentRule.reset();
105 if (nElement == XLS14_TOKEN(cfRule))
107 OUString aType = rAttribs.getString(XML_type, OUString());
108 OUString aId = rAttribs.getString(XML_id, OUString());
109 nPriority = rAttribs.getInteger( XML_priority, -1 );
110 maPriorities.push_back(nPriority);
112 if (aType == "dataBar")
114 // an ext entry does not need to have an existing corresponding entry
115 ExtLst::const_iterator aExt = getExtLst().find( aId );
116 if(aExt == getExtLst().end())
117 return nullptr;
119 ScDataBarFormatData* pInfo = aExt->second;
120 if (!pInfo)
122 return nullptr;
124 return new ExtCfRuleContext( *this, pInfo );
126 else if (aType == "iconSet")
128 ScDocument* pDoc = &getScDocument();
129 mpCurrentRule.reset(new IconSetRule(*this));
130 maEntries.push_back(std::make_unique<ScIconSetFormat>(pDoc));
131 return new IconSetContext(*this, mpCurrentRule.get());
133 else if (aType == "cellIs")
135 sal_Int32 aToken = rAttribs.getToken( XML_operator, XML_TOKEN_INVALID );
136 eOperator = CondFormatBuffer::convertToInternalOperator(aToken);
137 return this;
139 else
141 SAL_WARN("sc", "unhandled XLS14_TOKEN(cfRule) with type: " << aType);
144 else if (nElement == XLS14_TOKEN( dxf ))
146 return new DxfContext( *this, getStyles().createExtDxf() );
148 else if (nElement == XM_TOKEN( sqref ) || nElement == XM_TOKEN( f ))
150 return this;
153 return nullptr;
156 void ExtConditionalFormattingContext::onStartElement(const AttributeList& /*Attribs*/)
160 void ExtConditionalFormattingContext::onCharacters(const OUString& rCharacters)
162 switch (getCurrentElement())
164 case XM_TOKEN(f):
166 aChars = rCharacters;
167 isPreviousElementF = true;
169 break;
170 case XM_TOKEN(sqref):
172 aChars = rCharacters;
174 break;
179 void ExtConditionalFormattingContext::onEndElement()
181 switch (getCurrentElement())
183 case XM_TOKEN(f):
185 rFormulas.push_back(aChars);
187 break;
188 case XLS14_TOKEN( cfRule ):
190 getStyles().getExtDxfs().forEachMem( &Dxf::finalizeImport );
192 break;
193 case XM_TOKEN(sqref):
195 ScRangeList aRange;
196 ScDocument* pDoc = &getScDocument();
197 bool bSuccess = ScRangeStringConverter::GetRangeListFromString(aRange, aChars, pDoc, formula::FormulaGrammar::CONV_XL_OOX);
198 if (!bSuccess || aRange.empty())
199 break;
201 SCTAB nTab = getSheetIndex();
202 for (size_t i = 0; i < aRange.size(); ++i)
204 aRange[i].aStart.SetTab(nTab);
205 aRange[i].aEnd.SetTab(nTab);
208 if (isPreviousElementF) // sqref can be alone in some cases.
210 for (const OUString& rFormula : rFormulas)
212 ScAddress rPos = aRange.GetTopLeftCorner();
213 rStyle = getStyles().createExtDxfStyle(rStyleIdx);
214 ScCondFormatEntry* pEntry = new ScCondFormatEntry(eOperator, rFormula, "", pDoc,
215 rPos, rStyle, "", "",
216 formula::FormulaGrammar::GRAM_OOXML ,
217 formula::FormulaGrammar::GRAM_OOXML,
218 ScFormatEntry::Type::ExtCondition );
219 maEntries.push_back(std::unique_ptr<ScFormatEntry>(pEntry));
220 rStyleIdx++;
223 assert(rFormulas.size() == maPriorities.size());
224 rFormulas.clear();
227 std::vector< std::unique_ptr<ExtCfCondFormat> >& rExtFormats = getCondFormats().importExtCondFormat();
228 rExtFormats.push_back(std::make_unique<ExtCfCondFormat>(aRange, maEntries, &maPriorities));
230 maPriorities.clear();
231 isPreviousElementF = false;
233 break;
234 default:
235 break;
239 ExtLstLocalContext::ExtLstLocalContext( WorksheetContextBase& rFragment, ScDataBarFormatData* pTarget ):
240 WorksheetContextBase(rFragment),
241 mpTarget(pTarget)
245 ContextHandlerRef ExtLstLocalContext::onCreateContext( sal_Int32 nElement, const AttributeList& )
247 switch( getCurrentElement() )
249 case XLS_TOKEN( extLst ):
250 if(nElement == XLS_TOKEN( ext ))
251 return this;
252 else
253 return nullptr;
254 break;
255 case XLS_TOKEN( ext ):
256 if (nElement == XLS14_TOKEN( id ))
257 return this;
258 else
259 return nullptr;
261 return nullptr;
264 void ExtLstLocalContext::onStartElement( const AttributeList& )
266 switch( getCurrentElement() )
268 case XLS14_TOKEN( id ):
269 break;
273 void ExtLstLocalContext::onCharacters( const OUString& rChars )
275 if (getCurrentElement() == XLS14_TOKEN( id ))
277 getExtLst().insert( std::pair< OUString, ScDataBarFormatData*>(rChars, mpTarget) );
281 ExtGlobalContext::ExtGlobalContext( WorksheetContextBase& rFragment ):
282 WorksheetContextBase(rFragment)
286 ContextHandlerRef ExtGlobalContext::onCreateContext( sal_Int32 nElement, const AttributeList& /*rAttribs*/ )
288 switch (nElement)
290 case XLS14_TOKEN(conditionalFormatting): return new ExtConditionalFormattingContext(*this);
291 case XLS14_TOKEN(dataValidations): return new ExtDataValidationsContext(*this);
293 return this;
296 void ExtGlobalContext::onStartElement( const AttributeList& /*rAttribs*/ )
300 ExtLstGlobalContext::ExtLstGlobalContext( WorksheetFragment& rFragment ):
301 WorksheetContextBase(rFragment)
305 ContextHandlerRef ExtLstGlobalContext::onCreateContext( sal_Int32 nElement, const AttributeList& )
307 if (nElement == XLS_TOKEN( ext ))
308 return new ExtGlobalContext( *this );
310 return this;
313 ExtGlobalWorkbookContext::ExtGlobalWorkbookContext( WorkbookContextBase& rFragment ):
314 WorkbookContextBase(rFragment)
318 ContextHandlerRef ExtGlobalWorkbookContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
320 if (nElement == LOEXT_TOKEN(extCalcPr))
322 ScDocument* pDoc = &getScDocument();
323 sal_Int32 nToken = rAttribs.getToken( XML_stringRefSyntax, XML_CalcA1 );
324 ScCalcConfig aCalcConfig = pDoc->GetCalcConfig();
326 switch( nToken )
328 case XML_CalcA1:
329 aCalcConfig.SetStringRefSyntax( formula::FormulaGrammar::CONV_OOO );
330 break;
331 case XML_ExcelA1:
332 aCalcConfig.SetStringRefSyntax( formula::FormulaGrammar::CONV_XL_A1 );
333 break;
334 case XML_ExcelR1C1:
335 aCalcConfig.SetStringRefSyntax( formula::FormulaGrammar::CONV_XL_R1C1 );
336 break;
337 case XML_CalcA1ExcelA1:
338 aCalcConfig.SetStringRefSyntax( formula::FormulaGrammar::CONV_A1_XL_A1 );
339 break;
340 default:
341 aCalcConfig.SetStringRefSyntax( formula::FormulaGrammar::CONV_UNSPECIFIED );
342 break;
344 pDoc->SetCalcConfig(aCalcConfig);
347 return this;
350 void ExtGlobalWorkbookContext::onStartElement( const AttributeList& /*rAttribs*/ )
354 ExtLstGlobalWorkbookContext::ExtLstGlobalWorkbookContext( WorkbookFragment& rFragment ):
355 WorkbookContextBase(rFragment)
359 ContextHandlerRef ExtLstGlobalWorkbookContext::onCreateContext( sal_Int32 nElement, const AttributeList& )
361 if (nElement == XLS_TOKEN( ext ))
362 return new ExtGlobalWorkbookContext( *this );
364 return this;
367 } //namespace oox
368 } //namespace xls
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */