xref: /AOO41X/main/sc/source/filter/xml/XMLCalculationSettingsContext.cxx (revision b3f79822e811ac3493b185030a72c3c5a51f32d8)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 
28 
29 // INCLUDE ---------------------------------------------------------------
30 #include "XMLCalculationSettingsContext.hxx"
31 #include "xmlimprt.hxx"
32 #include "unonames.hxx"
33 #include "docoptio.hxx"
34 #include "document.hxx"
35 #include <xmloff/xmltoken.hxx>
36 #include <xmloff/xmlnmspe.hxx>
37 #include <xmloff/xmluconv.hxx>
38 #include <xmloff/nmspmap.hxx>
39 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
40 #include <comphelper/extract.hxx>
41 
42 using namespace com::sun::star;
43 using namespace xmloff::token;
44 
45 //------------------------------------------------------------------
46 
ScXMLCalculationSettingsContext(ScXMLImport & rImport,sal_uInt16 nPrfx,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList)47 ScXMLCalculationSettingsContext::ScXMLCalculationSettingsContext( ScXMLImport& rImport,
48                                       sal_uInt16 nPrfx,
49                                       const ::rtl::OUString& rLName,
50                                       const ::com::sun::star::uno::Reference<
51                                       ::com::sun::star::xml::sax::XAttributeList>& xAttrList) :
52     SvXMLImportContext( rImport, nPrfx, rLName ),
53     fIterationEpsilon(0.001),
54     nIterationCount(100),
55     nYear2000(1930),
56     bIsIterationEnabled(sal_False),
57     bCalcAsShown(sal_False),
58     bIgnoreCase(sal_False),
59     bLookUpLabels(sal_True),
60     bMatchWholeCell(sal_True),
61     bUseRegularExpressions(sal_True)
62 {
63     aNullDate.Day = 30;
64     aNullDate.Month = 12;
65     aNullDate.Year = 1899;
66     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
67     for( sal_Int16 i=0; i < nAttrCount; ++i )
68     {
69         const rtl::OUString& sAttrName(xAttrList->getNameByIndex( i ));
70         rtl::OUString aLocalName;
71         sal_uInt16 nPrefix = GetScImport().GetNamespaceMap().GetKeyByAttrName(
72                                             sAttrName, &aLocalName );
73         const rtl::OUString& sValue(xAttrList->getValueByIndex( i ));
74 
75         if (nPrefix == XML_NAMESPACE_TABLE)
76         {
77             if (IsXMLToken(aLocalName, XML_CASE_SENSITIVE))
78             {
79                 if (IsXMLToken(sValue, XML_FALSE))
80                     bIgnoreCase = sal_True;
81             }
82             else if (IsXMLToken(aLocalName, XML_PRECISION_AS_SHOWN))
83             {
84                 if (IsXMLToken(sValue, XML_TRUE))
85                     bCalcAsShown = sal_True;
86             }
87             else if (IsXMLToken(aLocalName, XML_SEARCH_CRITERIA_MUST_APPLY_TO_WHOLE_CELL))
88             {
89                 if (IsXMLToken(sValue, XML_FALSE))
90                     bMatchWholeCell = sal_False;
91             }
92             else if (IsXMLToken(aLocalName, XML_AUTOMATIC_FIND_LABELS))
93             {
94                 if (IsXMLToken(sValue, XML_FALSE))
95                     bLookUpLabels = sal_False;
96             }
97             else if (IsXMLToken(aLocalName, XML_NULL_YEAR))
98             {
99                 sal_Int32 nTemp;
100                 GetScImport().GetMM100UnitConverter().convertNumber(nTemp, sValue);
101                 nYear2000 = static_cast<sal_uInt16>(nTemp);
102             }
103             else if (IsXMLToken(aLocalName, XML_USE_REGULAR_EXPRESSIONS))
104             {
105                 if (IsXMLToken(sValue, XML_FALSE))
106                     bUseRegularExpressions = sal_False;
107             }
108         }
109     }
110 }
111 
~ScXMLCalculationSettingsContext()112 ScXMLCalculationSettingsContext::~ScXMLCalculationSettingsContext()
113 {
114 }
115 
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList)116 SvXMLImportContext *ScXMLCalculationSettingsContext::CreateChildContext( sal_uInt16 nPrefix,
117                                             const ::rtl::OUString& rLName,
118                                             const ::com::sun::star::uno::Reference<
119                                         ::com::sun::star::xml::sax::XAttributeList>& xAttrList )
120 {
121     SvXMLImportContext *pContext = 0;
122 
123     if (nPrefix == XML_NAMESPACE_TABLE)
124     {
125         if (IsXMLToken(rLName, XML_NULL_DATE))
126             pContext = new ScXMLNullDateContext(GetScImport(), nPrefix, rLName, xAttrList, this);
127         else if (IsXMLToken(rLName, XML_ITERATION))
128             pContext = new ScXMLIterationContext(GetScImport(), nPrefix, rLName, xAttrList, this);
129     }
130 
131     if( !pContext )
132         pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
133 
134     return pContext;
135 }
136 
EndElement()137 void ScXMLCalculationSettingsContext::EndElement()
138 {
139     if (GetScImport().GetModel().is())
140     {
141         uno::Reference <beans::XPropertySet> xPropertySet (GetScImport().GetModel(), uno::UNO_QUERY);
142         if (xPropertySet.is())
143         {
144             xPropertySet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNO_CALCASSHOWN)), uno::makeAny(bCalcAsShown) );
145             xPropertySet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNO_IGNORECASE)), uno::makeAny(bIgnoreCase) );
146             xPropertySet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNO_LOOKUPLABELS)), uno::makeAny(bLookUpLabels) );
147             xPropertySet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNO_MATCHWHOLE)), uno::makeAny(bMatchWholeCell) );
148             xPropertySet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNO_REGEXENABLED)), uno::makeAny(bUseRegularExpressions) );
149             xPropertySet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNO_ITERENABLED)), uno::makeAny(bIsIterationEnabled) );
150             xPropertySet->setPropertyValue( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNO_ITERCOUNT)), uno::makeAny(nIterationCount) );
151             xPropertySet->setPropertyValue( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNO_ITEREPSILON)), uno::makeAny(fIterationEpsilon) );
152             xPropertySet->setPropertyValue( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNO_NULLDATE)), uno::makeAny(aNullDate) );
153             if (GetScImport().GetDocument())
154             {
155                 GetScImport().LockSolarMutex();
156                 ScDocOptions aDocOptions (GetScImport().GetDocument()->GetDocOptions());
157                 aDocOptions.SetYear2000(nYear2000);
158                 GetScImport().GetDocument()->SetDocOptions(aDocOptions);
159                 GetScImport().UnlockSolarMutex();
160             }
161         }
162     }
163 }
164 
ScXMLNullDateContext(ScXMLImport & rImport,sal_uInt16 nPrfx,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList,ScXMLCalculationSettingsContext * pCalcSet)165 ScXMLNullDateContext::ScXMLNullDateContext( ScXMLImport& rImport,
166                                       sal_uInt16 nPrfx,
167                                       const ::rtl::OUString& rLName,
168                                       const ::com::sun::star::uno::Reference<
169                                       ::com::sun::star::xml::sax::XAttributeList>& xAttrList,
170                                       ScXMLCalculationSettingsContext* pCalcSet) :
171     SvXMLImportContext( rImport, nPrfx, rLName )
172 {
173     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
174     for( sal_Int16 i=0; i < nAttrCount; ++i )
175     {
176         const rtl::OUString& sAttrName(xAttrList->getNameByIndex( i ));
177         rtl::OUString aLocalName;
178         sal_uInt16 nPrefix = GetScImport().GetNamespaceMap().GetKeyByAttrName(
179                                             sAttrName, &aLocalName );
180         const rtl::OUString& sValue(xAttrList->getValueByIndex( i ));
181 
182         if (nPrefix == XML_NAMESPACE_TABLE && IsXMLToken(aLocalName, XML_DATE_VALUE))
183         {
184             util::DateTime aDateTime;
185             GetScImport().GetMM100UnitConverter().convertDateTime(aDateTime, sValue);
186             util::Date aDate;
187             aDate.Day = aDateTime.Day;
188             aDate.Month = aDateTime.Month;
189             aDate.Year = aDateTime.Year;
190             pCalcSet->SetNullDate(aDate);
191         }
192     }
193 }
194 
~ScXMLNullDateContext()195 ScXMLNullDateContext::~ScXMLNullDateContext()
196 {
197 }
198 
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> &)199 SvXMLImportContext *ScXMLNullDateContext::CreateChildContext( sal_uInt16 nPrefix,
200                                             const ::rtl::OUString& rLName,
201                                             const ::com::sun::star::uno::Reference<
202                                         ::com::sun::star::xml::sax::XAttributeList>& /* xAttrList */ )
203 {
204     SvXMLImportContext *pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
205 
206     return pContext;
207 }
208 
EndElement()209 void ScXMLNullDateContext::EndElement()
210 {
211 }
212 
ScXMLIterationContext(ScXMLImport & rImport,sal_uInt16 nPrfx,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList,ScXMLCalculationSettingsContext * pCalcSet)213 ScXMLIterationContext::ScXMLIterationContext( ScXMLImport& rImport,
214                                       sal_uInt16 nPrfx,
215                                       const ::rtl::OUString& rLName,
216                                       const ::com::sun::star::uno::Reference<
217                                       ::com::sun::star::xml::sax::XAttributeList>& xAttrList,
218                                       ScXMLCalculationSettingsContext* pCalcSet) :
219     SvXMLImportContext( rImport, nPrfx, rLName )
220 {
221     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
222     for( sal_Int16 i=0; i < nAttrCount; ++i )
223     {
224         const rtl::OUString& sAttrName(xAttrList->getNameByIndex( i ));
225         rtl::OUString aLocalName;
226         sal_uInt16 nPrefix = GetScImport().GetNamespaceMap().GetKeyByAttrName(
227                                             sAttrName, &aLocalName );
228         const rtl::OUString& sValue(xAttrList->getValueByIndex( i ));
229 
230         if (nPrefix == XML_NAMESPACE_TABLE)
231         {
232             if (IsXMLToken(aLocalName, XML_STATUS))
233             {
234                 if (IsXMLToken(sValue, XML_ENABLE))
235                     pCalcSet->SetIterationStatus(sal_True);
236             }
237             else if (IsXMLToken(aLocalName, XML_STEPS))
238             {
239                 sal_Int32 nSteps;
240                 GetScImport().GetMM100UnitConverter().convertNumber(nSteps, sValue);
241                 pCalcSet->SetIterationCount(nSteps);
242             }
243             else if (IsXMLToken(aLocalName, XML_MAXIMUM_DIFFERENCE))
244             {
245                 double fDif;
246                 GetScImport().GetMM100UnitConverter().convertDouble(fDif, sValue);
247                 pCalcSet->SetIterationEpsilon(fDif);
248             }
249         }
250     }
251 }
252 
~ScXMLIterationContext()253 ScXMLIterationContext::~ScXMLIterationContext()
254 {
255 }
256 
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> &)257 SvXMLImportContext *ScXMLIterationContext::CreateChildContext( sal_uInt16 nPrefix,
258                                             const ::rtl::OUString& rLName,
259                                             const ::com::sun::star::uno::Reference<
260                                         ::com::sun::star::xml::sax::XAttributeList>& /* xAttrList */ )
261 {
262     SvXMLImportContext *pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
263 
264     return pContext;
265 }
266 
EndElement()267 void ScXMLIterationContext::EndElement()
268 {
269 }
270