merge the formfield patch from ooo-build
[ooovba.git] / oox / source / helper / containerhelper.cxx
blobe09fe77bcc2fb5f2d8e904affdeed688accbb95e
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;
50 namespace oox {
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 );
61 catch( Exception& )
64 OSL_ENSURE( xContainer.is(), "ContainerHelper::createIndexContainer - cannot create container" );
65 return xContainer;
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" );
73 bool bRet = false;
74 try
76 rxIndexContainer->insertByIndex( nIndex, rObject );
77 bRet = true;
79 catch( Exception& )
82 OSL_ENSURE( bRet, "ContainerHelper::insertByIndex - cannot insert object" );
83 return bRet;
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 );
93 catch( Exception& )
96 OSL_ENSURE( xContainer.is(), "ContainerHelper::createNameContainer - cannot create container" );
97 return xContainer;
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();
110 return aNewName;
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" );
118 bool bRet = false;
121 if( bReplaceOldExisting && rxNameContainer->hasByName( rName ) )
122 rxNameContainer->replaceByName( rName, rObject );
123 else
124 rxNameContainer->insertByName( rName, rObject );
125 bRet = true;
127 catch( Exception& )
130 OSL_ENSURE( bRet, "ContainerHelper::insertByName - cannot insert object" );
131 return bRet;
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;
155 catch( Exception& )
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 );
163 return aNewName;
166 // ============================================================================
168 ObjectContainer::ObjectContainer( const Reference< XMultiServiceFactory >& rxFactory, const OUString& rServiceName ) :
169 mxFactory( rxFactory ),
170 maServiceName( rServiceName ),
171 mnIndex( 0 )
173 OSL_ENSURE( mxFactory.is(), "ObjectContainer::ObjectContainer - missing service factory" );
176 ObjectContainer::~ObjectContainer()
180 bool ObjectContainer::hasObject( const OUString& rObjName ) const
182 createContainer();
183 return mxContainer.is() && mxContainer->hasByName( rObjName );
186 Any ObjectContainer::getObject( const OUString& rObjName ) const
188 createContainer();
189 if( mxContainer.is() ) try
191 return mxContainer->getByName( rObjName );
193 catch( Exception& )
196 return Any();
199 OUString ObjectContainer::insertObject( const OUString& rObjName, const Any& rObj, bool bInsertByUnusedName )
201 createContainer();
202 if( mxContainer.is() )
204 if( bInsertByUnusedName )
205 return ContainerHelper::insertByUnusedName( mxContainer, rObjName + OUString::valueOf( ++mnIndex ), ' ', rObj );
206 if( ContainerHelper::insertByName( mxContainer, rObjName, rObj ) )
207 return rObjName;
209 return OUString();
212 void ObjectContainer::createContainer() const
214 if( !mxContainer.is() && mxFactory.is() ) try
216 mxContainer.set( mxFactory->createInstance( maServiceName ), UNO_QUERY_THROW );
218 catch( Exception& )
221 OSL_ENSURE( mxContainer.is(), "ObjectContainer::createContainer - container not found" );
224 // ============================================================================
226 } // namespace oox