Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
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.
15 : : *
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).
21 : : *
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.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
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>
36 : :
37 : : // STL includes
38 : : #include <algorithm>
39 : :
40 : :
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;
52 : :
53 : :
54 : 234 : MultiPropertySetHelper::MultiPropertySetHelper(
55 : : const sal_Char** pNames ) :
56 : : pPropertyNames( NULL ),
57 : : nLength( 0 ),
58 : : aPropertySequence(),
59 : : pSequenceIndex( NULL ),
60 : : aValues(),
61 [ + - ]: 234 : pValues( NULL )
62 : : {
63 : : // first count the elements
64 [ + + ]: 1200 : for( const sal_Char** pPtr = pNames; *pPtr != NULL; pPtr++ )
65 : 966 : nLength++;
66 : :
67 : : // allocate array and create strings
68 [ + - ][ + + ]: 1200 : pPropertyNames = new OUString[nLength];
69 [ + + ]: 1200 : for( sal_Int16 i = 0; i < nLength; i++ )
70 : 966 : pPropertyNames[i] = OUString::createFromAscii( pNames[i] );
71 : 234 : }
72 : :
73 : :
74 [ + - ]: 234 : MultiPropertySetHelper::~MultiPropertySetHelper()
75 : : {
76 : 234 : pValues = NULL; // memory 'owned' by aValues
77 : :
78 [ + - ]: 234 : delete[] pSequenceIndex;
79 [ + - ][ + + ]: 1200 : delete[] pPropertyNames;
80 : 234 : }
81 : :
82 : :
83 : 234 : void MultiPropertySetHelper::hasProperties(
84 : : const Reference<XPropertySetInfo> & rInfo )
85 : : {
86 : : DBG_ASSERT( rInfo.is(), "I'd really like an XPropertySetInfo here." );
87 : :
88 : : // allocate sequence index
89 [ + - ]: 234 : if ( NULL == pSequenceIndex )
90 : 234 : pSequenceIndex = new sal_Int16[nLength] ;
91 : :
92 : : // construct pSequenceIndex
93 : 234 : sal_Int16 nNumberOfProperties = 0;
94 : : sal_Int16 i;
95 : :
96 [ + + ]: 1200 : for( i = 0; i < nLength; i++ )
97 : : {
98 : : // ask for property
99 : : sal_Bool bHasProperty =
100 : 966 : rInfo->hasPropertyByName( pPropertyNames[i] );
101 : :
102 : : // set index and increment (if appropriate)
103 [ + + ]: 966 : pSequenceIndex[i]= bHasProperty ? nNumberOfProperties : -1;
104 [ + + ]: 966 : if ( bHasProperty )
105 : 339 : nNumberOfProperties++;
106 : : }
107 : :
108 : : // construct property sequence from index array
109 [ + - ]: 234 : if ( aPropertySequence.getLength() != nNumberOfProperties )
110 : 234 : aPropertySequence.realloc( nNumberOfProperties );
111 : 234 : OUString* pPropertySequence = aPropertySequence.getArray();
112 [ + + ]: 1200 : for( i = 0; i < nLength; i ++ )
113 : : {
114 : 966 : sal_Int16 nIndex = pSequenceIndex[i];
115 [ + + ]: 966 : if ( nIndex != -1 )
116 : 339 : pPropertySequence[nIndex] = pPropertyNames[i];
117 : : }
118 : 234 : }
119 : :
120 : 634 : sal_Bool MultiPropertySetHelper::checkedProperties()
121 : : {
122 : 634 : return (NULL != pSequenceIndex);
123 : : }
124 : :
125 : :
126 : :
127 : 224 : void MultiPropertySetHelper::getValues(
128 : : const Reference<XMultiPropertySet> & rMultiPropertySet )
129 : : {
130 : : DBG_ASSERT( rMultiPropertySet.is(), "We need an XMultiPropertySet." );
131 : :
132 [ + - ]: 224 : aValues = rMultiPropertySet->getPropertyValues( aPropertySequence );
133 : 224 : pValues = aValues.getConstArray();
134 : 224 : }
135 : :
136 : 0 : void MultiPropertySetHelper::getValues(
137 : : const Reference<XPropertySet> & rPropertySet )
138 : : {
139 : : DBG_ASSERT( rPropertySet.is(), "We need an XPropertySet." );
140 : :
141 : : // re-alloc aValues (if necessary) and fill with values from XPropertySet
142 : : sal_Int16 nSupportedPropertiesCount =
143 : 0 : (sal_Int16)aPropertySequence.getLength();
144 [ # # ]: 0 : if ( aValues.getLength() != nSupportedPropertiesCount )
145 : 0 : aValues.realloc( nSupportedPropertiesCount );
146 : 0 : Any* pMutableArray = aValues.getArray();
147 [ # # ]: 0 : for( sal_Int16 i = 0; i < nSupportedPropertiesCount; i++ )
148 : : {
149 : 0 : pMutableArray[i] = rPropertySet->getPropertyValue(
150 : 0 : pPropertyNames[ pSequenceIndex[ i ] ] );
151 : : }
152 : :
153 : : // re-establish pValues pointer
154 : 0 : pValues = aValues.getConstArray();
155 : 0 : }
156 : :
157 : :
158 : 224 : const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
159 : : const Reference< XPropertySet> & rPropSet,
160 : : sal_Bool bTryMulti )
161 : : {
162 [ + - ]: 224 : if( !pValues )
163 : : {
164 [ + - ]: 224 : if( bTryMulti )
165 : : {
166 : : Reference < XMultiPropertySet > xMultiPropSet( rPropSet,
167 [ + - ]: 224 : UNO_QUERY );
168 [ + - ]: 224 : if( xMultiPropSet.is() )
169 [ + - ]: 224 : getValues( xMultiPropSet );
170 : : else
171 [ # # ]: 224 : getValues( rPropSet );
172 : : }
173 : : else
174 : : {
175 : 0 : getValues( rPropSet );
176 : : }
177 : : }
178 : :
179 : 224 : return getValue( nIndex );
180 : : }
181 : :
182 : 531 : const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
183 : : const Reference< XMultiPropertySet> & rMultiPropSet )
184 : : {
185 [ - + ]: 531 : if( !pValues )
186 : 0 : getValues( rMultiPropSet );
187 : :
188 : 531 : return getValue( nIndex );
189 : : }
190 : :
191 : : // inline methods defined in header:
192 : : // inline Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex )
193 : : // inline sal_Bool MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo )
194 : :
195 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|