xref: /trunk/main/comphelper/source/container/NamedPropertyValuesContainer.cxx (revision 9c15e1abd703a7953928d7a80655ae1a76a41f6d)
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     virtual void SAL_CALL removeByName( const ::rtl::OUString& Name );
54 
55     // XNameReplace
56     virtual void SAL_CALL replaceByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement );
57 
58     // XNameAccess
59     virtual ::com::sun::star::uno::Any SAL_CALL getByName( const ::rtl::OUString& aName );
60     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  );
61     virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName );
62 
63     // XElementAccess
64     virtual ::com::sun::star::uno::Type SAL_CALL getElementType(  );
65     virtual sal_Bool SAL_CALL hasElements(  );
66 
67     //XServiceInfo
68     virtual ::rtl::OUString SAL_CALL getImplementationName(  );
69     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName );
70     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  );
71 
72     // XServiceInfo - static versions (used for component registration)
73     static ::rtl::OUString SAL_CALL getImplementationName_static();
74     static uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
75     static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
76 
77 private:
78     NamedPropertyValues maProperties;
79 };
80 
81 NamedPropertyValuesContainer::NamedPropertyValuesContainer() throw()
82 {
83 }
84 
85 NamedPropertyValuesContainer::~NamedPropertyValuesContainer() throw()
86 {
87 }
88 
89 // XNameContainer
90 void SAL_CALL NamedPropertyValuesContainer::insertByName( const rtl::OUString& aName, const uno::Any& aElement )
91 {
92     if( maProperties.find( aName ) != maProperties.end() )
93         throw container::ElementExistException();
94 
95     uno::Sequence<beans::PropertyValue> aProps;
96     if( !(aElement >>= aProps ) )
97         throw lang::IllegalArgumentException();
98 
99     maProperties.insert( NamedPropertyValues::value_type(aName ,aProps) );
100 }
101 
102 void SAL_CALL NamedPropertyValuesContainer::removeByName( const ::rtl::OUString& Name )
103 {
104     NamedPropertyValues::iterator aIter = maProperties.find( Name );
105     if( aIter == maProperties.end() )
106         throw container::NoSuchElementException();
107 
108     maProperties.erase( aIter );
109 }
110 
111 // XNameReplace
112 void SAL_CALL NamedPropertyValuesContainer::replaceByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
113 {
114     NamedPropertyValues::iterator aIter = maProperties.find( aName );
115     if( aIter == maProperties.end() )
116         throw container::NoSuchElementException();
117 
118     uno::Sequence<beans::PropertyValue> aProps;
119     if( !(aElement >>= aProps) )
120         throw lang::IllegalArgumentException();
121 
122     (*aIter).second = aProps;
123 }
124 
125 // XNameAccess
126 ::com::sun::star::uno::Any SAL_CALL NamedPropertyValuesContainer::getByName( const ::rtl::OUString& aName )
127 {
128     NamedPropertyValues::iterator aIter = maProperties.find( aName );
129     if( aIter == maProperties.end() )
130         throw container::NoSuchElementException();
131 
132     uno::Any aElement;
133 
134     aElement <<= (*aIter).second;
135 
136     return aElement;
137 }
138 
139 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL NamedPropertyValuesContainer::getElementNames(  )
140 {
141     NamedPropertyValues::iterator aIter = maProperties.begin();
142     const NamedPropertyValues::iterator aEnd = maProperties.end();
143 
144     uno::Sequence< rtl::OUString > aNames( maProperties.size() );
145     rtl::OUString* pNames = aNames.getArray();
146 
147     while( aIter != aEnd )
148     {
149         *pNames++ = (*aIter++).first;
150     }
151 
152     return aNames;
153 }
154 
155 sal_Bool SAL_CALL NamedPropertyValuesContainer::hasByName( const ::rtl::OUString& aName )
156 {
157     NamedPropertyValues::iterator aIter = maProperties.find( aName );
158     return aIter != maProperties.end();
159 }
160 
161 // XElementAccess
162 ::com::sun::star::uno::Type SAL_CALL NamedPropertyValuesContainer::getElementType(  )
163 {
164     return ::getCppuType((uno::Sequence<beans::PropertyValue> *)0);
165 }
166 
167 sal_Bool SAL_CALL NamedPropertyValuesContainer::hasElements(  )
168 {
169     return !maProperties.empty();
170 }
171 
172 //XServiceInfo
173 ::rtl::OUString SAL_CALL NamedPropertyValuesContainer::getImplementationName(  )
174 {
175     return getImplementationName_static();
176 }
177 
178 ::rtl::OUString SAL_CALL NamedPropertyValuesContainer::getImplementationName_static(  )
179 {
180     return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "NamedPropertyValuesContainer" ) );
181 }
182 
183 sal_Bool SAL_CALL NamedPropertyValuesContainer::supportsService( const ::rtl::OUString& ServiceName )
184 {
185     rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.NamedPropertyValues" ) );
186     return aServiceName == ServiceName;
187 }
188 
189 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames(  )
190 {
191     return getSupportedServiceNames_static();
192 }
193 
194 
195 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames_static(  )
196 {
197     const rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.NamedPropertyValues" ) );
198     const uno::Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
199     return aSeq;
200 }
201 
202 uno::Reference< uno::XInterface > SAL_CALL NamedPropertyValuesContainer::Create(
203                 const uno::Reference< uno::XComponentContext >&)
204 {
205     return (cppu::OWeakObject*)new NamedPropertyValuesContainer();
206 }
207 
208 void createRegistryInfo_NamedPropertyValuesContainer()
209 {
210     static ::comphelper::module::OAutoRegistration< NamedPropertyValuesContainer > aAutoRegistration;
211 }
212