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  pValues( nullptr )
40 {
41  // first count the elements
42  for( const char** pPtr = pNames; *pPtr != nullptr; pPtr++ )
43  nLength++;
44 
45  // allocate array and create strings
46  pPropertyNames.reset( new OUString[nLength] );
47  for( sal_Int16 i = 0; i < nLength; i++ )
48  pPropertyNames[i] = OUString::createFromAscii( pNames[i] );
49 }
50 
51 
53 {
54  pValues = nullptr; // memory 'owned' by aValues
55 }
56 
57 
59  const Reference<XPropertySetInfo> & rInfo )
60 {
61  SAL_WARN_IF( !rInfo.is(), "xmloff", "I'd really like an XPropertySetInfo here." );
62 
63  // allocate sequence index
64  if ( !pSequenceIndex )
65  pSequenceIndex.reset( new sal_Int16[nLength] );
66 
67  // construct pSequenceIndex
68  sal_Int16 nNumberOfProperties = 0;
69  sal_Int16 i;
70 
71  for( i = 0; i < nLength; i++ )
72  {
73  // ask for property
74  bool bHasProperty =
75  rInfo->hasPropertyByName( pPropertyNames[i] );
76 
77  // set index and increment (if appropriate)
78  pSequenceIndex[i]= bHasProperty ? nNumberOfProperties : -1;
79  if ( bHasProperty )
80  nNumberOfProperties++;
81  }
82 
83  // construct property sequence from index array
84  if ( aPropertySequence.getLength() != nNumberOfProperties )
85  aPropertySequence.realloc( nNumberOfProperties );
86  OUString* pPropertySequence = aPropertySequence.getArray();
87  for( i = 0; i < nLength; i ++ )
88  {
89  sal_Int16 nIndex = pSequenceIndex[i];
90  if ( nIndex != -1 )
91  pPropertySequence[nIndex] = pPropertyNames[i];
92  }
93 }
94 
96 {
97  return (nullptr != pSequenceIndex);
98 }
99 
100 
102  const Reference<XMultiPropertySet> & rMultiPropertySet )
103 {
104  SAL_WARN_IF( !rMultiPropertySet.is(), "xmloff", "We need an XMultiPropertySet." );
105 
106  aValues = rMultiPropertySet->getPropertyValues( aPropertySequence );
107  pValues = aValues.getConstArray();
108 }
109 
111  const Reference<XPropertySet> & rPropertySet )
112 {
113  SAL_WARN_IF( !rPropertySet.is(), "xmloff", "We need an XPropertySet." );
114 
115  // re-alloc aValues (if necessary) and fill with values from XPropertySet
116  sal_Int16 nSupportedPropertiesCount =
117  static_cast<sal_Int16>(aPropertySequence.getLength());
118  if ( aValues.getLength() != nSupportedPropertiesCount )
119  aValues.realloc( nSupportedPropertiesCount );
120  Any* pMutableArray = aValues.getArray();
121  for( sal_Int16 i = 0; i < nSupportedPropertiesCount; i++ )
122  {
123  pMutableArray[i] = rPropertySet->getPropertyValue(
124  pPropertyNames[ pSequenceIndex[ i ] ] );
125  }
126 
127  // re-establish pValues pointer
128  pValues = aValues.getConstArray();
129 }
130 
131 
132 const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
133  const Reference< XPropertySet> & rPropSet,
134  bool bTryMulti )
135 {
136  if( !pValues )
137  {
138  if( bTryMulti )
139  {
140  Reference < XMultiPropertySet > xMultiPropSet( rPropSet,
141  UNO_QUERY );
142  if( xMultiPropSet.is() )
143  getValues( xMultiPropSet );
144  else
145  getValues( rPropSet );
146  }
147  else
148  {
149  getValues( rPropSet );
150  }
151  }
152 
153  return getValue( nIndex );
154 }
155 
156 const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
157  const Reference< XMultiPropertySet> & rMultiPropSet )
158 {
159  if( !pValues )
160  getValues( rMultiPropSet );
161 
162  return getValue( nIndex );
163 }
164 
165 // inline methods defined in header:
166 // inline Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex )
167 // inline sal_Bool MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo )
168 
169 /* 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:38
css::uno::Sequence< css::uno::Any > aValues
the last set of values retrieved by getValues