bump product version to 6.3.0.0.beta1
[LibreOffice.git] / oox / source / helper / containerhelper.cxx
blobeea52e254ac29ca5af503621fcddd1f76bc05206
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <algorithm>
22 #include <oox/helper/containerhelper.hxx>
24 #include <com/sun/star/container/XIndexContainer.hpp>
25 #include <com/sun/star/container/XNameContainer.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/uno/XComponentContext.hpp>
28 #include <osl/diagnose.h>
29 #include <rtl/ustrbuf.hxx>
30 #include <oox/helper/helper.hxx>
32 namespace oox {
34 using namespace ::com::sun::star::container;
35 using namespace ::com::sun::star::lang;
36 using namespace ::com::sun::star::uno;
38 namespace {
40 struct ValueRangeComp
42 bool operator()( const ValueRange& rLHS, const ValueRange& rRHS ) const
44 return rLHS.mnLast < rRHS.mnFirst;
48 } // namespace
50 void ValueRangeSet::insert( const ValueRange& rRange )
52 // find the first range that contains or follows the starting point of the passed range
53 ValueRangeVector::iterator aBeg = maRanges.begin();
54 ValueRangeVector::iterator aEnd = maRanges.end();
55 ValueRangeVector::iterator aIt = ::std::lower_bound( aBeg, aEnd, rRange, ValueRangeComp() );
56 // nothing to do if found range contains passed range
57 if( (aIt != aEnd) && aIt->contains( rRange ) ) return;
58 // check if previous range can be used to merge with the passed range
59 if( (aIt != aBeg) && ((aIt - 1)->mnLast + 1 == rRange.mnFirst) ) --aIt;
60 // check if current range (aIt) can be used to merge with passed range
61 if( (aIt != aEnd) && aIt->intersects( rRange ) )
63 // set new start value to existing range
64 aIt->mnFirst = ::std::min( aIt->mnFirst, rRange.mnFirst );
65 // search first range that cannot be merged anymore (aNext)
66 ValueRangeVector::iterator aNext = aIt + 1;
67 while( (aNext != aEnd) && aNext->intersects( rRange ) ) ++aNext;
68 // set new end value to existing range
69 aIt->mnLast = ::std::max( (aNext - 1)->mnLast, rRange.mnLast );
70 // remove ranges covered by new existing range (aIt)
71 maRanges.erase( aIt + 1, aNext );
73 else
75 // merging not possible: insert new range
76 maRanges.insert( aIt, rRange );
80 OUString ContainerHelper::getUnusedName(
81 const Reference< XNameAccess >& rxNameAccess, const OUString& rSuggestedName,
82 sal_Unicode cSeparator )
84 OSL_ENSURE( rxNameAccess.is(), "ContainerHelper::getUnusedName - missing XNameAccess interface" );
86 OUString aNewName = rSuggestedName;
87 sal_Int32 nIndex = -1;
88 while( rxNameAccess->hasByName( aNewName ) )
89 aNewName = rSuggestedName + OUStringLiteral1(cSeparator) + OUString::number( nIndex++ );
90 return aNewName;
93 bool ContainerHelper::insertByName(
94 const Reference< XNameContainer >& rxNameContainer,
95 const OUString& rName, const Any& rObject )
97 OSL_ENSURE( rxNameContainer.is(), "ContainerHelper::insertByName - missing XNameContainer interface" );
98 bool bRet = false;
99 try
101 if( rxNameContainer->hasByName( rName ) )
102 rxNameContainer->replaceByName( rName, rObject );
103 else
104 rxNameContainer->insertByName( rName, rObject );
105 bRet = true;
107 catch( Exception& )
110 OSL_ENSURE( bRet, "ContainerHelper::insertByName - cannot insert object" );
111 return bRet;
114 OUString ContainerHelper::insertByUnusedName(
115 const Reference< XNameContainer >& rxNameContainer,
116 const OUString& rSuggestedName, sal_Unicode cSeparator,
117 const Any& rObject )
119 OSL_ENSURE( rxNameContainer.is(), "ContainerHelper::insertByUnusedName - missing XNameContainer interface" );
121 // find an unused name
122 OUString aNewName = getUnusedName( rxNameContainer, rSuggestedName, cSeparator );
124 // insert the new object and return its resulting name
125 insertByName( rxNameContainer, aNewName, rObject );
126 return aNewName;
129 } // namespace oox
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */