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_comphelper.hxx" 26 27 28 #include <comphelper/property.hxx> 29 #include <comphelper/sequence.hxx> 30 #include <comphelper/types.hxx> 31 #include <osl/diagnose.h> 32 33 #if OSL_DEBUG_LEVEL > 0 34 #include <rtl/strbuf.hxx> 35 #include <cppuhelper/exc_hlp.hxx> 36 #include <osl/thread.h> 37 #include <com/sun/star/lang/XServiceInfo.hpp> 38 #include <typeinfo> 39 #endif 40 #include <com/sun/star/beans/PropertyAttribute.hpp> 41 #include <com/sun/star/lang/IllegalArgumentException.hpp> 42 #include <com/sun/star/uno/genfunc.h> 43 44 #include <algorithm> 45 #include <boost/bind.hpp> 46 47 //......................................................................... 48 namespace comphelper 49 { 50 51 /** === begin UNO using === **/ 52 using ::com::sun::star::uno::Reference; 53 using ::com::sun::star::beans::XPropertySet; 54 using ::com::sun::star::beans::XPropertySetInfo; 55 using ::com::sun::star::beans::Property; 56 using ::com::sun::star::uno::Sequence; 57 using ::com::sun::star::uno::Exception; 58 using ::com::sun::star::uno::Any; 59 using ::com::sun::star::uno::Type; 60 using ::com::sun::star::uno::cpp_queryInterface; 61 using ::com::sun::star::uno::cpp_acquire; 62 using ::com::sun::star::uno::cpp_release; 63 #if OSL_DEBUG_LEVEL > 0 64 using ::com::sun::star::lang::XServiceInfo; 65 #endif 66 using ::com::sun::star::uno::UNO_QUERY; 67 /** === end UNO using === **/ 68 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute; 69 70 //------------------------------------------------------------------ 71 void copyProperties(const Reference<XPropertySet>& _rxSource, 72 const Reference<XPropertySet>& _rxDest) 73 { 74 if (!_rxSource.is() || !_rxDest.is()) 75 { 76 OSL_ENSURE(sal_False, "copyProperties: invalid arguments !"); 77 return; 78 } 79 80 Reference< XPropertySetInfo > xSourceProps = _rxSource->getPropertySetInfo(); 81 Reference< XPropertySetInfo > xDestProps = _rxDest->getPropertySetInfo(); 82 83 Sequence< Property > aSourceProps = xSourceProps->getProperties(); 84 const Property* pSourceProps = aSourceProps.getConstArray(); 85 Property aDestProp; 86 for (sal_Int32 i=0; i<aSourceProps.getLength(); ++i, ++pSourceProps) 87 { 88 if ( xDestProps->hasPropertyByName(pSourceProps->Name) ) 89 { 90 try 91 { 92 aDestProp = xDestProps->getPropertyByName(pSourceProps->Name); 93 if (0 == (aDestProp.Attributes & PropertyAttribute::READONLY) ) 94 { 95 const Any aSourceValue = _rxSource->getPropertyValue(pSourceProps->Name); 96 if ( 0 != (aDestProp.Attributes & PropertyAttribute::MAYBEVOID) || aSourceValue.hasValue() ) 97 _rxDest->setPropertyValue(pSourceProps->Name, aSourceValue); 98 } 99 } 100 catch (Exception&) 101 { 102 #if OSL_DEBUG_LEVEL > 0 103 ::rtl::OStringBuffer aBuffer; 104 aBuffer.append( "::comphelper::copyProperties: could not copy property '" ); 105 aBuffer.append( ::rtl::OString( pSourceProps->Name.getStr(), pSourceProps->Name.getLength(), RTL_TEXTENCODING_ASCII_US ) ); 106 aBuffer.append( "' to the destination set (a '" ); 107 108 Reference< XServiceInfo > xSI( _rxDest, UNO_QUERY ); 109 if ( xSI.is() ) 110 { 111 aBuffer.append( ::rtl::OUStringToOString( xSI->getImplementationName(), osl_getThreadTextEncoding() ) ); 112 } 113 else 114 { 115 aBuffer.append( typeid( *_rxDest.get() ).name() ); 116 } 117 aBuffer.append( "' implementation).\n" ); 118 119 Any aException( ::cppu::getCaughtException() ); 120 aBuffer.append( "Caught an exception of type '" ); 121 ::rtl::OUString sExceptionType( aException.getValueTypeName() ); 122 aBuffer.append( ::rtl::OString( sExceptionType.getStr(), sExceptionType.getLength(), RTL_TEXTENCODING_ASCII_US ) ); 123 aBuffer.append( "'" ); 124 125 Exception aBaseException; 126 if ( ( aException >>= aBaseException ) && aBaseException.Message.getLength() ) 127 { 128 aBuffer.append( ", saying '" ); 129 aBuffer.append( ::rtl::OString( aBaseException.Message.getStr(), aBaseException.Message.getLength(), osl_getThreadTextEncoding() ) ); 130 aBuffer.append( "'" ); 131 } 132 aBuffer.append( "." ); 133 134 OSL_ENSURE( sal_False, aBuffer.getStr() ); 135 #endif 136 } 137 } 138 } 139 } 140 141 //------------------------------------------------------------------ 142 sal_Bool hasProperty(const rtl::OUString& _rName, const Reference<XPropertySet>& _rxSet) 143 { 144 if (_rxSet.is()) 145 { 146 // XPropertySetInfoRef xInfo(rxSet->getPropertySetInfo()); 147 return _rxSet->getPropertySetInfo()->hasPropertyByName(_rName); 148 } 149 return sal_False; 150 } 151 152 //------------------------------------------------------------------ 153 bool findProperty(Property& o_rProp, 154 Sequence<Property>& i_seqProps, 155 const ::rtl::OUString& i_rPropName) 156 { 157 const Property* pAry(i_seqProps.getConstArray()); 158 const sal_Int32 nLen(i_seqProps.getLength()); 159 const Property* pRes( 160 std::find_if(pAry,pAry+nLen, 161 boost::bind(PropertyStringEqualFunctor(), 162 _1, 163 boost::cref(i_rPropName)))); 164 if( pRes == pAry+nLen ) 165 return false; 166 167 o_rProp = *pRes; 168 return true; 169 } 170 171 //------------------------------------------------------------------ 172 void RemoveProperty(Sequence<Property>& _rProps, const rtl::OUString& _rPropName) 173 { 174 sal_Int32 nLen = _rProps.getLength(); 175 176 // binaere Suche 177 const Property* pProperties = _rProps.getConstArray(); 178 const Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen, _rPropName,PropertyStringLessFunctor()); 179 180 // gefunden ? 181 if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == _rPropName) ) 182 { 183 OSL_ENSURE(pResult->Name.equals(_rPropName), "::RemoveProperty Properties nicht sortiert"); 184 removeElementAt(_rProps, pResult - pProperties); 185 } 186 } 187 188 //------------------------------------------------------------------ 189 void ModifyPropertyAttributes(Sequence<Property>& seqProps, const ::rtl::OUString& sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib) 190 { 191 sal_Int32 nLen = seqProps.getLength(); 192 193 // binaere Suche 194 Property* pProperties = seqProps.getArray(); 195 Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen,sPropName, PropertyStringLessFunctor()); 196 197 // gefunden ? 198 if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == sPropName) ) 199 { 200 pResult->Attributes |= nAddAttrib; 201 pResult->Attributes &= ~nRemoveAttrib; 202 } 203 } 204 205 //------------------------------------------------------------------ 206 sal_Bool tryPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rValueToSet, const Any& _rCurrentValue, const Type& _rExpectedType) 207 { 208 sal_Bool bModified(sal_False); 209 if (_rCurrentValue.getValue() != _rValueToSet.getValue()) 210 { 211 if ( _rValueToSet.hasValue() && ( !_rExpectedType.equals( _rValueToSet.getValueType() ) ) ) 212 { 213 _rConvertedValue = Any( NULL, _rExpectedType.getTypeLibType() ); 214 215 if ( !uno_type_assignData( 216 const_cast< void* >( _rConvertedValue.getValue() ), _rConvertedValue.getValueType().getTypeLibType(), 217 const_cast< void* >( _rValueToSet.getValue() ), _rValueToSet.getValueType().getTypeLibType(), 218 reinterpret_cast< uno_QueryInterfaceFunc >( 219 cpp_queryInterface), 220 reinterpret_cast< uno_AcquireFunc >(cpp_acquire), 221 reinterpret_cast< uno_ReleaseFunc >(cpp_release) 222 ) 223 ) 224 throw starlang::IllegalArgumentException(); 225 } 226 else 227 _rConvertedValue = _rValueToSet; 228 229 if ( _rCurrentValue != _rConvertedValue ) 230 { 231 _rOldValue = _rCurrentValue; 232 bModified = sal_True; 233 } 234 } 235 return bModified; 236 } 237 238 //......................................................................... 239 } 240 //......................................................................... 241 242