bump product version to 4.1.6.2
[LibreOffice.git] / comphelper / source / container / NamedPropertyValuesContainer.cxx
blob61ff126ee849c84c2060cfb52641e18879d45241
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"
23 #include <com/sun/star/container/XNameContainer.hpp>
24 #include <com/sun/star/uno/Sequence.h>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <cppuhelper/implbase2.hxx>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <comphelper/stl_types.hxx>
31 #include <map>
34 using namespace com::sun::star;
36 DECLARE_STL_USTRINGACCESS_MAP( uno::Sequence<beans::PropertyValue>, NamedPropertyValues );
38 class NamedPropertyValuesContainer : public cppu::WeakImplHelper2< container::XNameContainer, lang::XServiceInfo >
40 public:
41 NamedPropertyValuesContainer() throw();
42 virtual ~NamedPropertyValuesContainer() throw();
44 // XNameContainer
45 virtual void SAL_CALL insertByName( const OUString& aName, const ::com::sun::star::uno::Any& aElement )
46 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException,
47 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
48 virtual void SAL_CALL removeByName( const OUString& Name )
49 throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
50 ::com::sun::star::uno::RuntimeException);
52 // XNameReplace
53 virtual void SAL_CALL replaceByName( const OUString& aName, const ::com::sun::star::uno::Any& aElement )
54 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException,
55 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
57 // XNameAccess
58 virtual ::com::sun::star::uno::Any SAL_CALL getByName( const OUString& aName )
59 throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
60 ::com::sun::star::uno::RuntimeException);
61 virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getElementNames( )
62 throw(::com::sun::star::uno::RuntimeException);
63 virtual sal_Bool SAL_CALL hasByName( const OUString& aName )
64 throw(::com::sun::star::uno::RuntimeException);
66 // XElementAccess
67 virtual ::com::sun::star::uno::Type SAL_CALL getElementType( )
68 throw(::com::sun::star::uno::RuntimeException);
69 virtual sal_Bool SAL_CALL hasElements( )
70 throw(::com::sun::star::uno::RuntimeException);
72 //XServiceInfo
73 virtual OUString SAL_CALL getImplementationName( ) throw(::com::sun::star::uno::RuntimeException);
74 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);
75 virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException);
77 // XServiceInfo - static versions (used for component registration)
78 static OUString SAL_CALL getImplementationName_static();
79 static uno::Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
80 static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
82 private:
83 NamedPropertyValues maProperties;
86 NamedPropertyValuesContainer::NamedPropertyValuesContainer() throw()
90 NamedPropertyValuesContainer::~NamedPropertyValuesContainer() throw()
94 // XNameContainer
95 void SAL_CALL NamedPropertyValuesContainer::insertByName( const OUString& aName, const uno::Any& aElement )
96 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException,
97 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
99 if( maProperties.find( aName ) != maProperties.end() )
100 throw container::ElementExistException();
102 uno::Sequence<beans::PropertyValue> aProps;
103 if( !(aElement >>= aProps ) )
104 throw lang::IllegalArgumentException();
106 maProperties.insert( NamedPropertyValues::value_type(aName ,aProps) );
109 void SAL_CALL NamedPropertyValuesContainer::removeByName( const OUString& Name )
110 throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
111 ::com::sun::star::uno::RuntimeException)
113 NamedPropertyValues::iterator aIter = maProperties.find( Name );
114 if( aIter == maProperties.end() )
115 throw container::NoSuchElementException();
117 maProperties.erase( aIter );
120 // XNameReplace
121 void SAL_CALL NamedPropertyValuesContainer::replaceByName( const OUString& aName, const ::com::sun::star::uno::Any& aElement )
122 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException,
123 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
125 NamedPropertyValues::iterator aIter = maProperties.find( aName );
126 if( aIter == maProperties.end() )
127 throw container::NoSuchElementException();
129 uno::Sequence<beans::PropertyValue> aProps;
130 if( !(aElement >>= aProps) )
131 throw lang::IllegalArgumentException();
133 (*aIter).second = aProps;
136 // XNameAccess
137 ::com::sun::star::uno::Any SAL_CALL NamedPropertyValuesContainer::getByName( const OUString& aName )
138 throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
139 ::com::sun::star::uno::RuntimeException)
141 NamedPropertyValues::iterator aIter = maProperties.find( aName );
142 if( aIter == maProperties.end() )
143 throw container::NoSuchElementException();
145 uno::Any aElement;
147 aElement <<= (*aIter).second;
149 return aElement;
152 ::com::sun::star::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getElementNames( )
153 throw(::com::sun::star::uno::RuntimeException)
155 NamedPropertyValues::iterator aIter = maProperties.begin();
156 const NamedPropertyValues::iterator aEnd = maProperties.end();
158 uno::Sequence< OUString > aNames( maProperties.size() );
159 OUString* pNames = aNames.getArray();
161 while( aIter != aEnd )
163 *pNames++ = (*aIter++).first;
166 return aNames;
169 sal_Bool SAL_CALL NamedPropertyValuesContainer::hasByName( const OUString& aName )
170 throw(::com::sun::star::uno::RuntimeException)
172 NamedPropertyValues::iterator aIter = maProperties.find( aName );
173 return aIter != maProperties.end();
176 // XElementAccess
177 ::com::sun::star::uno::Type SAL_CALL NamedPropertyValuesContainer::getElementType( )
178 throw(::com::sun::star::uno::RuntimeException)
180 return ::getCppuType((uno::Sequence<beans::PropertyValue> *)0);
183 sal_Bool SAL_CALL NamedPropertyValuesContainer::hasElements( )
184 throw(::com::sun::star::uno::RuntimeException)
186 return !maProperties.empty();
189 //XServiceInfo
190 OUString SAL_CALL NamedPropertyValuesContainer::getImplementationName( ) throw(::com::sun::star::uno::RuntimeException)
192 return getImplementationName_static();
195 OUString SAL_CALL NamedPropertyValuesContainer::getImplementationName_static( )
197 return OUString( "NamedPropertyValuesContainer" );
200 sal_Bool SAL_CALL NamedPropertyValuesContainer::supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException)
202 OUString aServiceName( "com.sun.star.document.NamedPropertyValues" );
203 return aServiceName == ServiceName;
206 ::com::sun::star::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException)
208 return getSupportedServiceNames_static();
212 ::com::sun::star::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames_static( )
214 const OUString aServiceName( "com.sun.star.document.NamedPropertyValues" );
215 const uno::Sequence< OUString > aSeq( &aServiceName, 1 );
216 return aSeq;
219 uno::Reference< uno::XInterface > SAL_CALL NamedPropertyValuesContainer::Create(
220 SAL_UNUSED_PARAMETER const uno::Reference< uno::XComponentContext >&)
222 return (cppu::OWeakObject*)new NamedPropertyValuesContainer();
225 void createRegistryInfo_NamedPropertyValuesContainer()
227 static ::comphelper::module::OAutoRegistration< NamedPropertyValuesContainer > aAutoRegistration;
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */