1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <com/sun/star/container/XNameContainer.hpp>
21 #include <com/sun/star/uno/Sequence.h>
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <comphelper/sequence.hxx>
24 #include <cppuhelper/implbase.hxx>
25 #include <com/sun/star/lang/XServiceInfo.hpp>
26 #include <cppuhelper/supportsservice.hxx>
30 namespace com::sun::star::uno
{ class XComponentContext
; }
31 using namespace com::sun::star
;
33 typedef std::map
< OUString
, uno::Sequence
<beans::PropertyValue
> > NamedPropertyValues
;
37 class NamedPropertyValuesContainer
: public cppu::WeakImplHelper
< container::XNameContainer
, lang::XServiceInfo
>
40 NamedPropertyValuesContainer() noexcept
;
43 virtual void SAL_CALL
insertByName( const OUString
& aName
, const css::uno::Any
& aElement
) override
;
44 virtual void SAL_CALL
removeByName( const OUString
& Name
) override
;
47 virtual void SAL_CALL
replaceByName( const OUString
& aName
, const css::uno::Any
& aElement
) override
;
50 virtual css::uno::Any SAL_CALL
getByName( const OUString
& aName
) override
;
51 virtual css::uno::Sequence
< OUString
> SAL_CALL
getElementNames( ) override
;
52 virtual sal_Bool SAL_CALL
hasByName( const OUString
& aName
) override
;
55 virtual css::uno::Type SAL_CALL
getElementType( ) override
;
56 virtual sal_Bool SAL_CALL
hasElements( ) override
;
59 virtual OUString SAL_CALL
getImplementationName( ) override
;
60 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
;
61 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames( ) override
;
64 NamedPropertyValues maProperties
;
69 NamedPropertyValuesContainer::NamedPropertyValuesContainer() noexcept
74 void SAL_CALL
NamedPropertyValuesContainer::insertByName( const OUString
& aName
, const uno::Any
& aElement
)
76 if( maProperties
.find( aName
) != maProperties
.end() )
77 throw container::ElementExistException();
79 uno::Sequence
<beans::PropertyValue
> aProps
;
80 if( !(aElement
>>= aProps
) )
81 throw lang::IllegalArgumentException("element is not beans::PropertyValue", static_cast<cppu::OWeakObject
*>(this), 2);
83 maProperties
.emplace( aName
, aProps
);
86 void SAL_CALL
NamedPropertyValuesContainer::removeByName( const OUString
& Name
)
88 NamedPropertyValues::iterator aIter
= maProperties
.find( Name
);
89 if( aIter
== maProperties
.end() )
90 throw container::NoSuchElementException();
92 maProperties
.erase( aIter
);
96 void SAL_CALL
NamedPropertyValuesContainer::replaceByName( const OUString
& aName
, const css::uno::Any
& aElement
)
98 NamedPropertyValues::iterator aIter
= maProperties
.find( aName
);
99 if( aIter
== maProperties
.end() )
100 throw container::NoSuchElementException();
102 uno::Sequence
<beans::PropertyValue
> aProps
;
103 if( !(aElement
>>= aProps
) )
104 throw lang::IllegalArgumentException("element is not beans::PropertyValue", static_cast<cppu::OWeakObject
*>(this), 2);
106 (*aIter
).second
= aProps
;
110 css::uno::Any SAL_CALL
NamedPropertyValuesContainer::getByName( const OUString
& aName
)
112 NamedPropertyValues::iterator aIter
= maProperties
.find( aName
);
113 if( aIter
== maProperties
.end() )
114 throw container::NoSuchElementException();
118 aElement
<<= (*aIter
).second
;
123 css::uno::Sequence
< OUString
> SAL_CALL
NamedPropertyValuesContainer::getElementNames( )
125 return comphelper::mapKeysToSequence(maProperties
);
128 sal_Bool SAL_CALL
NamedPropertyValuesContainer::hasByName( const OUString
& aName
)
130 NamedPropertyValues::iterator aIter
= maProperties
.find( aName
);
131 return aIter
!= maProperties
.end();
135 css::uno::Type SAL_CALL
NamedPropertyValuesContainer::getElementType( )
137 return cppu::UnoType
<uno::Sequence
<beans::PropertyValue
>>::get();
140 sal_Bool SAL_CALL
NamedPropertyValuesContainer::hasElements( )
142 return !maProperties
.empty();
146 OUString SAL_CALL
NamedPropertyValuesContainer::getImplementationName( )
148 return "NamedPropertyValuesContainer";
151 sal_Bool SAL_CALL
NamedPropertyValuesContainer::supportsService( const OUString
& ServiceName
)
153 return cppu::supportsService(this, ServiceName
);
156 css::uno::Sequence
< OUString
> SAL_CALL
NamedPropertyValuesContainer::getSupportedServiceNames( )
158 return { "com.sun.star.document.NamedPropertyValues" };
161 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
162 NamedPropertyValuesContainer_get_implementation(
163 css::uno::XComponentContext
*,
164 css::uno::Sequence
<css::uno::Any
> const &)
166 return cppu::acquire(new NamedPropertyValuesContainer());
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */