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