Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "oox/helper/propertyset.hxx"
21 :
22 : #include <osl/diagnose.h>
23 : #include <rtl/strbuf.hxx>
24 : #include "oox/helper/propertymap.hxx"
25 :
26 : namespace oox {
27 :
28 : // ============================================================================
29 :
30 : using namespace ::com::sun::star::beans;
31 : using namespace ::com::sun::star::uno;
32 :
33 : using ::rtl::OStringBuffer;
34 : using ::rtl::OUString;
35 : using ::rtl::OUStringToOString;
36 :
37 : // ============================================================================
38 :
39 2476 : void PropertySet::set( const Reference< XPropertySet >& rxPropSet )
40 : {
41 2476 : mxPropSet = rxPropSet;
42 2476 : mxMultiPropSet.set( mxPropSet, UNO_QUERY );
43 2476 : if( mxPropSet.is() ) try
44 : {
45 2442 : mxPropSetInfo = mxPropSet->getPropertySetInfo();
46 : }
47 0 : catch( Exception& )
48 : {
49 : }
50 2476 : }
51 :
52 48 : bool PropertySet::hasProperty( sal_Int32 nPropId ) const
53 : {
54 48 : if( mxPropSetInfo.is() ) try
55 : {
56 48 : const OUString& rPropName = PropertyMap::getPropertyName( nPropId );
57 48 : return mxPropSetInfo->hasPropertyByName( rPropName );
58 : }
59 0 : catch( Exception& )
60 : {
61 : }
62 0 : return false;
63 : }
64 :
65 : // Get properties -------------------------------------------------------------
66 :
67 784 : Any PropertySet::getAnyProperty( sal_Int32 nPropId ) const
68 : {
69 784 : Any aValue;
70 784 : return implGetPropertyValue( aValue, PropertyMap::getPropertyName( nPropId ) ) ? aValue : Any();
71 : }
72 :
73 : // Set properties -------------------------------------------------------------
74 :
75 2310 : bool PropertySet::setAnyProperty( sal_Int32 nPropId, const Any& rValue )
76 : {
77 2310 : return implSetPropertyValue( PropertyMap::getPropertyName( nPropId ), rValue );
78 : }
79 :
80 820 : void PropertySet::setProperties( const Sequence< OUString >& rPropNames, const Sequence< Any >& rValues )
81 : {
82 : OSL_ENSURE( rPropNames.getLength() == rValues.getLength(),
83 : "PropertySet::setProperties - length of sequences different" );
84 :
85 820 : if( mxMultiPropSet.is() ) try
86 : {
87 578 : mxMultiPropSet->setPropertyValues( rPropNames, rValues );
88 1398 : return;
89 : }
90 0 : catch( Exception& )
91 : {
92 : OSL_FAIL( "PropertySet::setProperties - cannot set all property values, fallback to single mode" );
93 : }
94 :
95 242 : if( mxPropSet.is() )
96 : {
97 242 : const OUString* pPropName = rPropNames.getConstArray();
98 242 : const OUString* pPropNameEnd = pPropName + rPropNames.getLength();
99 242 : const Any* pValue = rValues.getConstArray();
100 2906 : for( ; pPropName != pPropNameEnd; ++pPropName, ++pValue )
101 2664 : implSetPropertyValue( *pPropName, *pValue );
102 : }
103 : }
104 :
105 886 : void PropertySet::setProperties( const PropertyMap& rPropertyMap )
106 : {
107 886 : if( !rPropertyMap.empty() )
108 : {
109 820 : Sequence< OUString > aPropNames;
110 820 : Sequence< Any > aValues;
111 820 : rPropertyMap.fillSequences( aPropNames, aValues );
112 820 : setProperties( aPropNames, aValues );
113 : }
114 886 : }
115 :
116 : // private --------------------------------------------------------------------
117 :
118 784 : bool PropertySet::implGetPropertyValue( Any& orValue, const OUString& rPropName ) const
119 : {
120 784 : if( mxPropSet.is() ) try
121 : {
122 784 : orValue = mxPropSet->getPropertyValue( rPropName );
123 784 : return true;
124 : }
125 0 : catch( Exception& )
126 : {
127 : OSL_FAIL( OStringBuffer( "PropertySet::implGetPropertyValue - cannot get property \"" ).
128 : append( OUStringToOString( rPropName, RTL_TEXTENCODING_ASCII_US ) ).append( '"' ).getStr() );
129 : }
130 0 : return false;
131 : }
132 :
133 4974 : bool PropertySet::implSetPropertyValue( const OUString& rPropName, const Any& rValue )
134 : {
135 4974 : if( mxPropSet.is() ) try
136 : {
137 4940 : mxPropSet->setPropertyValue( rPropName, rValue );
138 4634 : return true;
139 : }
140 306 : catch( Exception& )
141 : {
142 : OSL_FAIL( OStringBuffer( "PropertySet::implSetPropertyValue - cannot set property \"" ).
143 : append( OUStringToOString( rPropName, RTL_TEXTENCODING_ASCII_US ) ).append( '"' ).getStr() );
144 : }
145 340 : return false;
146 : }
147 :
148 : #ifdef DBG_UTIL
149 : void PropertySet::dump()
150 : {
151 : PropertyMap::dump( Reference< XPropertySet >( getXPropertySet(), UNO_QUERY ) );
152 : }
153 : #endif
154 :
155 : } // namespace oox
156 :
157 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|