LCOV - code coverage report
Current view: top level - svtools/source/table - cellvalueconversion.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 76 144 52.8 %
Date: 2015-06-13 12:38:46 Functions: 16 40 40.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "cellvalueconversion.hxx"
      21             : 
      22             : #include <com/sun/star/util/NumberFormatter.hpp>
      23             : #include <com/sun/star/util/NumberFormatsSupplier.hpp>
      24             : #include <com/sun/star/beans/XPropertySet.hpp>
      25             : #include <com/sun/star/util/Date.hpp>
      26             : #include <com/sun/star/util/Time.hpp>
      27             : #include <com/sun/star/util/DateTime.hpp>
      28             : #include <com/sun/star/util/XNumberFormatTypes.hpp>
      29             : #include <com/sun/star/util/NumberFormat.hpp>
      30             : #include <rtl/strbuf.hxx>
      31             : #include <rtl/math.hxx>
      32             : #include <tools/date.hxx>
      33             : #include <tools/time.hxx>
      34             : #include <tools/diagnose_ex.h>
      35             : #include <unotools/syslocale.hxx>
      36             : #include <comphelper/processfactory.hxx>
      37             : 
      38             : #include <boost/shared_ptr.hpp>
      39             : #include <unordered_map>
      40             : 
      41             : namespace svt
      42             : {
      43             : 
      44             : 
      45             :     using namespace ::com::sun::star::uno;
      46             :     using ::com::sun::star::util::XNumberFormatter;
      47             :     using ::com::sun::star::util::XNumberFormatter2;
      48             :     using ::com::sun::star::util::NumberFormatter;
      49             :     using ::com::sun::star::util::XNumberFormatsSupplier;
      50             :     using ::com::sun::star::util::NumberFormatsSupplier;
      51             :     using ::com::sun::star::beans::XPropertySet;
      52             :     using ::com::sun::star::lang::Locale;
      53             :     using ::com::sun::star::util::DateTime;
      54             :     using ::com::sun::star::util::XNumberFormatTypes;
      55             : 
      56             :     namespace NumberFormat = ::com::sun::star::util::NumberFormat;
      57             : 
      58             :     typedef ::com::sun::star::util::Time UnoTime;
      59             :     typedef ::com::sun::star::util::Date UnoDate;
      60             : 
      61             : 
      62             :     //= helper
      63             : 
      64             :     namespace
      65             :     {
      66             : 
      67           0 :         double lcl_convertDateToDays( long const i_day, long const i_month, long const i_year )
      68             :         {
      69           0 :             long const nNullDateDays = ::Date::DateToDays( 1, 1, 1900 );
      70           0 :             long const nValueDateDays = ::Date::DateToDays( i_day, i_month, i_year );
      71             : 
      72           0 :             return nValueDateDays - nNullDateDays;
      73             :         }
      74             : 
      75             : 
      76           0 :         double lcl_convertTimeToDays( long const i_hours, long const i_minutes, long const i_seconds, long const i_100thSeconds )
      77             :         {
      78           0 :             return tools::Time( i_hours, i_minutes, i_seconds, i_100thSeconds ).GetTimeInDays();
      79             :         }
      80             :     }
      81             : 
      82             : 
      83             :     //= IValueNormalization
      84             : 
      85           1 :     class SAL_NO_VTABLE IValueNormalization
      86             :     {
      87             :     public:
      88           1 :         virtual ~IValueNormalization() { }
      89             : 
      90             :         /** converts the given <code>Any</code> into a <code>double</code> value to be fed into a number formatter
      91             :         */
      92             :         virtual double convertToDouble( Any const & i_value ) const = 0;
      93             : 
      94             :         /** returns the format key to be used for formatting values
      95             :         */
      96             :         virtual ::sal_Int32 getFormatKey() const = 0;
      97             :     };
      98             : 
      99             :     typedef ::boost::shared_ptr< IValueNormalization > PValueNormalization;
     100             :     typedef std::unordered_map< OUString, PValueNormalization, OUStringHash >    NormalizerCache;
     101             : 
     102             : 
     103             :     //= CellValueConversion_Data
     104             : 
     105           1 :     struct CellValueConversion_Data
     106             :     {
     107             :         Reference< XNumberFormatter >           xNumberFormatter;
     108             :         bool                                    bAttemptedFormatterCreation;
     109             :         NormalizerCache                         aNormalizers;
     110             : 
     111           1 :         CellValueConversion_Data()
     112             :             :xNumberFormatter()
     113             :             ,bAttemptedFormatterCreation( false )
     114           1 :             ,aNormalizers()
     115             :         {
     116           1 :         }
     117             :     };
     118             : 
     119             : 
     120             :     //= StandardFormatNormalizer
     121             : 
     122           1 :     class StandardFormatNormalizer : public IValueNormalization
     123             :     {
     124             :     protected:
     125           1 :         StandardFormatNormalizer( Reference< XNumberFormatter > const & i_formatter, ::sal_Int32 const i_numberFormatType )
     126           1 :             :m_nFormatKey( 0 )
     127             :         {
     128             :             try
     129             :             {
     130           1 :                 ENSURE_OR_THROW( i_formatter.is(), "StandardFormatNormalizer: no formatter!" );
     131           1 :                 Reference< XNumberFormatsSupplier > const xSupplier( i_formatter->getNumberFormatsSupplier(), UNO_SET_THROW );
     132           2 :                 Reference< XNumberFormatTypes > const xTypes( xSupplier->getNumberFormats(), UNO_QUERY_THROW );
     133           2 :                 m_nFormatKey = xTypes->getStandardFormat( i_numberFormatType, SvtSysLocale().GetLanguageTag().getLocale() );
     134             :             }
     135           0 :             catch( const Exception& )
     136             :             {
     137             :                 DBG_UNHANDLED_EXCEPTION();
     138             :             }
     139           1 :         }
     140             : 
     141          13 :         virtual ::sal_Int32 getFormatKey() const SAL_OVERRIDE
     142             :         {
     143          13 :             return m_nFormatKey;
     144             :         }
     145             : 
     146             :     private:
     147             :         ::sal_Int32 m_nFormatKey;
     148             :     };
     149             : 
     150             : 
     151             :     //= DoubleNormalization
     152             : 
     153             :     class DoubleNormalization : public StandardFormatNormalizer
     154             :     {
     155             :     public:
     156           0 :         explicit DoubleNormalization( Reference< XNumberFormatter > const & i_formatter )
     157           0 :             :StandardFormatNormalizer( i_formatter, NumberFormat::NUMBER )
     158             :         {
     159           0 :         }
     160             : 
     161           0 :         virtual double convertToDouble( Any const & i_value ) const SAL_OVERRIDE
     162             :         {
     163           0 :             double returnValue(0);
     164           0 :             ::rtl::math::setNan( &returnValue );
     165           0 :             OSL_VERIFY( i_value >>= returnValue );
     166           0 :             return returnValue;
     167             :         }
     168             : 
     169           0 :         virtual ~DoubleNormalization() { }
     170             :     };
     171             : 
     172             : 
     173             :     //= IntegerNormalization
     174             : 
     175             :     class IntegerNormalization : public StandardFormatNormalizer
     176             :     {
     177             :     public:
     178           1 :         explicit IntegerNormalization( Reference< XNumberFormatter > const & i_formatter )
     179           1 :             :StandardFormatNormalizer( i_formatter, NumberFormat::NUMBER )
     180             :         {
     181           1 :         }
     182             : 
     183           2 :         virtual ~IntegerNormalization() {}
     184             : 
     185          13 :         virtual double convertToDouble( Any const & i_value ) const SAL_OVERRIDE
     186             :         {
     187          13 :             sal_Int64 value( 0 );
     188          13 :             OSL_VERIFY( i_value >>= value );
     189          13 :             return value;
     190             :         }
     191             :     };
     192             : 
     193             : 
     194             :     //= BooleanNormalization
     195             : 
     196             :     class BooleanNormalization : public StandardFormatNormalizer
     197             :     {
     198             :     public:
     199           0 :         explicit BooleanNormalization( Reference< XNumberFormatter > const & i_formatter )
     200           0 :             :StandardFormatNormalizer( i_formatter, NumberFormat::LOGICAL )
     201             :         {
     202           0 :         }
     203             : 
     204           0 :         virtual ~BooleanNormalization() {}
     205             : 
     206           0 :         virtual double convertToDouble( Any const & i_value ) const SAL_OVERRIDE
     207             :         {
     208           0 :             bool value( false );
     209           0 :             OSL_VERIFY( i_value >>= value );
     210           0 :             return value ? 1 : 0;
     211             :         }
     212             :     };
     213             : 
     214             : 
     215             :     //= DateTimeNormalization
     216             : 
     217             :     class DateTimeNormalization : public StandardFormatNormalizer
     218             :     {
     219             :     public:
     220           0 :         explicit DateTimeNormalization( Reference< XNumberFormatter > const & i_formatter )
     221           0 :             :StandardFormatNormalizer( i_formatter, NumberFormat::DATETIME )
     222             :         {
     223           0 :         }
     224             : 
     225           0 :         virtual ~DateTimeNormalization() {}
     226             : 
     227           0 :         virtual double convertToDouble( Any const & i_value ) const SAL_OVERRIDE
     228             :         {
     229           0 :             double returnValue(0);
     230           0 :             ::rtl::math::setNan( &returnValue );
     231             : 
     232             :             // extract actual UNO value
     233           0 :             DateTime aDateTimeValue;
     234           0 :             ENSURE_OR_RETURN( i_value >>= aDateTimeValue, "allowed for DateTime values only", returnValue );
     235             : 
     236             :             // date part
     237           0 :             returnValue = lcl_convertDateToDays( aDateTimeValue.Day, aDateTimeValue.Month, aDateTimeValue.Year );
     238             : 
     239             :             // time part
     240             :             returnValue += lcl_convertTimeToDays(
     241           0 :                 aDateTimeValue.Hours, aDateTimeValue.Minutes, aDateTimeValue.Seconds, aDateTimeValue.NanoSeconds );
     242             : 
     243             :             // done
     244           0 :             return returnValue;
     245             :         }
     246             :     };
     247             : 
     248             : 
     249             :     //= DateNormalization
     250             : 
     251             :     class DateNormalization : public StandardFormatNormalizer
     252             :     {
     253             :     public:
     254           0 :         explicit DateNormalization( Reference< XNumberFormatter > const & i_formatter )
     255           0 :             :StandardFormatNormalizer( i_formatter, NumberFormat::DATE )
     256             :         {
     257           0 :         }
     258             : 
     259           0 :         virtual ~DateNormalization() {}
     260             : 
     261           0 :         virtual double convertToDouble( Any const & i_value ) const SAL_OVERRIDE
     262             :         {
     263           0 :             double returnValue(0);
     264           0 :             ::rtl::math::setNan( &returnValue );
     265             : 
     266             :             // extract
     267           0 :             UnoDate aDateValue;
     268           0 :             ENSURE_OR_RETURN( i_value >>= aDateValue, "allowed for Date values only", returnValue );
     269             : 
     270             :             // convert
     271           0 :             returnValue = lcl_convertDateToDays( aDateValue.Day, aDateValue.Month, aDateValue.Year );
     272             : 
     273             :             // done
     274           0 :             return returnValue;
     275             :         }
     276             :     };
     277             : 
     278             : 
     279             :     //= TimeNormalization
     280             : 
     281             :     class TimeNormalization : public StandardFormatNormalizer
     282             :     {
     283             :     public:
     284           0 :         explicit TimeNormalization( Reference< XNumberFormatter > const & i_formatter )
     285           0 :             :StandardFormatNormalizer( i_formatter, NumberFormat::TIME )
     286             :         {
     287           0 :         }
     288             : 
     289           0 :         virtual ~TimeNormalization() {}
     290             : 
     291           0 :         virtual double convertToDouble( Any const & i_value ) const SAL_OVERRIDE
     292             :         {
     293           0 :             double returnValue(0);
     294           0 :             ::rtl::math::setNan( &returnValue );
     295             : 
     296             :             // extract
     297           0 :             UnoTime aTimeValue;
     298           0 :             ENSURE_OR_RETURN( i_value >>= aTimeValue, "allowed for tools::Time values only", returnValue );
     299             : 
     300             :             // convert
     301             :             returnValue += lcl_convertTimeToDays(
     302           0 :                 aTimeValue.Hours, aTimeValue.Minutes, aTimeValue.Seconds, aTimeValue.NanoSeconds );
     303             : 
     304             :             // done
     305           0 :             return returnValue;
     306             :         }
     307             :     };
     308             : 
     309             : 
     310             :     //= operations
     311             : 
     312             :     namespace
     313             :     {
     314             : 
     315          13 :         bool lcl_ensureNumberFormatter( CellValueConversion_Data & io_data )
     316             :         {
     317          13 :             if ( io_data.bAttemptedFormatterCreation )
     318          12 :                 return io_data.xNumberFormatter.is();
     319           1 :             io_data.bAttemptedFormatterCreation = true;
     320             : 
     321             :             try
     322             :             {
     323           1 :                 Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
     324             :                 // a number formatter
     325           2 :                 Reference< XNumberFormatter > const xFormatter( NumberFormatter::create( xContext ), UNO_QUERY_THROW );
     326             : 
     327             :                 // a supplier of number formats
     328           2 :                 Locale aLocale = SvtSysLocale().GetLanguageTag().getLocale();
     329             : 
     330             :                 Reference< XNumberFormatsSupplier > const xSupplier =
     331           2 :                     NumberFormatsSupplier::createWithLocale( xContext, aLocale );
     332             : 
     333             :                 // ensure a NullDate we will assume later on
     334           1 :                 UnoDate const aNullDate( 1, 1, 1900 );
     335           2 :                 Reference< XPropertySet > const xFormatSettings( xSupplier->getNumberFormatSettings(), UNO_SET_THROW );
     336           1 :                 xFormatSettings->setPropertyValue( "NullDate", makeAny( aNullDate ) );
     337             : 
     338             :                 // knit
     339           1 :                 xFormatter->attachNumberFormatsSupplier( xSupplier );
     340             : 
     341             :                 // done
     342           2 :                 io_data.xNumberFormatter = xFormatter;
     343             :             }
     344           0 :             catch( const Exception& )
     345             :             {
     346             :                 DBG_UNHANDLED_EXCEPTION();
     347             :             }
     348             : 
     349           1 :             return io_data.xNumberFormatter.is();
     350             :         }
     351             : 
     352             : 
     353          13 :         bool lcl_getValueNormalizer( CellValueConversion_Data & io_data, Type const & i_valueType,
     354             :             PValueNormalization & o_formatter )
     355             :         {
     356          13 :             NormalizerCache::const_iterator pos = io_data.aNormalizers.find( i_valueType.getTypeName() );
     357          13 :             if ( pos == io_data.aNormalizers.end() )
     358             :             {
     359             :                 // never encountered this type before
     360           1 :                 o_formatter.reset();
     361             : 
     362           1 :                 OUString const sTypeName( i_valueType.getTypeName() );
     363           1 :                 TypeClass const eTypeClass = i_valueType.getTypeClass();
     364             : 
     365           1 :                 if ( sTypeName.equals( ::cppu::UnoType< DateTime >::get().getTypeName() ) )
     366             :                 {
     367           0 :                     o_formatter.reset( new DateTimeNormalization( io_data.xNumberFormatter ) );
     368             :                 }
     369           1 :                 else if ( sTypeName.equals( ::cppu::UnoType< UnoDate >::get().getTypeName() ) )
     370             :                 {
     371           0 :                     o_formatter.reset( new DateNormalization( io_data.xNumberFormatter ) );
     372             :                 }
     373           1 :                 else if ( sTypeName.equals( ::cppu::UnoType< UnoTime >::get().getTypeName() ) )
     374             :                 {
     375           0 :                     o_formatter.reset( new TimeNormalization( io_data.xNumberFormatter ) );
     376             :                 }
     377           1 :                 else if ( sTypeName.equals( ::cppu::UnoType< sal_Bool >::get().getTypeName() ) )
     378             :                 {
     379           0 :                     o_formatter.reset( new BooleanNormalization( io_data.xNumberFormatter ) );
     380             :                 }
     381           4 :                 else if (   sTypeName.equals( ::cppu::UnoType< double >::get().getTypeName() )
     382           4 :                         ||  sTypeName.equals( ::cppu::UnoType< float >::get().getTypeName() )
     383             :                         )
     384             :                 {
     385           0 :                     o_formatter.reset( new DoubleNormalization( io_data.xNumberFormatter ) );
     386             :                 }
     387           1 :                 else if (   ( eTypeClass == TypeClass_BYTE )
     388           1 :                         ||  ( eTypeClass == TypeClass_SHORT )
     389           1 :                         ||  ( eTypeClass == TypeClass_UNSIGNED_SHORT )
     390           1 :                         ||  ( eTypeClass == TypeClass_LONG )
     391           0 :                         ||  ( eTypeClass == TypeClass_UNSIGNED_LONG )
     392           0 :                         ||  ( eTypeClass == TypeClass_HYPER )
     393             :                         )
     394             :                 {
     395           1 :                     o_formatter.reset( new IntegerNormalization( io_data.xNumberFormatter ) );
     396             :                 }
     397             :                 else
     398             :                 {
     399             :                     SAL_WARN( "svtools.table", "unsupported type '" << sTypeName << "'!" );
     400             :                 }
     401           1 :                 io_data.aNormalizers[ sTypeName ] = o_formatter;
     402             :             }
     403             :             else
     404          12 :                 o_formatter = pos->second;
     405             : 
     406          13 :             return !!o_formatter;
     407             :         }
     408             :     }
     409             : 
     410             : 
     411             :     //= CellValueConversion
     412             : 
     413             : 
     414           1 :     CellValueConversion::CellValueConversion()
     415           1 :         :m_pData( new CellValueConversion_Data )
     416             :     {
     417           1 :     }
     418             : 
     419             : 
     420           1 :     CellValueConversion::~CellValueConversion()
     421             :     {
     422           1 :     }
     423             : 
     424             : 
     425          13 :     OUString CellValueConversion::convertToString( const Any& i_value )
     426             :     {
     427          13 :         OUString sStringValue;
     428          13 :         if ( !i_value.hasValue() )
     429           0 :             return sStringValue;
     430             : 
     431          13 :         if ( ! ( i_value >>= sStringValue ) )
     432             :         {
     433          13 :             if ( lcl_ensureNumberFormatter( *m_pData ) )
     434             :             {
     435          13 :                 PValueNormalization pNormalizer;
     436          13 :                 if ( lcl_getValueNormalizer( *m_pData, i_value.getValueType(), pNormalizer ) )
     437             :                 {
     438             :                     try
     439             :                     {
     440          13 :                         double const formatterCompliantValue = pNormalizer->convertToDouble( i_value );
     441          13 :                         sal_Int32 const formatKey = pNormalizer->getFormatKey();
     442          39 :                         sStringValue = m_pData->xNumberFormatter->convertNumberToString(
     443          26 :                             formatKey, formatterCompliantValue );
     444             :                     }
     445           0 :                     catch( const Exception& )
     446             :                     {
     447             :                         DBG_UNHANDLED_EXCEPTION();
     448             :                     }
     449          13 :                 }
     450             :             }
     451             :         }
     452             : 
     453          13 :         return sStringValue;
     454             :     }
     455             : 
     456             : 
     457             : } // namespace svt
     458             : 
     459             : 
     460             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11