1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 #ifndef _XMLOFF_CONDITIONALMULTIPROPERTYSETHELPER_HXX 24 #define _XMLOFF_CONDITIONALMULTIPROPERTYSETHELPER_HXX 25 26 #include <rtl/ustring.hxx> 27 #include <com/sun/star/uno/Sequence.hxx> 28 #include <tools/debug.hxx> 29 30 31 namespace com { namespace sun { namespace star { 32 namespace beans { class XMultiPropertySet; } 33 namespace beans { class XPropertySet; } 34 namespace beans { class XPropertySetInfo; } 35 } } } 36 37 38 /** 39 * The MultiPropertySetHelper performs the follwing functions: 40 * 41 * Given a list of property names (as sal_Char** or OUString*), it can 42 * query an XMultiPropertySet (or XPropertySet) which of these properties 43 * it supports (method hasProperties(...)). The properties *MUST* be 44 * sorted alphabetically. 45 * 46 * Then, the X(Multi)PropertySet can be queried for values, and only 47 * the supported properties are queried. (method getValues(...)) The 48 * values are stored in the helper itself. 49 * 50 * Finally, each property can be queried for existence 51 * (method hasProperty(...)) or its value (method (getValue(...))). 52 * 53 * After some initial preparation (hasProperties, getValues) the 54 * MultiPropertySetHelper can be used similarly to an 55 * XPropertySet in that you can query the values in the places where you 56 * need them. However, if an XMultiPropertySet is supplied, the queries 57 * are more efficient, often significantly so. 58 */ 59 class MultiPropertySetHelper 60 { 61 /// names of all properties 62 ::rtl::OUString* pPropertyNames; 63 64 /// length of pPropertyNames array 65 sal_Int16 nLength; 66 67 /// the sequence of property names that the current (multi) 68 /// property set implementation supports 69 ::com::sun::star::uno::Sequence< ::rtl::OUString > aPropertySequence; 70 71 /// an array of indices that maps from pPropertyNames indices to 72 /// aPropertySequence indices 73 sal_Int16* pSequenceIndex; 74 75 /// the last set of values retrieved by getValues 76 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > aValues; 77 78 /// result of aValues.getConstArray() 79 const ::com::sun::star::uno::Any* pValues; 80 81 /// an empty Any 82 ::com::sun::star::uno::Any aEmptyAny; 83 84 public: 85 86 MultiPropertySetHelper( const sal_Char** pNames ); 87 88 MultiPropertySetHelper( const ::rtl::OUString* pNames ); 89 90 ~MultiPropertySetHelper(); 91 92 93 /** 94 * Call hasPropertiesByName for the provided XPropertySetInfo and build 95 * list of allowed properties. 96 */ 97 void hasProperties( const ::com::sun::star::uno::Reference< 98 ::com::sun::star::beans::XPropertySetInfo> & ); 99 100 101 /** 102 * Return whether hasProperties was called 103 * (i.e. if we are ready to call getValues) 104 */ 105 sal_Bool checkedProperties(); 106 107 /** 108 * Get values from the XMultiPropertySet. 109 * 110 * May only be called after hasProperties() was called for the 111 * appropriate XPropertySetInfo. 112 */ 113 void getValues( const ::com::sun::star::uno::Reference< 114 ::com::sun::star::beans::XMultiPropertySet> & ); 115 116 /** 117 * Get values from the XPropertySet. This can be much slower than 118 * getValues( const Reference<XMultiPropertySet& ) and hence 119 * should be avoided. 120 * 121 * May only be called after hasProperties() was called for the 122 * appropriate XPropertySetInfo. 123 */ 124 void getValues( const ::com::sun::star::uno::Reference< 125 ::com::sun::star::beans::XPropertySet> & ); 126 127 128 129 /** 130 * Get a value from the values array. 131 * 132 * May only be called after getValues() was called. 133 */ 134 inline const ::com::sun::star::uno::Any& getValue( sal_Int16 nIndex ); 135 136 /** 137 * Find out if this property is supported. 138 * 139 * May only be called after hasProperties() was called. 140 */ 141 inline sal_Bool hasProperty( sal_Int16 nIndex ); 142 143 /** 144 * Get a value from the XPropertySet on demand. 145 * 146 * If neither getValues nor getValueOnDemand has been called already 147 * after the last call to resetValues, the values are retrieved 148 * using getValues. Otherwise the value already retrieved is returned. 149 * In case XMultiPropertySet is supported by the XPropertySet and 150 * bTryMult is set, the XMultiPropertySet is used to get the values. 151 * 152 */ 153 const ::com::sun::star::uno::Any& getValue( sal_Int16 nIndex, 154 const ::com::sun::star::uno::Reference< 155 ::com::sun::star::beans::XPropertySet> &, 156 sal_Bool bTryMulti = sal_False ); 157 158 /** 159 * Get a value from the XMultiPropertySet on demand. 160 * 161 * If neither getValues nor getValueOnDemand has been called already 162 * after the last call to resetValues, the values are retrieved 163 * using getValues. Otherwise the value already retrieved is returned. 164 * In case XMultiPropertySet is supported by the XPropertySet, 165 * XMultiPropertySet is used to get the values. 166 * 167 */ 168 const ::com::sun::star::uno::Any& getValue( sal_Int16 nIndex, 169 const ::com::sun::star::uno::Reference< 170 ::com::sun::star::beans::XMultiPropertySet> & ); 171 172 inline void resetValues() { pValues = 0; } 173 }; 174 175 176 // inline implementations of the often-called methods getValue and hasProperty: 177 178 const ::com::sun::star::uno::Any& MultiPropertySetHelper::getValue( 179 sal_Int16 nValueNo ) 180 { 181 DBG_ASSERT( pValues != NULL, 182 "called getValue() without calling getValues() before"); 183 DBG_ASSERT( pSequenceIndex != NULL, 184 "called getValue() without calling hasProperties() before" ); 185 DBG_ASSERT( nValueNo < nLength, "index out of range" ); 186 187 sal_Int16 nIndex = pSequenceIndex[ nValueNo ]; 188 return ( nIndex != -1 ) ? pValues[ nIndex ] : aEmptyAny; 189 } 190 191 sal_Bool MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo ) 192 { 193 DBG_ASSERT( pSequenceIndex != NULL, 194 "called getValue() without calling hasProperties() before" ); 195 DBG_ASSERT( nValueNo < nLength, "index out of range" ); 196 197 return pSequenceIndex[ nValueNo ] != -1; 198 } 199 200 #endif 201