LCOV - code coverage report
Current view: top level - oox/source/helper - attributelist.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 106 137 77.4 %
Date: 2015-06-13 12:38:46 Functions: 27 33 81.8 %
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        5729 : bool lclAddHexDigit( sal_Unicode& orcChar, sal_Unicode cDigit, int nBitShift )
      39             : {
      40        5729 :     if( ('0' <= cDigit) && (cDigit <= '9') ) { orcChar |= ((cDigit - '0') << nBitShift); return true; }
      41           1 :     if( ('a' <= cDigit) && (cDigit <= 'f') ) { orcChar |= ((cDigit - 'a' + 10) << nBitShift); return true; }
      42           1 :     if( ('A' <= cDigit) && (cDigit <= 'F') ) { orcChar |= ((cDigit - 'A' + 10) << nBitShift); return true; }
      43           1 :     return false;
      44             : }
      45             : 
      46       29915 : sal_Unicode lclGetXChar( const sal_Unicode*& rpcStr, const sal_Unicode* pcEnd )
      47             : {
      48       29915 :     sal_Unicode cChar = 0;
      49       73356 :     if( (pcEnd - rpcStr >= XSTRING_ENCCHAR_LEN) &&
      50       14970 :         (rpcStr[ 0 ] == '_') &&
      51        2878 :         (rpcStr[ 1 ] == 'x') &&
      52        2867 :         (rpcStr[ 6 ] == '_') &&
      53        2865 :         lclAddHexDigit( cChar, rpcStr[ 2 ], 12 ) &&
      54        2864 :         lclAddHexDigit( cChar, rpcStr[ 3 ], 8 ) &&
      55       32779 :         lclAddHexDigit( cChar, rpcStr[ 4 ], 4 ) &&
      56        1432 :         lclAddHexDigit( cChar, rpcStr[ 5 ], 0 ) )
      57             :     {
      58        1432 :         rpcStr += XSTRING_ENCCHAR_LEN;
      59        1432 :         return cChar;
      60             :     }
      61       28483 :     return *rpcStr++;
      62             : }
      63             : 
      64             : } // namespace
      65             : 
      66       33604 : sal_Int32 AttributeConversion::decodeToken( const OUString& rValue )
      67             : {
      68       33604 :     return TokenMap::getTokenFromUnicode( rValue );
      69             : }
      70             : 
      71        4003 : OUString AttributeConversion::decodeXString( const OUString& rValue )
      72             : {
      73             :     // string shorter than one encoded character - no need to decode
      74        4003 :     if( rValue.getLength() < XSTRING_ENCCHAR_LEN )
      75         897 :         return rValue;
      76        3106 :     OUStringBuffer aBuffer;
      77        3106 :     const sal_Unicode* pcStr = rValue.getStr();
      78        3106 :     const sal_Unicode* pcEnd = pcStr + rValue.getLength();
      79       36127 :     while( pcStr < pcEnd )
      80       29915 :         aBuffer.append( lclGetXChar( pcStr, pcEnd ) );
      81        3106 :     return aBuffer.makeStringAndClear();
      82             : }
      83             : 
      84           0 : sal_Int32 AttributeConversion::decodeInteger( const OUString& rValue )
      85             : {
      86           0 :     return rValue.toInt32();
      87             : }
      88             : 
      89         138 : sal_uInt32 AttributeConversion::decodeUnsigned( const OUString& rValue )
      90             : {
      91         138 :     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       26579 : 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       26579 :     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      938723 : AttributeList::AttributeList( const Reference< XFastAttributeList >& rxAttribs ) :
     109             :     mxAttribs( rxAttribs ),
     110      938723 :     mpAttribList( NULL )
     111             : {
     112             :     OSL_ENSURE( mxAttribs.is(), "AttributeList::AttributeList - missing attribute list interface" );
     113      938724 : }
     114             : 
     115      339091 : sax_fastparser::FastAttributeList *AttributeList::getAttribList() const
     116             : {
     117      339091 :     if( mpAttribList == NULL )
     118             :     {
     119             :         assert( dynamic_cast< sax_fastparser::FastAttributeList *>( mxAttribs.get() ) != NULL );
     120      159152 :         mpAttribList = static_cast< sax_fastparser::FastAttributeList *>( mxAttribs.get() );
     121             :     }
     122      339091 :     return mpAttribList;
     123             : }
     124             : 
     125      212525 : bool AttributeList::hasAttribute( sal_Int32 nAttrToken ) const
     126             : {
     127      212525 :     return mxAttribs->hasAttribute( nAttrToken );
     128             : }
     129             : 
     130             : // optional return values -----------------------------------------------------
     131             : 
     132       32753 : OptValue< sal_Int32 > AttributeList::getToken( sal_Int32 nAttrToken ) const
     133             : {
     134       32753 :     sal_Int32 nToken = mxAttribs->getOptionalValueToken( nAttrToken, XML_TOKEN_INVALID );
     135       32753 :     return OptValue< sal_Int32 >( nToken != XML_TOKEN_INVALID, nToken );
     136             : }
     137             : 
     138      143053 : OptValue< OUString > AttributeList::getString( sal_Int32 nAttrToken ) const
     139             : {
     140             :     // check if the attribute exists (empty string may be different to missing attribute)
     141      143053 :     if( mxAttribs->hasAttribute( nAttrToken ) )
     142      117779 :         return OptValue< OUString >( mxAttribs->getOptionalValue( nAttrToken ) );
     143       25274 :     return OptValue< OUString >();
     144             : }
     145             : 
     146        4198 : OptValue< OUString > AttributeList::getXString( sal_Int32 nAttrToken ) const
     147             : {
     148             :     // check if the attribute exists (empty string may be different to missing attribute)
     149        4198 :     if( mxAttribs->hasAttribute( nAttrToken ) )
     150        3311 :         return OptValue< OUString >( AttributeConversion::decodeXString( mxAttribs->getOptionalValue( nAttrToken ) ) );
     151         887 :     return OptValue< OUString >();
     152             : }
     153             : 
     154       20749 : OptValue< double > AttributeList::getDouble( sal_Int32 nAttrToken ) const
     155             : {
     156             :     double nValue;
     157       20749 :     bool bValid = getAttribList()->getAsDouble( nAttrToken, nValue );
     158       20749 :     return OptValue< double >( bValid, nValue );
     159             : }
     160             : 
     161      226972 : OptValue< sal_Int32 > AttributeList::getInteger( sal_Int32 nAttrToken ) const
     162             : {
     163             :     sal_Int32 nValue;
     164      226972 :     bool bValid = getAttribList()->getAsInteger( nAttrToken, nValue );
     165      226973 :     return OptValue< sal_Int32 >( bValid, nValue );
     166             : }
     167             : 
     168         138 : OptValue< sal_uInt32 > AttributeList::getUnsigned( sal_Int32 nAttrToken ) const
     169             : {
     170         138 :     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
     171         138 :     bool bValid = !aValue.isEmpty();
     172         138 :     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       26586 : OptValue< sal_Int32 > AttributeList::getIntegerHex( sal_Int32 nAttrToken ) const
     183             : {
     184       26586 :     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
     185       26586 :     bool bValid = !aValue.isEmpty();
     186       26586 :     return OptValue< sal_Int32 >( bValid, bValid ? AttributeConversion::decodeIntegerHex( aValue ) : 0 );
     187             : }
     188             : 
     189       84245 : 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       84245 :     bool bHasAttr = getAttribList()->getAsChar( nAttrToken, pAttr );
     195       84244 :     if( !bHasAttr )
     196       50770 :         return OptValue< bool >();
     197       33474 :     if( !strcmp( pAttr, "false" ) )
     198        8938 :         return OptValue< bool >( false );
     199       24536 :     if( !strcmp( pAttr, "true" ) )
     200        2529 :         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       22007 :     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       22007 :     OptValue< sal_Int32 > onValue = getInteger( nAttrToken );
     215       22007 :     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      458991 : sal_Int32 AttributeList::getToken( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
     239             : {
     240      458991 :     return mxAttribs->getOptionalValueToken( nAttrToken, nDefault );
     241             : }
     242             : 
     243      196682 : OUString AttributeList::getString( sal_Int32 nAttrToken, const OUString& rDefault ) const
     244             : {
     245             :     // try to avoid slow exception throw/catch if we can
     246      196682 :     if (rDefault.isEmpty())
     247      196521 :         return mxAttribs->getOptionalValue( nAttrToken );
     248             : 
     249             :     try
     250             :     {
     251         161 :         return mxAttribs->getValue( nAttrToken );
     252             :     }
     253          10 :     catch( Exception& )
     254             :     {
     255             :     }
     256          10 :     return rDefault;
     257             : }
     258             : 
     259        4196 : OUString AttributeList::getXString( sal_Int32 nAttrToken, const OUString& rDefault ) const
     260             : {
     261        4196 :     return getXString( nAttrToken ).get( rDefault );
     262             : }
     263             : 
     264        7127 : const char* AttributeList::getChar( sal_Int32 nAttrToken ) const
     265             : {
     266        7127 :     const char* p = NULL;
     267        7127 :     bool bValid = getAttribList()->getAsChar(nAttrToken, p);
     268        7126 :     if (!bValid)
     269           3 :         p = NULL;
     270             : 
     271        7126 :     return p;
     272             : }
     273             : 
     274       20749 : double AttributeList::getDouble( sal_Int32 nAttrToken, double fDefault ) const
     275             : {
     276       20749 :     return getDouble( nAttrToken ).get( fDefault );
     277             : }
     278             : 
     279      183616 : sal_Int32 AttributeList::getInteger( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
     280             : {
     281      183616 :     return getInteger( nAttrToken ).get( nDefault );
     282             : }
     283             : 
     284         138 : sal_uInt32 AttributeList::getUnsigned( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
     285             : {
     286         138 :     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       26363 : sal_Int32 AttributeList::getIntegerHex( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
     295             : {
     296       26363 :     return getIntegerHex( nAttrToken ).get( nDefault );
     297             : }
     298             : 
     299          93 : sal_uInt32 AttributeList::getUnsignedHex( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
     300             : {
     301          93 :     return getIntegerHex( nAttrToken ).get( nDefault );
     302             : }
     303             : 
     304       71354 : bool AttributeList::getBool( sal_Int32 nAttrToken, bool bDefault ) const
     305             : {
     306       71354 :     return getBool( nAttrToken ).get( bDefault );
     307             : }
     308             : 
     309           0 : util::DateTime AttributeList::getDateTime( sal_Int32 nAttrToken, const util::DateTime& rDefault ) const
     310             : {
     311           0 :     return getDateTime( nAttrToken ).get( rDefault );
     312             : }
     313             : 
     314             : } // namespace oox
     315             : 
     316             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11