Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmloff / source / core / PropertySetMerger.cxx
blob65a936bb3d8042bf87d0614913010daaa06847c6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/beans/XPropertyState.hpp>
21 #include <PropertySetMerger.hxx>
22 #include <comphelper/sequence.hxx>
23 #include <cppuhelper/implbase.hxx>
25 using namespace ::com::sun::star;
26 using namespace ::com::sun::star::uno;
27 using namespace ::com::sun::star::beans;
28 using namespace ::com::sun::star::lang;
30 namespace {
32 class PropertySetMergerImpl : public ::cppu::WeakImplHelper< XPropertySet, XPropertyState, XPropertySetInfo >
34 private:
35 Reference< XPropertySet > mxPropSet1;
36 Reference< XPropertyState > mxPropSet1State;
37 Reference< XPropertySetInfo > mxPropSet1Info;
39 Reference< XPropertySet > mxPropSet2;
40 Reference< XPropertyState > mxPropSet2State;
41 Reference< XPropertySetInfo > mxPropSet2Info;
43 public:
44 PropertySetMergerImpl( const Reference< XPropertySet > & rPropSet1, const Reference< XPropertySet > & rPropSet2 );
46 // XPropertySet
47 virtual Reference< XPropertySetInfo > SAL_CALL getPropertySetInfo( ) override;
48 virtual void SAL_CALL setPropertyValue( const OUString& aPropertyName, const Any& aValue ) override;
49 virtual Any SAL_CALL getPropertyValue( const OUString& PropertyName ) override;
50 virtual void SAL_CALL addPropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener ) override;
51 virtual void SAL_CALL removePropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& aListener ) override;
52 virtual void SAL_CALL addVetoableChangeListener( const OUString& PropertyName, const Reference< XVetoableChangeListener >& aListener ) override;
53 virtual void SAL_CALL removeVetoableChangeListener( const OUString& PropertyName, const Reference< XVetoableChangeListener >& aListener ) override;
55 // XPropertyState
56 virtual PropertyState SAL_CALL getPropertyState( const OUString& PropertyName ) override;
57 virtual Sequence< PropertyState > SAL_CALL getPropertyStates( const Sequence< OUString >& aPropertyName ) override;
58 virtual void SAL_CALL setPropertyToDefault( const OUString& PropertyName ) override;
59 virtual Any SAL_CALL getPropertyDefault( const OUString& aPropertyName ) override;
61 // XPropertySetInfo
62 virtual Sequence< Property > SAL_CALL getProperties( ) override;
63 virtual Property SAL_CALL getPropertyByName( const OUString& aName ) override;
64 virtual sal_Bool SAL_CALL hasPropertyByName( const OUString& Name ) override;
69 // Interface implementation
71 PropertySetMergerImpl::PropertySetMergerImpl( Reference< XPropertySet > const & rPropSet1, Reference< XPropertySet > const & rPropSet2 )
72 : mxPropSet1( rPropSet1 )
73 , mxPropSet1State( rPropSet1, UNO_QUERY )
74 , mxPropSet1Info( rPropSet1->getPropertySetInfo() )
75 , mxPropSet2( rPropSet2 )
76 , mxPropSet2State( rPropSet2, UNO_QUERY )
77 , mxPropSet2Info( rPropSet2->getPropertySetInfo() )
81 // XPropertySet
82 Reference< XPropertySetInfo > SAL_CALL PropertySetMergerImpl::getPropertySetInfo( )
84 return this;
87 void SAL_CALL PropertySetMergerImpl::setPropertyValue( const OUString& aPropertyName, const Any& aValue )
89 if( mxPropSet1Info->hasPropertyByName( aPropertyName ) )
91 mxPropSet1->setPropertyValue( aPropertyName, aValue );
93 else
95 mxPropSet2->setPropertyValue( aPropertyName, aValue );
99 Any SAL_CALL PropertySetMergerImpl::getPropertyValue( const OUString& PropertyName )
101 if( mxPropSet1Info->hasPropertyByName( PropertyName ) )
103 return mxPropSet1->getPropertyValue( PropertyName );
105 else
107 return mxPropSet2->getPropertyValue( PropertyName );
111 void SAL_CALL PropertySetMergerImpl::addPropertyChangeListener( const OUString& /*aPropertyName*/, const Reference< XPropertyChangeListener >& /*xListener*/ )
115 void SAL_CALL PropertySetMergerImpl::removePropertyChangeListener( const OUString& /*aPropertyName*/, const Reference< XPropertyChangeListener >& /*aListener*/ )
119 void SAL_CALL PropertySetMergerImpl::addVetoableChangeListener( const OUString& /*PropertyName*/, const Reference< XVetoableChangeListener >& /*aListener*/ )
123 void SAL_CALL PropertySetMergerImpl::removeVetoableChangeListener( const OUString& /*PropertyName*/, const Reference< XVetoableChangeListener >& /*aListener*/ )
127 // XPropertyState
128 PropertyState SAL_CALL PropertySetMergerImpl::getPropertyState( const OUString& PropertyName )
130 if( mxPropSet1Info->hasPropertyByName( PropertyName ) )
132 if( mxPropSet1State.is() )
134 return mxPropSet1State->getPropertyState( PropertyName );
136 else
138 return PropertyState_DIRECT_VALUE;
141 else
143 if( mxPropSet2State.is() )
145 return mxPropSet2State->getPropertyState( PropertyName );
147 else
149 return PropertyState_DIRECT_VALUE;
154 Sequence< PropertyState > SAL_CALL PropertySetMergerImpl::getPropertyStates( const Sequence< OUString >& aPropertyName )
156 const sal_Int32 nCount = aPropertyName.getLength();
157 Sequence< PropertyState > aPropStates( nCount );
159 std::transform(aPropertyName.begin(), aPropertyName.end(), aPropStates.getArray(),
160 [this](const OUString& rPropName) -> PropertyState { return getPropertyState(rPropName); });
162 return aPropStates;
165 void SAL_CALL PropertySetMergerImpl::setPropertyToDefault( const OUString& PropertyName )
167 if( mxPropSet1State.is() && mxPropSet1Info->hasPropertyByName( PropertyName ) )
169 mxPropSet1State->setPropertyToDefault( PropertyName );
171 else
173 if( mxPropSet2State.is() )
175 mxPropSet2State->setPropertyToDefault( PropertyName );
180 Any SAL_CALL PropertySetMergerImpl::getPropertyDefault( const OUString& aPropertyName )
182 if( mxPropSet1State.is() && mxPropSet1Info->hasPropertyByName( aPropertyName ) )
184 return mxPropSet1State->getPropertyDefault( aPropertyName );
186 else
188 if( mxPropSet2State.is() )
190 return mxPropSet2State->getPropertyDefault( aPropertyName );
192 else
194 Any aAny;
195 return aAny;
200 // XPropertySetInfo
201 Sequence< Property > SAL_CALL PropertySetMergerImpl::getProperties()
203 Sequence< Property > aProps1( mxPropSet1Info->getProperties() );
204 Sequence< Property > aProps2( mxPropSet2Info->getProperties() );
206 return comphelper::concatSequences(aProps1, aProps2);
209 Property SAL_CALL PropertySetMergerImpl::getPropertyByName( const OUString& aName )
211 if( mxPropSet1Info->hasPropertyByName( aName ) )
212 return mxPropSet1Info->getPropertyByName( aName );
214 return mxPropSet2Info->getPropertyByName( aName );
217 sal_Bool SAL_CALL PropertySetMergerImpl::hasPropertyByName( const OUString& Name )
219 if(mxPropSet1Info->hasPropertyByName( Name ) )
220 return true;
222 return mxPropSet2Info->hasPropertyByName( Name );
225 Reference< XPropertySet > PropertySetMerger_CreateInstance( const Reference< XPropertySet >& rPropSet1, const Reference< XPropertySet >& rPropSet2 ) noexcept
227 return new PropertySetMergerImpl( rPropSet1, rPropSet2 );
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */