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 #include "comphelper/propertysetinfo.hxx"
28 
29 using namespace ::rtl;
30 using namespace ::comphelper;
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 namespace comphelper
37 {
38 class PropertyMapImpl
39 {
40 public:
41 	PropertyMapImpl() throw();
42 	virtual ~PropertyMapImpl() throw();
43 
44 	void add( PropertyMapEntry* pMap, sal_Int32 nCount = -1 ) throw();
45 	void remove( const OUString& aName ) throw();
46 
47 	Sequence< Property > getProperties() throw();
48 
49 	const PropertyMap* getPropertyMap() const throw();
50 
51 	Property getPropertyByName( const OUString& aName ) throw( UnknownPropertyException );
52 	sal_Bool hasPropertyByName( const OUString& aName ) throw();
53 
54 private:
55 	PropertyMap maPropertyMap;
56 	Sequence< Property > maProperties;
57 };
58 }
59 
PropertyMapImpl()60 PropertyMapImpl::PropertyMapImpl() throw()
61 {
62 }
63 
~PropertyMapImpl()64 PropertyMapImpl::~PropertyMapImpl() throw()
65 {
66 }
67 
add(PropertyMapEntry * pMap,sal_Int32 nCount)68 void PropertyMapImpl::add( PropertyMapEntry* pMap, sal_Int32 nCount ) throw()
69 {
70 	// nCount < 0	=> add all
71 	// nCount == 0	=> add nothing
72 	// nCount > 0	=> add at most nCount entries
73 
74 	while( pMap->mpName && ( ( nCount < 0) || ( nCount-- > 0 ) ) )
75 	{
76 		OUString aName( pMap->mpName, pMap->mnNameLen, RTL_TEXTENCODING_ASCII_US );
77 
78 #ifdef DBG_UTIL
79 		PropertyMap::iterator aIter = maPropertyMap.find( aName );
80 		if( aIter != maPropertyMap.end() )
81 		{
82 			OSL_ENSURE( sal_False, "Warning: PropertyMapEntry added twice, possible error!");
83 		}
84 #endif
85 		if( NULL == pMap->mpType )
86 		{
87 			OSL_ENSURE( sal_False, "No type in PropertyMapEntry!");
88 			pMap->mpType = &::getCppuType((const sal_Int32*)0);
89 		}
90 
91 		maPropertyMap[aName] = pMap;
92 
93 		if( maProperties.getLength() )
94 			maProperties.realloc( 0 );
95 
96 		pMap = &pMap[1];
97 	}
98 }
99 
remove(const OUString & aName)100 void PropertyMapImpl::remove( const OUString& aName ) throw()
101 {
102 	maPropertyMap.erase( aName );
103 
104 	if( maProperties.getLength() )
105 		maProperties.realloc( 0 );
106 }
107 
getProperties()108 Sequence< Property > PropertyMapImpl::getProperties() throw()
109 {
110 	// maybe we have to generate the properties after
111 	// a change in the property map or at first call
112 	// to getProperties
113 	if( maProperties.getLength() != (sal_Int32)maPropertyMap.size() )
114 	{
115 		maProperties = Sequence< Property >( maPropertyMap.size() );
116 		Property* pProperties = maProperties.getArray();
117 
118 		PropertyMap::iterator aIter = maPropertyMap.begin();
119 		const PropertyMap::iterator aEnd = maPropertyMap.end();
120 		while( aIter != aEnd )
121 		{
122 			PropertyMapEntry* pEntry = (*aIter).second;
123 
124 			pProperties->Name = OUString( pEntry->mpName, pEntry->mnNameLen, RTL_TEXTENCODING_ASCII_US );
125 			pProperties->Handle = pEntry->mnHandle;
126 			pProperties->Type = *pEntry->mpType;
127 			pProperties->Attributes = pEntry->mnAttributes;
128 
129 			pProperties++;
130 			aIter++;
131 		}
132 	}
133 
134 	return maProperties;
135 }
136 
getPropertyMap() const137 const PropertyMap* PropertyMapImpl::getPropertyMap() const throw()
138 {
139 	return &maPropertyMap;
140 }
141 
getPropertyByName(const OUString & aName)142 Property PropertyMapImpl::getPropertyByName( const OUString& aName ) throw( UnknownPropertyException )
143 {
144 	PropertyMap::iterator aIter = maPropertyMap.find( aName );
145 
146 	if( maPropertyMap.end() == aIter )
147 		throw UnknownPropertyException( aName, NULL );
148 
149 	PropertyMapEntry* pEntry = (*aIter).second;
150 
151 	return Property( aName, pEntry->mnHandle, *pEntry->mpType, pEntry->mnAttributes );
152 }
153 
hasPropertyByName(const OUString & aName)154 sal_Bool PropertyMapImpl::hasPropertyByName( const OUString& aName ) throw()
155 {
156 	return maPropertyMap.find( aName ) != maPropertyMap.end();
157 }
158 
159 ///////////////////////////////////////////////////////////////////////
160 
PropertySetInfo()161 PropertySetInfo::PropertySetInfo() throw()
162 {
163 	mpMap = new PropertyMapImpl();
164 }
165 
PropertySetInfo(PropertyMapEntry * pMap)166 PropertySetInfo::PropertySetInfo( PropertyMapEntry* pMap ) throw()
167 {
168 	mpMap = new PropertyMapImpl();
169 	mpMap->add( pMap );
170 }
171 
~PropertySetInfo()172 PropertySetInfo::~PropertySetInfo() throw()
173 {
174 	delete mpMap;
175 }
176 
add(PropertyMapEntry * pMap)177 void PropertySetInfo::add( PropertyMapEntry* pMap ) throw()
178 {
179 	mpMap->add( pMap );
180 }
181 
add(PropertyMapEntry * pMap,sal_Int32 nCount)182 void PropertySetInfo::add( PropertyMapEntry* pMap, sal_Int32 nCount ) throw()
183 {
184 	mpMap->add( pMap, nCount );
185 }
186 
remove(const rtl::OUString & aName)187 void PropertySetInfo::remove( const rtl::OUString& aName ) throw()
188 {
189 	mpMap->remove( aName );
190 }
191 
getProperties()192 Sequence< ::com::sun::star::beans::Property > SAL_CALL PropertySetInfo::getProperties() throw(::com::sun::star::uno::RuntimeException)
193 {
194 	return mpMap->getProperties();
195 }
196 
getPropertyByName(const::rtl::OUString & aName)197 Property SAL_CALL PropertySetInfo::getPropertyByName( const ::rtl::OUString& aName ) throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException)
198 {
199 	return mpMap->getPropertyByName( aName );
200 }
201 
hasPropertyByName(const::rtl::OUString & Name)202 sal_Bool SAL_CALL PropertySetInfo::hasPropertyByName( const ::rtl::OUString& Name ) throw(::com::sun::star::uno::RuntimeException)
203 {
204 	return mpMap->hasPropertyByName( Name );
205 }
206 
getPropertyMap() const207 const PropertyMap* PropertySetInfo::getPropertyMap() const throw()
208 {
209 	return mpMap->getPropertyMap();
210 }
211