Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / source / filter / xml / XMLCalculationSettingsContext.cxx
blobd5e214b7c20df826a8948ac410eb01de05369489
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "XMLCalculationSettingsContext.hxx"
21 #include "xmlimprt.hxx"
22 #include <unonames.hxx>
23 #include <docoptio.hxx>
24 #include <document.hxx>
25 #include <xmloff/xmltoken.hxx>
26 #include <xmloff/xmlnamespace.hxx>
27 #include <sax/tools/converter.hxx>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/frame/XModel.hpp>
32 using namespace com::sun::star;
33 using namespace xmloff::token;
35 ScXMLCalculationSettingsContext::ScXMLCalculationSettingsContext( ScXMLImport& rImport,
36 const rtl::Reference<sax_fastparser::FastAttributeList>& rAttrList ) :
37 ScXMLImportContext( rImport ),
38 fIterationEpsilon(0.001),
39 nIterationCount(100),
40 nYear2000(1930),
41 eSearchType(utl::SearchParam::SearchType::Regexp),
42 bIsIterationEnabled(false),
43 bCalcAsShown(false),
44 bIgnoreCase(false),
45 bLookUpLabels(true),
46 bMatchWholeCell(true)
48 aNullDate.Day = 30;
49 aNullDate.Month = 12;
50 aNullDate.Year = 1899;
51 if ( !rAttrList.is() )
52 return;
54 for (auto &aIter : *rAttrList)
56 switch (aIter.getToken())
58 case XML_ELEMENT( TABLE, XML_CASE_SENSITIVE ):
59 if( IsXMLToken( aIter, XML_FALSE ) )
60 bIgnoreCase = true;
61 break;
62 case XML_ELEMENT( TABLE, XML_PRECISION_AS_SHOWN ):
63 if( IsXMLToken( aIter, XML_TRUE ) )
64 bCalcAsShown = true;
65 break;
66 case XML_ELEMENT( TABLE, XML_SEARCH_CRITERIA_MUST_APPLY_TO_WHOLE_CELL ):
67 if( IsXMLToken( aIter, XML_FALSE ) )
68 bMatchWholeCell = false;
69 break;
70 case XML_ELEMENT( TABLE, XML_AUTOMATIC_FIND_LABELS ):
71 if( IsXMLToken( aIter, XML_FALSE ) )
72 bLookUpLabels = false;
73 break;
74 case XML_ELEMENT( TABLE, XML_NULL_YEAR ):
75 sal_Int32 nTemp;
76 ::sax::Converter::convertNumber( nTemp, aIter.toView() );
77 nYear2000 = static_cast<sal_uInt16>(nTemp);
78 break;
79 case XML_ELEMENT( TABLE, XML_USE_REGULAR_EXPRESSIONS ):
80 // Overwrite only the default (regex true) value, not wildcard.
81 if( eSearchType == utl::SearchParam::SearchType::Regexp && IsXMLToken( aIter, XML_FALSE ) )
82 eSearchType = utl::SearchParam::SearchType::Normal;
83 break;
84 case XML_ELEMENT( TABLE, XML_USE_WILDCARDS ):
85 if( IsXMLToken( aIter, XML_TRUE ) )
86 eSearchType = utl::SearchParam::SearchType::Wildcard;
87 break;
92 ScXMLCalculationSettingsContext::~ScXMLCalculationSettingsContext()
96 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL ScXMLCalculationSettingsContext::createFastChildContext(
97 sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList >& xAttrList )
99 SvXMLImportContext *pContext = nullptr;
100 sax_fastparser::FastAttributeList *pAttribList =
101 &sax_fastparser::castToFastAttributeList( xAttrList );
103 if (nElement == XML_ELEMENT( TABLE, XML_NULL_DATE ))
104 pContext = new ScXMLNullDateContext(GetScImport(), pAttribList, this);
105 else if (nElement == XML_ELEMENT( TABLE, XML_ITERATION ))
106 pContext = new ScXMLIterationContext(GetScImport(), pAttribList, this);
108 return pContext;
111 void SAL_CALL ScXMLCalculationSettingsContext::endFastElement( sal_Int32 /*nElement*/ )
113 if (!GetScImport().GetModel().is())
114 return;
116 uno::Reference <beans::XPropertySet> xPropertySet (GetScImport().GetModel(), uno::UNO_QUERY);
117 if (!xPropertySet.is())
118 return;
120 xPropertySet->setPropertyValue( SC_UNO_CALCASSHOWN, uno::Any(bCalcAsShown) );
121 xPropertySet->setPropertyValue( SC_UNO_IGNORECASE, uno::Any(bIgnoreCase) );
122 xPropertySet->setPropertyValue( SC_UNO_LOOKUPLABELS, uno::Any(bLookUpLabels) );
123 xPropertySet->setPropertyValue( SC_UNO_MATCHWHOLE, uno::Any(bMatchWholeCell) );
124 bool bWildcards, bRegex;
125 utl::SearchParam::ConvertToBool( eSearchType, bWildcards, bRegex);
126 xPropertySet->setPropertyValue( SC_UNO_REGEXENABLED, uno::Any(bRegex) );
127 xPropertySet->setPropertyValue( SC_UNO_WILDCARDSENABLED, uno::Any(bWildcards) );
128 xPropertySet->setPropertyValue( SC_UNO_ITERENABLED, uno::Any(bIsIterationEnabled) );
129 xPropertySet->setPropertyValue( SC_UNO_ITERCOUNT, uno::Any(nIterationCount) );
130 xPropertySet->setPropertyValue( SC_UNO_ITEREPSILON, uno::Any(fIterationEpsilon) );
131 xPropertySet->setPropertyValue( SC_UNO_NULLDATE, uno::Any(aNullDate) );
132 if (GetScImport().GetDocument())
134 ScXMLImport::MutexGuard aGuard(GetScImport());
135 ScDocOptions aDocOptions (GetScImport().GetDocument()->GetDocOptions());
136 aDocOptions.SetYear2000(nYear2000);
137 GetScImport().GetDocument()->SetDocOptions(aDocOptions);
141 ScXMLNullDateContext::ScXMLNullDateContext( ScXMLImport& rImport,
142 const rtl::Reference<sax_fastparser::FastAttributeList>& rAttrList,
143 ScXMLCalculationSettingsContext* pCalcSet) :
144 ScXMLImportContext( rImport )
146 if ( !rAttrList.is() )
147 return;
149 auto aIter( rAttrList->find( XML_ELEMENT( TABLE, XML_DATE_VALUE ) ) );
150 if (aIter != rAttrList->end())
152 util::DateTime aDateTime;
153 if (::sax::Converter::parseDateTime(aDateTime, aIter.toView()))
155 util::Date aDate;
156 aDate.Day = aDateTime.Day;
157 aDate.Month = aDateTime.Month;
158 aDate.Year = aDateTime.Year;
159 pCalcSet->SetNullDate(aDate);
161 else
163 SAL_WARN("sc.filter","ignoring invalid NullDate '" << aIter.toView() << "'");
168 ScXMLNullDateContext::~ScXMLNullDateContext()
172 ScXMLIterationContext::ScXMLIterationContext( ScXMLImport& rImport,
173 const rtl::Reference<sax_fastparser::FastAttributeList>& rAttrList,
174 ScXMLCalculationSettingsContext* pCalcSet) :
175 ScXMLImportContext( rImport )
177 if ( !rAttrList.is() )
178 return;
180 for (auto &aIter : *rAttrList)
182 switch (aIter.getToken())
184 case XML_ELEMENT( TABLE, XML_STATUS ):
185 if (IsXMLToken(aIter, XML_ENABLE))
186 pCalcSet->SetIterationStatus(true);
187 break;
188 case XML_ELEMENT( TABLE, XML_STEPS ):
189 pCalcSet->SetIterationCount(aIter.toInt32());
190 break;
191 case XML_ELEMENT( TABLE, XML_MAXIMUM_DIFFERENCE ):
192 pCalcSet->SetIterationEpsilon( aIter.toDouble() );
193 break;
198 ScXMLIterationContext::~ScXMLIterationContext()
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */