LCOV - code coverage report
Current view: top level - oox/source/helper - attributelist.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 104 135 77.0 %
Date: 2014-11-03 Functions: 26 32 81.2 %
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 "oox/helper/attributelist.hxx"
      21             : 
      22             : #include <cassert>
      23             : #include <osl/diagnose.h>
      24             : #include <rtl/ustrbuf.hxx>
      25             : #include <sax/fastattribs.hxx>
      26             : #include "oox/token/tokenmap.hxx"
      27             : 
      28             : namespace oox {
      29             : 
      30             : using namespace ::com::sun::star;
      31             : using namespace ::com::sun::star::uno;
      32             : using namespace ::com::sun::star::xml::sax;
      33             : 
      34             : namespace {
      35             : 
      36             : const sal_Int32 XSTRING_ENCCHAR_LEN     = 7;
      37             : 
      38       11442 : bool lclAddHexDigit( sal_Unicode& orcChar, sal_Unicode cDigit, int nBitShift )
      39             : {
      40       11442 :     if( ('0' <= cDigit) && (cDigit <= '9') ) { orcChar |= ((cDigit - '0') << nBitShift); return true; }
      41           2 :     if( ('a' <= cDigit) && (cDigit <= 'f') ) { orcChar |= ((cDigit - 'a' + 10) << nBitShift); return true; }
      42           2 :     if( ('A' <= cDigit) && (cDigit <= 'F') ) { orcChar |= ((cDigit - 'A' + 10) << nBitShift); return true; }
      43           2 :     return false;
      44             : }
      45             : 
      46       52816 : sal_Unicode lclGetXChar( const sal_Unicode*& rpcStr, const sal_Unicode* pcEnd )
      47             : {
      48       52816 :     sal_Unicode cChar = 0;
      49      130004 :     if( (pcEnd - rpcStr >= XSTRING_ENCCHAR_LEN) &&
      50       27256 :         (rpcStr[ 0 ] == '_') &&
      51        5748 :         (rpcStr[ 1 ] == 'x') &&
      52        5726 :         (rpcStr[ 6 ] == '_') &&
      53        5722 :         lclAddHexDigit( cChar, rpcStr[ 2 ], 12 ) &&
      54        5720 :         lclAddHexDigit( cChar, rpcStr[ 3 ], 8 ) &&
      55       58536 :         lclAddHexDigit( cChar, rpcStr[ 4 ], 4 ) &&
      56        2860 :         lclAddHexDigit( cChar, rpcStr[ 5 ], 0 ) )
      57             :     {
      58        2860 :         rpcStr += XSTRING_ENCCHAR_LEN;
      59        2860 :         return cChar;
      60             :     }
      61       49956 :     return *rpcStr++;
      62             : }
      63             : 
      64             : } // namespace
      65             : 
      66       57338 : sal_Int32 AttributeConversion::decodeToken( const OUString& rValue )
      67             : {
      68       57338 :     return StaticTokenMap::get().getTokenFromUnicode( rValue );
      69             : }
      70             : 
      71        6624 : OUString AttributeConversion::decodeXString( const OUString& rValue )
      72             : {
      73             :     // string shorter than one encoded character - no need to decode
      74        6624 :     if( rValue.getLength() < XSTRING_ENCCHAR_LEN )
      75        1132 :         return rValue;
      76        5492 :     OUStringBuffer aBuffer;
      77        5492 :     const sal_Unicode* pcStr = rValue.getStr();
      78        5492 :     const sal_Unicode* pcEnd = pcStr + rValue.getLength();
      79       63800 :     while( pcStr < pcEnd )
      80       52816 :         aBuffer.append( lclGetXChar( pcStr, pcEnd ) );
      81        5492 :     return aBuffer.makeStringAndClear();
      82             : }
      83             : 
      84           0 : sal_Int32 AttributeConversion::decodeInteger( const OUString& rValue )
      85             : {
      86           0 :     return rValue.toInt32();
      87             : }
      88             : 
      89         174 : sal_uInt32 AttributeConversion::decodeUnsigned( const OUString& rValue )
      90             : {
      91         174 :     return getLimitedValue< sal_uInt32, sal_Int64 >( rValue.toInt64(), 0, SAL_MAX_UINT32 );
      92             : }
      93             : 
      94           0 : sal_Int64 AttributeConversion::decodeHyper( const OUString& rValue )
      95             : {
      96           0 :     return rValue.toInt64();
      97             : }
      98             : 
      99       45278 : sal_Int32 AttributeConversion::decodeIntegerHex( const OUString& rValue )
     100             : {
     101             :     // It looks like all Office Open XML attributes containing hexadecimal
     102             :     // values are based on xsd:hexBinary and so use an unsigned representation:
     103       45278 :     return static_cast< sal_Int32 >(rValue.toUInt32( 16 ));
     104             :         //TODO: Change this function to return sal_uInt32 and get rid of the
     105             :         // cast, but that will have a ripple effect
     106             : }
     107             : 
     108     1610933 : AttributeList::AttributeList( const Reference< XFastAttributeList >& rxAttribs ) :
     109             :     mxAttribs( rxAttribs ),
     110     1610933 :     mpAttribList( NULL )
     111             : {
     112             :     OSL_ENSURE( mxAttribs.is(), "AttributeList::AttributeList - missing attribute list interface" );
     113     1610933 : }
     114             : 
     115      564674 : sax_fastparser::FastAttributeList *AttributeList::getAttribList() const
     116             : {
     117      564674 :     if( mpAttribList == NULL )
     118             :     {
     119             :         assert( dynamic_cast< sax_fastparser::FastAttributeList *>( mxAttribs.get() ) != NULL );
     120      271134 :         mpAttribList = static_cast< sax_fastparser::FastAttributeList *>( mxAttribs.get() );
     121             :     }
     122      564674 :     return mpAttribList;
     123             : }
     124             : 
     125      339982 : bool AttributeList::hasAttribute( sal_Int32 nAttrToken ) const
     126             : {
     127      339982 :     return mxAttribs->hasAttribute( nAttrToken );
     128             : }
     129             : 
     130             : // optional return values -----------------------------------------------------
     131             : 
     132       58658 : OptValue< sal_Int32 > AttributeList::getToken( sal_Int32 nAttrToken ) const
     133             : {
     134       58658 :     sal_Int32 nToken = mxAttribs->getOptionalValueToken( nAttrToken, XML_TOKEN_INVALID );
     135       58658 :     return OptValue< sal_Int32 >( nToken != XML_TOKEN_INVALID, nToken );
     136             : }
     137             : 
     138      245720 : OptValue< OUString > AttributeList::getString( sal_Int32 nAttrToken ) const
     139             : {
     140             :     // check if the attribute exists (empty string may be different to missing attribute)
     141      245720 :     if( mxAttribs->hasAttribute( nAttrToken ) )
     142      199930 :         return OptValue< OUString >( mxAttribs->getOptionalValue( nAttrToken ) );
     143       45790 :     return OptValue< OUString >();
     144             : }
     145             : 
     146        6878 : OptValue< OUString > AttributeList::getXString( sal_Int32 nAttrToken ) const
     147             : {
     148             :     // check if the attribute exists (empty string may be different to missing attribute)
     149        6878 :     if( mxAttribs->hasAttribute( nAttrToken ) )
     150        5260 :         return OptValue< OUString >( AttributeConversion::decodeXString( mxAttribs->getOptionalValue( nAttrToken ) ) );
     151        1618 :     return OptValue< OUString >();
     152             : }
     153             : 
     154       34880 : OptValue< double > AttributeList::getDouble( sal_Int32 nAttrToken ) const
     155             : {
     156             :     double nValue;
     157       34880 :     bool bValid = getAttribList()->getAsDouble( nAttrToken, nValue );
     158       34880 :     return OptValue< double >( bValid, nValue );
     159             : }
     160             : 
     161      386364 : OptValue< sal_Int32 > AttributeList::getInteger( sal_Int32 nAttrToken ) const
     162             : {
     163             :     sal_Int32 nValue;
     164      386364 :     bool bValid = getAttribList()->getAsInteger( nAttrToken, nValue );
     165      386363 :     return OptValue< sal_Int32 >( bValid, nValue );
     166             : }
     167             : 
     168         174 : OptValue< sal_uInt32 > AttributeList::getUnsigned( sal_Int32 nAttrToken ) const
     169             : {
     170         174 :     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
     171         174 :     bool bValid = !aValue.isEmpty();
     172         174 :     return OptValue< sal_uInt32 >( bValid, AttributeConversion::decodeUnsigned( aValue ) );
     173             : }
     174             : 
     175           0 : OptValue< sal_Int64 > AttributeList::getHyper( sal_Int32 nAttrToken ) const
     176             : {
     177           0 :     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
     178           0 :     bool bValid = !aValue.isEmpty();
     179           0 :     return OptValue< sal_Int64 >( bValid, bValid ? AttributeConversion::decodeHyper( aValue ) : 0 );
     180             : }
     181             : 
     182       45292 : OptValue< sal_Int32 > AttributeList::getIntegerHex( sal_Int32 nAttrToken ) const
     183             : {
     184       45292 :     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
     185       45292 :     bool bValid = !aValue.isEmpty();
     186       45292 :     return OptValue< sal_Int32 >( bValid, bValid ? AttributeConversion::decodeIntegerHex( aValue ) : 0 );
     187             : }
     188             : 
     189      130268 : OptValue< bool > AttributeList::getBool( sal_Int32 nAttrToken ) const
     190             : {
     191             :     const char *pAttr;
     192             : 
     193             :     // catch the common cases as quickly as possible first
     194      130268 :     bool bHasAttr = getAttribList()->getAsChar( nAttrToken, pAttr );
     195      130268 :     if( !bHasAttr )
     196       80408 :         return OptValue< bool >();
     197       49860 :     if( !strcmp( pAttr, "false" ) )
     198       11282 :         return OptValue< bool >( false );
     199       38578 :     if( !strcmp( pAttr, "true" ) )
     200        2884 :         return OptValue< bool >( true );
     201             : 
     202             :     // now for all the crazy stuff
     203             : 
     204             :     // boolean attributes may be "t", "f", "true", "false", "on", "off", "1", or "0"
     205       35694 :     switch( getToken( nAttrToken, XML_TOKEN_INVALID ) )
     206             :     {
     207           0 :         case XML_t:     return OptValue< bool >( true );  // used in VML
     208           0 :         case XML_true:  return OptValue< bool >( true );
     209           0 :         case XML_on:    return OptValue< bool >( true );
     210           0 :         case XML_f:     return OptValue< bool >( false ); // used in VML
     211           0 :         case XML_false: return OptValue< bool >( false );
     212           0 :         case XML_off:   return OptValue< bool >( false );
     213             :     }
     214       35694 :     OptValue< sal_Int32 > onValue = getInteger( nAttrToken );
     215       35694 :     return OptValue< bool >( onValue.has(), onValue.get() != 0 );
     216             : }
     217             : 
     218           0 : OptValue< util::DateTime > AttributeList::getDateTime( sal_Int32 nAttrToken ) const
     219             : {
     220           0 :     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
     221           0 :     util::DateTime aDateTime;
     222           0 :     bool bValid = (aValue.getLength() == 19) && (aValue[ 4 ] == '-') && (aValue[ 7 ] == '-') &&
     223           0 :         (aValue[ 10 ] == 'T') && (aValue[ 13 ] == ':') && (aValue[ 16 ] == ':');
     224           0 :     if( bValid )
     225             :     {
     226           0 :         aDateTime.Year    = static_cast< sal_uInt16 >( aValue.copy( 0, 4 ).toInt32() );
     227           0 :         aDateTime.Month   = static_cast< sal_uInt16 >( aValue.copy( 5, 2 ).toInt32() );
     228           0 :         aDateTime.Day     = static_cast< sal_uInt16 >( aValue.copy( 8, 2 ).toInt32() );
     229           0 :         aDateTime.Hours   = static_cast< sal_uInt16 >( aValue.copy( 11, 2 ).toInt32() );
     230           0 :         aDateTime.Minutes = static_cast< sal_uInt16 >( aValue.copy( 14, 2 ).toInt32() );
     231           0 :         aDateTime.Seconds = static_cast< sal_uInt16 >( aValue.copy( 17, 2 ).toInt32() );
     232             :     }
     233           0 :     return OptValue< util::DateTime >( bValid, aDateTime );
     234             : }
     235             : 
     236             : // defaulted return values ----------------------------------------------------
     237             : 
     238      780748 : sal_Int32 AttributeList::getToken( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
     239             : {
     240      780748 :     return mxAttribs->getOptionalValueToken( nAttrToken, nDefault );
     241             : }
     242             : 
     243      348524 : OUString AttributeList::getString( sal_Int32 nAttrToken, const OUString& rDefault ) const
     244             : {
     245             :     // try to avoid slow exception throw/catch if we can
     246      348524 :     if (rDefault.isEmpty())
     247      348336 :         return mxAttribs->getOptionalValue( nAttrToken );
     248             : 
     249             :     try
     250             :     {
     251         188 :         return mxAttribs->getValue( nAttrToken );
     252             :     }
     253          16 :     catch( Exception& )
     254             :     {
     255             :     }
     256          16 :     return rDefault;
     257             : }
     258             : 
     259        6874 : OUString AttributeList::getXString( sal_Int32 nAttrToken, const OUString& rDefault ) const
     260             : {
     261        6874 :     return getXString( nAttrToken ).get( rDefault );
     262             : }
     263             : 
     264       13164 : const char* AttributeList::getChar( sal_Int32 nAttrToken ) const
     265             : {
     266       13164 :     const char* p = NULL;
     267       13164 :     bool bValid = getAttribList()->getAsChar(nAttrToken, p);
     268       13164 :     if (!bValid)
     269           6 :         p = NULL;
     270             : 
     271       13164 :     return p;
     272             : }
     273             : 
     274       34880 : double AttributeList::getDouble( sal_Int32 nAttrToken, double fDefault ) const
     275             : {
     276       34880 :     return getDouble( nAttrToken ).get( fDefault );
     277             : }
     278             : 
     279      315546 : sal_Int32 AttributeList::getInteger( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
     280             : {
     281      315546 :     return getInteger( nAttrToken ).get( nDefault );
     282             : }
     283             : 
     284         174 : sal_uInt32 AttributeList::getUnsigned( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
     285             : {
     286         174 :     return getUnsigned( nAttrToken ).get( nDefault );
     287             : }
     288             : 
     289           0 : sal_Int64 AttributeList::getHyper( sal_Int32 nAttrToken, sal_Int64 nDefault ) const
     290             : {
     291           0 :     return getHyper( nAttrToken ).get( nDefault );
     292             : }
     293             : 
     294       45054 : sal_Int32 AttributeList::getIntegerHex( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
     295             : {
     296       45054 :     return getIntegerHex( nAttrToken ).get( nDefault );
     297             : }
     298             : 
     299      107872 : bool AttributeList::getBool( sal_Int32 nAttrToken, bool bDefault ) const
     300             : {
     301      107872 :     return getBool( nAttrToken ).get( bDefault );
     302             : }
     303             : 
     304           0 : util::DateTime AttributeList::getDateTime( sal_Int32 nAttrToken, const util::DateTime& rDefault ) const
     305             : {
     306           0 :     return getDateTime( nAttrToken ).get( rDefault );
     307             : }
     308             : 
     309             : } // namespace oox
     310             : 
     311             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10