LCOV - code coverage report
Current view: top level - oox/source/helper - containerhelper.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 24 39 61.5 %
Date: 2014-11-03 Functions: 4 5 80.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             : #include "oox/helper/containerhelper.hxx"
      21             : 
      22             : #include <com/sun/star/container/XIndexContainer.hpp>
      23             : #include <com/sun/star/container/XNameContainer.hpp>
      24             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      25             : #include <com/sun/star/uno/XComponentContext.hpp>
      26             : #include <rtl/ustrbuf.hxx>
      27             : #include "oox/helper/helper.hxx"
      28             : 
      29             : namespace oox {
      30             : 
      31             : using namespace ::com::sun::star::container;
      32             : using namespace ::com::sun::star::lang;
      33             : using namespace ::com::sun::star::uno;
      34             : 
      35             : namespace {
      36             : 
      37             : struct ValueRangeComp
      38             : {
      39           0 :     inline bool operator()( const ValueRange& rLHS, const ValueRange& rRHS ) const
      40             :     {
      41           0 :         return rLHS.mnLast < rRHS.mnFirst;
      42             :     }
      43             : };
      44             : 
      45             : } // namespace
      46             : 
      47        1278 : void ValueRangeSet::insert( const ValueRange& rRange )
      48             : {
      49             :     // find the first range that contains or follows the starting point of the passed range
      50        1278 :     ValueRangeVector::iterator aBeg = maRanges.begin();
      51        1278 :     ValueRangeVector::iterator aEnd = maRanges.end();
      52        1278 :     ValueRangeVector::iterator aIt = ::std::lower_bound( aBeg, aEnd, rRange, ValueRangeComp() );
      53             :     // nothing to do if found range contains passed range
      54        2556 :     if( (aIt != aEnd) && aIt->contains( rRange ) ) return;
      55             :     // check if previous range can be used to merge with the passed range
      56        1278 :     if( (aIt != aBeg) && ((aIt - 1)->mnLast + 1 == rRange.mnFirst) ) --aIt;
      57             :     // check if current range (aIt) can be used to merge with passed range
      58        1278 :     if( (aIt != aEnd) && aIt->intersects( rRange ) )
      59             :     {
      60             :         // set new start value to existing range
      61           0 :         aIt->mnFirst = ::std::min( aIt->mnFirst, rRange.mnFirst );
      62             :         // search first range that cannot be merged anymore (aNext)
      63           0 :         ValueRangeVector::iterator aNext = aIt + 1;
      64           0 :         while( (aNext != aEnd) && aNext->intersects( rRange ) ) ++aNext;
      65             :         // set new end value to existing range
      66           0 :         aIt->mnLast = ::std::max( (aNext - 1)->mnLast, rRange.mnLast );
      67             :         // remove ranges covered by new existing range (aIt)
      68           0 :         maRanges.erase( aIt + 1, aNext );
      69             :     }
      70             :     else
      71             :     {
      72             :         // merging not possible: insert new range
      73        1278 :         maRanges.insert( aIt, rRange );
      74             :     }
      75             : }
      76             : 
      77         590 : OUString ContainerHelper::getUnusedName(
      78             :         const Reference< XNameAccess >& rxNameAccess, const OUString& rSuggestedName,
      79             :         sal_Unicode cSeparator, sal_Int32 nFirstIndexToAppend )
      80             : {
      81             :     OSL_ENSURE( rxNameAccess.is(), "ContainerHelper::getUnusedName - missing XNameAccess interface" );
      82             : 
      83         590 :     OUString aNewName = rSuggestedName;
      84         590 :     sal_Int32 nIndex = nFirstIndexToAppend;
      85        1180 :     while( rxNameAccess->hasByName( aNewName ) )
      86           0 :         aNewName = OUStringBuffer( rSuggestedName ).append( cSeparator ).append( nIndex++ ).makeStringAndClear();
      87         590 :     return aNewName;
      88             : }
      89             : 
      90        1662 : bool ContainerHelper::insertByName(
      91             :         const Reference< XNameContainer >& rxNameContainer,
      92             :         const OUString& rName, const Any& rObject, bool bReplaceOldExisting )
      93             : {
      94             :     OSL_ENSURE( rxNameContainer.is(), "ContainerHelper::insertByName - missing XNameContainer interface" );
      95        1662 :     bool bRet = false;
      96             :     try
      97             :     {
      98        1662 :         if( bReplaceOldExisting && rxNameContainer->hasByName( rName ) )
      99           0 :             rxNameContainer->replaceByName( rName, rObject );
     100             :         else
     101        1662 :             rxNameContainer->insertByName( rName, rObject );
     102        1662 :         bRet = true;
     103             :     }
     104           0 :     catch( Exception& )
     105             :     {
     106             :     }
     107             :     OSL_ENSURE( bRet, "ContainerHelper::insertByName - cannot insert object" );
     108        1662 :     return bRet;
     109             : }
     110             : 
     111         438 : OUString ContainerHelper::insertByUnusedName(
     112             :         const Reference< XNameContainer >& rxNameContainer,
     113             :         const OUString& rSuggestedName, sal_Unicode cSeparator,
     114             :         const Any& rObject, bool bRenameOldExisting )
     115             : {
     116             :     OSL_ENSURE( rxNameContainer.is(), "ContainerHelper::insertByUnusedName - missing XNameContainer interface" );
     117             : 
     118             :     // find an unused name
     119         438 :     OUString aNewName = getUnusedName( rxNameContainer, rSuggestedName, cSeparator );
     120             : 
     121             :     // rename existing object
     122         438 :     if( bRenameOldExisting && rxNameContainer->hasByName( rSuggestedName ) )
     123             :     {
     124             :         try
     125             :         {
     126           0 :             Any aOldObject = rxNameContainer->getByName( rSuggestedName );
     127           0 :             rxNameContainer->removeByName( rSuggestedName );
     128           0 :             rxNameContainer->insertByName( aNewName, aOldObject );
     129           0 :             aNewName = rSuggestedName;
     130             :         }
     131           0 :         catch( Exception& )
     132             :         {
     133             :             OSL_FAIL( "ContainerHelper::insertByUnusedName - cannot rename old object" );
     134             :         }
     135             :     }
     136             : 
     137             :     // insert the new object and return its resulting name
     138         438 :     insertByName( rxNameContainer, aNewName, rObject );
     139         438 :     return aNewName;
     140             : }
     141             : 
     142             : } // namespace oox
     143             : 
     144             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10