xref: /AOO41X/main/comphelper/source/container/IndexedPropertyValuesContainer.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "comphelper_module.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <com/sun/star/container/XIndexContainer.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.h>
35*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
36*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
37*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #ifndef __SGI_STL_VECTOR
42*cdf0e10cSrcweir #include <vector>
43*cdf0e10cSrcweir #endif
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using namespace com::sun::star;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir typedef std::vector < uno::Sequence< beans::PropertyValue > > IndexedPropertyValues;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir class IndexedPropertyValuesContainer : public cppu::WeakImplHelper2< container::XIndexContainer, lang::XServiceInfo >
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir public:
52*cdf0e10cSrcweir 	IndexedPropertyValuesContainer() throw();
53*cdf0e10cSrcweir 	virtual ~IndexedPropertyValuesContainer() throw();
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 	// XIndexContainer
56*cdf0e10cSrcweir 	virtual void SAL_CALL insertByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
57*cdf0e10cSrcweir 		throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
58*cdf0e10cSrcweir 			::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
59*cdf0e10cSrcweir 	virtual void SAL_CALL removeByIndex( sal_Int32 nIndex )
60*cdf0e10cSrcweir 		throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
61*cdf0e10cSrcweir 			::com::sun::star::uno::RuntimeException);
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     // XIndexReplace
64*cdf0e10cSrcweir 	virtual void SAL_CALL replaceByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
65*cdf0e10cSrcweir 		throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
66*cdf0e10cSrcweir 			::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     // XIndexAccess
69*cdf0e10cSrcweir 	virtual sal_Int32 SAL_CALL getCount(  )
70*cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
71*cdf0e10cSrcweir 	virtual ::com::sun::star::uno::Any SAL_CALL getByIndex( sal_Int32 nIndex )
72*cdf0e10cSrcweir 		throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
73*cdf0e10cSrcweir 			::com::sun::star::uno::RuntimeException);
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 	// XElementAccess
76*cdf0e10cSrcweir 	virtual ::com::sun::star::uno::Type SAL_CALL getElementType(  )
77*cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
78*cdf0e10cSrcweir 	virtual sal_Bool SAL_CALL hasElements(  )
79*cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 	//XServiceInfo
82*cdf0e10cSrcweir 	virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException);
83*cdf0e10cSrcweir 	virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);
84*cdf0e10cSrcweir 	virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException);
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir     // XServiceInfo - static versions (used for component registration)
87*cdf0e10cSrcweir     static ::rtl::OUString SAL_CALL getImplementationName_static();
88*cdf0e10cSrcweir     static uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
89*cdf0e10cSrcweir     static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir private:
92*cdf0e10cSrcweir 	IndexedPropertyValues maProperties;
93*cdf0e10cSrcweir };
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir IndexedPropertyValuesContainer::IndexedPropertyValuesContainer() throw()
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir IndexedPropertyValuesContainer::~IndexedPropertyValuesContainer() throw()
100*cdf0e10cSrcweir {
101*cdf0e10cSrcweir }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir // XIndexContainer
104*cdf0e10cSrcweir void SAL_CALL IndexedPropertyValuesContainer::insertByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
105*cdf0e10cSrcweir 	throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
106*cdf0e10cSrcweir 		::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir 	sal_Int32 nSize(maProperties.size());
109*cdf0e10cSrcweir 	if ((nSize >= nIndex) && (nIndex >= 0))
110*cdf0e10cSrcweir 	{
111*cdf0e10cSrcweir 		uno::Sequence<beans::PropertyValue> aProps;
112*cdf0e10cSrcweir 		if (!(aElement >>= aProps))
113*cdf0e10cSrcweir 			throw lang::IllegalArgumentException();
114*cdf0e10cSrcweir 		if (nSize == nIndex)
115*cdf0e10cSrcweir 			maProperties.push_back(aProps);
116*cdf0e10cSrcweir 		else
117*cdf0e10cSrcweir 		{
118*cdf0e10cSrcweir 			IndexedPropertyValues::iterator aItr;
119*cdf0e10cSrcweir 			if ((nIndex * 2) < nSize)
120*cdf0e10cSrcweir 			{
121*cdf0e10cSrcweir 				aItr = maProperties.begin();
122*cdf0e10cSrcweir 				sal_Int32 i(0);
123*cdf0e10cSrcweir 				while(i < nIndex)
124*cdf0e10cSrcweir 				{
125*cdf0e10cSrcweir 					i++;
126*cdf0e10cSrcweir 					aItr++;
127*cdf0e10cSrcweir 				}
128*cdf0e10cSrcweir 			}
129*cdf0e10cSrcweir 			else
130*cdf0e10cSrcweir 			{
131*cdf0e10cSrcweir 				aItr = maProperties.end();
132*cdf0e10cSrcweir 				sal_Int32 i(nSize - 1);
133*cdf0e10cSrcweir 				while(i > nIndex)
134*cdf0e10cSrcweir 				{
135*cdf0e10cSrcweir 					i--;
136*cdf0e10cSrcweir 					aItr--;
137*cdf0e10cSrcweir 				}
138*cdf0e10cSrcweir 			}
139*cdf0e10cSrcweir 			maProperties.insert(aItr, aProps);
140*cdf0e10cSrcweir 		}
141*cdf0e10cSrcweir 	}
142*cdf0e10cSrcweir 	else
143*cdf0e10cSrcweir 		throw lang::IndexOutOfBoundsException();
144*cdf0e10cSrcweir }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir void SAL_CALL IndexedPropertyValuesContainer::removeByIndex( sal_Int32 nIndex )
147*cdf0e10cSrcweir 	throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
148*cdf0e10cSrcweir 		::com::sun::star::uno::RuntimeException)
149*cdf0e10cSrcweir {
150*cdf0e10cSrcweir 	sal_Int32 nSize(maProperties.size());
151*cdf0e10cSrcweir 	if ((nIndex < nSize) && (nIndex >= 0))
152*cdf0e10cSrcweir 	{
153*cdf0e10cSrcweir 		IndexedPropertyValues::iterator aItr;
154*cdf0e10cSrcweir 		if ((nIndex * 2) < nSize)
155*cdf0e10cSrcweir 		{
156*cdf0e10cSrcweir 			aItr = maProperties.begin();
157*cdf0e10cSrcweir 			sal_Int32 i(0);
158*cdf0e10cSrcweir 			while(i < nIndex)
159*cdf0e10cSrcweir 			{
160*cdf0e10cSrcweir 				i++;
161*cdf0e10cSrcweir 				aItr++;
162*cdf0e10cSrcweir 			}
163*cdf0e10cSrcweir 		}
164*cdf0e10cSrcweir 		else
165*cdf0e10cSrcweir 		{
166*cdf0e10cSrcweir 			aItr = maProperties.end();
167*cdf0e10cSrcweir 			sal_Int32 i(nSize - 1);
168*cdf0e10cSrcweir 			while(i > nIndex)
169*cdf0e10cSrcweir 			{
170*cdf0e10cSrcweir 				i--;
171*cdf0e10cSrcweir 				aItr--;
172*cdf0e10cSrcweir 			}
173*cdf0e10cSrcweir 		}
174*cdf0e10cSrcweir 		maProperties.erase(aItr);
175*cdf0e10cSrcweir 	}
176*cdf0e10cSrcweir 	else
177*cdf0e10cSrcweir 		throw lang::IndexOutOfBoundsException();
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir // XIndexReplace
181*cdf0e10cSrcweir void SAL_CALL IndexedPropertyValuesContainer::replaceByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
182*cdf0e10cSrcweir 	throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
183*cdf0e10cSrcweir 		::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir 	sal_Int32 nSize(maProperties.size());
186*cdf0e10cSrcweir 	if ((nIndex < nSize) && (nIndex >= 0))
187*cdf0e10cSrcweir 	{
188*cdf0e10cSrcweir 		uno::Sequence<beans::PropertyValue> aProps;
189*cdf0e10cSrcweir 		if (!(aElement >>= aProps))
190*cdf0e10cSrcweir 			throw lang::IllegalArgumentException();
191*cdf0e10cSrcweir 		maProperties[nIndex] = aProps;
192*cdf0e10cSrcweir 	}
193*cdf0e10cSrcweir 	else
194*cdf0e10cSrcweir 		throw lang::IndexOutOfBoundsException();
195*cdf0e10cSrcweir }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir // XIndexAccess
198*cdf0e10cSrcweir sal_Int32 SAL_CALL IndexedPropertyValuesContainer::getCount(  )
199*cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
200*cdf0e10cSrcweir {
201*cdf0e10cSrcweir 	return maProperties.size();
202*cdf0e10cSrcweir }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir ::com::sun::star::uno::Any SAL_CALL IndexedPropertyValuesContainer::getByIndex( sal_Int32 nIndex )
205*cdf0e10cSrcweir 	throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
206*cdf0e10cSrcweir 		::com::sun::star::uno::RuntimeException)
207*cdf0e10cSrcweir {
208*cdf0e10cSrcweir 	sal_Int32 nSize(maProperties.size());
209*cdf0e10cSrcweir 	if (!((nIndex < nSize) && (nIndex >= 0)))
210*cdf0e10cSrcweir 		throw lang::IndexOutOfBoundsException();
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir 	uno::Any aAny;
213*cdf0e10cSrcweir 	aAny <<= maProperties[nIndex];
214*cdf0e10cSrcweir 	return aAny;
215*cdf0e10cSrcweir }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir // XElementAccess
218*cdf0e10cSrcweir ::com::sun::star::uno::Type SAL_CALL IndexedPropertyValuesContainer::getElementType(  )
219*cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
220*cdf0e10cSrcweir {
221*cdf0e10cSrcweir 	return ::getCppuType((uno::Sequence<beans::PropertyValue> *)0);
222*cdf0e10cSrcweir }
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir sal_Bool SAL_CALL IndexedPropertyValuesContainer::hasElements(  )
225*cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
226*cdf0e10cSrcweir {
227*cdf0e10cSrcweir 	return !maProperties.empty();
228*cdf0e10cSrcweir }
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir //XServiceInfo
231*cdf0e10cSrcweir ::rtl::OUString SAL_CALL IndexedPropertyValuesContainer::getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException)
232*cdf0e10cSrcweir {
233*cdf0e10cSrcweir 	return getImplementationName_static();
234*cdf0e10cSrcweir }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir ::rtl::OUString SAL_CALL IndexedPropertyValuesContainer::getImplementationName_static(  )
237*cdf0e10cSrcweir {
238*cdf0e10cSrcweir 	return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IndexedPropertyValuesContainer" ) );
239*cdf0e10cSrcweir }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir sal_Bool SAL_CALL IndexedPropertyValuesContainer::supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException)
242*cdf0e10cSrcweir {
243*cdf0e10cSrcweir 	rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.IndexedPropertyValues" ) );
244*cdf0e10cSrcweir 	return aServiceName == ServiceName;
245*cdf0e10cSrcweir }
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL IndexedPropertyValuesContainer::getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException)
248*cdf0e10cSrcweir {
249*cdf0e10cSrcweir 	return getSupportedServiceNames_static();
250*cdf0e10cSrcweir }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL IndexedPropertyValuesContainer::getSupportedServiceNames_static(  )
254*cdf0e10cSrcweir {
255*cdf0e10cSrcweir 	const rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.IndexedPropertyValues" ) );
256*cdf0e10cSrcweir 	const uno::Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
257*cdf0e10cSrcweir 	return aSeq;
258*cdf0e10cSrcweir }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL IndexedPropertyValuesContainer::Create(
262*cdf0e10cSrcweir                 const uno::Reference< uno::XComponentContext >&)
263*cdf0e10cSrcweir {
264*cdf0e10cSrcweir 	return (cppu::OWeakObject*)new IndexedPropertyValuesContainer();
265*cdf0e10cSrcweir }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir void createRegistryInfo_IndexedPropertyValuesContainer()
268*cdf0e10cSrcweir {
269*cdf0e10cSrcweir     static ::comphelper::module::OAutoRegistration< IndexedPropertyValuesContainer > aAutoRegistration;
270*cdf0e10cSrcweir }
271