1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "oox/helper/containerhelper.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <com/sun/star/container/XIndexContainer.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
34*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
35*cdf0e10cSrcweir #include "oox/helper/helper.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir namespace oox {
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir // ============================================================================
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir using namespace ::com::sun::star::container;
42*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
43*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using ::rtl::OUString;
46*cdf0e10cSrcweir using ::rtl::OUStringBuffer;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir // ============================================================================
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir namespace {
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir struct ValueRangeComp
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir     inline bool         operator()( const ValueRange& rRange, sal_Int32 nValue ) const { return rRange.mnLast < nValue; }
55*cdf0e10cSrcweir };
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir } // namespace
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir // ----------------------------------------------------------------------------
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir void ValueRangeSet::insert( const ValueRange& rRange )
62*cdf0e10cSrcweir {
63*cdf0e10cSrcweir     // find the first range that contains or follows the starting point of the passed range
64*cdf0e10cSrcweir     ValueRangeVector::iterator aBeg = maRanges.begin();
65*cdf0e10cSrcweir     ValueRangeVector::iterator aEnd = maRanges.end();
66*cdf0e10cSrcweir     ValueRangeVector::iterator aIt = ::std::lower_bound( aBeg, aEnd, rRange.mnFirst, ValueRangeComp() );
67*cdf0e10cSrcweir     // nothing to do if found range contains passed range
68*cdf0e10cSrcweir     if( (aIt != aEnd) && aIt->contains( rRange ) ) return;
69*cdf0e10cSrcweir     // check if previous range can be used to merge with the passed range
70*cdf0e10cSrcweir     if( (aIt != aBeg) && ((aIt - 1)->mnLast + 1 == rRange.mnFirst) ) --aIt;
71*cdf0e10cSrcweir     // check if current range (aIt) can be used to merge with passed range
72*cdf0e10cSrcweir     if( (aIt != aEnd) && aIt->intersects( rRange ) )
73*cdf0e10cSrcweir     {
74*cdf0e10cSrcweir         // set new start value to existing range
75*cdf0e10cSrcweir         aIt->mnFirst = ::std::min( aIt->mnFirst, rRange.mnFirst );
76*cdf0e10cSrcweir         // search first range that cannot be merged anymore (aNext)
77*cdf0e10cSrcweir         ValueRangeVector::iterator aNext = aIt + 1;
78*cdf0e10cSrcweir         while( (aNext != aEnd) && aNext->intersects( rRange ) ) ++aNext;
79*cdf0e10cSrcweir         // set new end value to existing range
80*cdf0e10cSrcweir         aIt->mnLast = ::std::max( (aNext - 1)->mnLast, rRange.mnLast );
81*cdf0e10cSrcweir         // remove ranges covered by new existing range (aIt)
82*cdf0e10cSrcweir         maRanges.erase( aIt + 1, aNext );
83*cdf0e10cSrcweir     }
84*cdf0e10cSrcweir     else
85*cdf0e10cSrcweir     {
86*cdf0e10cSrcweir         // merging not possible: insert new range
87*cdf0e10cSrcweir         maRanges.insert( aIt, rRange );
88*cdf0e10cSrcweir     }
89*cdf0e10cSrcweir }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir ValueRangeVector ValueRangeSet::getIntersection( const ValueRange& rRange ) const
92*cdf0e10cSrcweir {
93*cdf0e10cSrcweir     ValueRangeVector aRanges;
94*cdf0e10cSrcweir     // find the range that contains nFirst or the first range that follows nFirst
95*cdf0e10cSrcweir     ValueRangeVector::const_iterator aIt = ::std::lower_bound( maRanges.begin(), maRanges.end(), rRange.mnFirst, ValueRangeComp() );
96*cdf0e10cSrcweir     for( ValueRangeVector::const_iterator aEnd = maRanges.end(); (aIt != aEnd) && (aIt->mnFirst <= rRange.mnLast); ++aIt )
97*cdf0e10cSrcweir         aRanges.push_back( ValueRange( ::std::max( aIt->mnFirst, rRange.mnFirst ), ::std::min( aIt->mnLast, rRange.mnLast ) ) );
98*cdf0e10cSrcweir     return aRanges;
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir // ============================================================================
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir Reference< XIndexContainer > ContainerHelper::createIndexContainer( const Reference< XComponentContext >& rxContext )
104*cdf0e10cSrcweir {
105*cdf0e10cSrcweir     Reference< XIndexContainer > xContainer;
106*cdf0e10cSrcweir     if( rxContext.is() ) try
107*cdf0e10cSrcweir     {
108*cdf0e10cSrcweir         Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW );
109*cdf0e10cSrcweir         xContainer.set( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.document.IndexedPropertyValues" ) ), UNO_QUERY_THROW );
110*cdf0e10cSrcweir     }
111*cdf0e10cSrcweir     catch( Exception& )
112*cdf0e10cSrcweir     {
113*cdf0e10cSrcweir     }
114*cdf0e10cSrcweir     OSL_ENSURE( xContainer.is(), "ContainerHelper::createIndexContainer - cannot create container" );
115*cdf0e10cSrcweir     return xContainer;
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir bool ContainerHelper::insertByIndex(
119*cdf0e10cSrcweir         const Reference< XIndexContainer >& rxIndexContainer,
120*cdf0e10cSrcweir         sal_Int32 nIndex, const Any& rObject )
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir     OSL_ENSURE( rxIndexContainer.is(), "ContainerHelper::insertByIndex - missing XIndexContainer interface" );
123*cdf0e10cSrcweir     bool bRet = false;
124*cdf0e10cSrcweir     try
125*cdf0e10cSrcweir     {
126*cdf0e10cSrcweir         rxIndexContainer->insertByIndex( nIndex, rObject );
127*cdf0e10cSrcweir         bRet = true;
128*cdf0e10cSrcweir     }
129*cdf0e10cSrcweir     catch( Exception& )
130*cdf0e10cSrcweir     {
131*cdf0e10cSrcweir     }
132*cdf0e10cSrcweir     OSL_ENSURE( bRet, "ContainerHelper::insertByIndex - cannot insert object" );
133*cdf0e10cSrcweir     return bRet;
134*cdf0e10cSrcweir }
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir Reference< XNameContainer > ContainerHelper::createNameContainer( const Reference< XComponentContext >& rxContext )
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir     Reference< XNameContainer > xContainer;
139*cdf0e10cSrcweir     if( rxContext.is() ) try
140*cdf0e10cSrcweir     {
141*cdf0e10cSrcweir         Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW );
142*cdf0e10cSrcweir         xContainer.set( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.document.NamedPropertyValues" ) ), UNO_QUERY_THROW );
143*cdf0e10cSrcweir     }
144*cdf0e10cSrcweir     catch( Exception& )
145*cdf0e10cSrcweir     {
146*cdf0e10cSrcweir     }
147*cdf0e10cSrcweir     OSL_ENSURE( xContainer.is(), "ContainerHelper::createNameContainer - cannot create container" );
148*cdf0e10cSrcweir     return xContainer;
149*cdf0e10cSrcweir }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir OUString ContainerHelper::getUnusedName(
152*cdf0e10cSrcweir         const Reference< XNameAccess >& rxNameAccess, const OUString& rSuggestedName,
153*cdf0e10cSrcweir         sal_Unicode cSeparator, sal_Int32 nFirstIndexToAppend )
154*cdf0e10cSrcweir {
155*cdf0e10cSrcweir     OSL_ENSURE( rxNameAccess.is(), "ContainerHelper::getUnusedName - missing XNameAccess interface" );
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     OUString aNewName = rSuggestedName;
158*cdf0e10cSrcweir     sal_Int32 nIndex = nFirstIndexToAppend;
159*cdf0e10cSrcweir     while( rxNameAccess->hasByName( aNewName ) )
160*cdf0e10cSrcweir         aNewName = OUStringBuffer( rSuggestedName ).append( cSeparator ).append( nIndex++ ).makeStringAndClear();
161*cdf0e10cSrcweir     return aNewName;
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir bool ContainerHelper::insertByName(
165*cdf0e10cSrcweir         const Reference< XNameContainer >& rxNameContainer,
166*cdf0e10cSrcweir         const OUString& rName, const Any& rObject, bool bReplaceOldExisting )
167*cdf0e10cSrcweir {
168*cdf0e10cSrcweir     OSL_ENSURE( rxNameContainer.is(), "ContainerHelper::insertByName - missing XNameContainer interface" );
169*cdf0e10cSrcweir     bool bRet = false;
170*cdf0e10cSrcweir     try
171*cdf0e10cSrcweir     {
172*cdf0e10cSrcweir         if( bReplaceOldExisting && rxNameContainer->hasByName( rName ) )
173*cdf0e10cSrcweir             rxNameContainer->replaceByName( rName, rObject );
174*cdf0e10cSrcweir         else
175*cdf0e10cSrcweir             rxNameContainer->insertByName( rName, rObject );
176*cdf0e10cSrcweir         bRet = true;
177*cdf0e10cSrcweir     }
178*cdf0e10cSrcweir     catch( Exception& )
179*cdf0e10cSrcweir     {
180*cdf0e10cSrcweir     }
181*cdf0e10cSrcweir     OSL_ENSURE( bRet, "ContainerHelper::insertByName - cannot insert object" );
182*cdf0e10cSrcweir     return bRet;
183*cdf0e10cSrcweir }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir OUString ContainerHelper::insertByUnusedName(
186*cdf0e10cSrcweir         const Reference< XNameContainer >& rxNameContainer,
187*cdf0e10cSrcweir         const OUString& rSuggestedName, sal_Unicode cSeparator,
188*cdf0e10cSrcweir         const Any& rObject, bool bRenameOldExisting )
189*cdf0e10cSrcweir {
190*cdf0e10cSrcweir     OSL_ENSURE( rxNameContainer.is(), "ContainerHelper::insertByUnusedName - missing XNameContainer interface" );
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir     // find an unused name
193*cdf0e10cSrcweir     Reference< XNameAccess > xNameAccess( rxNameContainer, UNO_QUERY );
194*cdf0e10cSrcweir     OUString aNewName = getUnusedName( xNameAccess, rSuggestedName, cSeparator );
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir     // rename existing object
197*cdf0e10cSrcweir     if( bRenameOldExisting && rxNameContainer->hasByName( rSuggestedName ) )
198*cdf0e10cSrcweir     {
199*cdf0e10cSrcweir         try
200*cdf0e10cSrcweir         {
201*cdf0e10cSrcweir             Any aOldObject = rxNameContainer->getByName( rSuggestedName );
202*cdf0e10cSrcweir             rxNameContainer->removeByName( rSuggestedName );
203*cdf0e10cSrcweir             rxNameContainer->insertByName( aNewName, aOldObject );
204*cdf0e10cSrcweir             aNewName = rSuggestedName;
205*cdf0e10cSrcweir         }
206*cdf0e10cSrcweir         catch( Exception& )
207*cdf0e10cSrcweir         {
208*cdf0e10cSrcweir             OSL_ENSURE( false, "ContainerHelper::insertByUnusedName - cannot rename old object" );
209*cdf0e10cSrcweir         }
210*cdf0e10cSrcweir     }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     // insert the new object and return its resulting name
213*cdf0e10cSrcweir     insertByName( rxNameContainer, aNewName, rObject );
214*cdf0e10cSrcweir     return aNewName;
215*cdf0e10cSrcweir }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir // ============================================================================
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir } // namespace oox
220