bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / container / NamedPropertyValuesContainer.cxx
blobd59944a813295544cdab3fcc4c09939a4106c67c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "comphelper_module.hxx"
22 #include "comphelper_services.hxx"
24 #include <com/sun/star/container/XNameContainer.hpp>
25 #include <com/sun/star/uno/Sequence.h>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <cppuhelper/implbase2.hxx>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <cppuhelper/supportsservice.hxx>
30 #include <map>
33 using namespace com::sun::star;
35 typedef std::map< OUString, uno::Sequence<beans::PropertyValue> > NamedPropertyValues;
37 class NamedPropertyValuesContainer : public cppu::WeakImplHelper2< container::XNameContainer, lang::XServiceInfo >
39 public:
40 NamedPropertyValuesContainer() throw();
41 virtual ~NamedPropertyValuesContainer() throw();
43 // XNameContainer
44 virtual void SAL_CALL insertByName( const OUString& aName, const ::com::sun::star::uno::Any& aElement )
45 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException,
46 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
47 virtual void SAL_CALL removeByName( const OUString& Name )
48 throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
49 ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
51 // XNameReplace
52 virtual void SAL_CALL replaceByName( const OUString& aName, const ::com::sun::star::uno::Any& aElement )
53 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException,
54 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
56 // XNameAccess
57 virtual ::com::sun::star::uno::Any SAL_CALL getByName( const OUString& aName )
58 throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
59 ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
60 virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getElementNames( )
61 throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
62 virtual sal_Bool SAL_CALL hasByName( const OUString& aName )
63 throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
65 // XElementAccess
66 virtual ::com::sun::star::uno::Type SAL_CALL getElementType( )
67 throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
68 virtual sal_Bool SAL_CALL hasElements( )
69 throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
71 //XServiceInfo
72 virtual OUString SAL_CALL getImplementationName( ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
73 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
74 virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
76 // XServiceInfo - static versions (used for component registration)
77 static OUString SAL_CALL getImplementationName_static();
78 static uno::Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
79 static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
81 private:
82 NamedPropertyValues maProperties;
85 NamedPropertyValuesContainer::NamedPropertyValuesContainer() throw()
89 NamedPropertyValuesContainer::~NamedPropertyValuesContainer() throw()
93 // XNameContainer
94 void SAL_CALL NamedPropertyValuesContainer::insertByName( const OUString& aName, const uno::Any& aElement )
95 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException,
96 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception)
98 if( maProperties.find( aName ) != maProperties.end() )
99 throw container::ElementExistException();
101 uno::Sequence<beans::PropertyValue> aProps;
102 if( !(aElement >>= aProps ) )
103 throw lang::IllegalArgumentException();
105 maProperties.insert( NamedPropertyValues::value_type(aName ,aProps) );
108 void SAL_CALL NamedPropertyValuesContainer::removeByName( const OUString& Name )
109 throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
110 ::com::sun::star::uno::RuntimeException, std::exception)
112 NamedPropertyValues::iterator aIter = maProperties.find( Name );
113 if( aIter == maProperties.end() )
114 throw container::NoSuchElementException();
116 maProperties.erase( aIter );
119 // XNameReplace
120 void SAL_CALL NamedPropertyValuesContainer::replaceByName( const OUString& aName, const ::com::sun::star::uno::Any& aElement )
121 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException,
122 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception)
124 NamedPropertyValues::iterator aIter = maProperties.find( aName );
125 if( aIter == maProperties.end() )
126 throw container::NoSuchElementException();
128 uno::Sequence<beans::PropertyValue> aProps;
129 if( !(aElement >>= aProps) )
130 throw lang::IllegalArgumentException();
132 (*aIter).second = aProps;
135 // XNameAccess
136 ::com::sun::star::uno::Any SAL_CALL NamedPropertyValuesContainer::getByName( const OUString& aName )
137 throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
138 ::com::sun::star::uno::RuntimeException, std::exception)
140 NamedPropertyValues::iterator aIter = maProperties.find( aName );
141 if( aIter == maProperties.end() )
142 throw container::NoSuchElementException();
144 uno::Any aElement;
146 aElement <<= (*aIter).second;
148 return aElement;
151 ::com::sun::star::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getElementNames( )
152 throw(::com::sun::star::uno::RuntimeException, std::exception)
154 NamedPropertyValues::iterator aIter = maProperties.begin();
155 const NamedPropertyValues::iterator aEnd = maProperties.end();
157 uno::Sequence< OUString > aNames( maProperties.size() );
158 OUString* pNames = aNames.getArray();
160 while( aIter != aEnd )
162 *pNames++ = (*aIter++).first;
165 return aNames;
168 sal_Bool SAL_CALL NamedPropertyValuesContainer::hasByName( const OUString& aName )
169 throw(::com::sun::star::uno::RuntimeException, std::exception)
171 NamedPropertyValues::iterator aIter = maProperties.find( aName );
172 return aIter != maProperties.end();
175 // XElementAccess
176 ::com::sun::star::uno::Type SAL_CALL NamedPropertyValuesContainer::getElementType( )
177 throw(::com::sun::star::uno::RuntimeException, std::exception)
179 return cppu::UnoType<uno::Sequence<beans::PropertyValue>>::get();
182 sal_Bool SAL_CALL NamedPropertyValuesContainer::hasElements( )
183 throw(::com::sun::star::uno::RuntimeException, std::exception)
185 return !maProperties.empty();
188 //XServiceInfo
189 OUString SAL_CALL NamedPropertyValuesContainer::getImplementationName( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
191 return getImplementationName_static();
194 OUString SAL_CALL NamedPropertyValuesContainer::getImplementationName_static( )
196 return OUString( "NamedPropertyValuesContainer" );
199 sal_Bool SAL_CALL NamedPropertyValuesContainer::supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException, std::exception)
201 return cppu::supportsService(this, ServiceName);
204 ::com::sun::star::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
206 return getSupportedServiceNames_static();
209 ::com::sun::star::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames_static( )
211 const OUString aServiceName( "com.sun.star.document.NamedPropertyValues" );
212 const uno::Sequence< OUString > aSeq( &aServiceName, 1 );
213 return aSeq;
216 uno::Reference< uno::XInterface > SAL_CALL NamedPropertyValuesContainer::Create(
217 SAL_UNUSED_PARAMETER const uno::Reference< uno::XComponentContext >&)
219 return (cppu::OWeakObject*)new NamedPropertyValuesContainer();
222 void createRegistryInfo_NamedPropertyValuesContainer()
224 static ::comphelper::module::OAutoRegistration< NamedPropertyValuesContainer > aAutoRegistration;
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */