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 #include <comphelper/namecontainer.hxx>
31 #include <cppuhelper/implbase1.hxx>
32 #include <osl/diagnose.h>
33 #include <osl/mutex.hxx>
34 #include <comphelper/stl_types.hxx>
35 
36 DECLARE_STL_USTRINGACCESS_MAP( ::com::sun::star::uno::Any, SvGenericNameContainerMapImpl );
37 
38 namespace comphelper
39 {
40 	class NameContainerImpl
41 	{
42 	public:
43 		osl::Mutex maMutex;
44 	};
45 
46 	/** this is the base helper class for NameContainer thats also declared in this header. */
47 	class NameContainer : public ::cppu::WeakImplHelper1< ::com::sun::star::container::XNameContainer >, private NameContainerImpl
48 	{
49 	public:
50 		NameContainer( ::com::sun::star::uno::Type aType );
51 		virtual ~NameContainer();
52 
53 		// XNameContainer
54 		virtual void SAL_CALL insertByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
55 			throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException,
56 			::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
57 		virtual void SAL_CALL removeByName( const ::rtl::OUString& Name )
58 			throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
59 				::com::sun::star::uno::RuntimeException);
60 
61 		// XNameReplace
62 		virtual void SAL_CALL replaceByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
63 			throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException,
64 				::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
65 
66 		// XNameAccess
67 		virtual ::com::sun::star::uno::Any SAL_CALL getByName( const ::rtl::OUString& aName )
68 			throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
69 				::com::sun::star::uno::RuntimeException);
70 		virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  )
71 			throw(::com::sun::star::uno::RuntimeException);
72 		virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName )
73 			throw(::com::sun::star::uno::RuntimeException);
74 
75 		// XElementAccess
76 		virtual sal_Bool SAL_CALL hasElements(  )
77 			throw(::com::sun::star::uno::RuntimeException);
78 		virtual ::com::sun::star::uno::Type SAL_CALL getElementType(  )
79 			throw(::com::sun::star::uno::RuntimeException);
80 
81 	private:
82 		SvGenericNameContainerMapImpl maProperties;
83 		const ::com::sun::star::uno::Type maType;
84 	};
85 }
86 
87 using namespace ::comphelper;
88 using namespace ::osl;
89 using namespace ::rtl;
90 using namespace ::com::sun::star::uno;
91 using namespace ::com::sun::star::container;
92 using namespace ::com::sun::star::lang;
93 
94 
95 NameContainer::NameContainer( ::com::sun::star::uno::Type aType )
96 : maType( aType )
97 {
98 }
99 
100 NameContainer::~NameContainer()
101 {
102 }
103 
104 // XNameContainer
105 void SAL_CALL NameContainer::insertByName( const rtl::OUString& aName, const Any& aElement )
106 	throw(IllegalArgumentException, ElementExistException,
107 		WrappedTargetException, RuntimeException)
108 {
109 	MutexGuard aGuard( maMutex );
110 
111 	if( maProperties.find( aName ) != maProperties.end() )
112 		throw ElementExistException();
113 
114 	if( aElement.getValueType() != maType )
115 		throw IllegalArgumentException();
116 
117 	maProperties.insert( SvGenericNameContainerMapImpl::value_type(aName,aElement));
118 }
119 
120 void SAL_CALL NameContainer::removeByName( const ::rtl::OUString& Name )
121 	throw(NoSuchElementException, WrappedTargetException,
122 		RuntimeException)
123 {
124 	MutexGuard aGuard( maMutex );
125 
126 	SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( Name );
127 	if( aIter == maProperties.end() )
128 		throw NoSuchElementException();
129 
130 	maProperties.erase( aIter );
131 }
132 
133 // XNameReplace
134 
135 void SAL_CALL NameContainer::replaceByName( const ::rtl::OUString& aName, const Any& aElement )
136 	throw(IllegalArgumentException, NoSuchElementException,
137 		WrappedTargetException, RuntimeException)
138 {
139 	MutexGuard aGuard( maMutex );
140 
141 	SvGenericNameContainerMapImpl::iterator aIter( maProperties.find( aName ) );
142 	if( aIter == maProperties.end() )
143 		throw NoSuchElementException();
144 
145 	if( aElement.getValueType() != maType )
146 		throw IllegalArgumentException();
147 
148 	(*aIter).second = aElement;
149 }
150 
151 // XNameAccess
152 
153 Any SAL_CALL NameContainer::getByName( const ::rtl::OUString& aName )
154 	throw(NoSuchElementException, WrappedTargetException,
155 		RuntimeException)
156 {
157 	MutexGuard aGuard( maMutex );
158 
159 	SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
160 	if( aIter == maProperties.end() )
161 		throw NoSuchElementException();
162 
163 	return (*aIter).second;
164 }
165 
166 Sequence< ::rtl::OUString > SAL_CALL NameContainer::getElementNames(  )
167 	throw(RuntimeException)
168 {
169 	MutexGuard aGuard( maMutex );
170 
171 	SvGenericNameContainerMapImpl::iterator aIter = maProperties.begin();
172 	const SvGenericNameContainerMapImpl::iterator aEnd = maProperties.end();
173 
174 	Sequence< rtl::OUString > aNames( maProperties.size() );
175 	rtl::OUString* pNames = aNames.getArray();
176 
177 	while( aIter != aEnd )
178 	{
179 		*pNames++ = (*aIter++).first;
180 	}
181 
182 	return aNames;
183 }
184 
185 sal_Bool SAL_CALL NameContainer::hasByName( const ::rtl::OUString& aName )
186 	throw(RuntimeException)
187 {
188 	MutexGuard aGuard( maMutex );
189 
190 	SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
191 	return aIter != maProperties.end();
192 }
193 
194 sal_Bool SAL_CALL NameContainer::hasElements(  )
195 	throw(RuntimeException)
196 {
197 	MutexGuard aGuard( maMutex );
198 
199 	return !maProperties.empty();
200 }
201 
202 Type SAL_CALL NameContainer::getElementType()
203 	throw( RuntimeException )
204 {
205 	return maType;
206 }
207 
208 Reference< XNameContainer > comphelper::NameContainer_createInstance( Type aType )
209 {
210 	return (XNameContainer*) new NameContainer( aType );
211 }
212