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