1*63bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*63bba73cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*63bba73cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*63bba73cSAndrew Rist  * distributed with this work for additional information
6*63bba73cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*63bba73cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*63bba73cSAndrew Rist  * "License"); you may not use this file except in compliance
9*63bba73cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*63bba73cSAndrew Rist  *
11*63bba73cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*63bba73cSAndrew Rist  *
13*63bba73cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*63bba73cSAndrew Rist  * software distributed under the License is distributed on an
15*63bba73cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63bba73cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*63bba73cSAndrew Rist  * specific language governing permissions and limitations
18*63bba73cSAndrew Rist  * under the License.
19*63bba73cSAndrew Rist  *
20*63bba73cSAndrew Rist  *************************************************************/
21*63bba73cSAndrew Rist 
22*63bba73cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #ifndef _XMLOFF_MULTIPROPERTYSETHELPER_HXX
28cdf0e10cSrcweir #include "MultiPropertySetHelper.hxx"
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySetInfo.hpp>
31cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
32cdf0e10cSrcweir #include <com/sun/star/beans/XMultiPropertySet.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
34cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir // STL includes
37cdf0e10cSrcweir #include <algorithm>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using ::com::sun::star::beans::XMultiPropertySet;
41cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySet;
42cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySetInfo;
43cdf0e10cSrcweir using ::com::sun::star::lang::XServiceInfo;
44cdf0e10cSrcweir using ::com::sun::star::uno::Any;
45cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
46cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
47cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY;
48cdf0e10cSrcweir using ::comphelper::UStringLess;
49cdf0e10cSrcweir using ::rtl::OUString;
50cdf0e10cSrcweir using ::std::sort;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 
MultiPropertySetHelper(const sal_Char ** pNames)53cdf0e10cSrcweir MultiPropertySetHelper::MultiPropertySetHelper(
54cdf0e10cSrcweir 	const sal_Char** pNames ) :
55cdf0e10cSrcweir 		pPropertyNames( NULL ),
56cdf0e10cSrcweir 		nLength( 0 ),
57cdf0e10cSrcweir 		aPropertySequence(),
58cdf0e10cSrcweir 		pSequenceIndex( NULL ),
59cdf0e10cSrcweir 		aValues(),
60cdf0e10cSrcweir 		pValues( NULL )
61cdf0e10cSrcweir {
62cdf0e10cSrcweir 	// first count the elements
63cdf0e10cSrcweir 	for( const sal_Char** pPtr = pNames; *pPtr != NULL; pPtr++ )
64cdf0e10cSrcweir 		nLength++;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 	// allocate array and create strings
67cdf0e10cSrcweir 	pPropertyNames = new OUString[nLength];
68cdf0e10cSrcweir 	for( sal_Int16 i = 0; i < nLength; i++ )
69cdf0e10cSrcweir 		pPropertyNames[i] = OUString::createFromAscii( pNames[i] );
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
MultiPropertySetHelper(const OUString * pNames)72cdf0e10cSrcweir MultiPropertySetHelper::MultiPropertySetHelper(
73cdf0e10cSrcweir 	const OUString* pNames ) :
74cdf0e10cSrcweir 		pPropertyNames( NULL ),
75cdf0e10cSrcweir 		nLength( 0 ),
76cdf0e10cSrcweir 		aPropertySequence(),
77cdf0e10cSrcweir 		pSequenceIndex( NULL ),
78cdf0e10cSrcweir 		aValues(),
79cdf0e10cSrcweir 		pValues( NULL )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir 	// count elements
82cdf0e10cSrcweir 	for( const OUString* pPtr = pNames; pPtr != NULL; pPtr++ )
83cdf0e10cSrcweir 		nLength++;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 	// allocate array and assign strings
86cdf0e10cSrcweir 	pPropertyNames = new OUString[nLength];
87cdf0e10cSrcweir 	for( sal_Int16 i = 0; i < nLength; i++ )
88cdf0e10cSrcweir 		pPropertyNames[i] = pNames[i];
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 
~MultiPropertySetHelper()92cdf0e10cSrcweir MultiPropertySetHelper::~MultiPropertySetHelper()
93cdf0e10cSrcweir {
94cdf0e10cSrcweir 	pValues = NULL;	// memory 'owned' by aValues
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	delete[] pSequenceIndex;
97cdf0e10cSrcweir 	delete[] pPropertyNames;
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 
hasProperties(const Reference<XPropertySetInfo> & rInfo)102cdf0e10cSrcweir void MultiPropertySetHelper::hasProperties(
103cdf0e10cSrcweir 	const Reference<XPropertySetInfo> & rInfo )
104cdf0e10cSrcweir {
105cdf0e10cSrcweir 	DBG_ASSERT( rInfo.is(), "I'd really like an XPropertySetInfo here." );
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 	// allocate sequence index
108cdf0e10cSrcweir 	if ( NULL == pSequenceIndex )
109cdf0e10cSrcweir 		pSequenceIndex = new sal_Int16[nLength] ;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	// construct pSequenceIndex
112cdf0e10cSrcweir 	sal_Int16 nNumberOfProperties = 0;
113cdf0e10cSrcweir 	sal_Int16 i;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 	for( i = 0; i < nLength; i++ )
116cdf0e10cSrcweir 	{
117cdf0e10cSrcweir 		// ask for property
118cdf0e10cSrcweir 		sal_Bool bHasProperty =
119cdf0e10cSrcweir 			rInfo->hasPropertyByName( pPropertyNames[i] );
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 		// set index and increment (if appropriate)
122cdf0e10cSrcweir 		pSequenceIndex[i]= bHasProperty ? nNumberOfProperties : -1;
123cdf0e10cSrcweir 		if ( bHasProperty )
124cdf0e10cSrcweir 			nNumberOfProperties++;
125cdf0e10cSrcweir 	}
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 	// construct property sequence from index array
128cdf0e10cSrcweir 	if ( aPropertySequence.getLength() != nNumberOfProperties )
129cdf0e10cSrcweir 		aPropertySequence.realloc( nNumberOfProperties );
130cdf0e10cSrcweir 	OUString* pPropertySequence = aPropertySequence.getArray();
131cdf0e10cSrcweir 	for( i = 0; i < nLength; i ++ )
132cdf0e10cSrcweir 	{
133cdf0e10cSrcweir 		sal_Int16 nIndex = pSequenceIndex[i];
134cdf0e10cSrcweir 		if ( nIndex != -1 )
135cdf0e10cSrcweir 			pPropertySequence[nIndex] = pPropertyNames[i];
136cdf0e10cSrcweir 	}
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
checkedProperties()139cdf0e10cSrcweir sal_Bool MultiPropertySetHelper::checkedProperties()
140cdf0e10cSrcweir {
141cdf0e10cSrcweir     return (NULL != pSequenceIndex);
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 
getValues(const Reference<XMultiPropertySet> & rMultiPropertySet)146cdf0e10cSrcweir void MultiPropertySetHelper::getValues(
147cdf0e10cSrcweir 	const Reference<XMultiPropertySet> & rMultiPropertySet )
148cdf0e10cSrcweir {
149cdf0e10cSrcweir 	DBG_ASSERT( rMultiPropertySet.is(), "We need an XMultiPropertySet." );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 	aValues = rMultiPropertySet->getPropertyValues( aPropertySequence );
152cdf0e10cSrcweir 	pValues = aValues.getConstArray();
153cdf0e10cSrcweir }
154cdf0e10cSrcweir 
getValues(const Reference<XPropertySet> & rPropertySet)155cdf0e10cSrcweir void MultiPropertySetHelper::getValues(
156cdf0e10cSrcweir 	const Reference<XPropertySet> & rPropertySet )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir 	DBG_ASSERT( rPropertySet.is(), "We need an XPropertySet." );
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 	// re-alloc aValues (if necessary) and fill with values from XPropertySet
161cdf0e10cSrcweir 	sal_Int16 nSupportedPropertiesCount =
162cdf0e10cSrcweir 		(sal_Int16)aPropertySequence.getLength();
163cdf0e10cSrcweir 	if ( aValues.getLength() != nSupportedPropertiesCount )
164cdf0e10cSrcweir 		aValues.realloc( nSupportedPropertiesCount );
165cdf0e10cSrcweir 	Any* pMutableArray = aValues.getArray();
166cdf0e10cSrcweir 	for( sal_Int16 i = 0; i < nSupportedPropertiesCount; i++ )
167cdf0e10cSrcweir 	{
168cdf0e10cSrcweir 		pMutableArray[i] = rPropertySet->getPropertyValue(
169cdf0e10cSrcweir 			pPropertyNames[ pSequenceIndex[ i ] ] );
170cdf0e10cSrcweir 	}
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 	// re-establish pValues pointer
173cdf0e10cSrcweir 	pValues = aValues.getConstArray();
174cdf0e10cSrcweir }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 
getValue(sal_Int16 nIndex,const Reference<XPropertySet> & rPropSet,sal_Bool bTryMulti)177cdf0e10cSrcweir const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
178cdf0e10cSrcweir 					 const Reference< XPropertySet> & rPropSet,
179cdf0e10cSrcweir 					 sal_Bool bTryMulti )
180cdf0e10cSrcweir {
181cdf0e10cSrcweir 	if( !pValues )
182cdf0e10cSrcweir 	{
183cdf0e10cSrcweir 		if( bTryMulti )
184cdf0e10cSrcweir 		{
185cdf0e10cSrcweir 			Reference < XMultiPropertySet > xMultiPropSet( rPropSet,
186cdf0e10cSrcweir 														   UNO_QUERY );
187cdf0e10cSrcweir 			if( xMultiPropSet.is() )
188cdf0e10cSrcweir 				getValues( xMultiPropSet );
189cdf0e10cSrcweir 			else
190cdf0e10cSrcweir 				getValues( rPropSet );
191cdf0e10cSrcweir 		}
192cdf0e10cSrcweir 		else
193cdf0e10cSrcweir 		{
194cdf0e10cSrcweir 			getValues( rPropSet );
195cdf0e10cSrcweir 		}
196cdf0e10cSrcweir 	}
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 	return getValue( nIndex );
199cdf0e10cSrcweir }
200cdf0e10cSrcweir 
getValue(sal_Int16 nIndex,const Reference<XMultiPropertySet> & rMultiPropSet)201cdf0e10cSrcweir const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
202cdf0e10cSrcweir 					 const Reference< XMultiPropertySet> & rMultiPropSet )
203cdf0e10cSrcweir {
204cdf0e10cSrcweir 	if( !pValues )
205cdf0e10cSrcweir 		getValues( rMultiPropSet );
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 	return getValue( nIndex );
208cdf0e10cSrcweir }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir // inline methods defined in header:
211cdf0e10cSrcweir // inline Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex )
212cdf0e10cSrcweir // inline sal_Bool MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo )
213