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