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

Generated by: LCOV version 1.10