1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: containerhelper.cxx,v $
10 * $Revision: 1.4.6.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "oox/helper/containerhelper.hxx"
32 #include <rtl/ustrbuf.hxx>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/container/XIndexContainer.hpp>
35 #include <com/sun/star/container/XNameContainer.hpp>
36 #include "oox/helper/helper.hxx"
38 using ::rtl::OUString
;
39 using ::rtl::OUStringBuffer
;
40 using ::com::sun::star::uno::Any
;
41 using ::com::sun::star::uno::Reference
;
42 using ::com::sun::star::uno::Exception
;
43 using ::com::sun::star::uno::UNO_QUERY
;
44 using ::com::sun::star::uno::UNO_QUERY_THROW
;
45 using ::com::sun::star::lang::XMultiServiceFactory
;
46 using ::com::sun::star::container::XIndexContainer
;
47 using ::com::sun::star::container::XNameAccess
;
48 using ::com::sun::star::container::XNameContainer
;
52 // ============================================================================
54 Reference
< XIndexContainer
> ContainerHelper::createIndexContainer( const Reference
< XMultiServiceFactory
>& rxFactory
)
56 Reference
< XIndexContainer
> xContainer
;
57 if( rxFactory
.is() ) try
59 xContainer
.set( rxFactory
->createInstance( CREATE_OUSTRING( "com.sun.star.document.IndexedPropertyValues" ) ), UNO_QUERY_THROW
);
64 OSL_ENSURE( xContainer
.is(), "ContainerHelper::createIndexContainer - cannot create container" );
68 bool ContainerHelper::insertByIndex(
69 const Reference
< XIndexContainer
>& rxIndexContainer
,
70 sal_Int32 nIndex
, const Any
& rObject
)
72 OSL_ENSURE( rxIndexContainer
.is(), "ContainerHelper::insertByIndex - missing XIndexContainer interface" );
76 rxIndexContainer
->insertByIndex( nIndex
, rObject
);
82 OSL_ENSURE( bRet
, "ContainerHelper::insertByIndex - cannot insert object" );
86 Reference
< XNameContainer
> ContainerHelper::createNameContainer( const Reference
< XMultiServiceFactory
>& rxFactory
)
88 Reference
< XNameContainer
> xContainer
;
89 if( rxFactory
.is() ) try
91 xContainer
.set( rxFactory
->createInstance( CREATE_OUSTRING( "com.sun.star.document.NamedPropertyValues" ) ), UNO_QUERY_THROW
);
96 OSL_ENSURE( xContainer
.is(), "ContainerHelper::createNameContainer - cannot create container" );
100 OUString
ContainerHelper::getUnusedName(
101 const Reference
< XNameAccess
>& rxNameAccess
, const OUString
& rSuggestedName
,
102 sal_Unicode cSeparator
, sal_Int32 nFirstIndexToAppend
)
104 OSL_ENSURE( rxNameAccess
.is(), "ContainerHelper::getUnusedName - missing XNameAccess interface" );
106 OUString aNewName
= rSuggestedName
;
107 sal_Int32 nIndex
= nFirstIndexToAppend
;
108 while( rxNameAccess
->hasByName( aNewName
) )
109 aNewName
= OUStringBuffer( rSuggestedName
).append( cSeparator
).append( nIndex
++ ).makeStringAndClear();
113 bool ContainerHelper::insertByName(
114 const Reference
< XNameContainer
>& rxNameContainer
,
115 const OUString
& rName
, const Any
& rObject
, bool bReplaceOldExisting
)
117 OSL_ENSURE( rxNameContainer
.is(), "ContainerHelper::insertByName - missing XNameContainer interface" );
121 if( bReplaceOldExisting
&& rxNameContainer
->hasByName( rName
) )
122 rxNameContainer
->replaceByName( rName
, rObject
);
124 rxNameContainer
->insertByName( rName
, rObject
);
130 OSL_ENSURE( bRet
, "ContainerHelper::insertByName - cannot insert object" );
134 OUString
ContainerHelper::insertByUnusedName(
135 const Reference
< XNameContainer
>& rxNameContainer
,
136 const OUString
& rSuggestedName
, sal_Unicode cSeparator
,
137 const Any
& rObject
, bool bRenameOldExisting
)
139 OSL_ENSURE( rxNameContainer
.is(), "ContainerHelper::insertByUnusedName - missing XNameContainer interface" );
141 // find an unused name
142 Reference
< XNameAccess
> xNameAccess( rxNameContainer
, UNO_QUERY
);
143 OUString aNewName
= getUnusedName( xNameAccess
, rSuggestedName
, cSeparator
);
145 // rename existing object
146 if( bRenameOldExisting
&& rxNameContainer
->hasByName( rSuggestedName
) )
150 Any aOldObject
= rxNameContainer
->getByName( rSuggestedName
);
151 rxNameContainer
->removeByName( rSuggestedName
);
152 rxNameContainer
->insertByName( aNewName
, aOldObject
);
153 aNewName
= rSuggestedName
;
157 OSL_ENSURE( false, "ContainerHelper::insertByUnusedName - cannot rename old object" );
161 // insert the new object and return its resulting name
162 insertByName( rxNameContainer
, aNewName
, rObject
);
166 // ============================================================================
168 ObjectContainer::ObjectContainer( const Reference
< XMultiServiceFactory
>& rxFactory
, const OUString
& rServiceName
) :
169 mxFactory( rxFactory
),
170 maServiceName( rServiceName
),
173 OSL_ENSURE( mxFactory
.is(), "ObjectContainer::ObjectContainer - missing service factory" );
176 ObjectContainer::~ObjectContainer()
180 bool ObjectContainer::hasObject( const OUString
& rObjName
) const
183 return mxContainer
.is() && mxContainer
->hasByName( rObjName
);
186 Any
ObjectContainer::getObject( const OUString
& rObjName
) const
189 if( mxContainer
.is() ) try
191 return mxContainer
->getByName( rObjName
);
199 OUString
ObjectContainer::insertObject( const OUString
& rObjName
, const Any
& rObj
, bool bInsertByUnusedName
)
202 if( mxContainer
.is() )
204 if( bInsertByUnusedName
)
205 return ContainerHelper::insertByUnusedName( mxContainer
, rObjName
+ OUString::valueOf( ++mnIndex
), ' ', rObj
);
206 if( ContainerHelper::insertByName( mxContainer
, rObjName
, rObj
) )
212 void ObjectContainer::createContainer() const
214 if( !mxContainer
.is() && mxFactory
.is() ) try
216 mxContainer
.set( mxFactory
->createInstance( maServiceName
), UNO_QUERY_THROW
);
221 OSL_ENSURE( mxContainer
.is(), "ObjectContainer::createContainer - container not found" );
224 // ============================================================================