xref: /AOO41X/main/svtools/source/table/cellvalueconversion.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*cdf0e10cSrcweir  *
4*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
7*cdf0e10cSrcweir  *
8*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
9*cdf0e10cSrcweir  *
10*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
11*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
12*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
13*cdf0e10cSrcweir  *
14*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
15*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
18*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
19*cdf0e10cSrcweir  *
20*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
21*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
22*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
23*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
24*cdf0e10cSrcweir  *
25*cdf0e10cSrcweir  ************************************************************************/
26*cdf0e10cSrcweir 
27*cdf0e10cSrcweir #include "precompiled_svtools.hxx"
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include "cellvalueconversion.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir /** === begin UNO includes === **/
32*cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatter.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/util/Date.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/util/DateTime.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/util/Time.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatTypes.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/util/NumberFormat.hpp>
40*cdf0e10cSrcweir /** === end UNO includes === **/
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
43*cdf0e10cSrcweir #include <rtl/math.hxx>
44*cdf0e10cSrcweir #include <rtl/strbuf.hxx>
45*cdf0e10cSrcweir #include <tools/date.hxx>
46*cdf0e10cSrcweir #include <tools/time.hxx>
47*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
48*cdf0e10cSrcweir #include <unotools/syslocale.hxx>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
51*cdf0e10cSrcweir #include <hash_map>
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir //......................................................................................................................
54*cdf0e10cSrcweir namespace svt
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir //......................................................................................................................
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir     /** === begin UNO using === **/
59*cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
60*cdf0e10cSrcweir     using ::com::sun::star::util::XNumberFormatter;
61*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
62*cdf0e10cSrcweir     using ::com::sun::star::util::XNumberFormatsSupplier;
63*cdf0e10cSrcweir     using ::com::sun::star::beans::XPropertySet;
64*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_SET_THROW;
65*cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
66*cdf0e10cSrcweir     using ::com::sun::star::util::DateTime;
67*cdf0e10cSrcweir     using ::com::sun::star::uno::TypeClass;
68*cdf0e10cSrcweir     using ::com::sun::star::util::XNumberFormatTypes;
69*cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
70*cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
71*cdf0e10cSrcweir     using ::com::sun::star::uno::makeAny;
72*cdf0e10cSrcweir     using ::com::sun::star::uno::Type;
73*cdf0e10cSrcweir     using ::com::sun::star::uno::TypeClass_BYTE;
74*cdf0e10cSrcweir     using ::com::sun::star::uno::TypeClass_SHORT;
75*cdf0e10cSrcweir     using ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT;
76*cdf0e10cSrcweir     using ::com::sun::star::uno::TypeClass_LONG;
77*cdf0e10cSrcweir     using ::com::sun::star::uno::TypeClass_UNSIGNED_LONG;
78*cdf0e10cSrcweir     using ::com::sun::star::uno::TypeClass_HYPER;
79*cdf0e10cSrcweir     /** === end UNO using === **/
80*cdf0e10cSrcweir     namespace NumberFormat = ::com::sun::star::util::NumberFormat;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     typedef ::com::sun::star::util::Time UnoTime;
83*cdf0e10cSrcweir     typedef ::com::sun::star::util::Date UnoDate;
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     //==================================================================================================================
86*cdf0e10cSrcweir     //= helper
87*cdf0e10cSrcweir     //==================================================================================================================
88*cdf0e10cSrcweir     namespace
89*cdf0e10cSrcweir     {
90*cdf0e10cSrcweir         //--------------------------------------------------------------------------------------------------------------
91*cdf0e10cSrcweir         double lcl_convertDateToDays( long const i_day, long const i_month, long const i_year )
92*cdf0e10cSrcweir         {
93*cdf0e10cSrcweir             long const nNullDateDays = ::Date::DateToDays( 1, 1, 1900 );
94*cdf0e10cSrcweir             long const nValueDateDays = ::Date::DateToDays( i_day, i_month, i_year );
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir             return nValueDateDays - nNullDateDays;
97*cdf0e10cSrcweir         }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir         //--------------------------------------------------------------------------------------------------------------
100*cdf0e10cSrcweir         double lcl_convertTimeToDays( long const i_hours, long const i_minutes, long const i_seconds, long const i_100thSeconds )
101*cdf0e10cSrcweir         {
102*cdf0e10cSrcweir             return Time( i_hours, i_minutes, i_seconds, i_100thSeconds ).GetTimeInDays();
103*cdf0e10cSrcweir         }
104*cdf0e10cSrcweir     }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir     //==================================================================================================================
107*cdf0e10cSrcweir     //= IValueNormalization
108*cdf0e10cSrcweir     //==================================================================================================================
109*cdf0e10cSrcweir     class SAL_NO_VTABLE IValueNormalization
110*cdf0e10cSrcweir     {
111*cdf0e10cSrcweir     public:
112*cdf0e10cSrcweir         virtual ~IValueNormalization() { }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         /** converts the given <code>Any</code> into a <code>double</code> value to be fed into a number formatter
115*cdf0e10cSrcweir         */
116*cdf0e10cSrcweir         virtual double convertToDouble( Any const & i_value ) const = 0;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir         /** returns the format key to be used for formatting values
119*cdf0e10cSrcweir         */
120*cdf0e10cSrcweir         virtual ::sal_Int32 getFormatKey() const = 0;
121*cdf0e10cSrcweir     };
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir     typedef ::boost::shared_ptr< IValueNormalization >                                      PValueNormalization;
124*cdf0e10cSrcweir     typedef ::std::hash_map< ::rtl::OUString, PValueNormalization, ::rtl::OUStringHash >    NormalizerCache;
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     //==================================================================================================================
127*cdf0e10cSrcweir     //= CellValueConversion_Data
128*cdf0e10cSrcweir     //==================================================================================================================
129*cdf0e10cSrcweir     struct CellValueConversion_Data
130*cdf0e10cSrcweir     {
131*cdf0e10cSrcweir         ::comphelper::ComponentContext const    aContext;
132*cdf0e10cSrcweir         Reference< XNumberFormatter >           xNumberFormatter;
133*cdf0e10cSrcweir         bool                                    bAttemptedFormatterCreation;
134*cdf0e10cSrcweir         NormalizerCache                         aNormalizers;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         CellValueConversion_Data( ::comphelper::ComponentContext const & i_context )
137*cdf0e10cSrcweir             :aContext( i_context )
138*cdf0e10cSrcweir             ,xNumberFormatter()
139*cdf0e10cSrcweir             ,bAttemptedFormatterCreation( false )
140*cdf0e10cSrcweir             ,aNormalizers()
141*cdf0e10cSrcweir         {
142*cdf0e10cSrcweir         }
143*cdf0e10cSrcweir     };
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     //==================================================================================================================
146*cdf0e10cSrcweir     //= StandardFormatNormalizer
147*cdf0e10cSrcweir     //==================================================================================================================
148*cdf0e10cSrcweir     class StandardFormatNormalizer : public IValueNormalization
149*cdf0e10cSrcweir     {
150*cdf0e10cSrcweir     protected:
151*cdf0e10cSrcweir         StandardFormatNormalizer( Reference< XNumberFormatter > const & i_formatter, ::sal_Int32 const i_numberFormatType )
152*cdf0e10cSrcweir             :m_nFormatKey( 0 )
153*cdf0e10cSrcweir         {
154*cdf0e10cSrcweir             try
155*cdf0e10cSrcweir             {
156*cdf0e10cSrcweir                 ENSURE_OR_THROW( i_formatter.is(), "StandardFormatNormalizer: no formatter!" );
157*cdf0e10cSrcweir                 Reference< XNumberFormatsSupplier > const xSupplier( i_formatter->getNumberFormatsSupplier(), UNO_SET_THROW );
158*cdf0e10cSrcweir                 Reference< XNumberFormatTypes > const xTypes( xSupplier->getNumberFormats(), UNO_QUERY_THROW );
159*cdf0e10cSrcweir                 m_nFormatKey = xTypes->getStandardFormat( i_numberFormatType, SvtSysLocale().GetLocale() );
160*cdf0e10cSrcweir             }
161*cdf0e10cSrcweir             catch( const Exception& )
162*cdf0e10cSrcweir             {
163*cdf0e10cSrcweir             	DBG_UNHANDLED_EXCEPTION();
164*cdf0e10cSrcweir             }
165*cdf0e10cSrcweir         }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir         virtual ::sal_Int32 getFormatKey() const
168*cdf0e10cSrcweir         {
169*cdf0e10cSrcweir             return m_nFormatKey;
170*cdf0e10cSrcweir         }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir     private:
173*cdf0e10cSrcweir         ::sal_Int32 m_nFormatKey;
174*cdf0e10cSrcweir     };
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     //==================================================================================================================
177*cdf0e10cSrcweir     //= DoubleNormalization
178*cdf0e10cSrcweir     //==================================================================================================================
179*cdf0e10cSrcweir     class DoubleNormalization : public StandardFormatNormalizer
180*cdf0e10cSrcweir     {
181*cdf0e10cSrcweir     public:
182*cdf0e10cSrcweir         DoubleNormalization( Reference< XNumberFormatter > const & i_formatter )
183*cdf0e10cSrcweir             :StandardFormatNormalizer( i_formatter, NumberFormat::NUMBER )
184*cdf0e10cSrcweir         {
185*cdf0e10cSrcweir         }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir         virtual double convertToDouble( Any const & i_value ) const
188*cdf0e10cSrcweir         {
189*cdf0e10cSrcweir             double returnValue(0);
190*cdf0e10cSrcweir             ::rtl::math::setNan( &returnValue );
191*cdf0e10cSrcweir             OSL_VERIFY( i_value >>= returnValue );
192*cdf0e10cSrcweir             return returnValue;
193*cdf0e10cSrcweir         }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir         virtual ~DoubleNormalization() { }
196*cdf0e10cSrcweir     };
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir     //==================================================================================================================
199*cdf0e10cSrcweir     //= IntegerNormalization
200*cdf0e10cSrcweir     //==================================================================================================================
201*cdf0e10cSrcweir     class IntegerNormalization : public StandardFormatNormalizer
202*cdf0e10cSrcweir     {
203*cdf0e10cSrcweir     public:
204*cdf0e10cSrcweir         IntegerNormalization( Reference< XNumberFormatter > const & i_formatter )
205*cdf0e10cSrcweir             :StandardFormatNormalizer( i_formatter, NumberFormat::NUMBER )
206*cdf0e10cSrcweir         {
207*cdf0e10cSrcweir         }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir         virtual ~IntegerNormalization() {}
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir         virtual double convertToDouble( Any const & i_value ) const
212*cdf0e10cSrcweir         {
213*cdf0e10cSrcweir             sal_Int64 value( 0 );
214*cdf0e10cSrcweir             OSL_VERIFY( i_value >>= value );
215*cdf0e10cSrcweir             return value;
216*cdf0e10cSrcweir         }
217*cdf0e10cSrcweir     };
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir     //==================================================================================================================
220*cdf0e10cSrcweir     //= BooleanNormalization
221*cdf0e10cSrcweir     //==================================================================================================================
222*cdf0e10cSrcweir     class BooleanNormalization : public StandardFormatNormalizer
223*cdf0e10cSrcweir     {
224*cdf0e10cSrcweir     public:
225*cdf0e10cSrcweir         BooleanNormalization( Reference< XNumberFormatter > const & i_formatter )
226*cdf0e10cSrcweir             :StandardFormatNormalizer( i_formatter, NumberFormat::LOGICAL )
227*cdf0e10cSrcweir         {
228*cdf0e10cSrcweir         }
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir         virtual ~BooleanNormalization() {}
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir         virtual double convertToDouble( Any const & i_value ) const
233*cdf0e10cSrcweir         {
234*cdf0e10cSrcweir             bool value( false );
235*cdf0e10cSrcweir             OSL_VERIFY( i_value >>= value );
236*cdf0e10cSrcweir             return value ? 1 : 0;
237*cdf0e10cSrcweir         }
238*cdf0e10cSrcweir     };
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir     //==================================================================================================================
241*cdf0e10cSrcweir     //= DateTimeNormalization
242*cdf0e10cSrcweir     //==================================================================================================================
243*cdf0e10cSrcweir     class DateTimeNormalization : public StandardFormatNormalizer
244*cdf0e10cSrcweir     {
245*cdf0e10cSrcweir     public:
246*cdf0e10cSrcweir         DateTimeNormalization( Reference< XNumberFormatter > const & i_formatter )
247*cdf0e10cSrcweir             :StandardFormatNormalizer( i_formatter, NumberFormat::DATETIME )
248*cdf0e10cSrcweir         {
249*cdf0e10cSrcweir         }
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir         virtual ~DateTimeNormalization() {}
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir         virtual double convertToDouble( Any const & i_value ) const
254*cdf0e10cSrcweir         {
255*cdf0e10cSrcweir             double returnValue(0);
256*cdf0e10cSrcweir             ::rtl::math::setNan( &returnValue );
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir             // extract actual UNO value
259*cdf0e10cSrcweir             DateTime aDateTimeValue;
260*cdf0e10cSrcweir             ENSURE_OR_RETURN( i_value >>= aDateTimeValue, "allowed for DateTime values only", returnValue );
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir             // date part
263*cdf0e10cSrcweir             returnValue = lcl_convertDateToDays( aDateTimeValue.Day, aDateTimeValue.Month, aDateTimeValue.Year );
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir             // time part
266*cdf0e10cSrcweir             returnValue += lcl_convertTimeToDays(
267*cdf0e10cSrcweir                 aDateTimeValue.Hours, aDateTimeValue.Minutes, aDateTimeValue.Seconds, aDateTimeValue.HundredthSeconds );
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir             // done
270*cdf0e10cSrcweir             return returnValue;
271*cdf0e10cSrcweir         }
272*cdf0e10cSrcweir     };
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir     //==================================================================================================================
275*cdf0e10cSrcweir     //= DateNormalization
276*cdf0e10cSrcweir     //==================================================================================================================
277*cdf0e10cSrcweir     class DateNormalization : public StandardFormatNormalizer
278*cdf0e10cSrcweir     {
279*cdf0e10cSrcweir     public:
280*cdf0e10cSrcweir         DateNormalization( Reference< XNumberFormatter > const & i_formatter )
281*cdf0e10cSrcweir             :StandardFormatNormalizer( i_formatter, NumberFormat::DATE )
282*cdf0e10cSrcweir         {
283*cdf0e10cSrcweir         }
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir         virtual ~DateNormalization() {}
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir         virtual double convertToDouble( Any const & i_value ) const
288*cdf0e10cSrcweir         {
289*cdf0e10cSrcweir             double returnValue(0);
290*cdf0e10cSrcweir             ::rtl::math::setNan( &returnValue );
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir             // extract
293*cdf0e10cSrcweir             UnoDate aDateValue;
294*cdf0e10cSrcweir             ENSURE_OR_RETURN( i_value >>= aDateValue, "allowed for Date values only", returnValue );
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir             // convert
297*cdf0e10cSrcweir             returnValue = lcl_convertDateToDays( aDateValue.Day, aDateValue.Month, aDateValue.Year );
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir             // done
300*cdf0e10cSrcweir             return returnValue;
301*cdf0e10cSrcweir         }
302*cdf0e10cSrcweir     };
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir     //==================================================================================================================
305*cdf0e10cSrcweir     //= TimeNormalization
306*cdf0e10cSrcweir     //==================================================================================================================
307*cdf0e10cSrcweir     class TimeNormalization : public StandardFormatNormalizer
308*cdf0e10cSrcweir     {
309*cdf0e10cSrcweir     public:
310*cdf0e10cSrcweir         TimeNormalization( Reference< XNumberFormatter > const & i_formatter )
311*cdf0e10cSrcweir             :StandardFormatNormalizer( i_formatter, NumberFormat::TIME )
312*cdf0e10cSrcweir         {
313*cdf0e10cSrcweir         }
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir         virtual ~TimeNormalization() {}
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir         virtual double convertToDouble( Any const & i_value ) const
318*cdf0e10cSrcweir         {
319*cdf0e10cSrcweir             double returnValue(0);
320*cdf0e10cSrcweir             ::rtl::math::setNan( &returnValue );
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir             // extract
323*cdf0e10cSrcweir             UnoTime aTimeValue;
324*cdf0e10cSrcweir             ENSURE_OR_RETURN( i_value >>= aTimeValue, "allowed for Time values only", returnValue );
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir             // convert
327*cdf0e10cSrcweir             returnValue += lcl_convertTimeToDays(
328*cdf0e10cSrcweir                 aTimeValue.Hours, aTimeValue.Minutes, aTimeValue.Seconds, aTimeValue.HundredthSeconds );
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir             // done
331*cdf0e10cSrcweir             return returnValue;
332*cdf0e10cSrcweir         }
333*cdf0e10cSrcweir     };
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir     //==================================================================================================================
336*cdf0e10cSrcweir     //= operations
337*cdf0e10cSrcweir     //==================================================================================================================
338*cdf0e10cSrcweir     namespace
339*cdf0e10cSrcweir     {
340*cdf0e10cSrcweir         //--------------------------------------------------------------------------------------------------------------
341*cdf0e10cSrcweir         bool lcl_ensureNumberFormatter( CellValueConversion_Data & io_data )
342*cdf0e10cSrcweir         {
343*cdf0e10cSrcweir             if ( io_data.bAttemptedFormatterCreation )
344*cdf0e10cSrcweir                 return io_data.xNumberFormatter.is();
345*cdf0e10cSrcweir             io_data.bAttemptedFormatterCreation = true;
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir             try
348*cdf0e10cSrcweir             {
349*cdf0e10cSrcweir                 // a number formatter
350*cdf0e10cSrcweir                 Reference< XNumberFormatter > const xFormatter(
351*cdf0e10cSrcweir                     io_data.aContext.createComponent( "com.sun.star.util.NumberFormatter" ), UNO_QUERY_THROW );
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir                 // a supplier of number formats
354*cdf0e10cSrcweir                 Sequence< Any > aInitArgs(1);
355*cdf0e10cSrcweir                 aInitArgs[0] <<= SvtSysLocale().GetLocale();
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir                 Reference< XNumberFormatsSupplier > const xSupplier(
358*cdf0e10cSrcweir                     io_data.aContext.createComponentWithArguments( "com.sun.star.util.NumberFormatsSupplier", aInitArgs ),
359*cdf0e10cSrcweir                     UNO_QUERY_THROW
360*cdf0e10cSrcweir                 );
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir                 // ensure a NullDate we will assume later on
363*cdf0e10cSrcweir                 UnoDate const aNullDate( 1, 1, 1900 );
364*cdf0e10cSrcweir                 Reference< XPropertySet > const xFormatSettings( xSupplier->getNumberFormatSettings(), UNO_SET_THROW );
365*cdf0e10cSrcweir                 xFormatSettings->setPropertyValue( ::rtl::OUString::createFromAscii( "NullDate" ), makeAny( aNullDate ) );
366*cdf0e10cSrcweir 
367*cdf0e10cSrcweir                 // knit
368*cdf0e10cSrcweir                 xFormatter->attachNumberFormatsSupplier( xSupplier );
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir                 // done
371*cdf0e10cSrcweir                 io_data.xNumberFormatter = xFormatter;
372*cdf0e10cSrcweir             }
373*cdf0e10cSrcweir             catch( const Exception& )
374*cdf0e10cSrcweir             {
375*cdf0e10cSrcweir             	DBG_UNHANDLED_EXCEPTION();
376*cdf0e10cSrcweir             }
377*cdf0e10cSrcweir 
378*cdf0e10cSrcweir             return io_data.xNumberFormatter.is();
379*cdf0e10cSrcweir         }
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir         //--------------------------------------------------------------------------------------------------------------
382*cdf0e10cSrcweir         bool lcl_getValueNormalizer( CellValueConversion_Data & io_data, Type const & i_valueType,
383*cdf0e10cSrcweir             PValueNormalization & o_formatter )
384*cdf0e10cSrcweir         {
385*cdf0e10cSrcweir             NormalizerCache::const_iterator pos = io_data.aNormalizers.find( i_valueType.getTypeName() );
386*cdf0e10cSrcweir             if ( pos == io_data.aNormalizers.end() )
387*cdf0e10cSrcweir             {
388*cdf0e10cSrcweir                 // never encountered this type before
389*cdf0e10cSrcweir                 o_formatter.reset();
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir                 ::rtl::OUString const sTypeName( i_valueType.getTypeName() );
392*cdf0e10cSrcweir                 TypeClass const eTypeClass = i_valueType.getTypeClass();
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir                 if ( sTypeName.equals( ::cppu::UnoType< DateTime >::get().getTypeName() ) )
395*cdf0e10cSrcweir                 {
396*cdf0e10cSrcweir                     o_formatter.reset( new DateTimeNormalization( io_data.xNumberFormatter ) );
397*cdf0e10cSrcweir                 }
398*cdf0e10cSrcweir                 else if ( sTypeName.equals( ::cppu::UnoType< UnoDate >::get().getTypeName() ) )
399*cdf0e10cSrcweir                 {
400*cdf0e10cSrcweir                     o_formatter.reset( new DateNormalization( io_data.xNumberFormatter ) );
401*cdf0e10cSrcweir                 }
402*cdf0e10cSrcweir                 else if ( sTypeName.equals( ::cppu::UnoType< UnoTime >::get().getTypeName() ) )
403*cdf0e10cSrcweir                 {
404*cdf0e10cSrcweir                     o_formatter.reset( new TimeNormalization( io_data.xNumberFormatter ) );
405*cdf0e10cSrcweir                 }
406*cdf0e10cSrcweir                 else if ( sTypeName.equals( ::cppu::UnoType< ::sal_Bool >::get().getTypeName() ) )
407*cdf0e10cSrcweir                 {
408*cdf0e10cSrcweir                     o_formatter.reset( new BooleanNormalization( io_data.xNumberFormatter ) );
409*cdf0e10cSrcweir                 }
410*cdf0e10cSrcweir                 else if (   sTypeName.equals( ::cppu::UnoType< double >::get().getTypeName() )
411*cdf0e10cSrcweir                         ||  sTypeName.equals( ::cppu::UnoType< float >::get().getTypeName() )
412*cdf0e10cSrcweir                         )
413*cdf0e10cSrcweir                 {
414*cdf0e10cSrcweir                     o_formatter.reset( new DoubleNormalization( io_data.xNumberFormatter ) );
415*cdf0e10cSrcweir                 }
416*cdf0e10cSrcweir                 else if (   ( eTypeClass == TypeClass_BYTE )
417*cdf0e10cSrcweir                         ||  ( eTypeClass == TypeClass_SHORT )
418*cdf0e10cSrcweir                         ||  ( eTypeClass == TypeClass_UNSIGNED_SHORT )
419*cdf0e10cSrcweir                         ||  ( eTypeClass == TypeClass_LONG )
420*cdf0e10cSrcweir                         ||  ( eTypeClass == TypeClass_UNSIGNED_LONG )
421*cdf0e10cSrcweir                         ||  ( eTypeClass == TypeClass_HYPER )
422*cdf0e10cSrcweir                         )
423*cdf0e10cSrcweir                 {
424*cdf0e10cSrcweir                     o_formatter.reset( new IntegerNormalization( io_data.xNumberFormatter ) );
425*cdf0e10cSrcweir                 }
426*cdf0e10cSrcweir                 else
427*cdf0e10cSrcweir                 {
428*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
429*cdf0e10cSrcweir                     ::rtl::OStringBuffer message( "lcl_getValueNormalizer: unsupported type '" );
430*cdf0e10cSrcweir                     message.append( ::rtl::OUStringToOString( sTypeName, RTL_TEXTENCODING_ASCII_US ) );
431*cdf0e10cSrcweir                     message.append( "'!" );
432*cdf0e10cSrcweir                     OSL_ENSURE( false, message.makeStringAndClear() );
433*cdf0e10cSrcweir #endif
434*cdf0e10cSrcweir                 }
435*cdf0e10cSrcweir                 io_data.aNormalizers[ sTypeName ] = o_formatter;
436*cdf0e10cSrcweir             }
437*cdf0e10cSrcweir             else
438*cdf0e10cSrcweir                 o_formatter = pos->second;
439*cdf0e10cSrcweir 
440*cdf0e10cSrcweir             return !!o_formatter;
441*cdf0e10cSrcweir         }
442*cdf0e10cSrcweir     }
443*cdf0e10cSrcweir 
444*cdf0e10cSrcweir     //==================================================================================================================
445*cdf0e10cSrcweir     //= CellValueConversion
446*cdf0e10cSrcweir     //==================================================================================================================
447*cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
448*cdf0e10cSrcweir     CellValueConversion::CellValueConversion( ::comphelper::ComponentContext const & i_context )
449*cdf0e10cSrcweir         :m_pData( new CellValueConversion_Data( i_context ) )
450*cdf0e10cSrcweir     {
451*cdf0e10cSrcweir     }
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
454*cdf0e10cSrcweir     CellValueConversion::~CellValueConversion()
455*cdf0e10cSrcweir     {
456*cdf0e10cSrcweir     }
457*cdf0e10cSrcweir 
458*cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
459*cdf0e10cSrcweir     ::rtl::OUString CellValueConversion::convertToString( const Any& i_value )
460*cdf0e10cSrcweir     {
461*cdf0e10cSrcweir         ::rtl::OUString sStringValue;
462*cdf0e10cSrcweir         if ( !i_value.hasValue() )
463*cdf0e10cSrcweir             return sStringValue;
464*cdf0e10cSrcweir 
465*cdf0e10cSrcweir         if ( ! ( i_value >>= sStringValue ) )
466*cdf0e10cSrcweir         {
467*cdf0e10cSrcweir             if ( lcl_ensureNumberFormatter( *m_pData ) )
468*cdf0e10cSrcweir             {
469*cdf0e10cSrcweir                 PValueNormalization pNormalizer;
470*cdf0e10cSrcweir                 if ( lcl_getValueNormalizer( *m_pData, i_value.getValueType(), pNormalizer ) )
471*cdf0e10cSrcweir                 {
472*cdf0e10cSrcweir                     try
473*cdf0e10cSrcweir                     {
474*cdf0e10cSrcweir                         double const formatterCompliantValue = pNormalizer->convertToDouble( i_value );
475*cdf0e10cSrcweir                         sal_Int32 const formatKey = pNormalizer->getFormatKey();
476*cdf0e10cSrcweir                         sStringValue = m_pData->xNumberFormatter->convertNumberToString(
477*cdf0e10cSrcweir                             formatKey, formatterCompliantValue );
478*cdf0e10cSrcweir                     }
479*cdf0e10cSrcweir                     catch( const Exception& )
480*cdf0e10cSrcweir                     {
481*cdf0e10cSrcweir                     	DBG_UNHANDLED_EXCEPTION();
482*cdf0e10cSrcweir                     }
483*cdf0e10cSrcweir                 }
484*cdf0e10cSrcweir             }
485*cdf0e10cSrcweir         }
486*cdf0e10cSrcweir 
487*cdf0e10cSrcweir         return sStringValue;
488*cdf0e10cSrcweir     }
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir //......................................................................................................................
491*cdf0e10cSrcweir } // namespace svt
492*cdf0e10cSrcweir //......................................................................................................................
493