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