LCOV - code coverage report
Current view: top level - libreoffice/i18npool/source/numberformatcode - numberformatcode.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 91 137 66.4 %
Date: 2012-12-17 Functions: 11 17 64.7 %
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             : 
      21             : #include <numberformatcode.hxx>
      22             : #include <com/sun/star/i18n/KNumberFormatUsage.hpp>
      23             : #include <com/sun/star/i18n/KNumberFormatType.hpp>
      24             : #include <com/sun/star/i18n/LocaleData.hpp>
      25             : #include <comphelper/componentcontext.hxx>
      26             : 
      27             : 
      28             : 
      29       15656 : NumberFormatCodeMapper::NumberFormatCodeMapper(
      30             :             const ::com::sun::star::uno::Reference <
      31             :                 ::com::sun::star::uno::XComponentContext >& rxContext )
      32             :         :
      33             :         mxContext( rxContext ),
      34       15656 :         bFormatsValid( false )
      35             : {
      36       15656 : }
      37             : 
      38             : 
      39       31312 : NumberFormatCodeMapper::~NumberFormatCodeMapper()
      40             : {
      41       31312 : }
      42             : 
      43             : 
      44             : ::com::sun::star::i18n::NumberFormatCode SAL_CALL
      45           0 : NumberFormatCodeMapper::getDefault( sal_Int16 formatType, sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
      46             : {
      47             : 
      48           0 :     ::rtl::OUString elementType = mapElementTypeShortToString(formatType);
      49           0 :     ::rtl::OUString elementUsage = mapElementUsageShortToString(formatUsage);
      50             : 
      51           0 :     getFormats( rLocale );
      52             : 
      53           0 :     for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
      54           0 :         if(aFormatSeq[i].isDefault && aFormatSeq[i].formatType == elementType &&
      55           0 :             aFormatSeq[i].formatUsage == elementUsage) {
      56             :             com::sun::star::i18n::NumberFormatCode anumberFormatCode(formatType,
      57             :                                                                     formatUsage,
      58           0 :                                                                     aFormatSeq[i].formatCode,
      59           0 :                                                                     aFormatSeq[i].formatName,
      60           0 :                                                                     aFormatSeq[i].formatKey,
      61           0 :                                                                     aFormatSeq[i].formatIndex,
      62           0 :                                                                     sal_True);
      63           0 :             return anumberFormatCode;
      64             :         }
      65             :     }
      66           0 :     com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
      67           0 :     return defaultNumberFormatCode;
      68             : }
      69             : 
      70             : 
      71             : 
      72             : ::com::sun::star::i18n::NumberFormatCode SAL_CALL
      73        8436 : NumberFormatCodeMapper::getFormatCode( sal_Int16 formatIndex, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
      74             : {
      75        8436 :     getFormats( rLocale );
      76             : 
      77        8436 :     for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
      78        8436 :         if(aFormatSeq[i].formatIndex == formatIndex) {
      79        8436 :             com::sun::star::i18n::NumberFormatCode anumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
      80        8436 :                                                                     mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
      81        8436 :                                                                     aFormatSeq[i].formatCode,
      82        8436 :                                                                     aFormatSeq[i].formatName,
      83        8436 :                                                                     aFormatSeq[i].formatKey,
      84        8436 :                                                                     aFormatSeq[i].formatIndex,
      85       59052 :                                                                     aFormatSeq[i].isDefault);
      86        8436 :             return anumberFormatCode;
      87             :         }
      88             :     }
      89           0 :     com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
      90           0 :     return defaultNumberFormatCode;
      91             : 
      92             : }
      93             : 
      94             : 
      95             : 
      96             : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
      97       14178 : NumberFormatCodeMapper::getAllFormatCode( sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
      98             : {
      99       14178 :     getFormats( rLocale );
     100             : 
     101             :     sal_Int32 i, count;
     102       14178 :     count = 0;
     103      787358 :     for(i = 0; i < aFormatSeq.getLength(); i++) {
     104      773180 :         sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
     105      773180 :         if( elementUsage == formatUsage)
     106      106622 :             count++;
     107             :     }
     108             : 
     109       14178 :     ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(count);
     110       14178 :     sal_Int32 j = 0;
     111      787358 :     for(i = 0; i < aFormatSeq.getLength(); i++) {
     112      773180 :         sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
     113      773180 :         if( elementUsage == formatUsage) {
     114      213244 :             seq[j] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
     115             :                                                             formatUsage,
     116      106622 :                                                             aFormatSeq[i].formatCode,
     117      106622 :                                                             aFormatSeq[i].formatName,
     118      106622 :                                                             aFormatSeq[i].formatKey,
     119      106622 :                                                             aFormatSeq[i].formatIndex,
     120      746354 :                                                             aFormatSeq[i].isDefault);
     121      106622 :             j++;
     122             :         }
     123             :     }
     124       14178 :     return seq;
     125             : 
     126             : }
     127             : 
     128             : 
     129             : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
     130         994 : NumberFormatCodeMapper::getAllFormatCodes( const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
     131             : {
     132         994 :     getFormats( rLocale );
     133             : 
     134         994 :     ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(aFormatSeq.getLength());
     135       60568 :     for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++)
     136             :     {
     137      119148 :         seq[i] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
     138       59574 :                                                         mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
     139       59574 :                                                         aFormatSeq[i].formatCode,
     140       59574 :                                                         aFormatSeq[i].formatName,
     141       59574 :                                                         aFormatSeq[i].formatKey,
     142       59574 :                                                         aFormatSeq[i].formatIndex,
     143      476592 :                                                         aFormatSeq[i].isDefault);
     144             :     }
     145         994 :     return seq;
     146             : }
     147             : 
     148             : 
     149             : // --- private implementation -----------------------------------------
     150             : 
     151       23608 : void NumberFormatCodeMapper::setupLocale( const ::com::sun::star::lang::Locale& rLocale )
     152             : {
     153       39624 :     if ( aLocale.Country    != rLocale.Country
     154        8064 :       || aLocale.Language   != rLocale.Language
     155        7952 :       || aLocale.Variant    != rLocale.Variant )
     156             :     {
     157       15656 :         bFormatsValid = false;
     158       15656 :         aLocale = rLocale;
     159             :     }
     160       23608 : }
     161             : 
     162             : 
     163       23608 : void NumberFormatCodeMapper::getFormats( const ::com::sun::star::lang::Locale& rLocale )
     164             : {
     165       23608 :     setupLocale( rLocale );
     166       23608 :     if ( !bFormatsValid )
     167             :     {
     168       15656 :         createLocaleDataObject();
     169       15656 :         if( !mxLocaleData.is() )
     170           0 :             aFormatSeq = ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::FormatElement > (0);
     171             :         else
     172       15656 :             aFormatSeq = mxLocaleData->getAllFormats( aLocale );
     173       15656 :         bFormatsValid = true;
     174             :     }
     175       23608 : }
     176             : 
     177             : 
     178             : ::rtl::OUString
     179           0 : NumberFormatCodeMapper::mapElementTypeShortToString(sal_Int16 formatType)
     180             : {
     181             : 
     182           0 :     switch ( formatType )
     183             :     {
     184             :         case com::sun::star::i18n::KNumberFormatType::SHORT :
     185           0 :             return ::rtl::OUString(  "short"  );
     186             :         case com::sun::star::i18n::KNumberFormatType::MEDIUM :
     187           0 :             return ::rtl::OUString(  "medium"  );
     188             :         case com::sun::star::i18n::KNumberFormatType::LONG :
     189           0 :             return ::rtl::OUString(  "long"  );
     190             :     }
     191           0 :     return ::rtl::OUString();
     192             : }
     193             : 
     194             : sal_Int16
     195      174632 : NumberFormatCodeMapper::mapElementTypeStringToShort(const ::rtl::OUString& formatType)
     196             : {
     197      174632 :     if ( formatType == "short" )
     198       30424 :         return com::sun::star::i18n::KNumberFormatType::SHORT;
     199      144208 :     if ( formatType == "medium" )
     200      114062 :         return com::sun::star::i18n::KNumberFormatType::MEDIUM;
     201       30146 :     if ( formatType == "long" )
     202       30146 :         return com::sun::star::i18n::KNumberFormatType::LONG;
     203             : 
     204           0 :     return com::sun::star::i18n::KNumberFormatType::SHORT;
     205             : }
     206             : 
     207             : ::rtl::OUString
     208           0 : NumberFormatCodeMapper::mapElementUsageShortToString(sal_Int16 formatUsage)
     209             : {
     210           0 :     switch ( formatUsage )
     211             :     {
     212             :         case com::sun::star::i18n::KNumberFormatUsage::DATE :
     213           0 :             return ::rtl::OUString(  "DATE"  );
     214             :         case com::sun::star::i18n::KNumberFormatUsage::TIME :
     215           0 :             return ::rtl::OUString(  "TIME"  );
     216             :         case com::sun::star::i18n::KNumberFormatUsage::DATE_TIME :
     217           0 :             return ::rtl::OUString(  "DATE_TIME"  );
     218             :         case com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER :
     219           0 :             return ::rtl::OUString(  "FIXED_NUMBER"  );
     220             :         case com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER :
     221           0 :             return ::rtl::OUString(  "FRACTION_NUMBER"  );
     222             :         case com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER :
     223           0 :             return ::rtl::OUString(  "PERCENT_NUMBER"  );
     224             :         case com::sun::star::i18n::KNumberFormatUsage::CURRENCY :
     225           0 :             return ::rtl::OUString(  "CURRENCY"  );
     226             :         case com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER :
     227           0 :             return ::rtl::OUString(  "SCIENTIFIC_NUMBER"  );
     228             :     }
     229           0 :     return ::rtl::OUString();
     230             : }
     231             : 
     232             : 
     233             : sal_Int16
     234     1614370 : NumberFormatCodeMapper::mapElementUsageStringToShort(const ::rtl::OUString& formatUsage)
     235             : {
     236     1614370 :     if ( formatUsage == "DATE" )
     237      791230 :         return com::sun::star::i18n::KNumberFormatUsage::DATE;
     238      823140 :     if ( formatUsage == "TIME" )
     239      208600 :         return com::sun::star::i18n::KNumberFormatUsage::TIME;
     240      614540 :     if ( formatUsage == "DATE_TIME" )
     241       60784 :         return com::sun::star::i18n::KNumberFormatUsage::DATE_TIME;
     242      553756 :     if ( formatUsage == "FIXED_NUMBER" )
     243      223208 :         return com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER;
     244      330548 :     if ( formatUsage == "FRACTION_NUMBER" )
     245           0 :         return com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER;
     246      330548 :     if ( formatUsage == "PERCENT_NUMBER" )
     247       59148 :         return  com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER;
     248      271400 :     if ( formatUsage == "CURRENCY" )
     249      212644 :         return com::sun::star::i18n::KNumberFormatUsage::CURRENCY;
     250       58756 :     if ( formatUsage == "SCIENTIFIC_NUMBER" )
     251       58756 :         return com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER;
     252             : 
     253           0 :     return 0;
     254             : }
     255             : 
     256             : 
     257             : void
     258       15656 : NumberFormatCodeMapper::createLocaleDataObject() {
     259             : 
     260       15656 :     if(mxLocaleData.is())
     261       15656 :         return;
     262             : 
     263       15656 :     mxLocaleData.set( com::sun::star::i18n::LocaleData::create(mxContext) );
     264             : }
     265             : 
     266             : ::rtl::OUString SAL_CALL
     267           0 : NumberFormatCodeMapper::getImplementationName(void)
     268             :                 throw( ::com::sun::star::uno::RuntimeException )
     269             : {
     270           0 :     return ::rtl::OUString("com.sun.star.i18n.NumberFormatCodeMapper");
     271             : }
     272             : 
     273             : const sal_Char cNumFormat[] = "com.sun.star.i18n.NumberFormatMapper";
     274             : 
     275             : sal_Bool SAL_CALL
     276           0 : NumberFormatCodeMapper::supportsService(const rtl::OUString& rServiceName)
     277             :                 throw( ::com::sun::star::uno::RuntimeException )
     278             : {
     279           0 :     return !rServiceName.compareToAscii(cNumFormat);
     280             : }
     281             : 
     282             : ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL
     283           0 : NumberFormatCodeMapper::getSupportedServiceNames(void) throw( ::com::sun::star::uno::RuntimeException )
     284             : {
     285           0 :     ::com::sun::star::uno::Sequence< ::rtl::OUString > aRet(1);
     286           0 :     aRet[0] = ::rtl::OUString::createFromAscii(cNumFormat);
     287           0 :     return aRet;
     288             : }
     289             : 
     290             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10