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_module.hxx"
28 
29 #include <com/sun/star/container/XNameContainer.hpp>
30 #include <com/sun/star/uno/Sequence.h>
31 #include <com/sun/star/beans/PropertyValue.hpp>
32 #include <com/sun/star/uno/XComponentContext.hpp>
33 #include <cppuhelper/implbase2.hxx>
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <comphelper/stl_types.hxx>
36 
37 
38 #include <map>
39 
40 
41 using namespace com::sun::star;
42 
43 DECLARE_STL_USTRINGACCESS_MAP( uno::Sequence<beans::PropertyValue>, NamedPropertyValues );
44 
45 class NamedPropertyValuesContainer : public cppu::WeakImplHelper2< container::XNameContainer, lang::XServiceInfo >
46 {
47 public:
48 	NamedPropertyValuesContainer() throw();
49 	virtual ~NamedPropertyValuesContainer() throw();
50 
51 	// XNameContainer
52 	virtual void SAL_CALL insertByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
53 		throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException,
54 		::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
55 	virtual void SAL_CALL removeByName( const ::rtl::OUString& Name )
56 		throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
57 			::com::sun::star::uno::RuntimeException);
58 
59 	// XNameReplace
60 	virtual void SAL_CALL replaceByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
61 		throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException,
62 			::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
63 
64 	// XNameAccess
65 	virtual ::com::sun::star::uno::Any SAL_CALL getByName( const ::rtl::OUString& aName )
66 		throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
67 			::com::sun::star::uno::RuntimeException);
68 	virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  )
69 		throw(::com::sun::star::uno::RuntimeException);
70 	virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName )
71 		throw(::com::sun::star::uno::RuntimeException);
72 
73 	// XElementAccess
74 	virtual ::com::sun::star::uno::Type SAL_CALL getElementType(  )
75 		throw(::com::sun::star::uno::RuntimeException);
76 	virtual sal_Bool SAL_CALL hasElements(  )
77 		throw(::com::sun::star::uno::RuntimeException);
78 
79 	//XServiceInfo
80 	virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException);
81 	virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);
82 	virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException);
83 
84 	// XServiceInfo - static versions (used for component registration)
85 	static ::rtl::OUString SAL_CALL getImplementationName_static();
86 	static uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
87 	static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
88 
89 private:
90 	NamedPropertyValues maProperties;
91 };
92 
NamedPropertyValuesContainer()93 NamedPropertyValuesContainer::NamedPropertyValuesContainer() throw()
94 {
95 }
96 
~NamedPropertyValuesContainer()97 NamedPropertyValuesContainer::~NamedPropertyValuesContainer() throw()
98 {
99 }
100 
101 // XNameContainer
insertByName(const rtl::OUString & aName,const uno::Any & aElement)102 void SAL_CALL NamedPropertyValuesContainer::insertByName( const rtl::OUString& aName, const uno::Any& aElement )
103 	throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException,
104 		::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
105 {
106 	if( maProperties.find( aName ) != maProperties.end() )
107 		throw container::ElementExistException();
108 
109 	uno::Sequence<beans::PropertyValue> aProps;
110 	if( !(aElement >>= aProps ) )
111 		throw lang::IllegalArgumentException();
112 
113 	maProperties.insert( NamedPropertyValues::value_type(aName ,aProps) );
114 }
115 
removeByName(const::rtl::OUString & Name)116 void SAL_CALL NamedPropertyValuesContainer::removeByName( const ::rtl::OUString& Name )
117 	throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
118 		::com::sun::star::uno::RuntimeException)
119 {
120 	NamedPropertyValues::iterator aIter = maProperties.find( Name );
121 	if( aIter == maProperties.end() )
122 		throw container::NoSuchElementException();
123 
124 	maProperties.erase( aIter );
125 }
126 
127 // XNameReplace
replaceByName(const::rtl::OUString & aName,const::com::sun::star::uno::Any & aElement)128 void SAL_CALL NamedPropertyValuesContainer::replaceByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
129 	throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException,
130 		::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
131 {
132 	NamedPropertyValues::iterator aIter = maProperties.find( aName );
133 	if( aIter == maProperties.end() )
134 		throw container::NoSuchElementException();
135 
136 	uno::Sequence<beans::PropertyValue> aProps;
137 	if( !(aElement >>= aProps) )
138 		throw lang::IllegalArgumentException();
139 
140 	(*aIter).second = aProps;
141 }
142 
143 // XNameAccess
getByName(const::rtl::OUString & aName)144 ::com::sun::star::uno::Any SAL_CALL NamedPropertyValuesContainer::getByName( const ::rtl::OUString& aName )
145 	throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
146 		::com::sun::star::uno::RuntimeException)
147 {
148 	NamedPropertyValues::iterator aIter = maProperties.find( aName );
149 	if( aIter == maProperties.end() )
150 		throw container::NoSuchElementException();
151 
152 	uno::Any aElement;
153 
154 	aElement <<= (*aIter).second;
155 
156 	return aElement;
157 }
158 
getElementNames()159 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL NamedPropertyValuesContainer::getElementNames(  )
160 	throw(::com::sun::star::uno::RuntimeException)
161 {
162 	NamedPropertyValues::iterator aIter = maProperties.begin();
163 	const NamedPropertyValues::iterator aEnd = maProperties.end();
164 
165 	uno::Sequence< rtl::OUString > aNames( maProperties.size() );
166 	rtl::OUString* pNames = aNames.getArray();
167 
168 	while( aIter != aEnd )
169 	{
170 		*pNames++ = (*aIter++).first;
171 	}
172 
173 	return aNames;
174 }
175 
hasByName(const::rtl::OUString & aName)176 sal_Bool SAL_CALL NamedPropertyValuesContainer::hasByName( const ::rtl::OUString& aName )
177 	throw(::com::sun::star::uno::RuntimeException)
178 {
179 	NamedPropertyValues::iterator aIter = maProperties.find( aName );
180 	return aIter != maProperties.end();
181 }
182 
183 // XElementAccess
getElementType()184 ::com::sun::star::uno::Type SAL_CALL NamedPropertyValuesContainer::getElementType(  )
185 	throw(::com::sun::star::uno::RuntimeException)
186 {
187 	return ::getCppuType((uno::Sequence<beans::PropertyValue> *)0);
188 }
189 
hasElements()190 sal_Bool SAL_CALL NamedPropertyValuesContainer::hasElements(  )
191 	throw(::com::sun::star::uno::RuntimeException)
192 {
193 	return !maProperties.empty();
194 }
195 
196 //XServiceInfo
getImplementationName()197 ::rtl::OUString SAL_CALL NamedPropertyValuesContainer::getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException)
198 {
199 	return getImplementationName_static();
200 }
201 
getImplementationName_static()202 ::rtl::OUString SAL_CALL NamedPropertyValuesContainer::getImplementationName_static(  )
203 {
204 	return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "NamedPropertyValuesContainer" ) );
205 }
206 
supportsService(const::rtl::OUString & ServiceName)207 sal_Bool SAL_CALL NamedPropertyValuesContainer::supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException)
208 {
209 	rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.NamedPropertyValues" ) );
210 	return aServiceName == ServiceName;
211 }
212 
getSupportedServiceNames()213 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException)
214 {
215 	return getSupportedServiceNames_static();
216 }
217 
218 
getSupportedServiceNames_static()219 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames_static(  )
220 {
221 	const rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.NamedPropertyValues" ) );
222 	const uno::Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
223 	return aSeq;
224 }
225 
Create(const uno::Reference<uno::XComponentContext> &)226 uno::Reference< uno::XInterface > SAL_CALL NamedPropertyValuesContainer::Create(
227 				const uno::Reference< uno::XComponentContext >&)
228 {
229 	return (cppu::OWeakObject*)new NamedPropertyValuesContainer();
230 }
231 
createRegistryInfo_NamedPropertyValuesContainer()232 void createRegistryInfo_NamedPropertyValuesContainer()
233 {
234 	static ::comphelper::module::OAutoRegistration< NamedPropertyValuesContainer > aAutoRegistration;
235 }
236