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 55 : void ValueRangeSet::insert( const ValueRange& rRange )
54 : {
55 : // find the first range that contains or follows the starting point of the passed range
56 55 : ValueRangeVector::iterator aBeg = maRanges.begin();
57 55 : ValueRangeVector::iterator aEnd = maRanges.end();
58 55 : ValueRangeVector::iterator aIt = ::std::lower_bound( aBeg, aEnd, rRange, ValueRangeComp() );
59 : // nothing to do if found range contains passed range
60 110 : if( (aIt != aEnd) && aIt->contains( rRange ) ) return;
61 : // check if previous range can be used to merge with the passed range
62 55 : 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 55 : 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 55 : maRanges.insert( aIt, rRange );
80 : }
81 : }
82 :
83 : // ============================================================================
84 :
85 11 : Reference< XIndexContainer > ContainerHelper::createIndexContainer( const Reference< XComponentContext >& rxContext )
86 : {
87 11 : Reference< XIndexContainer > xContainer;
88 11 : if( rxContext.is() ) try
89 : {
90 11 : Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW );
91 11 : xContainer.set( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.document.IndexedPropertyValues" ) ), UNO_QUERY_THROW );
92 : }
93 0 : catch( Exception& )
94 : {
95 : }
96 : OSL_ENSURE( xContainer.is(), "ContainerHelper::createIndexContainer - cannot create container" );
97 11 : return xContainer;
98 : }
99 :
100 11 : Reference< XNameContainer > ContainerHelper::createNameContainer( const Reference< XComponentContext >& rxContext )
101 : {
102 11 : Reference< XNameContainer > xContainer;
103 11 : if( rxContext.is() ) try
104 : {
105 11 : Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW );
106 11 : xContainer.set( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.document.NamedPropertyValues" ) ), UNO_QUERY_THROW );
107 : }
108 0 : catch( Exception& )
109 : {
110 : }
111 : OSL_ENSURE( xContainer.is(), "ContainerHelper::createNameContainer - cannot create container" );
112 11 : return xContainer;
113 : }
114 :
115 75 : OUString ContainerHelper::getUnusedName(
116 : const Reference< XNameAccess >& rxNameAccess, const OUString& rSuggestedName,
117 : sal_Unicode cSeparator, sal_Int32 nFirstIndexToAppend )
118 : {
119 : OSL_ENSURE( rxNameAccess.is(), "ContainerHelper::getUnusedName - missing XNameAccess interface" );
120 :
121 75 : OUString aNewName = rSuggestedName;
122 75 : sal_Int32 nIndex = nFirstIndexToAppend;
123 150 : while( rxNameAccess->hasByName( aNewName ) )
124 0 : aNewName = OUStringBuffer( rSuggestedName ).append( cSeparator ).append( nIndex++ ).makeStringAndClear();
125 75 : return aNewName;
126 : }
127 :
128 144 : bool ContainerHelper::insertByName(
129 : const Reference< XNameContainer >& rxNameContainer,
130 : const OUString& rName, const Any& rObject, bool bReplaceOldExisting )
131 : {
132 : OSL_ENSURE( rxNameContainer.is(), "ContainerHelper::insertByName - missing XNameContainer interface" );
133 144 : bool bRet = false;
134 : try
135 : {
136 144 : if( bReplaceOldExisting && rxNameContainer->hasByName( rName ) )
137 0 : rxNameContainer->replaceByName( rName, rObject );
138 : else
139 144 : rxNameContainer->insertByName( rName, rObject );
140 144 : bRet = true;
141 : }
142 0 : catch( Exception& )
143 : {
144 : }
145 : OSL_ENSURE( bRet, "ContainerHelper::insertByName - cannot insert object" );
146 144 : return bRet;
147 : }
148 :
149 61 : OUString ContainerHelper::insertByUnusedName(
150 : const Reference< XNameContainer >& rxNameContainer,
151 : const OUString& rSuggestedName, sal_Unicode cSeparator,
152 : const Any& rObject, bool bRenameOldExisting )
153 : {
154 : OSL_ENSURE( rxNameContainer.is(), "ContainerHelper::insertByUnusedName - missing XNameContainer interface" );
155 :
156 : // find an unused name
157 61 : Reference< XNameAccess > xNameAccess( rxNameContainer, UNO_QUERY );
158 61 : OUString aNewName = getUnusedName( xNameAccess, rSuggestedName, cSeparator );
159 :
160 : // rename existing object
161 61 : if( bRenameOldExisting && rxNameContainer->hasByName( rSuggestedName ) )
162 : {
163 : try
164 : {
165 0 : Any aOldObject = rxNameContainer->getByName( rSuggestedName );
166 0 : rxNameContainer->removeByName( rSuggestedName );
167 0 : rxNameContainer->insertByName( aNewName, aOldObject );
168 0 : aNewName = rSuggestedName;
169 : }
170 0 : catch( Exception& )
171 : {
172 : OSL_FAIL( "ContainerHelper::insertByUnusedName - cannot rename old object" );
173 : }
174 : }
175 :
176 : // insert the new object and return its resulting name
177 61 : insertByName( rxNameContainer, aNewName, rObject );
178 61 : return aNewName;
179 : }
180 :
181 : // ============================================================================
182 :
183 : } // namespace oox
184 :
185 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|