LCOV - code coverage report
Current view: top level - sax/source/tools - fastattribs.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 124 130 95.4 %
Date: 2014-04-11 Functions: 25 27 92.6 %
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 <algorithm>
      21             : 
      22             : #include <sax/fastattribs.hxx>
      23             : 
      24             : using namespace ::com::sun::star::uno;
      25             : using namespace ::com::sun::star::xml;
      26             : using namespace ::com::sun::star::xml::sax;
      27             : namespace sax_fastparser
      28             : {
      29             : 
      30             : // wasteage to keep MSVC happy vs. an in-line {}
      31       16801 : FastTokenHandlerBase::~FastTokenHandlerBase()
      32             : {
      33       16801 : }
      34             : 
      35        5204 : UnknownAttribute::UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const sal_Char* pValue )
      36        5204 :     : maNamespaceURL( rNamespaceURL ), maName( rName ), maValue( pValue )
      37             : {
      38        5204 : }
      39             : 
      40          34 : UnknownAttribute::UnknownAttribute( const OString& rName, const sal_Char* pValue )
      41          34 :     : maName( rName ), maValue( pValue )
      42             : {
      43          34 : }
      44             : 
      45           4 : void UnknownAttribute::FillAttribute( Attribute* pAttrib ) const
      46             : {
      47           4 :     if( pAttrib )
      48             :     {
      49           4 :         pAttrib->Name = OStringToOUString( maName, RTL_TEXTENCODING_UTF8 );
      50           4 :         pAttrib->NamespaceURL = maNamespaceURL;
      51           4 :         pAttrib->Value = OStringToOUString( maValue, RTL_TEXTENCODING_UTF8 );
      52             :     }
      53           4 : }
      54             : 
      55      611053 : FastAttributeList::FastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& xTokenHandler,
      56             :                                       sax_fastparser::FastTokenHandlerBase *pTokenHandler)
      57             : : mxTokenHandler( xTokenHandler ),
      58      611053 :   mpTokenHandler( pTokenHandler )
      59             : {
      60             :     // random initial size of buffer to store attribute values
      61      611053 :     mnChunkLength = 58;
      62      611053 :     mpChunk = (sal_Char *) malloc( mnChunkLength );
      63      611053 :     maAttributeValues.push_back( 0 );
      64      611053 : }
      65             : 
      66     1813262 : FastAttributeList::~FastAttributeList()
      67             : {
      68      604421 :     free( mpChunk );
      69     1208841 : }
      70             : 
      71     1220891 : void FastAttributeList::clear()
      72             : {
      73     1220891 :     maAttributeTokens.clear();
      74     1220891 :     maAttributeValues.clear();
      75     1220891 :     maAttributeValues.push_back( 0 );
      76     1220891 :     maUnknownAttributes.clear();
      77     1220891 : }
      78             : 
      79     2461571 : void FastAttributeList::add( sal_Int32 nToken, const sal_Char* pValue, size_t nValueLength )
      80             : {
      81     2461571 :     maAttributeTokens.push_back( nToken );
      82     2461571 :     if (nValueLength == 0)
      83     2081989 :         nValueLength = strlen(pValue);
      84     2461571 :     sal_Int32 nWritePosition = maAttributeValues.back();
      85     2461571 :     maAttributeValues.push_back( maAttributeValues.back() + nValueLength + 1 );
      86     2461571 :     if (maAttributeValues.back() > mnChunkLength)
      87             :     {
      88       38397 :         mnChunkLength = maAttributeValues.back();
      89       38397 :         mpChunk = (sal_Char *) realloc( mpChunk, mnChunkLength );
      90             :     }
      91     2461571 :     strncpy(mpChunk + nWritePosition, pValue, nValueLength);
      92     2461571 :     mpChunk[nWritePosition + nValueLength] = '\0';
      93     2461571 : }
      94             : 
      95      382802 : void FastAttributeList::add( sal_Int32 nToken, const OString& rValue )
      96             : {
      97      382802 :     add( nToken, rValue.getStr(), rValue.getLength() );
      98      382802 : }
      99             : 
     100         138 : void FastAttributeList::addNS( sal_Int32 nNamespaceToken, sal_Int32 nToken, const OString& rValue )
     101             : {
     102         138 :     sal_Int32 nCombinedToken = (nNamespaceToken << 16) | nToken;
     103         138 :     add( nCombinedToken, rValue );
     104         138 : }
     105             : 
     106        5204 : void FastAttributeList::addUnknown( const OUString& rNamespaceURL, const OString& rName, const sal_Char* pValue )
     107             : {
     108        5204 :     maUnknownAttributes.push_back( UnknownAttribute( rNamespaceURL, rName, pValue ) );
     109        5204 : }
     110             : 
     111          34 : void FastAttributeList::addUnknown( const OString& rName, const sal_Char* pValue )
     112             : {
     113          34 :     maUnknownAttributes.push_back( UnknownAttribute( rName, pValue ) );
     114          34 : }
     115             : 
     116             : // XFastAttributeList
     117     3012476 : sal_Bool FastAttributeList::hasAttribute( ::sal_Int32 Token ) throw (RuntimeException, std::exception)
     118             : {
     119     7335829 :     for (size_t i = 0; i < maAttributeTokens.size(); ++i)
     120     5928705 :         if (maAttributeTokens[i] == Token)
     121     1605352 :             return sal_True;
     122             : 
     123     1407124 :     return sal_False;
     124             : }
     125             : 
     126           0 : sal_Int32 FastAttributeList::getValueToken( ::sal_Int32 Token ) throw (SAXException, RuntimeException, std::exception)
     127             : {
     128           0 :     for (size_t i = 0; i < maAttributeTokens.size(); ++i)
     129           0 :         if (maAttributeTokens[i] == Token)
     130             :             return maTokenLookup.getTokenFromChars( mxTokenHandler, mpTokenHandler,
     131           0 :                                                     mpChunk + maAttributeValues[ i ],
     132           0 :                                                     AttributeValueLength( i ) );
     133             : 
     134           0 :     throw SAXException();
     135             : }
     136             : 
     137      240557 : sal_Int32 FastAttributeList::getOptionalValueToken( ::sal_Int32 Token, ::sal_Int32 Default ) throw (RuntimeException, std::exception)
     138             : {
     139      475894 :     for (size_t i = 0; i < maAttributeTokens.size(); ++i)
     140      285714 :         if (maAttributeTokens[i] == Token)
     141             :             return maTokenLookup.getTokenFromChars( mxTokenHandler, mpTokenHandler,
     142       50377 :                                                     mpChunk + maAttributeValues[ i ],
     143      100754 :                                                     AttributeValueLength( i ) );
     144             : 
     145      190180 :     return Default;
     146             : }
     147             : 
     148             : // performance sensitive shortcuts to avoid allocation ...
     149      105536 : bool FastAttributeList::getAsInteger( sal_Int32 nToken, sal_Int32 &rInt)
     150             : {
     151      105536 :     rInt = 0;
     152      240250 :     for (size_t i = 0; i < maAttributeTokens.size(); ++i)
     153      203074 :         if (maAttributeTokens[i] == nToken)
     154             :         {
     155       68360 :             rInt = rtl_str_toInt32( mpChunk + maAttributeValues[i], 10 );
     156       68360 :             return true;
     157             :         }
     158       37176 :     return false;
     159             : }
     160             : 
     161       10831 : bool FastAttributeList::getAsDouble( sal_Int32 nToken, double &rDouble)
     162             : {
     163       10831 :     rDouble = 0.0;
     164       14015 :     for (size_t i = 0; i < maAttributeTokens.size(); ++i)
     165       13580 :         if (maAttributeTokens[i] == nToken)
     166             :         {
     167       10396 :             rDouble = rtl_str_toDouble( mpChunk + maAttributeValues[i] );
     168       10396 :             return true;
     169             :         }
     170         435 :     return false;
     171             : }
     172             : 
     173       39100 : bool FastAttributeList::getAsChar( sal_Int32 nToken, const char*& rPos ) const
     174             : {
     175      125855 :     for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
     176             :     {
     177      103063 :         if (maAttributeTokens[i] != nToken)
     178       86755 :             continue;
     179             : 
     180       16308 :         sal_Int32 nOffset = maAttributeValues[i];
     181       16308 :         rPos = mpChunk + nOffset;
     182       16308 :         return true;
     183             :     }
     184             : 
     185       22792 :     return false;
     186             : }
     187             : 
     188     1898983 : OUString FastAttributeList::getValue( ::sal_Int32 Token ) throw (SAXException, RuntimeException, std::exception)
     189             : {
     190     3573737 :     for (size_t i = 0; i < maAttributeTokens.size(); ++i)
     191     3573728 :         if (maAttributeTokens[i] == Token)
     192     3797948 :             return OUString( mpChunk + maAttributeValues[i], AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
     193             : 
     194           9 :     throw SAXException();
     195             : }
     196             : 
     197      175394 : OUString FastAttributeList::getOptionalValue( ::sal_Int32 Token ) throw (RuntimeException, std::exception)
     198             : {
     199      341699 :     for (size_t i = 0; i < maAttributeTokens.size(); ++i)
     200      316867 :         if (maAttributeTokens[i] == Token)
     201      150562 :             return OUString( mpChunk + maAttributeValues[i], AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
     202             : 
     203       24832 :     return OUString();
     204             : }
     205      281458 : Sequence< Attribute > FastAttributeList::getUnknownAttributes(  ) throw (RuntimeException, std::exception)
     206             : {
     207      281458 :     Sequence< Attribute > aSeq( maUnknownAttributes.size() );
     208      281458 :     Attribute* pAttr = aSeq.getArray();
     209      281462 :     for( UnknownAttributeList::iterator attrIter = maUnknownAttributes.begin(); attrIter != maUnknownAttributes.end(); ++attrIter )
     210           4 :         (*attrIter).FillAttribute( pAttr++ );
     211      281458 :     return aSeq;
     212             : }
     213      286815 : Sequence< FastAttribute > FastAttributeList::getFastAttributes(  ) throw (RuntimeException, std::exception)
     214             : {
     215      286815 :     Sequence< FastAttribute > aSeq( maAttributeTokens.size() );
     216      286815 :     FastAttribute* pAttr = aSeq.getArray();
     217      672597 :     for (size_t i = 0; i < maAttributeTokens.size(); ++i)
     218             :     {
     219      385782 :         pAttr->Token = maAttributeTokens[i];
     220      385782 :         pAttr->Value = OUString( mpChunk + maAttributeValues[i], AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
     221      385782 :         pAttr++;
     222             :     }
     223      286815 :     return aSeq;
     224             : }
     225             : 
     226     2485695 : sal_Int32 FastAttributeList::AttributeValueLength(sal_Int32 i)
     227             : {
     228             :     // Pointers to null terminated strings
     229     2485695 :     return maAttributeValues[i + 1] - maAttributeValues[i] - 1;
     230             : }
     231             : 
     232      625945 : FastTokenLookup::FastTokenLookup()
     233             : {
     234      625945 :     maUtf8Buffer.realloc( mnUtf8BufferSize );
     235      625945 : }
     236             : 
     237             : /**
     238             :  * Avoid doing any memory allocation if we can, instead keep a
     239             :  * pet sequence around and do some heavy petting on it.
     240             :  */
     241     3273135 : sal_Int32 FastTokenLookup::getTokenFromChars(
     242             :         const ::css::uno::Reference< ::css::xml::sax::XFastTokenHandler > &xTokenHandler,
     243             :         FastTokenHandlerBase *pTokenHandler,
     244             :         const char *pToken, size_t nLen /* = 0 */ )
     245             : {
     246             :     sal_Int32 nRet;
     247             : 
     248     3273135 :     if( !nLen )
     249           8 :         nLen = strlen( pToken );
     250             : 
     251     3273135 :     if( pTokenHandler )
     252     3242547 :         nRet = pTokenHandler->getTokenDirect( pToken, (sal_Int32) nLen );
     253             :     else
     254             :     {
     255             :         // heap allocate, copy & then free
     256       30588 :         Sequence< sal_Int8 > aSeq( (sal_Int8*)pToken, nLen );
     257       30588 :         nRet = xTokenHandler->getTokenFromUTF8( aSeq );
     258             :     }
     259             : 
     260     3273136 :     return nRet;
     261             : }
     262             : 
     263             : }
     264             : 
     265             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10