xref: /AOO41X/main/xmloff/inc/MultiPropertySetHelper.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef _XMLOFF_CONDITIONALMULTIPROPERTYSETHELPER_HXX
28 #define _XMLOFF_CONDITIONALMULTIPROPERTYSETHELPER_HXX
29 
30 #include <rtl/ustring.hxx>
31 #include <com/sun/star/uno/Sequence.hxx>
32 #include <tools/debug.hxx>
33 
34 
35 namespace com { namespace sun { namespace star {
36 	namespace beans { class XMultiPropertySet; }
37 	namespace beans { class XPropertySet; }
38 	namespace beans { class XPropertySetInfo; }
39 } } }
40 
41 
42 /**
43  * The MultiPropertySetHelper performs the follwing functions:
44  *
45  * Given a list of property names (as sal_Char** or OUString*), it can
46  * query an XMultiPropertySet (or XPropertySet) which of these properties
47  * it supports (method hasProperties(...)). The properties *MUST* be
48  * sorted alphabetically.
49  *
50  * Then, the X(Multi)PropertySet can be queried for values, and only
51  * the supported properties are queried. (method getValues(...)) The
52  * values are stored in the helper itself.
53  *
54  * Finally, each property can be queried for existence
55  * (method hasProperty(...)) or its value (method (getValue(...))).
56  *
57  * After some initial preparation (hasProperties, getValues) the
58  * MultiPropertySetHelper can be used similarly to an
59  * XPropertySet in that you can query the values in the places where you
60  * need them. However, if an XMultiPropertySet is supplied, the queries
61  * are more efficient, often significantly so.
62  */
63 class MultiPropertySetHelper
64 {
65 	/// names of all properties
66 	::rtl::OUString* pPropertyNames;
67 
68 	/// length of pPropertyNames array
69 	sal_Int16 nLength;
70 
71 	/// the sequence of property names that the current (multi)
72 	/// property set implementation supports
73 	::com::sun::star::uno::Sequence< ::rtl::OUString > aPropertySequence;
74 
75 	/// an array of indices that maps from pPropertyNames indices to
76 	/// aPropertySequence indices
77 	sal_Int16* pSequenceIndex;
78 
79 	/// the last set of values retrieved by getValues
80 	::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > aValues;
81 
82 	/// result of aValues.getConstArray()
83 	const ::com::sun::star::uno::Any* pValues;
84 
85 	/// an empty Any
86 	::com::sun::star::uno::Any aEmptyAny;
87 
88 public:
89 
90 	MultiPropertySetHelper( const sal_Char** pNames );
91 
92 	MultiPropertySetHelper( const ::rtl::OUString* pNames );
93 
94 	~MultiPropertySetHelper();
95 
96 
97 	/**
98 	 * Call hasPropertiesByName for the provided XPropertySetInfo and build
99 	 * list of allowed properties.
100 	 */
101 	void hasProperties( const ::com::sun::star::uno::Reference<
102 							::com::sun::star::beans::XPropertySetInfo> & );
103 
104 
105     /**
106      * Return whether hasProperties was called
107      * (i.e. if we are ready to call getValues)
108      */
109     sal_Bool checkedProperties();
110 
111 	/**
112 	 * Get values from the XMultiPropertySet.
113 	 *
114 	 * May only be called after hasProperties() was called for the
115 	 * appropriate XPropertySetInfo.
116 	 */
117     void getValues( const ::com::sun::star::uno::Reference<
118 							::com::sun::star::beans::XMultiPropertySet> & );
119 
120 	/**
121 	 * Get values from the XPropertySet. This can be much slower than
122 	 * getValues( const Reference<XMultiPropertySet& ) and hence
123 	 * should be avoided.
124 	 *
125 	 * May only be called after hasProperties() was called for the
126 	 * appropriate XPropertySetInfo.
127 	 */
128 	void getValues( const ::com::sun::star::uno::Reference<
129 							::com::sun::star::beans::XPropertySet> & );
130 
131 
132 
133 	/**
134 	 * Get a value from the values array.
135 	 *
136 	 * May only be called after getValues() was called.
137 	 */
138 	inline const ::com::sun::star::uno::Any& getValue( sal_Int16 nIndex );
139 
140 	/**
141 	 * Find out if this property is supported.
142 	 *
143 	 * May only be called after hasProperties() was called.
144 	 */
145 	inline sal_Bool hasProperty( sal_Int16 nIndex );
146 
147 	/**
148 	 * Get a value from the XPropertySet on demand.
149 	 *
150 	 * If neither getValues nor getValueOnDemand has been called already
151 	 * after the last call to resetValues, the values are retrieved
152 	 * using getValues. Otherwise the value already retrieved is returned.
153 	 * In case XMultiPropertySet is supported by the XPropertySet and
154 	 * bTryMult is set, the XMultiPropertySet is used to get the values.
155 	 *
156 	 */
157 	const ::com::sun::star::uno::Any& getValue( sal_Int16 nIndex,
158 						const ::com::sun::star::uno::Reference<
159 							::com::sun::star::beans::XPropertySet> &,
160 						sal_Bool bTryMulti = sal_False );
161 
162 	/**
163 	 * Get a value from the XMultiPropertySet on demand.
164 	 *
165 	 * If neither getValues nor getValueOnDemand has been called already
166 	 * after the last call to resetValues, the values are retrieved
167 	 * using getValues. Otherwise the value already retrieved is returned.
168 	 * In case XMultiPropertySet is supported by the XPropertySet,
169 	 * XMultiPropertySet is used to get the values.
170 	 *
171 	 */
172 	const ::com::sun::star::uno::Any& getValue( sal_Int16 nIndex,
173 						const ::com::sun::star::uno::Reference<
174 							::com::sun::star::beans::XMultiPropertySet> & );
175 
176 	inline void resetValues() { pValues = 0; }
177 };
178 
179 
180 // inline implementations of the often-called methods getValue and hasProperty:
181 
182 const ::com::sun::star::uno::Any& MultiPropertySetHelper::getValue(
183 	sal_Int16 nValueNo )
184 {
185 	DBG_ASSERT( pValues != NULL,
186 				"called getValue() without calling getValues() before");
187 	DBG_ASSERT( pSequenceIndex != NULL,
188 				"called getValue() without calling hasProperties() before" );
189 	DBG_ASSERT( nValueNo < nLength, "index out of range" );
190 
191 	sal_Int16 nIndex = pSequenceIndex[ nValueNo ];
192 	return ( nIndex != -1 ) ? pValues[ nIndex ] : aEmptyAny;
193 }
194 
195 sal_Bool MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo )
196 {
197 	DBG_ASSERT( pSequenceIndex != NULL,
198 				"called getValue() without calling hasProperties() before" );
199 	DBG_ASSERT( nValueNo < nLength, "index out of range" );
200 
201 	return pSequenceIndex[ nValueNo ] != -1;
202 }
203 
204 #endif
205