LCOV - code coverage report
Current view: top level - libreoffice/forms/source/xforms - convert.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 258 0.0 %
Date: 2012-12-17 Functions: 0 41 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             : 
      21             : #include "convert.hxx"
      22             : 
      23             : #include "unohelper.hxx"
      24             : #include <memory>
      25             : #include <algorithm>
      26             : #include <functional>
      27             : #include <o3tl/compat_functional.hxx>
      28             : #include <rtl/math.hxx>
      29             : #include <rtl/ustrbuf.hxx>
      30             : #include <tools/date.hxx>
      31             : #include <com/sun/star/uno/Type.hxx>
      32             : #include <com/sun/star/xsd/WhiteSpaceTreatment.hpp>
      33             : #include <com/sun/star/util/Date.hpp>
      34             : #include <com/sun/star/util/DateTime.hpp>
      35             : #include <com/sun/star/util/Time.hpp>
      36             : 
      37             : using xforms::Convert;
      38             : using ::rtl::OUString;
      39             : using ::rtl::OUStringBuffer;
      40             : using com::sun::star::uno::Any;
      41             : using com::sun::star::uno::makeAny;
      42             : using com::sun::star::util::Time;
      43             : using namespace std;
      44             : using namespace o3tl;
      45             : 
      46             : typedef com::sun::star::util::Date UNODate;
      47             : typedef com::sun::star::util::Time UNOTime;
      48             : typedef com::sun::star::util::DateTime UNODateTime;
      49             : 
      50           0 : Convert::Convert()
      51           0 :     : maMap()
      52             : {
      53           0 :     init();
      54           0 : }
      55             : 
      56             : #define ADD_ENTRY(XCONVERT,TYPE) XCONVERT->maMap[ getCppuType( static_cast<TYPE*>( NULL ) ) ] = Convert_t( &lcl_toXSD_##TYPE, &lcl_toAny_##TYPE )
      57             : 
      58             : namespace
      59             : {
      60             :     // ========================================================================
      61           0 :     struct StringToken
      62             :     {
      63             :     private:
      64             :         ::rtl::OUString m_sString;
      65             :         sal_Int32       m_nTokenStart;
      66             :         sal_Int32       m_nTokenEnd;
      67             : 
      68             :     public:
      69           0 :         StringToken() : m_sString(), m_nTokenStart( 0 ), m_nTokenEnd( 0 ) { }
      70             :         StringToken( const ::rtl::OUString& _rString, sal_Int32 _nTokenStart, sal_Int32 _nTokenEnd );
      71             :         StringToken( const StringToken& );
      72             :         StringToken& operator=( const StringToken& );
      73             : 
      74           0 :         inline  bool                isEmpty() const { return m_nTokenEnd <= m_nTokenStart; }
      75             :         inline  sal_Int32           getLength() const { return isEmpty() ? 0 : m_nTokenEnd - m_nTokenStart - 1; }
      76           0 :         inline  const sal_Unicode*  begin() const { return m_sString.getStr() + m_nTokenStart; }
      77           0 :         inline  const sal_Unicode*  end() const { return m_sString.getStr() + m_nTokenEnd; }
      78             : 
      79             :         bool    toInt32( sal_Int32& _rValue ) const;
      80             :     };
      81             : 
      82             :     // ------------------------------------------------------------------------
      83           0 :     StringToken::StringToken( const ::rtl::OUString& _rString, sal_Int32 _nTokenStart, sal_Int32 _nTokenEnd )
      84             :         :m_sString( _rString )
      85             :         ,m_nTokenStart( _nTokenStart )
      86           0 :         ,m_nTokenEnd( _nTokenEnd )
      87             :     {
      88             :         OSL_ENSURE( ( m_nTokenStart >= 0 ) && ( m_nTokenStart <= m_sString.getLength() ), "StringToken::StringToken: invalid token start!" );
      89             :         OSL_ENSURE( ( m_nTokenEnd >= 0 ) && ( m_nTokenEnd <= m_sString.getLength() ), "StringToken::StringToken: invalid token end!" );
      90           0 :     }
      91             : 
      92             :     // ------------------------------------------------------------------------
      93           0 :     StringToken::StringToken( const StringToken& _rRHS )
      94             :     {
      95           0 :         *this = _rRHS;
      96           0 :     }
      97             : 
      98             :     // ------------------------------------------------------------------------
      99           0 :     StringToken& StringToken::operator=( const StringToken& _rRHS )
     100             :     {
     101           0 :         if ( this == &_rRHS )
     102           0 :             return *this;
     103             : 
     104           0 :         m_sString = _rRHS.m_sString;
     105           0 :         m_nTokenStart = _rRHS.m_nTokenStart;
     106           0 :         m_nTokenEnd = _rRHS.m_nTokenEnd;
     107             : 
     108           0 :         return *this;
     109             :     }
     110             : 
     111             :     // ------------------------------------------------------------------------
     112           0 :     bool StringToken::toInt32( sal_Int32& _rValue ) const
     113             :     {
     114           0 :         if ( isEmpty() )
     115           0 :             return false;
     116             : 
     117           0 :         _rValue = 0;
     118           0 :         const sal_Unicode* pStr = begin();
     119           0 :         while ( pStr < end() )
     120             :         {
     121           0 :             if ( ( *pStr < '0' ) || ( *pStr > '9' ) )
     122           0 :                 return false;
     123             : 
     124           0 :             _rValue *= 10;
     125           0 :             _rValue += ( *pStr - '0' );
     126             : 
     127           0 :             ++pStr;
     128             :         }
     129             : 
     130           0 :         return true;
     131             :     }
     132             : 
     133             :     // ========================================================================
     134           0 :     class StringTokenizer
     135             :     {
     136             :     private:
     137             :         ::rtl::OUString     m_sString;
     138             :         const sal_Unicode   m_nTokenSeparator;
     139             :         sal_Int32           m_nTokenStart;
     140             : 
     141             :     public:
     142             :         /** constructs a tokenizer
     143             :             @param _rString             the string to tokenize
     144             :             @param _nTokenSeparator     the token value. May be 0, in this case the tokenizer
     145             :                                         will recognize exactly one token, being the whole string.
     146             :                                         This may make sense if you want to apply <type>StringToken</type>
     147             :                                         methods to a whole string.
     148             :         */
     149             :         StringTokenizer( const ::rtl::OUString& _rString, sal_Unicode _nTokenSeparator = ';' );
     150             : 
     151             :         /// resets the tokenizer to the beginning of the string
     152             :         void    reset();
     153             : 
     154             :         /// determines whether there is a next token
     155             :         bool    hasNextToken() const;
     156             : 
     157             :         /// retrieves the next token
     158             :         StringToken
     159             :                 getNextToken();
     160             :     };
     161             : 
     162             :     // ------------------------------------------------------------------------
     163           0 :     StringTokenizer::StringTokenizer( const ::rtl::OUString& _rString, sal_Unicode _nTokenSeparator )
     164             :         :m_sString( _rString )
     165           0 :         ,m_nTokenSeparator( _nTokenSeparator )
     166             :     {
     167           0 :         reset();
     168           0 :     }
     169             : 
     170             :     // ------------------------------------------------------------------------
     171           0 :     void StringTokenizer::reset()
     172             :     {
     173           0 :         m_nTokenStart = 0;
     174           0 :     }
     175             : 
     176             :     // ------------------------------------------------------------------------
     177           0 :     bool StringTokenizer::hasNextToken() const
     178             :     {
     179           0 :         return ( m_nTokenStart < m_sString.getLength() );
     180             :     }
     181             : 
     182             :     // ------------------------------------------------------------------------
     183           0 :     StringToken StringTokenizer::getNextToken()
     184             :     {
     185             :         OSL_PRECOND( hasNextToken(), "StringTokenizer::getNextToken: there is no next token!" );
     186           0 :         if ( !hasNextToken() )
     187           0 :             return StringToken();
     188             : 
     189             :         // determine the end of the current token
     190           0 :         sal_Int32 nTokenEnd = m_nTokenSeparator ? m_sString.indexOf( m_nTokenSeparator, m_nTokenStart ) : m_sString.getLength();
     191           0 :         bool bLastToken = !m_nTokenSeparator || ( nTokenEnd == -1 );
     192             : 
     193             :         // construct a new token
     194           0 :         StringToken aToken( m_sString, m_nTokenStart, bLastToken ? m_sString.getLength() : nTokenEnd );
     195             :         // advance
     196           0 :         m_nTokenStart = bLastToken ? m_sString.getLength() : nTokenEnd + 1;
     197             :         // outta here
     198           0 :         return aToken;
     199             :     }
     200             : 
     201             :     // ========================================================================
     202             :     // ------------------------------------------------------------------------
     203           0 :     OUString lcl_toXSD_OUString( const Any& rAny )
     204           0 :     { OUString sStr; rAny >>= sStr; return sStr; }
     205             : 
     206             :     // ------------------------------------------------------------------------
     207           0 :     Any lcl_toAny_OUString( const OUString& rStr )
     208           0 :     { Any aAny; aAny <<= rStr; return aAny; }
     209             : 
     210             :     // ------------------------------------------------------------------------
     211           0 :     OUString lcl_toXSD_bool( const Any& rAny )
     212           0 :     { bool b = false; rAny >>= b; return b ? OUSTRING("true") : OUSTRING("false"); }
     213             : 
     214             :     // ------------------------------------------------------------------------
     215           0 :     Any lcl_toAny_bool( const OUString& rStr )
     216             :     {
     217           0 :         bool b = ( rStr == OUSTRING("true")  ||  rStr == OUSTRING("1") );
     218           0 :         return makeAny( b );
     219             :     }
     220             : 
     221             :     // ------------------------------------------------------------------------
     222           0 :     OUString lcl_toXSD_double( const Any& rAny )
     223             :     {
     224           0 :         double f = 0.0;
     225           0 :         rAny >>= f;
     226             : 
     227           0 :         return rtl::math::isFinite( f )
     228             :             ? rtl::math::doubleToUString( f, rtl_math_StringFormat_Automatic,
     229             :                                         rtl_math_DecimalPlaces_Max, '.',
     230             :                                         sal_True )
     231           0 :             : OUString();
     232             :     }
     233             : 
     234             :     // ------------------------------------------------------------------------
     235           0 :     Any lcl_toAny_double( const OUString& rString )
     236             :     {
     237             :         rtl_math_ConversionStatus eStatus;
     238             :         double f = rtl::math::stringToDouble(
     239           0 :             rString, sal_Unicode('.'), sal_Unicode(','), &eStatus, NULL );
     240           0 :         return ( eStatus == rtl_math_ConversionStatus_Ok ) ? makeAny( f ) : Any();
     241             :     }
     242             : 
     243             :     // ------------------------------------------------------------------------
     244           0 :     void lcl_appendInt32ToBuffer( const sal_Int32 _nValue, ::rtl::OUStringBuffer& _rBuffer, sal_Int16 _nMinDigits )
     245             :     {
     246           0 :         if ( ( _nMinDigits >= 4 ) && ( _nValue < 1000 ) )
     247           0 :             _rBuffer.append( (sal_Unicode)'0' );
     248           0 :         if ( ( _nMinDigits >= 3 ) && ( _nValue < 100 ) )
     249           0 :             _rBuffer.append( (sal_Unicode)'0' );
     250           0 :         if ( ( _nMinDigits >= 2 ) && ( _nValue < 10 ) )
     251           0 :             _rBuffer.append( (sal_Unicode)'0' );
     252           0 :         _rBuffer.append( _nValue );
     253           0 :     }
     254             : 
     255             :     // ------------------------------------------------------------------------
     256           0 :     OUString lcl_toXSD_UNODate_typed( const UNODate& rDate )
     257             :     {
     258             : 
     259           0 :         ::rtl::OUStringBuffer sInfo;
     260           0 :         lcl_appendInt32ToBuffer( rDate.Year, sInfo, 4 );
     261           0 :         sInfo.appendAscii( "-" );
     262           0 :         lcl_appendInt32ToBuffer( rDate.Month, sInfo, 2 );
     263           0 :         sInfo.appendAscii( "-" );
     264           0 :         lcl_appendInt32ToBuffer( rDate.Day, sInfo, 2 );
     265             : 
     266           0 :         return sInfo.makeStringAndClear();
     267             :     }
     268             : 
     269             :     // ------------------------------------------------------------------------
     270           0 :     OUString lcl_toXSD_UNODate( const Any& rAny )
     271             :     {
     272           0 :         UNODate aDate;
     273           0 :         OSL_VERIFY( rAny >>= aDate );
     274           0 :         return lcl_toXSD_UNODate_typed( aDate );
     275             :     }
     276             : 
     277             :     // ------------------------------------------------------------------------
     278           0 :     UNODate lcl_toUNODate( const OUString& rString )
     279             :     {
     280           0 :         bool bWellformed = true;
     281             : 
     282           0 :         UNODate aDate( 1, 1, 1900 );
     283             : 
     284           0 :         sal_Int32 nToken = 0;
     285           0 :         StringTokenizer aTokenizer( rString, '-' );
     286           0 :         while ( aTokenizer.hasNextToken() )
     287             :         {
     288           0 :             sal_Int32 nTokenValue = 0;
     289           0 :             if ( !aTokenizer.getNextToken().toInt32( nTokenValue ) )
     290             :             {
     291           0 :                 bWellformed = false;
     292             :                 break;
     293             :             }
     294             : 
     295           0 :             if ( nToken == 0 )
     296           0 :                 aDate.Year = (sal_uInt16)nTokenValue;
     297           0 :             else if ( nToken == 1 )
     298           0 :                 aDate.Month = (sal_uInt16)nTokenValue;
     299           0 :             else if ( nToken == 2 )
     300           0 :                 aDate.Day = (sal_uInt16)nTokenValue;
     301             :             else
     302             :             {
     303           0 :                 bWellformed = false;
     304             :                 break;
     305             :             }
     306           0 :             ++nToken;
     307             :         }
     308             : 
     309             :         // sanity checks
     310           0 :         if ( ( aDate.Year > 9999 ) || ( aDate.Month < 1 ) || ( aDate.Month > 12 ) || ( aDate.Day < 1 ) || ( aDate.Day > 31 ) )
     311           0 :             bWellformed = false;
     312             :         else
     313             :         {
     314           0 :             ::Date aDateCheck( 1, aDate.Month, aDate.Year );
     315           0 :             if ( aDate.Day > aDateCheck.GetDaysInMonth() )
     316           0 :                 bWellformed = false;
     317             :         }
     318             : 
     319             :         // all okay?
     320           0 :         if ( !bWellformed )
     321           0 :             return UNODate( 1, 1, 1900 );
     322             : 
     323           0 :         return aDate;
     324             :     }
     325             : 
     326             :     // ------------------------------------------------------------------------
     327           0 :     Any lcl_toAny_UNODate( const OUString& rString )
     328             :     {
     329           0 :         return makeAny( lcl_toUNODate( rString ) );
     330             :     }
     331             : 
     332             :     // ------------------------------------------------------------------------
     333           0 :     OUString lcl_toXSD_UNOTime_typed( const UNOTime& rTime )
     334             :     {
     335             : 
     336           0 :         ::rtl::OUStringBuffer sInfo;
     337           0 :         lcl_appendInt32ToBuffer( rTime.Hours, sInfo, 2 );
     338           0 :         sInfo.appendAscii( ":" );
     339           0 :         lcl_appendInt32ToBuffer( rTime.Minutes, sInfo, 2 );
     340           0 :         sInfo.appendAscii( ":" );
     341           0 :         lcl_appendInt32ToBuffer( rTime.Seconds, sInfo, 2 );
     342           0 :         if ( rTime.HundredthSeconds )
     343             :         {
     344           0 :             sInfo.appendAscii( "." );
     345           0 :             lcl_appendInt32ToBuffer( rTime.HundredthSeconds, sInfo, 2 );
     346             :         }
     347             : 
     348           0 :         return sInfo.makeStringAndClear();
     349             :     }
     350             : 
     351             :     // ------------------------------------------------------------------------
     352           0 :     OUString lcl_toXSD_UNOTime( const Any& rAny )
     353             :     {
     354           0 :         UNOTime aTime;
     355           0 :         OSL_VERIFY( rAny >>= aTime );
     356           0 :         return lcl_toXSD_UNOTime_typed( aTime );
     357             :     }
     358             : 
     359             :     // ------------------------------------------------------------------------
     360           0 :     UNOTime lcl_toUNOTime( const OUString& rString )
     361             :     {
     362           0 :         bool bWellformed = true;
     363             : 
     364           0 :         UNOTime aTime( 0, 0, 0, 0 );
     365             : 
     366           0 :         ::rtl::OUString sString( rString );
     367             :         // see if there's a decimal separator for the seconds,
     368             :         // and if so, handle it separately
     369           0 :         sal_Int32 nDecimalSepPos = rString.indexOf( '.' );
     370           0 :         if ( nDecimalSepPos == -1 )
     371             :             // ISO 8601 allows for both a comma and a dot
     372           0 :             nDecimalSepPos = rString.indexOf( ',' );
     373           0 :         if ( nDecimalSepPos != -1 )
     374             :         {
     375             :             // handle fractional seconds
     376           0 :             ::rtl::OUString sFractional = sString.copy( nDecimalSepPos + 1 );
     377           0 :             if ( sFractional.getLength() > 2 )
     378             :                 // our precision is HundrethSeconds - it's all a css.util.Time can hold
     379           0 :                 sFractional = sFractional.copy( 0, 2 );
     380           0 :             if ( !sFractional.isEmpty() )
     381             :             {
     382           0 :                 sal_Int32 nFractional = 0;
     383           0 :                 if ( StringTokenizer( sFractional, 0 ).getNextToken().toInt32( nFractional ) )
     384             :                 {
     385           0 :                     aTime.HundredthSeconds = (sal_uInt16)nFractional;
     386           0 :                     if ( nFractional < 10 )
     387           0 :                         aTime.HundredthSeconds *= 10;
     388             :                 }
     389             :                 else
     390           0 :                     bWellformed = false;
     391             :             }
     392             : 
     393             :             // strip the fraction before further processing
     394           0 :             sString = sString.copy( 0, nDecimalSepPos );
     395             :         }
     396             : 
     397             :         // split into the tokens which are separated by colon
     398           0 :         sal_Int32 nToken = 0;
     399           0 :         StringTokenizer aTokenizer( sString, ':' );
     400           0 :         while ( aTokenizer.hasNextToken() )
     401             :         {
     402           0 :             sal_Int32 nTokenValue = 0;
     403           0 :             if ( !aTokenizer.getNextToken().toInt32( nTokenValue ) )
     404             :             {
     405           0 :                 bWellformed = false;
     406             :                 break;
     407             :             }
     408             : 
     409           0 :             if ( nToken == 0 )
     410           0 :                 aTime.Hours = (sal_uInt16)nTokenValue;
     411           0 :             else if ( nToken == 1 )
     412           0 :                 aTime.Minutes = (sal_uInt16)nTokenValue;
     413           0 :             else if ( nToken == 2 )
     414           0 :                 aTime.Seconds = (sal_uInt16)nTokenValue;
     415             :             else
     416             :             {
     417           0 :                 bWellformed = false;
     418             :                 break;
     419             :             }
     420           0 :             ++nToken;
     421             :         }
     422             : 
     423             :         // sanity checks
     424             :         // note that Seconds == 60 denotes leap seconds. Normally, they're not allowed everywhere,
     425             :         // but we accept them all the time for simplicity reasons
     426           0 :         if  (  ( aTime.Hours > 24 )
     427             :             || ( aTime.Minutes > 59 )
     428             :             || ( aTime.Seconds > 60 )
     429             :             )
     430           0 :             bWellformed = false;
     431             : 
     432           0 :         if  (   bWellformed
     433             :             &&  ( aTime.Hours == 24 )
     434             :             &&  (   ( aTime.Minutes != 0 )
     435             :                 ||  ( aTime.Seconds != 0 )
     436             :                 ||  ( aTime.HundredthSeconds != 0 )
     437             :                 )
     438             :             )
     439           0 :             bWellformed = false;
     440             : 
     441             :         // all okay?
     442           0 :         if ( !bWellformed )
     443           0 :             return UNOTime( 0, 0, 0, 0 );
     444             : 
     445           0 :         return aTime;
     446             :     }
     447             : 
     448             :     // ------------------------------------------------------------------------
     449           0 :     Any lcl_toAny_UNOTime( const OUString& rString )
     450             :     {
     451           0 :         return makeAny( lcl_toUNOTime( rString ) );
     452             :     }
     453             : 
     454             :     // ------------------------------------------------------------------------
     455           0 :     OUString lcl_toXSD_UNODateTime( const Any& rAny )
     456             :     {
     457           0 :         UNODateTime aDateTime;
     458           0 :         OSL_VERIFY( rAny >>= aDateTime );
     459             : 
     460           0 :         UNODate aDate( aDateTime.Day, aDateTime.Month, aDateTime.Year );
     461           0 :         ::rtl::OUString sDate = lcl_toXSD_UNODate_typed( aDate );
     462             : 
     463           0 :         UNOTime aTime( aDateTime.HundredthSeconds, aDateTime.Seconds, aDateTime.Minutes, aDateTime.Hours );
     464           0 :         ::rtl::OUString sTime = lcl_toXSD_UNOTime_typed( aTime );
     465             : 
     466           0 :         ::rtl::OUStringBuffer sInfo;
     467           0 :         sInfo.append( sDate );
     468           0 :         sInfo.append( (sal_Unicode) 'T' );
     469           0 :         sInfo.append( sTime );
     470           0 :         return sInfo.makeStringAndClear();
     471             :     }
     472             : 
     473             :     // ------------------------------------------------------------------------
     474           0 :     Any lcl_toAny_UNODateTime( const OUString& rString )
     475             :     {
     476             :         // separate the date from the time part
     477           0 :         sal_Int32 nDateTimeSep = rString.indexOf( 'T' );
     478           0 :         if ( nDateTimeSep == -1 )
     479           0 :             nDateTimeSep = rString.indexOf( 't' );
     480             : 
     481           0 :         UNODate aDate;
     482           0 :         UNOTime aTime;
     483           0 :         if ( nDateTimeSep == -1 )
     484             :         {   // no time part
     485           0 :             aDate = lcl_toUNODate( rString );
     486           0 :             aTime = UNOTime( 0, 0, 0, 0 );
     487             :         }
     488             :         else
     489             :         {
     490           0 :             aDate = lcl_toUNODate( rString.copy( 0, nDateTimeSep ) );
     491           0 :             aTime = lcl_toUNOTime( rString.copy( nDateTimeSep + 1 ) );
     492             :         }
     493             :         UNODateTime aDateTime(
     494             :             aTime.HundredthSeconds, aTime.Seconds, aTime.Minutes, aTime.Hours,
     495             :             aDate.Day, aDate.Month, aDate.Year
     496           0 :         );
     497           0 :         return makeAny( aDateTime );
     498             :     }
     499             : }
     500             : 
     501             : // ============================================================================
     502           0 : void Convert::init()
     503             : {
     504           0 :     ADD_ENTRY( this, OUString );
     505           0 :     ADD_ENTRY( this, bool );
     506           0 :     ADD_ENTRY( this, double );
     507           0 :     ADD_ENTRY( this, UNODate );
     508           0 :     ADD_ENTRY( this, UNOTime );
     509           0 :     ADD_ENTRY( this, UNODateTime );
     510           0 : }
     511             : 
     512             : 
     513           0 : Convert& Convert::get()
     514             : {
     515             :     // create our Singleton instance on demand
     516             :     static Convert* pConvert = NULL;
     517           0 :     if( pConvert == NULL )
     518           0 :         pConvert = new Convert();
     519             : 
     520             :     OSL_ENSURE( pConvert != NULL, "no converter?" );
     521           0 :     return *pConvert;
     522             : }
     523             : 
     524           0 : bool Convert::hasType( const Type_t& rType )
     525             : {
     526           0 :     return maMap.find( rType ) != maMap.end();
     527             : }
     528             : 
     529           0 : Convert::Types_t Convert::getTypes()
     530             : {
     531           0 :     Types_t aTypes( maMap.size() );
     532             :     transform( maMap.begin(), maMap.end(), aTypes.getArray(),
     533           0 :                o3tl::select1st<Map_t::value_type>() );
     534           0 :     return aTypes;
     535             : }
     536             : 
     537           0 : rtl::OUString Convert::toXSD( const Any_t& rAny )
     538             : {
     539           0 :     Map_t::iterator aIter = maMap.find( rAny.getValueType() );
     540           0 :     return aIter != maMap.end() ? aIter->second.first( rAny ) : OUString();
     541             : }
     542             : 
     543           0 : Convert::Any_t Convert::toAny( const rtl::OUString& rValue,
     544             :                                const Type_t& rType )
     545             : {
     546           0 :     Map_t::iterator aIter = maMap.find( rType );
     547           0 :     return aIter != maMap.end() ? aIter->second.second( rValue ) : Any_t();
     548             : }
     549             : 
     550             : //------------------------------------------------------------------------
     551           0 : ::rtl::OUString Convert::convertWhitespace( const ::rtl::OUString& _rString, sal_Int16 _nWhitespaceTreatment )
     552             : {
     553           0 :     ::rtl::OUString sConverted;
     554           0 :     switch( _nWhitespaceTreatment )
     555             :     {
     556             :     default:
     557             :         OSL_FAIL( "Convert::convertWhitespace: invalid whitespace treatment constant!" );
     558             :         // NO break
     559             :     case com::sun::star::xsd::WhiteSpaceTreatment::Preserve:
     560           0 :         sConverted = _rString;
     561           0 :         break;
     562             :     case com::sun::star::xsd::WhiteSpaceTreatment::Replace:
     563           0 :         sConverted = replaceWhitespace( _rString );
     564           0 :         break;
     565             :     case com::sun::star::xsd::WhiteSpaceTreatment::Collapse:
     566           0 :         sConverted = collapseWhitespace( _rString );
     567           0 :         break;
     568             :     }
     569           0 :     return sConverted;
     570             : }
     571             : 
     572             : //------------------------------------------------------------------------
     573           0 : ::rtl::OUString Convert::replaceWhitespace( const ::rtl::OUString& _rString )
     574             : {
     575           0 :     OUStringBuffer aBuffer( _rString );
     576           0 :     sal_Int32 nLength = aBuffer.getLength();
     577           0 :     const sal_Unicode* pBuffer = aBuffer.getStr();
     578           0 :     for( sal_Int32 i = 0; i < nLength; i++ )
     579             :     {
     580           0 :         sal_Unicode c = pBuffer[i];
     581           0 :         if( c == sal_Unicode(0x08) ||
     582             :             c == sal_Unicode(0x0A) ||
     583             :             c == sal_Unicode(0x0D) )
     584           0 :             aBuffer[i] = sal_Unicode(0x20);
     585             :     }
     586           0 :     return aBuffer.makeStringAndClear();
     587             : }
     588             : 
     589             : //------------------------------------------------------------------------
     590           0 : ::rtl::OUString Convert::collapseWhitespace( const ::rtl::OUString& _rString )
     591             : {
     592           0 :     sal_Int32 nLength = _rString.getLength();
     593           0 :     OUStringBuffer aBuffer( nLength );
     594           0 :     const sal_Unicode* pStr = _rString.getStr();
     595           0 :     bool bStrip = true;
     596           0 :     for( sal_Int32 i = 0; i < nLength; i++ )
     597             :     {
     598           0 :         sal_Unicode c = pStr[i];
     599           0 :         if( c == sal_Unicode(0x08) ||
     600             :             c == sal_Unicode(0x0A) ||
     601             :             c == sal_Unicode(0x0D) ||
     602             :             c == sal_Unicode(0x20) )
     603             :         {
     604           0 :             if( ! bStrip )
     605             :             {
     606           0 :                 aBuffer.append( sal_Unicode(0x20) );
     607           0 :                 bStrip = true;
     608             :             }
     609             :         }
     610             :         else
     611             :         {
     612           0 :             bStrip = false;
     613           0 :             aBuffer.append( c );
     614             :         }
     615             :     }
     616           0 :     if( aBuffer[ aBuffer.getLength() - 1 ] == sal_Unicode( 0x20 ) )
     617           0 :         aBuffer.setLength( aBuffer.getLength() - 1 );
     618           0 :     return aBuffer.makeStringAndClear();
     619             : }
     620             : 
     621             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10