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