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 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 #include <com/sun/star/beans/XPropertyState.hpp>
27 #include "PropertySetMerger.hxx"
28 
29 using ::rtl::OUString;
30 
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::beans;
34 using namespace ::com::sun::star::lang;
35 
36 #ifndef _CPPUHELPER_IMPLBASE1_HXX_
37 #include <cppuhelper/implbase3.hxx>
38 #endif
39 
40 class SvXMLAttrContainerItem_Impl;
41 
42 class PropertySetMergerImpl : public ::cppu::WeakAggImplHelper3< XPropertySet, XPropertyState, XPropertySetInfo >
43 {
44 private:
45 	Reference< XPropertySet > mxPropSet1;
46 	Reference< XPropertyState > mxPropSet1State;
47 	Reference< XPropertySetInfo > mxPropSet1Info;
48 
49 	Reference< XPropertySet > mxPropSet2;
50 	Reference< XPropertyState > mxPropSet2State;
51 	Reference< XPropertySetInfo > mxPropSet2Info;
52 
53 public:
54 	PropertySetMergerImpl( const Reference< XPropertySet > rPropSet1, const Reference< XPropertySet > rPropSet2 );
55 	virtual ~PropertySetMergerImpl();
56 
57     // XPropertySet
58     virtual Reference< XPropertySetInfo > SAL_CALL getPropertySetInfo(  ) throw(RuntimeException);
59     virtual void SAL_CALL setPropertyValue( const OUString& aPropertyName, const Any& aValue ) throw(UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException);
60     virtual Any SAL_CALL getPropertyValue( const OUString& PropertyName ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException);
61     virtual void SAL_CALL addPropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException);
62     virtual void SAL_CALL removePropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& aListener ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException);
63     virtual void SAL_CALL addVetoableChangeListener( const OUString& PropertyName, const Reference< XVetoableChangeListener >& aListener ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException);
64     virtual void SAL_CALL removeVetoableChangeListener( const OUString& PropertyName, const Reference< XVetoableChangeListener >& aListener ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException);
65 
66     // XPropertyState
67     virtual PropertyState SAL_CALL getPropertyState( const OUString& PropertyName ) throw(UnknownPropertyException, RuntimeException);
68     virtual Sequence< PropertyState > SAL_CALL getPropertyStates( const Sequence< OUString >& aPropertyName ) throw(UnknownPropertyException, RuntimeException);
69     virtual void SAL_CALL setPropertyToDefault( const OUString& PropertyName ) throw(UnknownPropertyException, RuntimeException);
70     virtual Any SAL_CALL getPropertyDefault( const OUString& aPropertyName ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException);
71 
72     // XPropertySetInfo
73     virtual Sequence< Property > SAL_CALL getProperties(  ) throw(RuntimeException);
74     virtual Property SAL_CALL getPropertyByName( const OUString& aName ) throw(UnknownPropertyException, RuntimeException);
75     virtual sal_Bool SAL_CALL hasPropertyByName( const OUString& Name ) throw(RuntimeException);
76 };
77 
78 // --------------------------------------------------------------------
79 // Interface implementation
80 // --------------------------------------------------------------------
81 
PropertySetMergerImpl(Reference<XPropertySet> rPropSet1,Reference<XPropertySet> rPropSet2)82 PropertySetMergerImpl::PropertySetMergerImpl( Reference< XPropertySet > rPropSet1, Reference< XPropertySet > rPropSet2 )
83 : mxPropSet1( rPropSet1 )
84 , mxPropSet1State( rPropSet1, UNO_QUERY )
85 , mxPropSet1Info( rPropSet1->getPropertySetInfo() )
86 , mxPropSet2( rPropSet2 )
87 , mxPropSet2State( rPropSet2, UNO_QUERY )
88 , mxPropSet2Info( rPropSet2->getPropertySetInfo() )
89 {
90 }
91 
~PropertySetMergerImpl()92 PropertySetMergerImpl::~PropertySetMergerImpl()
93 {
94 }
95 
96 // XPropertySet
getPropertySetInfo()97 Reference< XPropertySetInfo > SAL_CALL PropertySetMergerImpl::getPropertySetInfo(  ) throw(RuntimeException)
98 {
99 	return this;
100 }
101 
setPropertyValue(const OUString & aPropertyName,const Any & aValue)102 void SAL_CALL PropertySetMergerImpl::setPropertyValue( const OUString& aPropertyName, const Any& aValue ) throw(UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
103 {
104 	if( mxPropSet1Info->hasPropertyByName( aPropertyName ) )
105 	{
106 		mxPropSet1->setPropertyValue( aPropertyName, aValue );
107 	}
108 	else
109 	{
110 		mxPropSet2->setPropertyValue( aPropertyName, aValue );
111 	}
112 }
113 
getPropertyValue(const OUString & PropertyName)114 Any SAL_CALL PropertySetMergerImpl::getPropertyValue( const OUString& PropertyName ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
115 {
116 	if( mxPropSet1Info->hasPropertyByName( PropertyName ) )
117 	{
118 		return mxPropSet1->getPropertyValue( PropertyName );
119 	}
120 	else
121 	{
122 		return mxPropSet2->getPropertyValue( PropertyName );
123 	}
124 }
125 
addPropertyChangeListener(const OUString &,const Reference<XPropertyChangeListener> &)126 void SAL_CALL PropertySetMergerImpl::addPropertyChangeListener( const OUString& /*aPropertyName*/, const Reference< XPropertyChangeListener >& /*xListener*/ ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
127 {
128 }
129 
removePropertyChangeListener(const OUString &,const Reference<XPropertyChangeListener> &)130 void SAL_CALL PropertySetMergerImpl::removePropertyChangeListener( const OUString& /*aPropertyName*/, const Reference< XPropertyChangeListener >& /*aListener*/ ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
131 {
132 }
133 
addVetoableChangeListener(const OUString &,const Reference<XVetoableChangeListener> &)134 void SAL_CALL PropertySetMergerImpl::addVetoableChangeListener( const OUString& /*PropertyName*/, const Reference< XVetoableChangeListener >& /*aListener*/ ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
135 {
136 }
137 
removeVetoableChangeListener(const OUString &,const Reference<XVetoableChangeListener> &)138 void SAL_CALL PropertySetMergerImpl::removeVetoableChangeListener( const OUString& /*PropertyName*/, const Reference< XVetoableChangeListener >& /*aListener*/ ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
139 {
140 }
141 
142 // XPropertyState
getPropertyState(const OUString & PropertyName)143 PropertyState SAL_CALL PropertySetMergerImpl::getPropertyState( const OUString& PropertyName ) throw(UnknownPropertyException, RuntimeException)
144 {
145 	if( mxPropSet1Info->hasPropertyByName( PropertyName ) )
146 	{
147 		if( mxPropSet1State.is() )
148 		{
149 			return mxPropSet1State->getPropertyState( PropertyName );
150 		}
151 		else
152 		{
153 			return PropertyState_DIRECT_VALUE;
154 		}
155 	}
156 	else
157 	{
158 		if( mxPropSet2State.is() )
159 		{
160 			return mxPropSet2State->getPropertyState( PropertyName );
161 		}
162 		else
163 		{
164 			return PropertyState_DIRECT_VALUE;
165 		}
166 	}
167 }
168 
getPropertyStates(const Sequence<OUString> & aPropertyName)169 Sequence< PropertyState > SAL_CALL PropertySetMergerImpl::getPropertyStates( const Sequence< OUString >& aPropertyName ) throw(UnknownPropertyException, RuntimeException)
170 {
171 	const sal_Int32 nCount = aPropertyName.getLength();
172 	Sequence< PropertyState > aPropStates( nCount );
173 	PropertyState* pPropStates = aPropStates.getArray();
174 	const OUString* pPropNames = aPropertyName.getConstArray();
175 
176 	sal_Int32 nIndex;
177 	for( nIndex = 0; nIndex < nCount; nIndex++ )
178 		*pPropStates++ = getPropertyState( *pPropNames++ );
179 
180 	return aPropStates;
181 }
182 
setPropertyToDefault(const OUString & PropertyName)183 void SAL_CALL PropertySetMergerImpl::setPropertyToDefault( const OUString& PropertyName ) throw(UnknownPropertyException, RuntimeException)
184 {
185 	if( mxPropSet1State.is() && mxPropSet1Info->hasPropertyByName( PropertyName ) )
186 	{
187 		mxPropSet1State->setPropertyToDefault( PropertyName );
188 	}
189 	else
190 	{
191 		if( mxPropSet2State.is() )
192 		{
193 			mxPropSet2State->setPropertyToDefault( PropertyName );
194 		}
195 	}
196 }
197 
getPropertyDefault(const OUString & aPropertyName)198 Any SAL_CALL PropertySetMergerImpl::getPropertyDefault( const OUString& aPropertyName ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
199 {
200 	if( mxPropSet1State.is() && mxPropSet1Info->hasPropertyByName( aPropertyName ) )
201 	{
202 		return mxPropSet1State->getPropertyDefault( aPropertyName );
203 	}
204 	else
205 	{
206 		if( mxPropSet2State.is() )
207 		{
208 			return mxPropSet2State->getPropertyDefault( aPropertyName );
209 		}
210 		else
211 		{
212 			Any aAny;
213 			return aAny;
214 		}
215 	}
216 }
217 
218 // XPropertySetInfo
getProperties()219 Sequence< Property > SAL_CALL PropertySetMergerImpl::getProperties() throw(RuntimeException)
220 {
221 	Sequence< Property > aProps1( mxPropSet1Info->getProperties() );
222 	const Property* pProps1 = aProps1.getArray();
223 	const sal_Int32 nCount1 = aProps1.getLength();
224 
225 	Sequence< Property > aProps2( mxPropSet1Info->getProperties() );
226 	const Property* pProps2 = aProps2.getArray();
227 	const sal_Int32 nCount2 = aProps2.getLength();
228 
229 	Sequence< Property > aProperties( nCount1 + nCount2 );
230 
231 	sal_Int32 nIndex;
232 
233 	Property* pProperties = aProperties.getArray();
234 
235 	for( nIndex = 0; nIndex < nCount1; nIndex++ )
236 		*pProperties++ = *pProps1++;
237 
238 	for( nIndex = 0; nIndex < nCount2; nIndex++ )
239 		*pProperties++ = *pProps2++;
240 
241 	return aProperties;
242 }
243 
getPropertyByName(const OUString & aName)244 Property SAL_CALL PropertySetMergerImpl::getPropertyByName( const OUString& aName ) throw(UnknownPropertyException, RuntimeException)
245 {
246 	if( mxPropSet1Info->hasPropertyByName( aName ) )
247 		return mxPropSet1Info->getPropertyByName( aName );
248 
249 	return mxPropSet2Info->getPropertyByName( aName );
250 }
251 
hasPropertyByName(const OUString & Name)252 sal_Bool SAL_CALL PropertySetMergerImpl::hasPropertyByName( const OUString& Name ) throw(RuntimeException)
253 {
254 	if(mxPropSet1Info->hasPropertyByName( Name ) )
255 		return sal_True;
256 
257 	return mxPropSet2Info->hasPropertyByName( Name );
258 }
259 
PropertySetMerger_CreateInstance(Reference<XPropertySet> rPropSet1,Reference<XPropertySet> rPropSet2)260 Reference< XPropertySet > PropertySetMerger_CreateInstance( Reference< XPropertySet > rPropSet1, Reference< XPropertySet > rPropSet2 ) throw()
261 {
262 	return new PropertySetMergerImpl( rPropSet1, rPropSet2 );
263 }
264