1*ca5ec200SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ca5ec200SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ca5ec200SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ca5ec200SAndrew Rist * distributed with this work for additional information 6*ca5ec200SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ca5ec200SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ca5ec200SAndrew Rist * "License"); you may not use this file except in compliance 9*ca5ec200SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*ca5ec200SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*ca5ec200SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ca5ec200SAndrew Rist * software distributed under the License is distributed on an 15*ca5ec200SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ca5ec200SAndrew Rist * KIND, either express or implied. See the License for the 17*ca5ec200SAndrew Rist * specific language governing permissions and limitations 18*ca5ec200SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*ca5ec200SAndrew Rist *************************************************************/ 21*ca5ec200SAndrew Rist 22*ca5ec200SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include "oox/helper/containerhelper.hxx" 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <com/sun/star/container/XIndexContainer.hpp> 27cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp> 28cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp> 29cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp> 30cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 31cdf0e10cSrcweir #include "oox/helper/helper.hxx" 32cdf0e10cSrcweir 33cdf0e10cSrcweir namespace oox { 34cdf0e10cSrcweir 35cdf0e10cSrcweir // ============================================================================ 36cdf0e10cSrcweir 37cdf0e10cSrcweir using namespace ::com::sun::star::container; 38cdf0e10cSrcweir using namespace ::com::sun::star::lang; 39cdf0e10cSrcweir using namespace ::com::sun::star::uno; 40cdf0e10cSrcweir 41cdf0e10cSrcweir using ::rtl::OUString; 42cdf0e10cSrcweir using ::rtl::OUStringBuffer; 43cdf0e10cSrcweir 44cdf0e10cSrcweir // ============================================================================ 45cdf0e10cSrcweir 46cdf0e10cSrcweir namespace { 47cdf0e10cSrcweir 48cdf0e10cSrcweir struct ValueRangeComp 49cdf0e10cSrcweir { 50cdf0e10cSrcweir inline bool operator()( const ValueRange& rRange, sal_Int32 nValue ) const { return rRange.mnLast < nValue; } 51cdf0e10cSrcweir }; 52cdf0e10cSrcweir 53cdf0e10cSrcweir } // namespace 54cdf0e10cSrcweir 55cdf0e10cSrcweir // ---------------------------------------------------------------------------- 56cdf0e10cSrcweir 57cdf0e10cSrcweir void ValueRangeSet::insert( const ValueRange& rRange ) 58cdf0e10cSrcweir { 59cdf0e10cSrcweir // find the first range that contains or follows the starting point of the passed range 60cdf0e10cSrcweir ValueRangeVector::iterator aBeg = maRanges.begin(); 61cdf0e10cSrcweir ValueRangeVector::iterator aEnd = maRanges.end(); 62cdf0e10cSrcweir ValueRangeVector::iterator aIt = ::std::lower_bound( aBeg, aEnd, rRange.mnFirst, ValueRangeComp() ); 63cdf0e10cSrcweir // nothing to do if found range contains passed range 64cdf0e10cSrcweir if( (aIt != aEnd) && aIt->contains( rRange ) ) return; 65cdf0e10cSrcweir // check if previous range can be used to merge with the passed range 66cdf0e10cSrcweir if( (aIt != aBeg) && ((aIt - 1)->mnLast + 1 == rRange.mnFirst) ) --aIt; 67cdf0e10cSrcweir // check if current range (aIt) can be used to merge with passed range 68cdf0e10cSrcweir if( (aIt != aEnd) && aIt->intersects( rRange ) ) 69cdf0e10cSrcweir { 70cdf0e10cSrcweir // set new start value to existing range 71cdf0e10cSrcweir aIt->mnFirst = ::std::min( aIt->mnFirst, rRange.mnFirst ); 72cdf0e10cSrcweir // search first range that cannot be merged anymore (aNext) 73cdf0e10cSrcweir ValueRangeVector::iterator aNext = aIt + 1; 74cdf0e10cSrcweir while( (aNext != aEnd) && aNext->intersects( rRange ) ) ++aNext; 75cdf0e10cSrcweir // set new end value to existing range 76cdf0e10cSrcweir aIt->mnLast = ::std::max( (aNext - 1)->mnLast, rRange.mnLast ); 77cdf0e10cSrcweir // remove ranges covered by new existing range (aIt) 78cdf0e10cSrcweir maRanges.erase( aIt + 1, aNext ); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir else 81cdf0e10cSrcweir { 82cdf0e10cSrcweir // merging not possible: insert new range 83cdf0e10cSrcweir maRanges.insert( aIt, rRange ); 84cdf0e10cSrcweir } 85cdf0e10cSrcweir } 86cdf0e10cSrcweir 87cdf0e10cSrcweir ValueRangeVector ValueRangeSet::getIntersection( const ValueRange& rRange ) const 88cdf0e10cSrcweir { 89cdf0e10cSrcweir ValueRangeVector aRanges; 90cdf0e10cSrcweir // find the range that contains nFirst or the first range that follows nFirst 91cdf0e10cSrcweir ValueRangeVector::const_iterator aIt = ::std::lower_bound( maRanges.begin(), maRanges.end(), rRange.mnFirst, ValueRangeComp() ); 92cdf0e10cSrcweir for( ValueRangeVector::const_iterator aEnd = maRanges.end(); (aIt != aEnd) && (aIt->mnFirst <= rRange.mnLast); ++aIt ) 93cdf0e10cSrcweir aRanges.push_back( ValueRange( ::std::max( aIt->mnFirst, rRange.mnFirst ), ::std::min( aIt->mnLast, rRange.mnLast ) ) ); 94cdf0e10cSrcweir return aRanges; 95cdf0e10cSrcweir } 96cdf0e10cSrcweir 97cdf0e10cSrcweir // ============================================================================ 98cdf0e10cSrcweir 99cdf0e10cSrcweir Reference< XIndexContainer > ContainerHelper::createIndexContainer( const Reference< XComponentContext >& rxContext ) 100cdf0e10cSrcweir { 101cdf0e10cSrcweir Reference< XIndexContainer > xContainer; 102cdf0e10cSrcweir if( rxContext.is() ) try 103cdf0e10cSrcweir { 104cdf0e10cSrcweir Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW ); 105cdf0e10cSrcweir xContainer.set( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.document.IndexedPropertyValues" ) ), UNO_QUERY_THROW ); 106cdf0e10cSrcweir } 107cdf0e10cSrcweir catch( Exception& ) 108cdf0e10cSrcweir { 109cdf0e10cSrcweir } 110cdf0e10cSrcweir OSL_ENSURE( xContainer.is(), "ContainerHelper::createIndexContainer - cannot create container" ); 111cdf0e10cSrcweir return xContainer; 112cdf0e10cSrcweir } 113cdf0e10cSrcweir 114cdf0e10cSrcweir bool ContainerHelper::insertByIndex( 115cdf0e10cSrcweir const Reference< XIndexContainer >& rxIndexContainer, 116cdf0e10cSrcweir sal_Int32 nIndex, const Any& rObject ) 117cdf0e10cSrcweir { 118cdf0e10cSrcweir OSL_ENSURE( rxIndexContainer.is(), "ContainerHelper::insertByIndex - missing XIndexContainer interface" ); 119cdf0e10cSrcweir bool bRet = false; 120cdf0e10cSrcweir try 121cdf0e10cSrcweir { 122cdf0e10cSrcweir rxIndexContainer->insertByIndex( nIndex, rObject ); 123cdf0e10cSrcweir bRet = true; 124cdf0e10cSrcweir } 125cdf0e10cSrcweir catch( Exception& ) 126cdf0e10cSrcweir { 127cdf0e10cSrcweir } 128cdf0e10cSrcweir OSL_ENSURE( bRet, "ContainerHelper::insertByIndex - cannot insert object" ); 129cdf0e10cSrcweir return bRet; 130cdf0e10cSrcweir } 131cdf0e10cSrcweir 132cdf0e10cSrcweir Reference< XNameContainer > ContainerHelper::createNameContainer( const Reference< XComponentContext >& rxContext ) 133cdf0e10cSrcweir { 134cdf0e10cSrcweir Reference< XNameContainer > xContainer; 135cdf0e10cSrcweir if( rxContext.is() ) try 136cdf0e10cSrcweir { 137cdf0e10cSrcweir Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW ); 138cdf0e10cSrcweir xContainer.set( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.document.NamedPropertyValues" ) ), UNO_QUERY_THROW ); 139cdf0e10cSrcweir } 140cdf0e10cSrcweir catch( Exception& ) 141cdf0e10cSrcweir { 142cdf0e10cSrcweir } 143cdf0e10cSrcweir OSL_ENSURE( xContainer.is(), "ContainerHelper::createNameContainer - cannot create container" ); 144cdf0e10cSrcweir return xContainer; 145cdf0e10cSrcweir } 146cdf0e10cSrcweir 147cdf0e10cSrcweir OUString ContainerHelper::getUnusedName( 148cdf0e10cSrcweir const Reference< XNameAccess >& rxNameAccess, const OUString& rSuggestedName, 149cdf0e10cSrcweir sal_Unicode cSeparator, sal_Int32 nFirstIndexToAppend ) 150cdf0e10cSrcweir { 151cdf0e10cSrcweir OSL_ENSURE( rxNameAccess.is(), "ContainerHelper::getUnusedName - missing XNameAccess interface" ); 152cdf0e10cSrcweir 153cdf0e10cSrcweir OUString aNewName = rSuggestedName; 154cdf0e10cSrcweir sal_Int32 nIndex = nFirstIndexToAppend; 155cdf0e10cSrcweir while( rxNameAccess->hasByName( aNewName ) ) 156cdf0e10cSrcweir aNewName = OUStringBuffer( rSuggestedName ).append( cSeparator ).append( nIndex++ ).makeStringAndClear(); 157cdf0e10cSrcweir return aNewName; 158cdf0e10cSrcweir } 159cdf0e10cSrcweir 160cdf0e10cSrcweir bool ContainerHelper::insertByName( 161cdf0e10cSrcweir const Reference< XNameContainer >& rxNameContainer, 162cdf0e10cSrcweir const OUString& rName, const Any& rObject, bool bReplaceOldExisting ) 163cdf0e10cSrcweir { 164cdf0e10cSrcweir OSL_ENSURE( rxNameContainer.is(), "ContainerHelper::insertByName - missing XNameContainer interface" ); 165cdf0e10cSrcweir bool bRet = false; 166cdf0e10cSrcweir try 167cdf0e10cSrcweir { 168cdf0e10cSrcweir if( bReplaceOldExisting && rxNameContainer->hasByName( rName ) ) 169cdf0e10cSrcweir rxNameContainer->replaceByName( rName, rObject ); 170cdf0e10cSrcweir else 171cdf0e10cSrcweir rxNameContainer->insertByName( rName, rObject ); 172cdf0e10cSrcweir bRet = true; 173cdf0e10cSrcweir } 174cdf0e10cSrcweir catch( Exception& ) 175cdf0e10cSrcweir { 176cdf0e10cSrcweir } 177cdf0e10cSrcweir OSL_ENSURE( bRet, "ContainerHelper::insertByName - cannot insert object" ); 178cdf0e10cSrcweir return bRet; 179cdf0e10cSrcweir } 180cdf0e10cSrcweir 181cdf0e10cSrcweir OUString ContainerHelper::insertByUnusedName( 182cdf0e10cSrcweir const Reference< XNameContainer >& rxNameContainer, 183cdf0e10cSrcweir const OUString& rSuggestedName, sal_Unicode cSeparator, 184cdf0e10cSrcweir const Any& rObject, bool bRenameOldExisting ) 185cdf0e10cSrcweir { 186cdf0e10cSrcweir OSL_ENSURE( rxNameContainer.is(), "ContainerHelper::insertByUnusedName - missing XNameContainer interface" ); 187cdf0e10cSrcweir 188cdf0e10cSrcweir // find an unused name 189cdf0e10cSrcweir Reference< XNameAccess > xNameAccess( rxNameContainer, UNO_QUERY ); 190cdf0e10cSrcweir OUString aNewName = getUnusedName( xNameAccess, rSuggestedName, cSeparator ); 191cdf0e10cSrcweir 192cdf0e10cSrcweir // rename existing object 193cdf0e10cSrcweir if( bRenameOldExisting && rxNameContainer->hasByName( rSuggestedName ) ) 194cdf0e10cSrcweir { 195cdf0e10cSrcweir try 196cdf0e10cSrcweir { 197cdf0e10cSrcweir Any aOldObject = rxNameContainer->getByName( rSuggestedName ); 198cdf0e10cSrcweir rxNameContainer->removeByName( rSuggestedName ); 199cdf0e10cSrcweir rxNameContainer->insertByName( aNewName, aOldObject ); 200cdf0e10cSrcweir aNewName = rSuggestedName; 201cdf0e10cSrcweir } 202cdf0e10cSrcweir catch( Exception& ) 203cdf0e10cSrcweir { 204cdf0e10cSrcweir OSL_ENSURE( false, "ContainerHelper::insertByUnusedName - cannot rename old object" ); 205cdf0e10cSrcweir } 206cdf0e10cSrcweir } 207cdf0e10cSrcweir 208cdf0e10cSrcweir // insert the new object and return its resulting name 209cdf0e10cSrcweir insertByName( rxNameContainer, aNewName, rObject ); 210cdf0e10cSrcweir return aNewName; 211cdf0e10cSrcweir } 212cdf0e10cSrcweir 213cdf0e10cSrcweir // ============================================================================ 214cdf0e10cSrcweir 215cdf0e10cSrcweir } // namespace oox 216