xref: /AOO41X/main/xmloff/source/style/MultiPropertySetHelper.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_xmloff.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #ifndef _XMLOFF_MULTIPROPERTYSETHELPER_HXX
32*cdf0e10cSrcweir #include "MultiPropertySetHelper.hxx"
33*cdf0e10cSrcweir #endif
34*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySetInfo.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/beans/XMultiPropertySet.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
38*cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir // STL includes
41*cdf0e10cSrcweir #include <algorithm>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir using ::com::sun::star::beans::XMultiPropertySet;
45*cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySet;
46*cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySetInfo;
47*cdf0e10cSrcweir using ::com::sun::star::lang::XServiceInfo;
48*cdf0e10cSrcweir using ::com::sun::star::uno::Any;
49*cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
50*cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
51*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY;
52*cdf0e10cSrcweir using ::comphelper::UStringLess;
53*cdf0e10cSrcweir using ::rtl::OUString;
54*cdf0e10cSrcweir using ::std::sort;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir MultiPropertySetHelper::MultiPropertySetHelper(
58*cdf0e10cSrcweir 	const sal_Char** pNames ) :
59*cdf0e10cSrcweir 		pPropertyNames( NULL ),
60*cdf0e10cSrcweir 		nLength( 0 ),
61*cdf0e10cSrcweir 		aPropertySequence(),
62*cdf0e10cSrcweir 		pSequenceIndex( NULL ),
63*cdf0e10cSrcweir 		aValues(),
64*cdf0e10cSrcweir 		pValues( NULL )
65*cdf0e10cSrcweir {
66*cdf0e10cSrcweir 	// first count the elements
67*cdf0e10cSrcweir 	for( const sal_Char** pPtr = pNames; *pPtr != NULL; pPtr++ )
68*cdf0e10cSrcweir 		nLength++;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 	// allocate array and create strings
71*cdf0e10cSrcweir 	pPropertyNames = new OUString[nLength];
72*cdf0e10cSrcweir 	for( sal_Int16 i = 0; i < nLength; i++ )
73*cdf0e10cSrcweir 		pPropertyNames[i] = OUString::createFromAscii( pNames[i] );
74*cdf0e10cSrcweir }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir MultiPropertySetHelper::MultiPropertySetHelper(
77*cdf0e10cSrcweir 	const OUString* pNames ) :
78*cdf0e10cSrcweir 		pPropertyNames( NULL ),
79*cdf0e10cSrcweir 		nLength( 0 ),
80*cdf0e10cSrcweir 		aPropertySequence(),
81*cdf0e10cSrcweir 		pSequenceIndex( NULL ),
82*cdf0e10cSrcweir 		aValues(),
83*cdf0e10cSrcweir 		pValues( NULL )
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir 	// count elements
86*cdf0e10cSrcweir 	for( const OUString* pPtr = pNames; pPtr != NULL; pPtr++ )
87*cdf0e10cSrcweir 		nLength++;
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 	// allocate array and assign strings
90*cdf0e10cSrcweir 	pPropertyNames = new OUString[nLength];
91*cdf0e10cSrcweir 	for( sal_Int16 i = 0; i < nLength; i++ )
92*cdf0e10cSrcweir 		pPropertyNames[i] = pNames[i];
93*cdf0e10cSrcweir }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir MultiPropertySetHelper::~MultiPropertySetHelper()
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir 	pValues = NULL;	// memory 'owned' by aValues
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 	delete[] pSequenceIndex;
101*cdf0e10cSrcweir 	delete[] pPropertyNames;
102*cdf0e10cSrcweir }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir void MultiPropertySetHelper::hasProperties(
107*cdf0e10cSrcweir 	const Reference<XPropertySetInfo> & rInfo )
108*cdf0e10cSrcweir {
109*cdf0e10cSrcweir 	DBG_ASSERT( rInfo.is(), "I'd really like an XPropertySetInfo here." );
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 	// allocate sequence index
112*cdf0e10cSrcweir 	if ( NULL == pSequenceIndex )
113*cdf0e10cSrcweir 		pSequenceIndex = new sal_Int16[nLength] ;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 	// construct pSequenceIndex
116*cdf0e10cSrcweir 	sal_Int16 nNumberOfProperties = 0;
117*cdf0e10cSrcweir 	sal_Int16 i;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 	for( i = 0; i < nLength; i++ )
120*cdf0e10cSrcweir 	{
121*cdf0e10cSrcweir 		// ask for property
122*cdf0e10cSrcweir 		sal_Bool bHasProperty =
123*cdf0e10cSrcweir 			rInfo->hasPropertyByName( pPropertyNames[i] );
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 		// set index and increment (if appropriate)
126*cdf0e10cSrcweir 		pSequenceIndex[i]= bHasProperty ? nNumberOfProperties : -1;
127*cdf0e10cSrcweir 		if ( bHasProperty )
128*cdf0e10cSrcweir 			nNumberOfProperties++;
129*cdf0e10cSrcweir 	}
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 	// construct property sequence from index array
132*cdf0e10cSrcweir 	if ( aPropertySequence.getLength() != nNumberOfProperties )
133*cdf0e10cSrcweir 		aPropertySequence.realloc( nNumberOfProperties );
134*cdf0e10cSrcweir 	OUString* pPropertySequence = aPropertySequence.getArray();
135*cdf0e10cSrcweir 	for( i = 0; i < nLength; i ++ )
136*cdf0e10cSrcweir 	{
137*cdf0e10cSrcweir 		sal_Int16 nIndex = pSequenceIndex[i];
138*cdf0e10cSrcweir 		if ( nIndex != -1 )
139*cdf0e10cSrcweir 			pPropertySequence[nIndex] = pPropertyNames[i];
140*cdf0e10cSrcweir 	}
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir sal_Bool MultiPropertySetHelper::checkedProperties()
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir     return (NULL != pSequenceIndex);
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir void MultiPropertySetHelper::getValues(
151*cdf0e10cSrcweir 	const Reference<XMultiPropertySet> & rMultiPropertySet )
152*cdf0e10cSrcweir {
153*cdf0e10cSrcweir 	DBG_ASSERT( rMultiPropertySet.is(), "We need an XMultiPropertySet." );
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 	aValues = rMultiPropertySet->getPropertyValues( aPropertySequence );
156*cdf0e10cSrcweir 	pValues = aValues.getConstArray();
157*cdf0e10cSrcweir }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir void MultiPropertySetHelper::getValues(
160*cdf0e10cSrcweir 	const Reference<XPropertySet> & rPropertySet )
161*cdf0e10cSrcweir {
162*cdf0e10cSrcweir 	DBG_ASSERT( rPropertySet.is(), "We need an XPropertySet." );
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 	// re-alloc aValues (if necessary) and fill with values from XPropertySet
165*cdf0e10cSrcweir 	sal_Int16 nSupportedPropertiesCount =
166*cdf0e10cSrcweir 		(sal_Int16)aPropertySequence.getLength();
167*cdf0e10cSrcweir 	if ( aValues.getLength() != nSupportedPropertiesCount )
168*cdf0e10cSrcweir 		aValues.realloc( nSupportedPropertiesCount );
169*cdf0e10cSrcweir 	Any* pMutableArray = aValues.getArray();
170*cdf0e10cSrcweir 	for( sal_Int16 i = 0; i < nSupportedPropertiesCount; i++ )
171*cdf0e10cSrcweir 	{
172*cdf0e10cSrcweir 		pMutableArray[i] = rPropertySet->getPropertyValue(
173*cdf0e10cSrcweir 			pPropertyNames[ pSequenceIndex[ i ] ] );
174*cdf0e10cSrcweir 	}
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 	// re-establish pValues pointer
177*cdf0e10cSrcweir 	pValues = aValues.getConstArray();
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
182*cdf0e10cSrcweir 					 const Reference< XPropertySet> & rPropSet,
183*cdf0e10cSrcweir 					 sal_Bool bTryMulti )
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir 	if( !pValues )
186*cdf0e10cSrcweir 	{
187*cdf0e10cSrcweir 		if( bTryMulti )
188*cdf0e10cSrcweir 		{
189*cdf0e10cSrcweir 			Reference < XMultiPropertySet > xMultiPropSet( rPropSet,
190*cdf0e10cSrcweir 														   UNO_QUERY );
191*cdf0e10cSrcweir 			if( xMultiPropSet.is() )
192*cdf0e10cSrcweir 				getValues( xMultiPropSet );
193*cdf0e10cSrcweir 			else
194*cdf0e10cSrcweir 				getValues( rPropSet );
195*cdf0e10cSrcweir 		}
196*cdf0e10cSrcweir 		else
197*cdf0e10cSrcweir 		{
198*cdf0e10cSrcweir 			getValues( rPropSet );
199*cdf0e10cSrcweir 		}
200*cdf0e10cSrcweir 	}
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 	return getValue( nIndex );
203*cdf0e10cSrcweir }
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
206*cdf0e10cSrcweir 					 const Reference< XMultiPropertySet> & rMultiPropSet )
207*cdf0e10cSrcweir {
208*cdf0e10cSrcweir 	if( !pValues )
209*cdf0e10cSrcweir 		getValues( rMultiPropSet );
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 	return getValue( nIndex );
212*cdf0e10cSrcweir }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir // inline methods defined in header:
215*cdf0e10cSrcweir // inline Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex )
216*cdf0e10cSrcweir // inline sal_Bool MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo )
217