Update ooo320-m1
[ooovba.git] / xmloff / source / style / MultiPropertySetHelper.cxx
blobbf856604dfe67dcd4a4756af5fb12e3d829bbcc2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MultiPropertySetHelper.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
34 #ifndef _XMLOFF_MULTIPROPERTYSETHELPER_HXX
35 #include "MultiPropertySetHelper.hxx"
36 #endif
37 #include <com/sun/star/beans/XPropertySetInfo.hpp>
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <com/sun/star/beans/XMultiPropertySet.hpp>
40 #include <com/sun/star/lang/XServiceInfo.hpp>
41 #include <comphelper/stl_types.hxx>
43 // STL includes
44 #include <algorithm>
47 using ::com::sun::star::beans::XMultiPropertySet;
48 using ::com::sun::star::beans::XPropertySet;
49 using ::com::sun::star::beans::XPropertySetInfo;
50 using ::com::sun::star::lang::XServiceInfo;
51 using ::com::sun::star::uno::Any;
52 using ::com::sun::star::uno::Reference;
53 using ::com::sun::star::uno::Sequence;
54 using ::com::sun::star::uno::UNO_QUERY;
55 using ::comphelper::UStringLess;
56 using ::rtl::OUString;
57 using ::std::sort;
60 MultiPropertySetHelper::MultiPropertySetHelper(
61 const sal_Char** pNames ) :
62 pPropertyNames( NULL ),
63 nLength( 0 ),
64 aPropertySequence(),
65 pSequenceIndex( NULL ),
66 aValues(),
67 pValues( NULL )
69 // first count the elements
70 for( const sal_Char** pPtr = pNames; *pPtr != NULL; pPtr++ )
71 nLength++;
73 // allocate array and create strings
74 pPropertyNames = new OUString[nLength];
75 for( sal_Int16 i = 0; i < nLength; i++ )
76 pPropertyNames[i] = OUString::createFromAscii( pNames[i] );
79 MultiPropertySetHelper::MultiPropertySetHelper(
80 const OUString* pNames ) :
81 pPropertyNames( NULL ),
82 nLength( 0 ),
83 aPropertySequence(),
84 pSequenceIndex( NULL ),
85 aValues(),
86 pValues( NULL )
88 // count elements
89 for( const OUString* pPtr = pNames; pPtr != NULL; pPtr++ )
90 nLength++;
92 // allocate array and assign strings
93 pPropertyNames = new OUString[nLength];
94 for( sal_Int16 i = 0; i < nLength; i++ )
95 pPropertyNames[i] = pNames[i];
99 MultiPropertySetHelper::~MultiPropertySetHelper()
101 pValues = NULL; // memory 'owned' by aValues
103 delete[] pSequenceIndex;
104 delete[] pPropertyNames;
109 void MultiPropertySetHelper::hasProperties(
110 const Reference<XPropertySetInfo> & rInfo )
112 DBG_ASSERT( rInfo.is(), "I'd really like an XPropertySetInfo here." );
114 // allocate sequence index
115 if ( NULL == pSequenceIndex )
116 pSequenceIndex = new sal_Int16[nLength] ;
118 // construct pSequenceIndex
119 sal_Int16 nNumberOfProperties = 0;
120 sal_Int16 i;
122 for( i = 0; i < nLength; i++ )
124 // ask for property
125 sal_Bool bHasProperty =
126 rInfo->hasPropertyByName( pPropertyNames[i] );
128 // set index and increment (if appropriate)
129 pSequenceIndex[i]= bHasProperty ? nNumberOfProperties : -1;
130 if ( bHasProperty )
131 nNumberOfProperties++;
134 // construct property sequence from index array
135 if ( aPropertySequence.getLength() != nNumberOfProperties )
136 aPropertySequence.realloc( nNumberOfProperties );
137 OUString* pPropertySequence = aPropertySequence.getArray();
138 for( i = 0; i < nLength; i ++ )
140 sal_Int16 nIndex = pSequenceIndex[i];
141 if ( nIndex != -1 )
142 pPropertySequence[nIndex] = pPropertyNames[i];
146 sal_Bool MultiPropertySetHelper::checkedProperties()
148 return (NULL != pSequenceIndex);
153 void MultiPropertySetHelper::getValues(
154 const Reference<XMultiPropertySet> & rMultiPropertySet )
156 DBG_ASSERT( rMultiPropertySet.is(), "We need an XMultiPropertySet." );
158 aValues = rMultiPropertySet->getPropertyValues( aPropertySequence );
159 pValues = aValues.getConstArray();
162 void MultiPropertySetHelper::getValues(
163 const Reference<XPropertySet> & rPropertySet )
165 DBG_ASSERT( rPropertySet.is(), "We need an XPropertySet." );
167 // re-alloc aValues (if necessary) and fill with values from XPropertySet
168 sal_Int16 nSupportedPropertiesCount =
169 (sal_Int16)aPropertySequence.getLength();
170 if ( aValues.getLength() != nSupportedPropertiesCount )
171 aValues.realloc( nSupportedPropertiesCount );
172 Any* pMutableArray = aValues.getArray();
173 for( sal_Int16 i = 0; i < nSupportedPropertiesCount; i++ )
175 pMutableArray[i] = rPropertySet->getPropertyValue(
176 pPropertyNames[ pSequenceIndex[ i ] ] );
179 // re-establish pValues pointer
180 pValues = aValues.getConstArray();
184 const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
185 const Reference< XPropertySet> & rPropSet,
186 sal_Bool bTryMulti )
188 if( !pValues )
190 if( bTryMulti )
192 Reference < XMultiPropertySet > xMultiPropSet( rPropSet,
193 UNO_QUERY );
194 if( xMultiPropSet.is() )
195 getValues( xMultiPropSet );
196 else
197 getValues( rPropSet );
199 else
201 getValues( rPropSet );
205 return getValue( nIndex );
208 const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
209 const Reference< XMultiPropertySet> & rMultiPropSet )
211 if( !pValues )
212 getValues( rMultiPropSet );
214 return getValue( nIndex );
217 // inline methods defined in header:
218 // inline Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex )
219 // inline sal_Bool MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo )