1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 
31 #include "comphelper_module.hxx"
32 
33 #include <com/sun/star/container/XNameContainer.hpp>
34 #include <com/sun/star/uno/Sequence.h>
35 #include <com/sun/star/beans/PropertyValue.hpp>
36 #include <com/sun/star/uno/XComponentContext.hpp>
37 #include <cppuhelper/implbase2.hxx>
38 #include <com/sun/star/lang/XServiceInfo.hpp>
39 #include <comphelper/stl_types.hxx>
40 
41 
42 #include <map>
43 
44 
45 using namespace com::sun::star;
46 
47 DECLARE_STL_USTRINGACCESS_MAP( uno::Sequence<beans::PropertyValue>, NamedPropertyValues );
48 
49 class NamedPropertyValuesContainer : public cppu::WeakImplHelper2< container::XNameContainer, lang::XServiceInfo >
50 {
51 public:
52 	NamedPropertyValuesContainer() throw();
53 	virtual ~NamedPropertyValuesContainer() throw();
54 
55 	// XNameContainer
56 	virtual void SAL_CALL insertByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
57 		throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException,
58 		::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
59 	virtual void SAL_CALL removeByName( const ::rtl::OUString& Name )
60 		throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
61 			::com::sun::star::uno::RuntimeException);
62 
63     // XNameReplace
64 	virtual void SAL_CALL replaceByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
65 		throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException,
66 			::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
67 
68     // XNameAccess
69 	virtual ::com::sun::star::uno::Any SAL_CALL getByName( const ::rtl::OUString& aName )
70 		throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
71 			::com::sun::star::uno::RuntimeException);
72 	virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  )
73 		throw(::com::sun::star::uno::RuntimeException);
74 	virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName )
75 		throw(::com::sun::star::uno::RuntimeException);
76 
77 	// XElementAccess
78 	virtual ::com::sun::star::uno::Type SAL_CALL getElementType(  )
79 		throw(::com::sun::star::uno::RuntimeException);
80 	virtual sal_Bool SAL_CALL hasElements(  )
81 		throw(::com::sun::star::uno::RuntimeException);
82 
83 	//XServiceInfo
84 	virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException);
85 	virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);
86 	virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException);
87 
88     // XServiceInfo - static versions (used for component registration)
89     static ::rtl::OUString SAL_CALL getImplementationName_static();
90     static uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
91     static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
92 
93 private:
94 	NamedPropertyValues maProperties;
95 };
96 
97 NamedPropertyValuesContainer::NamedPropertyValuesContainer() throw()
98 {
99 }
100 
101 NamedPropertyValuesContainer::~NamedPropertyValuesContainer() throw()
102 {
103 }
104 
105 // XNameContainer
106 void SAL_CALL NamedPropertyValuesContainer::insertByName( const rtl::OUString& aName, const uno::Any& aElement )
107 	throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException,
108 		::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
109 {
110 	if( maProperties.find( aName ) != maProperties.end() )
111 		throw container::ElementExistException();
112 
113 	uno::Sequence<beans::PropertyValue> aProps;
114 	if( !(aElement >>= aProps ) )
115 		throw lang::IllegalArgumentException();
116 
117 	maProperties.insert(  NamedPropertyValues::value_type(aName ,aProps) );
118 }
119 
120 void SAL_CALL NamedPropertyValuesContainer::removeByName( const ::rtl::OUString& Name )
121 	throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
122 		::com::sun::star::uno::RuntimeException)
123 {
124 	NamedPropertyValues::iterator aIter = maProperties.find( Name );
125 	if( aIter == maProperties.end() )
126 		throw container::NoSuchElementException();
127 
128 	maProperties.erase( aIter );
129 }
130 
131 // XNameReplace
132 void SAL_CALL NamedPropertyValuesContainer::replaceByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
133 	throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException,
134 		::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
135 {
136 	NamedPropertyValues::iterator aIter = maProperties.find( aName );
137 	if( aIter == maProperties.end() )
138 		throw container::NoSuchElementException();
139 
140 	uno::Sequence<beans::PropertyValue> aProps;
141 	if( !(aElement >>= aProps) )
142 		throw lang::IllegalArgumentException();
143 
144 	(*aIter).second = aProps;
145 }
146 
147 // XNameAccess
148 ::com::sun::star::uno::Any SAL_CALL NamedPropertyValuesContainer::getByName( const ::rtl::OUString& aName )
149 	throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
150 		::com::sun::star::uno::RuntimeException)
151 {
152 	NamedPropertyValues::iterator aIter = maProperties.find( aName );
153 	if( aIter == maProperties.end() )
154 		throw container::NoSuchElementException();
155 
156 	uno::Any aElement;
157 
158 	aElement <<= (*aIter).second;
159 
160 	return aElement;
161 }
162 
163 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL NamedPropertyValuesContainer::getElementNames(  )
164 	throw(::com::sun::star::uno::RuntimeException)
165 {
166 	NamedPropertyValues::iterator aIter = maProperties.begin();
167 	const NamedPropertyValues::iterator aEnd = maProperties.end();
168 
169 	uno::Sequence< rtl::OUString > aNames( maProperties.size() );
170 	rtl::OUString* pNames = aNames.getArray();
171 
172 	while( aIter != aEnd )
173 	{
174 		*pNames++ = (*aIter++).first;
175 	}
176 
177 	return aNames;
178 }
179 
180 sal_Bool SAL_CALL NamedPropertyValuesContainer::hasByName( const ::rtl::OUString& aName )
181 	throw(::com::sun::star::uno::RuntimeException)
182 {
183 	NamedPropertyValues::iterator aIter = maProperties.find( aName );
184 	return aIter != maProperties.end();
185 }
186 
187 // XElementAccess
188 ::com::sun::star::uno::Type SAL_CALL NamedPropertyValuesContainer::getElementType(  )
189 	throw(::com::sun::star::uno::RuntimeException)
190 {
191 	return ::getCppuType((uno::Sequence<beans::PropertyValue> *)0);
192 }
193 
194 sal_Bool SAL_CALL NamedPropertyValuesContainer::hasElements(  )
195 	throw(::com::sun::star::uno::RuntimeException)
196 {
197 	return !maProperties.empty();
198 }
199 
200 //XServiceInfo
201 ::rtl::OUString SAL_CALL NamedPropertyValuesContainer::getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException)
202 {
203 	return getImplementationName_static();
204 }
205 
206 ::rtl::OUString SAL_CALL NamedPropertyValuesContainer::getImplementationName_static(  )
207 {
208 	return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "NamedPropertyValuesContainer" ) );
209 }
210 
211 sal_Bool SAL_CALL NamedPropertyValuesContainer::supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException)
212 {
213 	rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.NamedPropertyValues" ) );
214 	return aServiceName == ServiceName;
215 }
216 
217 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException)
218 {
219 	return getSupportedServiceNames_static();
220 }
221 
222 
223 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames_static(  )
224 {
225 	const rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.NamedPropertyValues" ) );
226 	const uno::Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
227 	return aSeq;
228 }
229 
230 uno::Reference< uno::XInterface > SAL_CALL NamedPropertyValuesContainer::Create(
231                 const uno::Reference< uno::XComponentContext >&)
232 {
233 	return (cppu::OWeakObject*)new NamedPropertyValuesContainer();
234 }
235 
236 void createRegistryInfo_NamedPropertyValuesContainer()
237 {
238     static ::comphelper::module::OAutoRegistration< NamedPropertyValuesContainer > aAutoRegistration;
239 }
240