xref: /trunk/main/xmloff/source/style/numehelp.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "xmloff/numehelp.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
34*cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
35*cdf0e10cSrcweir #include <xmloff/xmluconv.hxx>
36*cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
37*cdf0e10cSrcweir #include <xmloff/xmlexp.hxx>
38*cdf0e10cSrcweir #include <com/sun/star/uno/Reference.h>
39*cdf0e10cSrcweir #include <rtl/ustring.hxx>
40*cdf0e10cSrcweir #include <svl/zforlist.hxx>
41*cdf0e10cSrcweir #include <com/sun/star/util/NumberFormat.hpp>
42*cdf0e10cSrcweir #include <rtl/math.hxx>
43*cdf0e10cSrcweir #include <tools/debug.hxx>
44*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir using namespace com::sun::star;
47*cdf0e10cSrcweir using namespace xmloff::token;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #define XML_TYPE "Type"
50*cdf0e10cSrcweir #define XML_CURRENCYSYMBOL "CurrencySymbol"
51*cdf0e10cSrcweir #define XML_CURRENCYABBREVIATION "CurrencyAbbreviation"
52*cdf0e10cSrcweir #define XML_STANDARDFORMAT "StandardFormat"
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir XMLNumberFormatAttributesExportHelper::XMLNumberFormatAttributesExportHelper(
55*cdf0e10cSrcweir             ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatsSupplier >& xTempNumberFormatsSupplier)
56*cdf0e10cSrcweir     : xNumberFormats(xTempNumberFormatsSupplier.is() ? xTempNumberFormatsSupplier->getNumberFormats() : ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormats > ()),
57*cdf0e10cSrcweir     pExport(NULL),
58*cdf0e10cSrcweir     sStandardFormat(RTL_CONSTASCII_USTRINGPARAM(XML_STANDARDFORMAT)),
59*cdf0e10cSrcweir     sType(RTL_CONSTASCII_USTRINGPARAM(XML_TYPE)),
60*cdf0e10cSrcweir     msCurrencySymbol(RTL_CONSTASCII_USTRINGPARAM(XML_CURRENCYSYMBOL)),
61*cdf0e10cSrcweir     msCurrencyAbbreviation(RTL_CONSTASCII_USTRINGPARAM(XML_CURRENCYABBREVIATION)),
62*cdf0e10cSrcweir     aNumberFormats()
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir }
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir XMLNumberFormatAttributesExportHelper::XMLNumberFormatAttributesExportHelper(
67*cdf0e10cSrcweir             ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatsSupplier >& xTempNumberFormatsSupplier,
68*cdf0e10cSrcweir             SvXMLExport& rTempExport )
69*cdf0e10cSrcweir :   xNumberFormats(xTempNumberFormatsSupplier.is() ? xTempNumberFormatsSupplier->getNumberFormats() : ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormats > ()),
70*cdf0e10cSrcweir     pExport(&rTempExport),
71*cdf0e10cSrcweir     sStandardFormat(RTL_CONSTASCII_USTRINGPARAM(XML_STANDARDFORMAT)),
72*cdf0e10cSrcweir     sType(RTL_CONSTASCII_USTRINGPARAM(XML_TYPE)),
73*cdf0e10cSrcweir     sAttrValueType(rTempExport.GetNamespaceMap().GetQNameByKey( XML_NAMESPACE_OFFICE, GetXMLToken(XML_VALUE_TYPE))),
74*cdf0e10cSrcweir     sAttrValue(rTempExport.GetNamespaceMap().GetQNameByKey( XML_NAMESPACE_OFFICE, GetXMLToken(XML_VALUE))),
75*cdf0e10cSrcweir     sAttrDateValue(rTempExport.GetNamespaceMap().GetQNameByKey( XML_NAMESPACE_OFFICE, GetXMLToken(XML_DATE_VALUE))),
76*cdf0e10cSrcweir     sAttrTimeValue(rTempExport.GetNamespaceMap().GetQNameByKey( XML_NAMESPACE_OFFICE, GetXMLToken(XML_TIME_VALUE))),
77*cdf0e10cSrcweir     sAttrBooleanValue(rTempExport.GetNamespaceMap().GetQNameByKey( XML_NAMESPACE_OFFICE, GetXMLToken(XML_BOOLEAN_VALUE))),
78*cdf0e10cSrcweir     sAttrStringValue(rTempExport.GetNamespaceMap().GetQNameByKey( XML_NAMESPACE_OFFICE, GetXMLToken(XML_STRING_VALUE))),
79*cdf0e10cSrcweir     sAttrCurrency(rTempExport.GetNamespaceMap().GetQNameByKey( XML_NAMESPACE_OFFICE, GetXMLToken(XML_CURRENCY))),
80*cdf0e10cSrcweir     msCurrencySymbol(RTL_CONSTASCII_USTRINGPARAM(XML_CURRENCYSYMBOL)),
81*cdf0e10cSrcweir     msCurrencyAbbreviation(RTL_CONSTASCII_USTRINGPARAM(XML_CURRENCYABBREVIATION)),
82*cdf0e10cSrcweir     aNumberFormats()
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir XMLNumberFormatAttributesExportHelper::~XMLNumberFormatAttributesExportHelper()
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir sal_Int16 XMLNumberFormatAttributesExportHelper::GetCellType(const sal_Int32 nNumberFormat, rtl::OUString& sCurrency, sal_Bool& bIsStandard)
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir     XMLNumberFormat aFormat(sEmpty, nNumberFormat, 0);
93*cdf0e10cSrcweir     XMLNumberFormatSet::iterator aItr(aNumberFormats.find(aFormat));
94*cdf0e10cSrcweir     XMLNumberFormatSet::iterator aEndItr(aNumberFormats.end());
95*cdf0e10cSrcweir     if (aItr != aEndItr)
96*cdf0e10cSrcweir     {
97*cdf0e10cSrcweir         bIsStandard = aItr->bIsStandard;
98*cdf0e10cSrcweir         sCurrency = aItr->sCurrency;
99*cdf0e10cSrcweir         return aItr->nType;
100*cdf0e10cSrcweir     }
101*cdf0e10cSrcweir     else
102*cdf0e10cSrcweir     {
103*cdf0e10cSrcweir         aFormat.nType = GetCellType(nNumberFormat, bIsStandard);
104*cdf0e10cSrcweir         aFormat.bIsStandard = bIsStandard;
105*cdf0e10cSrcweir         if ((aFormat.nType & ~util::NumberFormat::DEFINED) == util::NumberFormat::CURRENCY)
106*cdf0e10cSrcweir             if (GetCurrencySymbol(nNumberFormat, aFormat.sCurrency))
107*cdf0e10cSrcweir                 sCurrency = aFormat.sCurrency;
108*cdf0e10cSrcweir         aNumberFormats.insert(aFormat);
109*cdf0e10cSrcweir         return aFormat.nType;
110*cdf0e10cSrcweir     }
111*cdf0e10cSrcweir }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir void XMLNumberFormatAttributesExportHelper::WriteAttributes(SvXMLExport& rXMLExport,
114*cdf0e10cSrcweir                                 const sal_Int16 nTypeKey,
115*cdf0e10cSrcweir                                 const double& rValue,
116*cdf0e10cSrcweir                                 const rtl::OUString& rCurrency,
117*cdf0e10cSrcweir                                 sal_Bool bExportValue)
118*cdf0e10cSrcweir {
119*cdf0e10cSrcweir     sal_Bool bWasSetTypeAttribute = sal_False;
120*cdf0e10cSrcweir     switch(nTypeKey & ~util::NumberFormat::DEFINED)
121*cdf0e10cSrcweir     {
122*cdf0e10cSrcweir     case 0:
123*cdf0e10cSrcweir     case util::NumberFormat::NUMBER:
124*cdf0e10cSrcweir     case util::NumberFormat::SCIENTIFIC:
125*cdf0e10cSrcweir     case util::NumberFormat::FRACTION:
126*cdf0e10cSrcweir         {
127*cdf0e10cSrcweir             if (!bWasSetTypeAttribute)
128*cdf0e10cSrcweir             {
129*cdf0e10cSrcweir                 rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_VALUE_TYPE, XML_FLOAT);
130*cdf0e10cSrcweir                 bWasSetTypeAttribute = sal_True;
131*cdf0e10cSrcweir             }
132*cdf0e10cSrcweir         }       // No Break
133*cdf0e10cSrcweir     case util::NumberFormat::PERCENT:
134*cdf0e10cSrcweir         {
135*cdf0e10cSrcweir             if (!bWasSetTypeAttribute)
136*cdf0e10cSrcweir             {
137*cdf0e10cSrcweir                 rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_VALUE_TYPE, XML_PERCENTAGE);
138*cdf0e10cSrcweir                 bWasSetTypeAttribute = sal_True;
139*cdf0e10cSrcweir             }
140*cdf0e10cSrcweir         }       // No Break
141*cdf0e10cSrcweir     case util::NumberFormat::CURRENCY:
142*cdf0e10cSrcweir         {
143*cdf0e10cSrcweir             if (!bWasSetTypeAttribute)
144*cdf0e10cSrcweir             {
145*cdf0e10cSrcweir                 rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_VALUE_TYPE, XML_CURRENCY);
146*cdf0e10cSrcweir                 if (rCurrency.getLength() > 0)
147*cdf0e10cSrcweir                     rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_CURRENCY, rCurrency);
148*cdf0e10cSrcweir                 bWasSetTypeAttribute = sal_True;
149*cdf0e10cSrcweir             }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir             if (bExportValue)
152*cdf0e10cSrcweir             {
153*cdf0e10cSrcweir                 rtl::OUString sValue( ::rtl::math::doubleToUString( rValue,
154*cdf0e10cSrcweir                             rtl_math_StringFormat_Automatic,
155*cdf0e10cSrcweir                             rtl_math_DecimalPlaces_Max, '.', sal_True));
156*cdf0e10cSrcweir                 rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_VALUE, sValue);
157*cdf0e10cSrcweir             }
158*cdf0e10cSrcweir         }
159*cdf0e10cSrcweir         break;
160*cdf0e10cSrcweir     case util::NumberFormat::DATE:
161*cdf0e10cSrcweir     case util::NumberFormat::DATETIME:
162*cdf0e10cSrcweir         {
163*cdf0e10cSrcweir             if (!bWasSetTypeAttribute)
164*cdf0e10cSrcweir             {
165*cdf0e10cSrcweir                 rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_VALUE_TYPE, XML_DATE);
166*cdf0e10cSrcweir                 bWasSetTypeAttribute = sal_True;
167*cdf0e10cSrcweir             }
168*cdf0e10cSrcweir             if (bExportValue)
169*cdf0e10cSrcweir             {
170*cdf0e10cSrcweir                 if ( rXMLExport.SetNullDateOnUnitConverter() )
171*cdf0e10cSrcweir                 {
172*cdf0e10cSrcweir                     rtl::OUStringBuffer sBuffer;
173*cdf0e10cSrcweir                     rXMLExport.GetMM100UnitConverter().convertDateTime(sBuffer, rValue);
174*cdf0e10cSrcweir                     rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_DATE_VALUE, sBuffer.makeStringAndClear());
175*cdf0e10cSrcweir                 }
176*cdf0e10cSrcweir             }
177*cdf0e10cSrcweir         }
178*cdf0e10cSrcweir         break;
179*cdf0e10cSrcweir     case util::NumberFormat::TIME:
180*cdf0e10cSrcweir         {
181*cdf0e10cSrcweir             if (!bWasSetTypeAttribute)
182*cdf0e10cSrcweir             {
183*cdf0e10cSrcweir                 rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_VALUE_TYPE, XML_TIME);
184*cdf0e10cSrcweir                 bWasSetTypeAttribute = sal_True;
185*cdf0e10cSrcweir             }
186*cdf0e10cSrcweir             if (bExportValue)
187*cdf0e10cSrcweir             {
188*cdf0e10cSrcweir                 rtl::OUStringBuffer sBuffer;
189*cdf0e10cSrcweir                 rXMLExport.GetMM100UnitConverter().convertTime(sBuffer, rValue);
190*cdf0e10cSrcweir                 rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_TIME_VALUE, sBuffer.makeStringAndClear());
191*cdf0e10cSrcweir             }
192*cdf0e10cSrcweir         }
193*cdf0e10cSrcweir         break;
194*cdf0e10cSrcweir     case util::NumberFormat::LOGICAL:
195*cdf0e10cSrcweir         {
196*cdf0e10cSrcweir             if (!bWasSetTypeAttribute)
197*cdf0e10cSrcweir             {
198*cdf0e10cSrcweir                 rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_VALUE_TYPE, XML_BOOLEAN);
199*cdf0e10cSrcweir                 bWasSetTypeAttribute = sal_True;
200*cdf0e10cSrcweir             }
201*cdf0e10cSrcweir             if (bExportValue)
202*cdf0e10cSrcweir             {
203*cdf0e10cSrcweir                 double fTempValue = rValue;
204*cdf0e10cSrcweir                 if (::rtl::math::approxEqual( fTempValue, 1.0 ))
205*cdf0e10cSrcweir                 {
206*cdf0e10cSrcweir                     rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_BOOLEAN_VALUE, XML_TRUE);
207*cdf0e10cSrcweir                 }
208*cdf0e10cSrcweir                 else
209*cdf0e10cSrcweir                 {
210*cdf0e10cSrcweir                     if (::rtl::math::approxEqual( rValue, 0.0 ))
211*cdf0e10cSrcweir                     {
212*cdf0e10cSrcweir                         rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_BOOLEAN_VALUE, XML_FALSE);
213*cdf0e10cSrcweir                     }
214*cdf0e10cSrcweir                     else
215*cdf0e10cSrcweir                     {
216*cdf0e10cSrcweir                         rtl::OUString sValue( ::rtl::math::doubleToUString(
217*cdf0e10cSrcweir                                     fTempValue,
218*cdf0e10cSrcweir                                     rtl_math_StringFormat_Automatic,
219*cdf0e10cSrcweir                                     rtl_math_DecimalPlaces_Max, '.',
220*cdf0e10cSrcweir                                     sal_True));
221*cdf0e10cSrcweir                         rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_BOOLEAN_VALUE, sValue);
222*cdf0e10cSrcweir                     }
223*cdf0e10cSrcweir                 }
224*cdf0e10cSrcweir             }
225*cdf0e10cSrcweir         }
226*cdf0e10cSrcweir         break;
227*cdf0e10cSrcweir     case util::NumberFormat::TEXT:
228*cdf0e10cSrcweir         {
229*cdf0e10cSrcweir             if (!bWasSetTypeAttribute)
230*cdf0e10cSrcweir             {
231*cdf0e10cSrcweir                 rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_VALUE_TYPE, XML_FLOAT);
232*cdf0e10cSrcweir                 bWasSetTypeAttribute = sal_True;
233*cdf0e10cSrcweir                 if (bExportValue)
234*cdf0e10cSrcweir                 {
235*cdf0e10cSrcweir                     rtl::OUString sValue( ::rtl::math::doubleToUString( rValue,
236*cdf0e10cSrcweir                                 rtl_math_StringFormat_Automatic,
237*cdf0e10cSrcweir                                 rtl_math_DecimalPlaces_Max, '.', sal_True));
238*cdf0e10cSrcweir                     rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_VALUE, sValue);
239*cdf0e10cSrcweir                 }
240*cdf0e10cSrcweir             }
241*cdf0e10cSrcweir         }
242*cdf0e10cSrcweir         break;
243*cdf0e10cSrcweir     }
244*cdf0e10cSrcweir }
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir sal_Bool XMLNumberFormatAttributesExportHelper::GetCurrencySymbol(const sal_Int32 nNumberFormat, rtl::OUString& sCurrencySymbol,
247*cdf0e10cSrcweir     uno::Reference <util::XNumberFormatsSupplier>& xNumberFormatsSupplier)
248*cdf0e10cSrcweir {
249*cdf0e10cSrcweir     if (xNumberFormatsSupplier.is())
250*cdf0e10cSrcweir     {
251*cdf0e10cSrcweir         uno::Reference <util::XNumberFormats> xNumberFormats(xNumberFormatsSupplier->getNumberFormats());
252*cdf0e10cSrcweir         if (xNumberFormats.is())
253*cdf0e10cSrcweir         {
254*cdf0e10cSrcweir             try
255*cdf0e10cSrcweir             {
256*cdf0e10cSrcweir                 uno::Reference <beans::XPropertySet> xNumberPropertySet(xNumberFormats->getByKey(nNumberFormat));
257*cdf0e10cSrcweir                 if ( xNumberPropertySet->getPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(XML_CURRENCYSYMBOL))) >>= sCurrencySymbol)
258*cdf0e10cSrcweir                 {
259*cdf0e10cSrcweir                     rtl::OUString sCurrencyAbbreviation;
260*cdf0e10cSrcweir                     if ( xNumberPropertySet->getPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(XML_CURRENCYABBREVIATION))) >>= sCurrencyAbbreviation)
261*cdf0e10cSrcweir                     {
262*cdf0e10cSrcweir                         if ( sCurrencyAbbreviation.getLength() != 0 )
263*cdf0e10cSrcweir                             sCurrencySymbol = sCurrencyAbbreviation;
264*cdf0e10cSrcweir                         else
265*cdf0e10cSrcweir                         {
266*cdf0e10cSrcweir                             if ( sCurrencySymbol.getLength() == 1 && sCurrencySymbol.toChar() == NfCurrencyEntry::GetEuroSymbol() )
267*cdf0e10cSrcweir                                 sCurrencySymbol = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("EUR"));
268*cdf0e10cSrcweir                         }
269*cdf0e10cSrcweir                     }
270*cdf0e10cSrcweir                     return sal_True;
271*cdf0e10cSrcweir                 }
272*cdf0e10cSrcweir             }
273*cdf0e10cSrcweir             catch ( uno::Exception& )
274*cdf0e10cSrcweir             {
275*cdf0e10cSrcweir                 DBG_ERROR("Numberformat not found");
276*cdf0e10cSrcweir             }
277*cdf0e10cSrcweir         }
278*cdf0e10cSrcweir     }
279*cdf0e10cSrcweir     return sal_False;
280*cdf0e10cSrcweir }
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir sal_Int16 XMLNumberFormatAttributesExportHelper::GetCellType(const sal_Int32 nNumberFormat, sal_Bool& bIsStandard,
284*cdf0e10cSrcweir     uno::Reference <util::XNumberFormatsSupplier>& xNumberFormatsSupplier)
285*cdf0e10cSrcweir {
286*cdf0e10cSrcweir     if (xNumberFormatsSupplier.is())
287*cdf0e10cSrcweir     {
288*cdf0e10cSrcweir         uno::Reference <util::XNumberFormats> xNumberFormats(xNumberFormatsSupplier->getNumberFormats());
289*cdf0e10cSrcweir         if (xNumberFormats.is())
290*cdf0e10cSrcweir         {
291*cdf0e10cSrcweir             try
292*cdf0e10cSrcweir             {
293*cdf0e10cSrcweir                 uno::Reference <beans::XPropertySet> xNumberPropertySet(xNumberFormats->getByKey(nNumberFormat));
294*cdf0e10cSrcweir                 xNumberPropertySet->getPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STANDARDFORMAT))) >>= bIsStandard;
295*cdf0e10cSrcweir                 sal_Int16 nNumberType = sal_Int16();
296*cdf0e10cSrcweir                 if ( xNumberPropertySet->getPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(XML_TYPE))) >>= nNumberType )
297*cdf0e10cSrcweir                 {
298*cdf0e10cSrcweir                     return nNumberType;
299*cdf0e10cSrcweir                 }
300*cdf0e10cSrcweir             }
301*cdf0e10cSrcweir             catch ( uno::Exception& )
302*cdf0e10cSrcweir             {
303*cdf0e10cSrcweir                 DBG_ERROR("Numberformat not found");
304*cdf0e10cSrcweir             }
305*cdf0e10cSrcweir         }
306*cdf0e10cSrcweir     }
307*cdf0e10cSrcweir     return 0;
308*cdf0e10cSrcweir }
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir void XMLNumberFormatAttributesExportHelper::SetNumberFormatAttributes(SvXMLExport& rXMLExport,
311*cdf0e10cSrcweir     const sal_Int32 nNumberFormat, const double& rValue, sal_Bool bExportValue)
312*cdf0e10cSrcweir {
313*cdf0e10cSrcweir     sal_Bool bIsStandard;
314*cdf0e10cSrcweir     sal_Int16 nTypeKey = GetCellType(nNumberFormat, bIsStandard, rXMLExport.GetNumberFormatsSupplier());
315*cdf0e10cSrcweir     rtl::OUString sCurrency;
316*cdf0e10cSrcweir     if ((nTypeKey & ~util::NumberFormat::DEFINED) == util::NumberFormat::CURRENCY)
317*cdf0e10cSrcweir         GetCurrencySymbol(nNumberFormat, sCurrency, rXMLExport.GetNumberFormatsSupplier());
318*cdf0e10cSrcweir     WriteAttributes(rXMLExport, nTypeKey, rValue, sCurrency, bExportValue);
319*cdf0e10cSrcweir }
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir void XMLNumberFormatAttributesExportHelper::SetNumberFormatAttributes(SvXMLExport& rXMLExport,
322*cdf0e10cSrcweir     const rtl::OUString& rValue, const rtl::OUString& rCharacters,
323*cdf0e10cSrcweir     sal_Bool bExportValue, sal_Bool bExportTypeAttribute)
324*cdf0e10cSrcweir {
325*cdf0e10cSrcweir     if (bExportTypeAttribute)
326*cdf0e10cSrcweir         rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_VALUE_TYPE, XML_STRING);
327*cdf0e10cSrcweir     if (bExportValue && rValue.getLength() && (rValue != rCharacters))
328*cdf0e10cSrcweir         rXMLExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_STRING_VALUE, rValue);
329*cdf0e10cSrcweir }
330*cdf0e10cSrcweir 
331*cdf0e10cSrcweir sal_Bool XMLNumberFormatAttributesExportHelper::GetCurrencySymbol(const sal_Int32 nNumberFormat, rtl::OUString& rCurrencySymbol)
332*cdf0e10cSrcweir {
333*cdf0e10cSrcweir     if (!xNumberFormats.is() && pExport && pExport->GetNumberFormatsSupplier().is())
334*cdf0e10cSrcweir         xNumberFormats.set(pExport->GetNumberFormatsSupplier()->getNumberFormats());
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir     if (xNumberFormats.is())
337*cdf0e10cSrcweir     {
338*cdf0e10cSrcweir         try
339*cdf0e10cSrcweir         {
340*cdf0e10cSrcweir             uno::Reference <beans::XPropertySet> xNumberPropertySet(xNumberFormats->getByKey(nNumberFormat));
341*cdf0e10cSrcweir             if ( xNumberPropertySet->getPropertyValue(msCurrencySymbol) >>= rCurrencySymbol)
342*cdf0e10cSrcweir             {
343*cdf0e10cSrcweir                 rtl::OUString sCurrencyAbbreviation;
344*cdf0e10cSrcweir                 if ( xNumberPropertySet->getPropertyValue(msCurrencyAbbreviation) >>= sCurrencyAbbreviation)
345*cdf0e10cSrcweir                 {
346*cdf0e10cSrcweir                     if ( sCurrencyAbbreviation.getLength() != 0 )
347*cdf0e10cSrcweir                         rCurrencySymbol = sCurrencyAbbreviation;
348*cdf0e10cSrcweir                     else
349*cdf0e10cSrcweir                     {
350*cdf0e10cSrcweir                         if ( rCurrencySymbol.getLength() == 1 && rCurrencySymbol.toChar() == NfCurrencyEntry::GetEuroSymbol() )
351*cdf0e10cSrcweir                             rCurrencySymbol = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("EUR"));
352*cdf0e10cSrcweir                     }
353*cdf0e10cSrcweir                 }
354*cdf0e10cSrcweir                 return sal_True;
355*cdf0e10cSrcweir             }
356*cdf0e10cSrcweir         }
357*cdf0e10cSrcweir         catch ( uno::Exception& )
358*cdf0e10cSrcweir         {
359*cdf0e10cSrcweir             DBG_ERROR("Numberformat not found");
360*cdf0e10cSrcweir         }
361*cdf0e10cSrcweir     }
362*cdf0e10cSrcweir     return sal_False;
363*cdf0e10cSrcweir }
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir sal_Int16 XMLNumberFormatAttributesExportHelper::GetCellType(const sal_Int32 nNumberFormat, sal_Bool& bIsStandard)
366*cdf0e10cSrcweir {
367*cdf0e10cSrcweir     if (!xNumberFormats.is() && pExport && pExport->GetNumberFormatsSupplier().is())
368*cdf0e10cSrcweir         xNumberFormats.set(pExport->GetNumberFormatsSupplier()->getNumberFormats());
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir     if (xNumberFormats.is())
371*cdf0e10cSrcweir     {
372*cdf0e10cSrcweir         try
373*cdf0e10cSrcweir         {
374*cdf0e10cSrcweir             uno::Reference <beans::XPropertySet> xNumberPropertySet(xNumberFormats->getByKey(nNumberFormat));
375*cdf0e10cSrcweir             if (xNumberPropertySet.is())
376*cdf0e10cSrcweir             {
377*cdf0e10cSrcweir                 xNumberPropertySet->getPropertyValue(sStandardFormat) >>= bIsStandard;
378*cdf0e10cSrcweir                 sal_Int16 nNumberType = sal_Int16();
379*cdf0e10cSrcweir                 if ( xNumberPropertySet->getPropertyValue(sType) >>= nNumberType )
380*cdf0e10cSrcweir                 {
381*cdf0e10cSrcweir                     return nNumberType;
382*cdf0e10cSrcweir                 }
383*cdf0e10cSrcweir             }
384*cdf0e10cSrcweir         }
385*cdf0e10cSrcweir         catch ( uno::Exception& )
386*cdf0e10cSrcweir         {
387*cdf0e10cSrcweir             DBG_ERROR("Numberformat not found");
388*cdf0e10cSrcweir         }
389*cdf0e10cSrcweir     }
390*cdf0e10cSrcweir     return 0;
391*cdf0e10cSrcweir }
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir void XMLNumberFormatAttributesExportHelper::WriteAttributes(
394*cdf0e10cSrcweir                                 const sal_Int16 nTypeKey,
395*cdf0e10cSrcweir                                 const double& rValue,
396*cdf0e10cSrcweir                                 const rtl::OUString& rCurrency,
397*cdf0e10cSrcweir                                 sal_Bool bExportValue)
398*cdf0e10cSrcweir {
399*cdf0e10cSrcweir     if (!pExport)
400*cdf0e10cSrcweir         return;
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir     sal_Bool bWasSetTypeAttribute = sal_False;
403*cdf0e10cSrcweir     switch(nTypeKey & ~util::NumberFormat::DEFINED)
404*cdf0e10cSrcweir     {
405*cdf0e10cSrcweir     case 0:
406*cdf0e10cSrcweir     case util::NumberFormat::NUMBER:
407*cdf0e10cSrcweir     case util::NumberFormat::SCIENTIFIC:
408*cdf0e10cSrcweir     case util::NumberFormat::FRACTION:
409*cdf0e10cSrcweir         {
410*cdf0e10cSrcweir             if (!bWasSetTypeAttribute)
411*cdf0e10cSrcweir             {
412*cdf0e10cSrcweir                 pExport->AddAttribute(sAttrValueType, XML_FLOAT);
413*cdf0e10cSrcweir                 bWasSetTypeAttribute = sal_True;
414*cdf0e10cSrcweir             }
415*cdf0e10cSrcweir         }       // No Break
416*cdf0e10cSrcweir     case util::NumberFormat::PERCENT:
417*cdf0e10cSrcweir         {
418*cdf0e10cSrcweir             if (!bWasSetTypeAttribute)
419*cdf0e10cSrcweir             {
420*cdf0e10cSrcweir                 pExport->AddAttribute(sAttrValueType, XML_PERCENTAGE);
421*cdf0e10cSrcweir                 bWasSetTypeAttribute = sal_True;
422*cdf0e10cSrcweir             }
423*cdf0e10cSrcweir         }       // No Break
424*cdf0e10cSrcweir     case util::NumberFormat::CURRENCY:
425*cdf0e10cSrcweir         {
426*cdf0e10cSrcweir             if (!bWasSetTypeAttribute)
427*cdf0e10cSrcweir             {
428*cdf0e10cSrcweir                 pExport->AddAttribute(sAttrValueType, XML_CURRENCY);
429*cdf0e10cSrcweir                 if (rCurrency.getLength() > 0)
430*cdf0e10cSrcweir                     pExport->AddAttribute(sAttrCurrency, rCurrency);
431*cdf0e10cSrcweir                 bWasSetTypeAttribute = sal_True;
432*cdf0e10cSrcweir             }
433*cdf0e10cSrcweir 
434*cdf0e10cSrcweir             if (bExportValue)
435*cdf0e10cSrcweir             {
436*cdf0e10cSrcweir                 rtl::OUString sValue( ::rtl::math::doubleToUString( rValue,
437*cdf0e10cSrcweir                             rtl_math_StringFormat_Automatic,
438*cdf0e10cSrcweir                             rtl_math_DecimalPlaces_Max, '.', sal_True));
439*cdf0e10cSrcweir                 pExport->AddAttribute(sAttrValue, sValue);
440*cdf0e10cSrcweir             }
441*cdf0e10cSrcweir         }
442*cdf0e10cSrcweir         break;
443*cdf0e10cSrcweir     case util::NumberFormat::DATE:
444*cdf0e10cSrcweir     case util::NumberFormat::DATETIME:
445*cdf0e10cSrcweir         {
446*cdf0e10cSrcweir             if (!bWasSetTypeAttribute)
447*cdf0e10cSrcweir             {
448*cdf0e10cSrcweir                 pExport->AddAttribute(sAttrValueType, XML_DATE);
449*cdf0e10cSrcweir                 bWasSetTypeAttribute = sal_True;
450*cdf0e10cSrcweir             }
451*cdf0e10cSrcweir             if (bExportValue)
452*cdf0e10cSrcweir             {
453*cdf0e10cSrcweir                 if ( pExport->SetNullDateOnUnitConverter() )
454*cdf0e10cSrcweir                 {
455*cdf0e10cSrcweir                     rtl::OUStringBuffer sBuffer;
456*cdf0e10cSrcweir                     pExport->GetMM100UnitConverter().convertDateTime(sBuffer, rValue);
457*cdf0e10cSrcweir                     pExport->AddAttribute(sAttrDateValue, sBuffer.makeStringAndClear());
458*cdf0e10cSrcweir                 }
459*cdf0e10cSrcweir             }
460*cdf0e10cSrcweir         }
461*cdf0e10cSrcweir         break;
462*cdf0e10cSrcweir     case util::NumberFormat::TIME:
463*cdf0e10cSrcweir         {
464*cdf0e10cSrcweir             if (!bWasSetTypeAttribute)
465*cdf0e10cSrcweir             {
466*cdf0e10cSrcweir                 pExport->AddAttribute(sAttrValueType, XML_TIME);
467*cdf0e10cSrcweir                 bWasSetTypeAttribute = sal_True;
468*cdf0e10cSrcweir             }
469*cdf0e10cSrcweir             if (bExportValue)
470*cdf0e10cSrcweir             {
471*cdf0e10cSrcweir                 rtl::OUStringBuffer sBuffer;
472*cdf0e10cSrcweir                 pExport->GetMM100UnitConverter().convertTime(sBuffer, rValue);
473*cdf0e10cSrcweir                 pExport->AddAttribute(sAttrTimeValue, sBuffer.makeStringAndClear());
474*cdf0e10cSrcweir             }
475*cdf0e10cSrcweir         }
476*cdf0e10cSrcweir         break;
477*cdf0e10cSrcweir     case util::NumberFormat::LOGICAL:
478*cdf0e10cSrcweir         {
479*cdf0e10cSrcweir             if (!bWasSetTypeAttribute)
480*cdf0e10cSrcweir             {
481*cdf0e10cSrcweir                 pExport->AddAttribute(sAttrValueType, XML_BOOLEAN);
482*cdf0e10cSrcweir                 bWasSetTypeAttribute = sal_True;
483*cdf0e10cSrcweir             }
484*cdf0e10cSrcweir             if (bExportValue)
485*cdf0e10cSrcweir             {
486*cdf0e10cSrcweir                 double fTempValue = rValue;
487*cdf0e10cSrcweir                 if (::rtl::math::approxEqual( fTempValue, 1.0 ))
488*cdf0e10cSrcweir                 {
489*cdf0e10cSrcweir                     pExport->AddAttribute(sAttrBooleanValue, XML_TRUE);
490*cdf0e10cSrcweir                 }
491*cdf0e10cSrcweir                 else
492*cdf0e10cSrcweir                 {
493*cdf0e10cSrcweir                     if (::rtl::math::approxEqual( rValue, 0.0 ))
494*cdf0e10cSrcweir                     {
495*cdf0e10cSrcweir                         pExport->AddAttribute(sAttrBooleanValue, XML_FALSE);
496*cdf0e10cSrcweir                     }
497*cdf0e10cSrcweir                     else
498*cdf0e10cSrcweir                     {
499*cdf0e10cSrcweir                         rtl::OUString sValue( ::rtl::math::doubleToUString(
500*cdf0e10cSrcweir                                     fTempValue,
501*cdf0e10cSrcweir                                     rtl_math_StringFormat_Automatic,
502*cdf0e10cSrcweir                                     rtl_math_DecimalPlaces_Max, '.',
503*cdf0e10cSrcweir                                     sal_True));
504*cdf0e10cSrcweir                         pExport->AddAttribute(sAttrBooleanValue, sValue);
505*cdf0e10cSrcweir                     }
506*cdf0e10cSrcweir                 }
507*cdf0e10cSrcweir             }
508*cdf0e10cSrcweir         }
509*cdf0e10cSrcweir         break;
510*cdf0e10cSrcweir     case util::NumberFormat::TEXT:
511*cdf0e10cSrcweir         {
512*cdf0e10cSrcweir             if (!bWasSetTypeAttribute)
513*cdf0e10cSrcweir             {
514*cdf0e10cSrcweir                 pExport->AddAttribute(sAttrValueType, XML_FLOAT);
515*cdf0e10cSrcweir                 bWasSetTypeAttribute = sal_True;
516*cdf0e10cSrcweir                 if (bExportValue)
517*cdf0e10cSrcweir                 {
518*cdf0e10cSrcweir                     rtl::OUString sValue( ::rtl::math::doubleToUString( rValue,
519*cdf0e10cSrcweir                                 rtl_math_StringFormat_Automatic,
520*cdf0e10cSrcweir                                 rtl_math_DecimalPlaces_Max, '.', sal_True));
521*cdf0e10cSrcweir                     pExport->AddAttribute(sAttrValue, sValue);
522*cdf0e10cSrcweir                 }
523*cdf0e10cSrcweir             }
524*cdf0e10cSrcweir         }
525*cdf0e10cSrcweir         break;
526*cdf0e10cSrcweir     }
527*cdf0e10cSrcweir }
528*cdf0e10cSrcweir 
529*cdf0e10cSrcweir void XMLNumberFormatAttributesExportHelper::SetNumberFormatAttributes(
530*cdf0e10cSrcweir     const sal_Int32 nNumberFormat, const double& rValue, sal_Bool bExportValue)
531*cdf0e10cSrcweir {
532*cdf0e10cSrcweir     if (pExport)
533*cdf0e10cSrcweir     {
534*cdf0e10cSrcweir         sal_Bool bIsStandard;
535*cdf0e10cSrcweir         rtl::OUString sCurrency;
536*cdf0e10cSrcweir         sal_Int16 nTypeKey = GetCellType(nNumberFormat, sCurrency, bIsStandard);
537*cdf0e10cSrcweir         WriteAttributes(nTypeKey, rValue, sCurrency, bExportValue);
538*cdf0e10cSrcweir     }
539*cdf0e10cSrcweir     else {
540*cdf0e10cSrcweir         DBG_ERROR("no SvXMLExport given");
541*cdf0e10cSrcweir     }
542*cdf0e10cSrcweir }
543*cdf0e10cSrcweir 
544*cdf0e10cSrcweir void XMLNumberFormatAttributesExportHelper::SetNumberFormatAttributes(
545*cdf0e10cSrcweir     const rtl::OUString& rValue, const rtl::OUString& rCharacters,
546*cdf0e10cSrcweir     sal_Bool bExportValue, sal_Bool bExportTypeAttribute)
547*cdf0e10cSrcweir {
548*cdf0e10cSrcweir     if (pExport)
549*cdf0e10cSrcweir     {
550*cdf0e10cSrcweir         if (bExportTypeAttribute)
551*cdf0e10cSrcweir             pExport->AddAttribute(sAttrValueType, XML_STRING);
552*cdf0e10cSrcweir         if (bExportValue && rValue.getLength() && (rValue != rCharacters))
553*cdf0e10cSrcweir             pExport->AddAttribute(sAttrStringValue, rValue);
554*cdf0e10cSrcweir     }
555*cdf0e10cSrcweir     else {
556*cdf0e10cSrcweir         DBG_ERROR("no SvXMLExport given");
557*cdf0e10cSrcweir     }
558*cdf0e10cSrcweir }
559