LibreOffice Module xmloff (master)  1
MultiPropertySetHelper.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 
22 #include <com/sun/star/beans/XPropertySetInfo.hpp>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/beans/XMultiPropertySet.hpp>
25 
26 #include <sal/log.hxx>
27 
28 using ::com::sun::star::beans::XMultiPropertySet;
29 using ::com::sun::star::beans::XPropertySet;
30 using ::com::sun::star::beans::XPropertySetInfo;
31 using ::com::sun::star::uno::Any;
32 using ::com::sun::star::uno::Reference;
33 using ::com::sun::star::uno::UNO_QUERY;
34 
35 
37  const char** pNames ) :
38  nLength( 0 ),
39  aPropertySequence(),
40  aValues(),
41  pValues( nullptr )
42 {
43  // first count the elements
44  for( const char** pPtr = pNames; *pPtr != nullptr; pPtr++ )
45  nLength++;
46 
47  // allocate array and create strings
48  pPropertyNames.reset( new OUString[nLength] );
49  for( sal_Int16 i = 0; i < nLength; i++ )
50  pPropertyNames[i] = OUString::createFromAscii( pNames[i] );
51 }
52 
53 
55 {
56  pValues = nullptr; // memory 'owned' by aValues
57 }
58 
59 
61  const Reference<XPropertySetInfo> & rInfo )
62 {
63  SAL_WARN_IF( !rInfo.is(), "xmloff", "I'd really like an XPropertySetInfo here." );
64 
65  // allocate sequence index
66  if ( !pSequenceIndex )
67  pSequenceIndex.reset( new sal_Int16[nLength] );
68 
69  // construct pSequenceIndex
70  sal_Int16 nNumberOfProperties = 0;
71  sal_Int16 i;
72 
73  for( i = 0; i < nLength; i++ )
74  {
75  // ask for property
76  bool bHasProperty =
77  rInfo->hasPropertyByName( pPropertyNames[i] );
78 
79  // set index and increment (if appropriate)
80  pSequenceIndex[i]= bHasProperty ? nNumberOfProperties : -1;
81  if ( bHasProperty )
82  nNumberOfProperties++;
83  }
84 
85  // construct property sequence from index array
86  if ( aPropertySequence.getLength() != nNumberOfProperties )
87  aPropertySequence.realloc( nNumberOfProperties );
88  OUString* pPropertySequence = aPropertySequence.getArray();
89  for( i = 0; i < nLength; i ++ )
90  {
91  sal_Int16 nIndex = pSequenceIndex[i];
92  if ( nIndex != -1 )
93  pPropertySequence[nIndex] = pPropertyNames[i];
94  }
95 }
96 
98 {
99  return (nullptr != pSequenceIndex);
100 }
101 
102 
104  const Reference<XMultiPropertySet> & rMultiPropertySet )
105 {
106  SAL_WARN_IF( !rMultiPropertySet.is(), "xmloff", "We need an XMultiPropertySet." );
107 
108  aValues = rMultiPropertySet->getPropertyValues( aPropertySequence );
109  pValues = aValues.getConstArray();
110 }
111 
113  const Reference<XPropertySet> & rPropertySet )
114 {
115  SAL_WARN_IF( !rPropertySet.is(), "xmloff", "We need an XPropertySet." );
116 
117  // re-alloc aValues (if necessary) and fill with values from XPropertySet
118  sal_Int16 nSupportedPropertiesCount =
119  static_cast<sal_Int16>(aPropertySequence.getLength());
120  if ( aValues.getLength() != nSupportedPropertiesCount )
121  aValues.realloc( nSupportedPropertiesCount );
122  Any* pMutableArray = aValues.getArray();
123  for( sal_Int16 i = 0; i < nSupportedPropertiesCount; i++ )
124  {
125  pMutableArray[i] = rPropertySet->getPropertyValue(
126  pPropertyNames[ pSequenceIndex[ i ] ] );
127  }
128 
129  // re-establish pValues pointer
130  pValues = aValues.getConstArray();
131 }
132 
133 
134 const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
135  const Reference< XPropertySet> & rPropSet,
136  bool bTryMulti )
137 {
138  if( !pValues )
139  {
140  if( bTryMulti )
141  {
142  Reference < XMultiPropertySet > xMultiPropSet( rPropSet,
143  UNO_QUERY );
144  if( xMultiPropSet.is() )
145  getValues( xMultiPropSet );
146  else
147  getValues( rPropSet );
148  }
149  else
150  {
151  getValues( rPropSet );
152  }
153  }
154 
155  return getValue( nIndex );
156 }
157 
158 const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
159  const Reference< XMultiPropertySet> & rMultiPropSet )
160 {
161  if( !pValues )
162  getValues( rMultiPropSet );
163 
164  return getValue( nIndex );
165 }
166 
167 // inline methods defined in header:
168 // inline Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex )
169 // inline sal_Bool MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo )
170 
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int16 nLength
length of pPropertyNames array
sal_Int32 nIndex
bool checkedProperties()
Return whether hasProperties was called (i.e.
void getValues(const css::uno::Reference< css::beans::XMultiPropertySet > &)
Get values from the XMultiPropertySet.
const css::uno::Any & getValue(sal_Int16 nIndex)
Get a value from the values array.
const css::uno::Any * pValues
result of aValues.getConstArray()
int i
void hasProperties(const css::uno::Reference< css::beans::XPropertySetInfo > &)
Call hasPropertiesByName for the provided XPropertySetInfo and build list of allowed properties...
MultiPropertySetHelper(const char **pNames)
const PropertyValue * pValues
std::unique_ptr< OUString[]> pPropertyNames
names of all properties
#define SAL_WARN_IF(condition, area, stream)
std::unique_ptr< sal_Int16[]> pSequenceIndex
an array of indices that maps from pPropertyNames indices to aPropertySequence indices ...
css::uno::Sequence< OUString > aPropertySequence
the sequence of property names that the current (multi) property set implementation supports ...
sal_Int32 nLength
Definition: xmltoken.cxx:36
css::uno::Sequence< css::uno::Any > aValues
the last set of values retrieved by getValues