/src/libreoffice/oox/source/helper/containerhelper.cxx
Line | Count | Source (jump to first uncovered line) |
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 <algorithm> |
21 | | |
22 | | #include <oox/helper/containerhelper.hxx> |
23 | | |
24 | | #include <com/sun/star/container/XNameContainer.hpp> |
25 | | #include <osl/diagnose.h> |
26 | | |
27 | | namespace oox { |
28 | | |
29 | | using namespace ::com::sun::star::container; |
30 | | using namespace ::com::sun::star::uno; |
31 | | |
32 | | namespace { |
33 | | |
34 | | struct ValueRangeComp |
35 | | { |
36 | | bool operator()( const ValueRange& rLHS, const ValueRange& rRHS ) const |
37 | 0 | { |
38 | 0 | return rLHS.mnLast < rRHS.mnFirst; |
39 | 0 | } |
40 | | }; |
41 | | |
42 | | } // namespace |
43 | | |
44 | | void ValueRangeSet::insert( const ValueRange& rRange ) |
45 | 3 | { |
46 | | // find the first range that contains or follows the starting point of the passed range |
47 | 3 | ValueRangeVector::iterator aBeg = maRanges.begin(); |
48 | 3 | ValueRangeVector::iterator aEnd = maRanges.end(); |
49 | 3 | ValueRangeVector::iterator aIt = ::std::lower_bound( aBeg, aEnd, rRange, ValueRangeComp() ); |
50 | | // nothing to do if found range contains passed range |
51 | 3 | if( (aIt != aEnd) && aIt->contains( rRange ) ) return; |
52 | | // check if previous range can be used to merge with the passed range |
53 | 3 | if( (aIt != aBeg) && ((aIt - 1)->mnLast + 1 == rRange.mnFirst) ) --aIt; |
54 | | // check if current range (aIt) can be used to merge with passed range |
55 | 3 | if( (aIt != aEnd) && aIt->intersects( rRange ) ) |
56 | 0 | { |
57 | | // set new start value to existing range |
58 | 0 | aIt->mnFirst = ::std::min( aIt->mnFirst, rRange.mnFirst ); |
59 | | // search first range that cannot be merged anymore (aNext) |
60 | 0 | ValueRangeVector::iterator aNext = aIt + 1; |
61 | 0 | while( (aNext != aEnd) && aNext->intersects( rRange ) ) ++aNext; |
62 | | // set new end value to existing range |
63 | 0 | aIt->mnLast = ::std::max( (aNext - 1)->mnLast, rRange.mnLast ); |
64 | | // remove ranges covered by new existing range (aIt) |
65 | 0 | maRanges.erase( aIt + 1, aNext ); |
66 | 0 | } |
67 | 3 | else |
68 | 3 | { |
69 | | // merging not possible: insert new range |
70 | 3 | maRanges.insert( aIt, rRange ); |
71 | 3 | } |
72 | 3 | } |
73 | | |
74 | | OUString ContainerHelper::getUnusedName( |
75 | | const Reference< XNameAccess >& rxNameAccess, const OUString& rSuggestedName, |
76 | | sal_Unicode cSeparator ) |
77 | 16.7k | { |
78 | 16.7k | OSL_ENSURE( rxNameAccess.is(), "ContainerHelper::getUnusedName - missing XNameAccess interface" ); |
79 | | |
80 | 16.7k | OUString aNewName = rSuggestedName; |
81 | 16.7k | sal_Int32 nIndex = -1; |
82 | 16.7k | while( rxNameAccess->hasByName( aNewName ) ) |
83 | 0 | aNewName = rSuggestedName + OUStringChar(cSeparator) + OUString::number( nIndex++ ); |
84 | 16.7k | return aNewName; |
85 | 16.7k | } |
86 | | |
87 | | bool ContainerHelper::insertByName( |
88 | | const Reference< XNameContainer >& rxNameContainer, |
89 | | const OUString& rName, const Any& rObject ) |
90 | 14.7k | { |
91 | 14.7k | OSL_ENSURE( rxNameContainer.is(), "ContainerHelper::insertByName - missing XNameContainer interface" ); |
92 | 14.7k | bool bRet = false; |
93 | 14.7k | try |
94 | 14.7k | { |
95 | 14.7k | if( rxNameContainer->hasByName( rName ) ) |
96 | 0 | rxNameContainer->replaceByName( rName, rObject ); |
97 | 14.7k | else |
98 | 14.7k | rxNameContainer->insertByName( rName, rObject ); |
99 | 14.7k | bRet = true; |
100 | 14.7k | } |
101 | 14.7k | catch( Exception& ) |
102 | 14.7k | { |
103 | 0 | } |
104 | 14.7k | OSL_ENSURE( bRet, "ContainerHelper::insertByName - cannot insert object" ); |
105 | 14.7k | return bRet; |
106 | 14.7k | } |
107 | | |
108 | | OUString ContainerHelper::insertByUnusedName( |
109 | | const Reference< XNameContainer >& rxNameContainer, |
110 | | const OUString& rSuggestedName, sal_Unicode cSeparator, |
111 | | const Any& rObject ) |
112 | 7.30k | { |
113 | 7.30k | OSL_ENSURE( rxNameContainer.is(), "ContainerHelper::insertByUnusedName - missing XNameContainer interface" ); |
114 | | |
115 | | // find an unused name |
116 | 7.30k | OUString aNewName = getUnusedName( rxNameContainer, rSuggestedName, cSeparator ); |
117 | | |
118 | | // insert the new object and return its resulting name |
119 | 7.30k | insertByName( rxNameContainer, aNewName, rObject ); |
120 | 7.30k | return aNewName; |
121 | 7.30k | } |
122 | | |
123 | | } // namespace oox |
124 | | |
125 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |