Bump for 3.6-28
[LibreOffice.git] / xmloff / source / style / MultiPropertySetHelper.cxx
blob9bf81c053afb508b6f589801b391d8815de393dc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "MultiPropertySetHelper.hxx"
31 #include <com/sun/star/beans/XPropertySetInfo.hpp>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/beans/XMultiPropertySet.hpp>
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <comphelper/stl_types.hxx>
37 // STL includes
38 #include <algorithm>
41 using ::com::sun::star::beans::XMultiPropertySet;
42 using ::com::sun::star::beans::XPropertySet;
43 using ::com::sun::star::beans::XPropertySetInfo;
44 using ::com::sun::star::lang::XServiceInfo;
45 using ::com::sun::star::uno::Any;
46 using ::com::sun::star::uno::Reference;
47 using ::com::sun::star::uno::Sequence;
48 using ::com::sun::star::uno::UNO_QUERY;
49 using ::comphelper::UStringLess;
50 using ::rtl::OUString;
51 using ::std::sort;
54 MultiPropertySetHelper::MultiPropertySetHelper(
55 const sal_Char** pNames ) :
56 pPropertyNames( NULL ),
57 nLength( 0 ),
58 aPropertySequence(),
59 pSequenceIndex( NULL ),
60 aValues(),
61 pValues( NULL )
63 // first count the elements
64 for( const sal_Char** pPtr = pNames; *pPtr != NULL; pPtr++ )
65 nLength++;
67 // allocate array and create strings
68 pPropertyNames = new OUString[nLength];
69 for( sal_Int16 i = 0; i < nLength; i++ )
70 pPropertyNames[i] = OUString::createFromAscii( pNames[i] );
74 MultiPropertySetHelper::~MultiPropertySetHelper()
76 pValues = NULL; // memory 'owned' by aValues
78 delete[] pSequenceIndex;
79 delete[] pPropertyNames;
83 void MultiPropertySetHelper::hasProperties(
84 const Reference<XPropertySetInfo> & rInfo )
86 DBG_ASSERT( rInfo.is(), "I'd really like an XPropertySetInfo here." );
88 // allocate sequence index
89 if ( NULL == pSequenceIndex )
90 pSequenceIndex = new sal_Int16[nLength] ;
92 // construct pSequenceIndex
93 sal_Int16 nNumberOfProperties = 0;
94 sal_Int16 i;
96 for( i = 0; i < nLength; i++ )
98 // ask for property
99 sal_Bool bHasProperty =
100 rInfo->hasPropertyByName( pPropertyNames[i] );
102 // set index and increment (if appropriate)
103 pSequenceIndex[i]= bHasProperty ? nNumberOfProperties : -1;
104 if ( bHasProperty )
105 nNumberOfProperties++;
108 // construct property sequence from index array
109 if ( aPropertySequence.getLength() != nNumberOfProperties )
110 aPropertySequence.realloc( nNumberOfProperties );
111 OUString* pPropertySequence = aPropertySequence.getArray();
112 for( i = 0; i < nLength; i ++ )
114 sal_Int16 nIndex = pSequenceIndex[i];
115 if ( nIndex != -1 )
116 pPropertySequence[nIndex] = pPropertyNames[i];
120 sal_Bool MultiPropertySetHelper::checkedProperties()
122 return (NULL != pSequenceIndex);
127 void MultiPropertySetHelper::getValues(
128 const Reference<XMultiPropertySet> & rMultiPropertySet )
130 DBG_ASSERT( rMultiPropertySet.is(), "We need an XMultiPropertySet." );
132 aValues = rMultiPropertySet->getPropertyValues( aPropertySequence );
133 pValues = aValues.getConstArray();
136 void MultiPropertySetHelper::getValues(
137 const Reference<XPropertySet> & rPropertySet )
139 DBG_ASSERT( rPropertySet.is(), "We need an XPropertySet." );
141 // re-alloc aValues (if necessary) and fill with values from XPropertySet
142 sal_Int16 nSupportedPropertiesCount =
143 (sal_Int16)aPropertySequence.getLength();
144 if ( aValues.getLength() != nSupportedPropertiesCount )
145 aValues.realloc( nSupportedPropertiesCount );
146 Any* pMutableArray = aValues.getArray();
147 for( sal_Int16 i = 0; i < nSupportedPropertiesCount; i++ )
149 pMutableArray[i] = rPropertySet->getPropertyValue(
150 pPropertyNames[ pSequenceIndex[ i ] ] );
153 // re-establish pValues pointer
154 pValues = aValues.getConstArray();
158 const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
159 const Reference< XPropertySet> & rPropSet,
160 sal_Bool bTryMulti )
162 if( !pValues )
164 if( bTryMulti )
166 Reference < XMultiPropertySet > xMultiPropSet( rPropSet,
167 UNO_QUERY );
168 if( xMultiPropSet.is() )
169 getValues( xMultiPropSet );
170 else
171 getValues( rPropSet );
173 else
175 getValues( rPropSet );
179 return getValue( nIndex );
182 const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
183 const Reference< XMultiPropertySet> & rMultiPropSet )
185 if( !pValues )
186 getValues( rMultiPropSet );
188 return getValue( nIndex );
191 // inline methods defined in header:
192 // inline Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex )
193 // inline sal_Bool MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo )
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */