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