1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*dde7d3faSAndrew Rist  * distributed with this work for additional information
6*dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*dde7d3faSAndrew Rist  *
11*dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*dde7d3faSAndrew Rist  *
13*dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17*dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist  * under the License.
19*dde7d3faSAndrew Rist  *
20*dde7d3faSAndrew Rist  *************************************************************/
21*dde7d3faSAndrew Rist 
22*dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "comphelper_module.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <com/sun/star/container/XIndexContainer.hpp>
30cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.h>
31cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
32cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
33cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
34cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #ifndef __SGI_STL_VECTOR
38cdf0e10cSrcweir #include <vector>
39cdf0e10cSrcweir #endif
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using namespace com::sun::star;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir typedef std::vector < uno::Sequence< beans::PropertyValue > > IndexedPropertyValues;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir class IndexedPropertyValuesContainer : public cppu::WeakImplHelper2< container::XIndexContainer, lang::XServiceInfo >
46cdf0e10cSrcweir {
47cdf0e10cSrcweir public:
48cdf0e10cSrcweir 	IndexedPropertyValuesContainer() throw();
49cdf0e10cSrcweir 	virtual ~IndexedPropertyValuesContainer() throw();
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	// XIndexContainer
52cdf0e10cSrcweir 	virtual void SAL_CALL insertByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
53cdf0e10cSrcweir 		throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
54cdf0e10cSrcweir 			::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
55cdf0e10cSrcweir 	virtual void SAL_CALL removeByIndex( sal_Int32 nIndex )
56cdf0e10cSrcweir 		throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
57cdf0e10cSrcweir 			::com::sun::star::uno::RuntimeException);
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     // XIndexReplace
60cdf0e10cSrcweir 	virtual void SAL_CALL replaceByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
61cdf0e10cSrcweir 		throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
62cdf0e10cSrcweir 			::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
63cdf0e10cSrcweir 
64cdf0e10cSrcweir     // XIndexAccess
65cdf0e10cSrcweir 	virtual sal_Int32 SAL_CALL getCount(  )
66cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
67cdf0e10cSrcweir 	virtual ::com::sun::star::uno::Any SAL_CALL getByIndex( sal_Int32 nIndex )
68cdf0e10cSrcweir 		throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
69cdf0e10cSrcweir 			::com::sun::star::uno::RuntimeException);
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 	// XElementAccess
72cdf0e10cSrcweir 	virtual ::com::sun::star::uno::Type SAL_CALL getElementType(  )
73cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
74cdf0e10cSrcweir 	virtual sal_Bool SAL_CALL hasElements(  )
75cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 	//XServiceInfo
78cdf0e10cSrcweir 	virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException);
79cdf0e10cSrcweir 	virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);
80cdf0e10cSrcweir 	virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException);
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     // XServiceInfo - static versions (used for component registration)
83cdf0e10cSrcweir     static ::rtl::OUString SAL_CALL getImplementationName_static();
84cdf0e10cSrcweir     static uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
85cdf0e10cSrcweir     static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir private:
88cdf0e10cSrcweir 	IndexedPropertyValues maProperties;
89cdf0e10cSrcweir };
90cdf0e10cSrcweir 
IndexedPropertyValuesContainer()91cdf0e10cSrcweir IndexedPropertyValuesContainer::IndexedPropertyValuesContainer() throw()
92cdf0e10cSrcweir {
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
~IndexedPropertyValuesContainer()95cdf0e10cSrcweir IndexedPropertyValuesContainer::~IndexedPropertyValuesContainer() throw()
96cdf0e10cSrcweir {
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir // XIndexContainer
insertByIndex(sal_Int32 nIndex,const::com::sun::star::uno::Any & aElement)100cdf0e10cSrcweir void SAL_CALL IndexedPropertyValuesContainer::insertByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
101cdf0e10cSrcweir 	throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
102cdf0e10cSrcweir 		::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
103cdf0e10cSrcweir {
104cdf0e10cSrcweir 	sal_Int32 nSize(maProperties.size());
105cdf0e10cSrcweir 	if ((nSize >= nIndex) && (nIndex >= 0))
106cdf0e10cSrcweir 	{
107cdf0e10cSrcweir 		uno::Sequence<beans::PropertyValue> aProps;
108cdf0e10cSrcweir 		if (!(aElement >>= aProps))
109cdf0e10cSrcweir 			throw lang::IllegalArgumentException();
110cdf0e10cSrcweir 		if (nSize == nIndex)
111cdf0e10cSrcweir 			maProperties.push_back(aProps);
112cdf0e10cSrcweir 		else
113cdf0e10cSrcweir 		{
114cdf0e10cSrcweir 			IndexedPropertyValues::iterator aItr;
115cdf0e10cSrcweir 			if ((nIndex * 2) < nSize)
116cdf0e10cSrcweir 			{
117cdf0e10cSrcweir 				aItr = maProperties.begin();
118cdf0e10cSrcweir 				sal_Int32 i(0);
119cdf0e10cSrcweir 				while(i < nIndex)
120cdf0e10cSrcweir 				{
121cdf0e10cSrcweir 					i++;
122cdf0e10cSrcweir 					aItr++;
123cdf0e10cSrcweir 				}
124cdf0e10cSrcweir 			}
125cdf0e10cSrcweir 			else
126cdf0e10cSrcweir 			{
127cdf0e10cSrcweir 				aItr = maProperties.end();
128cdf0e10cSrcweir 				sal_Int32 i(nSize - 1);
129cdf0e10cSrcweir 				while(i > nIndex)
130cdf0e10cSrcweir 				{
131cdf0e10cSrcweir 					i--;
132cdf0e10cSrcweir 					aItr--;
133cdf0e10cSrcweir 				}
134cdf0e10cSrcweir 			}
135cdf0e10cSrcweir 			maProperties.insert(aItr, aProps);
136cdf0e10cSrcweir 		}
137cdf0e10cSrcweir 	}
138cdf0e10cSrcweir 	else
139cdf0e10cSrcweir 		throw lang::IndexOutOfBoundsException();
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
removeByIndex(sal_Int32 nIndex)142cdf0e10cSrcweir void SAL_CALL IndexedPropertyValuesContainer::removeByIndex( sal_Int32 nIndex )
143cdf0e10cSrcweir 	throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
144cdf0e10cSrcweir 		::com::sun::star::uno::RuntimeException)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir 	sal_Int32 nSize(maProperties.size());
147cdf0e10cSrcweir 	if ((nIndex < nSize) && (nIndex >= 0))
148cdf0e10cSrcweir 	{
149cdf0e10cSrcweir 		IndexedPropertyValues::iterator aItr;
150cdf0e10cSrcweir 		if ((nIndex * 2) < nSize)
151cdf0e10cSrcweir 		{
152cdf0e10cSrcweir 			aItr = maProperties.begin();
153cdf0e10cSrcweir 			sal_Int32 i(0);
154cdf0e10cSrcweir 			while(i < nIndex)
155cdf0e10cSrcweir 			{
156cdf0e10cSrcweir 				i++;
157cdf0e10cSrcweir 				aItr++;
158cdf0e10cSrcweir 			}
159cdf0e10cSrcweir 		}
160cdf0e10cSrcweir 		else
161cdf0e10cSrcweir 		{
162cdf0e10cSrcweir 			aItr = maProperties.end();
163cdf0e10cSrcweir 			sal_Int32 i(nSize - 1);
164cdf0e10cSrcweir 			while(i > nIndex)
165cdf0e10cSrcweir 			{
166cdf0e10cSrcweir 				i--;
167cdf0e10cSrcweir 				aItr--;
168cdf0e10cSrcweir 			}
169cdf0e10cSrcweir 		}
170cdf0e10cSrcweir 		maProperties.erase(aItr);
171cdf0e10cSrcweir 	}
172cdf0e10cSrcweir 	else
173cdf0e10cSrcweir 		throw lang::IndexOutOfBoundsException();
174cdf0e10cSrcweir }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir // XIndexReplace
replaceByIndex(sal_Int32 nIndex,const::com::sun::star::uno::Any & aElement)177cdf0e10cSrcweir void SAL_CALL IndexedPropertyValuesContainer::replaceByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
178cdf0e10cSrcweir 	throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
179cdf0e10cSrcweir 		::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
180cdf0e10cSrcweir {
181cdf0e10cSrcweir 	sal_Int32 nSize(maProperties.size());
182cdf0e10cSrcweir 	if ((nIndex < nSize) && (nIndex >= 0))
183cdf0e10cSrcweir 	{
184cdf0e10cSrcweir 		uno::Sequence<beans::PropertyValue> aProps;
185cdf0e10cSrcweir 		if (!(aElement >>= aProps))
186cdf0e10cSrcweir 			throw lang::IllegalArgumentException();
187cdf0e10cSrcweir 		maProperties[nIndex] = aProps;
188cdf0e10cSrcweir 	}
189cdf0e10cSrcweir 	else
190cdf0e10cSrcweir 		throw lang::IndexOutOfBoundsException();
191cdf0e10cSrcweir }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir // XIndexAccess
getCount()194cdf0e10cSrcweir sal_Int32 SAL_CALL IndexedPropertyValuesContainer::getCount(  )
195cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir 	return maProperties.size();
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
getByIndex(sal_Int32 nIndex)200cdf0e10cSrcweir ::com::sun::star::uno::Any SAL_CALL IndexedPropertyValuesContainer::getByIndex( sal_Int32 nIndex )
201cdf0e10cSrcweir 	throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
202cdf0e10cSrcweir 		::com::sun::star::uno::RuntimeException)
203cdf0e10cSrcweir {
204cdf0e10cSrcweir 	sal_Int32 nSize(maProperties.size());
205cdf0e10cSrcweir 	if (!((nIndex < nSize) && (nIndex >= 0)))
206cdf0e10cSrcweir 		throw lang::IndexOutOfBoundsException();
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 	uno::Any aAny;
209cdf0e10cSrcweir 	aAny <<= maProperties[nIndex];
210cdf0e10cSrcweir 	return aAny;
211cdf0e10cSrcweir }
212cdf0e10cSrcweir 
213cdf0e10cSrcweir // XElementAccess
getElementType()214cdf0e10cSrcweir ::com::sun::star::uno::Type SAL_CALL IndexedPropertyValuesContainer::getElementType(  )
215cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
216cdf0e10cSrcweir {
217cdf0e10cSrcweir 	return ::getCppuType((uno::Sequence<beans::PropertyValue> *)0);
218cdf0e10cSrcweir }
219cdf0e10cSrcweir 
hasElements()220cdf0e10cSrcweir sal_Bool SAL_CALL IndexedPropertyValuesContainer::hasElements(  )
221cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
222cdf0e10cSrcweir {
223cdf0e10cSrcweir 	return !maProperties.empty();
224cdf0e10cSrcweir }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir //XServiceInfo
getImplementationName()227cdf0e10cSrcweir ::rtl::OUString SAL_CALL IndexedPropertyValuesContainer::getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException)
228cdf0e10cSrcweir {
229cdf0e10cSrcweir 	return getImplementationName_static();
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
getImplementationName_static()232cdf0e10cSrcweir ::rtl::OUString SAL_CALL IndexedPropertyValuesContainer::getImplementationName_static(  )
233cdf0e10cSrcweir {
234cdf0e10cSrcweir 	return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IndexedPropertyValuesContainer" ) );
235cdf0e10cSrcweir }
236cdf0e10cSrcweir 
supportsService(const::rtl::OUString & ServiceName)237cdf0e10cSrcweir sal_Bool SAL_CALL IndexedPropertyValuesContainer::supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException)
238cdf0e10cSrcweir {
239cdf0e10cSrcweir 	rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.IndexedPropertyValues" ) );
240cdf0e10cSrcweir 	return aServiceName == ServiceName;
241cdf0e10cSrcweir }
242cdf0e10cSrcweir 
getSupportedServiceNames()243cdf0e10cSrcweir ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL IndexedPropertyValuesContainer::getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException)
244cdf0e10cSrcweir {
245cdf0e10cSrcweir 	return getSupportedServiceNames_static();
246cdf0e10cSrcweir }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 
getSupportedServiceNames_static()249cdf0e10cSrcweir ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL IndexedPropertyValuesContainer::getSupportedServiceNames_static(  )
250cdf0e10cSrcweir {
251cdf0e10cSrcweir 	const rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.IndexedPropertyValues" ) );
252cdf0e10cSrcweir 	const uno::Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
253cdf0e10cSrcweir 	return aSeq;
254cdf0e10cSrcweir }
255cdf0e10cSrcweir 
256cdf0e10cSrcweir 
Create(const uno::Reference<uno::XComponentContext> &)257cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL IndexedPropertyValuesContainer::Create(
258cdf0e10cSrcweir                 const uno::Reference< uno::XComponentContext >&)
259cdf0e10cSrcweir {
260cdf0e10cSrcweir 	return (cppu::OWeakObject*)new IndexedPropertyValuesContainer();
261cdf0e10cSrcweir }
262cdf0e10cSrcweir 
createRegistryInfo_IndexedPropertyValuesContainer()263cdf0e10cSrcweir void createRegistryInfo_IndexedPropertyValuesContainer()
264cdf0e10cSrcweir {
265cdf0e10cSrcweir     static ::comphelper::module::OAutoRegistration< IndexedPropertyValuesContainer > aAutoRegistration;
266cdf0e10cSrcweir }
267