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