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

Generated by: LCOV version 1.10