/src/libreoffice/comphelper/source/container/NamedPropertyValuesContainer.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | |
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> |
27 | | #include <map> |
28 | | |
29 | | |
30 | | namespace com::sun::star::uno { class XComponentContext; } |
31 | | using namespace com::sun::star; |
32 | | |
33 | | typedef std::map< OUString, uno::Sequence<beans::PropertyValue> > NamedPropertyValues; |
34 | | |
35 | | namespace { |
36 | | |
37 | | class NamedPropertyValuesContainer : public cppu::WeakImplHelper< container::XNameContainer, lang::XServiceInfo > |
38 | | { |
39 | | public: |
40 | | NamedPropertyValuesContainer() noexcept; |
41 | | |
42 | | // XNameContainer |
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; |
45 | | |
46 | | // XNameReplace |
47 | | virtual void SAL_CALL replaceByName( const OUString& aName, const css::uno::Any& aElement ) override; |
48 | | |
49 | | // XNameAccess |
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; |
53 | | |
54 | | // XElementAccess |
55 | | virtual css::uno::Type SAL_CALL getElementType( ) override; |
56 | | virtual sal_Bool SAL_CALL hasElements( ) override; |
57 | | |
58 | | //XServiceInfo |
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; |
62 | | |
63 | | private: |
64 | | NamedPropertyValues maProperties; |
65 | | }; |
66 | | |
67 | | } |
68 | | |
69 | | NamedPropertyValuesContainer::NamedPropertyValuesContainer() noexcept |
70 | 5.17k | { |
71 | 5.17k | } |
72 | | |
73 | | // XNameContainer |
74 | | void SAL_CALL NamedPropertyValuesContainer::insertByName( const OUString& aName, const uno::Any& aElement ) |
75 | 7.80k | { |
76 | 7.80k | if( maProperties.contains( aName ) ) |
77 | 5 | throw container::ElementExistException(); |
78 | | |
79 | 7.79k | uno::Sequence<beans::PropertyValue> aProps; |
80 | 7.79k | if( !(aElement >>= aProps ) ) |
81 | 1 | throw lang::IllegalArgumentException(u"element is not beans::PropertyValue"_ustr, static_cast<cppu::OWeakObject*>(this), 2); |
82 | | |
83 | 7.79k | maProperties.emplace( aName, aProps ); |
84 | 7.79k | } |
85 | | |
86 | | void SAL_CALL NamedPropertyValuesContainer::removeByName( const OUString& Name ) |
87 | 0 | { |
88 | 0 | NamedPropertyValues::iterator aIter = maProperties.find( Name ); |
89 | 0 | if( aIter == maProperties.end() ) |
90 | 0 | throw container::NoSuchElementException(); |
91 | | |
92 | 0 | maProperties.erase( aIter ); |
93 | 0 | } |
94 | | |
95 | | // XNameReplace |
96 | | void SAL_CALL NamedPropertyValuesContainer::replaceByName( const OUString& aName, const css::uno::Any& aElement ) |
97 | 0 | { |
98 | 0 | NamedPropertyValues::iterator aIter = maProperties.find( aName ); |
99 | 0 | if( aIter == maProperties.end() ) |
100 | 0 | throw container::NoSuchElementException(); |
101 | | |
102 | 0 | uno::Sequence<beans::PropertyValue> aProps; |
103 | 0 | if( !(aElement >>= aProps) ) |
104 | 0 | throw lang::IllegalArgumentException(u"element is not beans::PropertyValue"_ustr, static_cast<cppu::OWeakObject*>(this), 2); |
105 | 0 | (*aIter).second = std::move(aProps); |
106 | 0 | } |
107 | | |
108 | | // XNameAccess |
109 | | css::uno::Any SAL_CALL NamedPropertyValuesContainer::getByName( const OUString& aName ) |
110 | 2.61k | { |
111 | 2.61k | NamedPropertyValues::iterator aIter = maProperties.find( aName ); |
112 | 2.61k | if( aIter == maProperties.end() ) |
113 | 0 | throw container::NoSuchElementException(); |
114 | | |
115 | 2.61k | uno::Any aElement; |
116 | | |
117 | 2.61k | aElement <<= (*aIter).second; |
118 | | |
119 | 2.61k | return aElement; |
120 | 2.61k | } |
121 | | |
122 | | css::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getElementNames( ) |
123 | 0 | { |
124 | 0 | return comphelper::mapKeysToSequence(maProperties); |
125 | 0 | } |
126 | | |
127 | | sal_Bool SAL_CALL NamedPropertyValuesContainer::hasByName( const OUString& aName ) |
128 | 10.6k | { |
129 | 10.6k | NamedPropertyValues::iterator aIter = maProperties.find( aName ); |
130 | 10.6k | return aIter != maProperties.end(); |
131 | 10.6k | } |
132 | | |
133 | | // XElementAccess |
134 | | css::uno::Type SAL_CALL NamedPropertyValuesContainer::getElementType( ) |
135 | 0 | { |
136 | 0 | return cppu::UnoType<uno::Sequence<beans::PropertyValue>>::get(); |
137 | 0 | } |
138 | | |
139 | | sal_Bool SAL_CALL NamedPropertyValuesContainer::hasElements( ) |
140 | 0 | { |
141 | 0 | return !maProperties.empty(); |
142 | 0 | } |
143 | | |
144 | | //XServiceInfo |
145 | | OUString SAL_CALL NamedPropertyValuesContainer::getImplementationName( ) |
146 | 0 | { |
147 | 0 | return u"NamedPropertyValuesContainer"_ustr; |
148 | 0 | } |
149 | | |
150 | | sal_Bool SAL_CALL NamedPropertyValuesContainer::supportsService( const OUString& ServiceName ) |
151 | 0 | { |
152 | 0 | return cppu::supportsService(this, ServiceName); |
153 | 0 | } |
154 | | |
155 | | css::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames( ) |
156 | 0 | { |
157 | 0 | return { u"com.sun.star.document.NamedPropertyValues"_ustr }; |
158 | 0 | } |
159 | | |
160 | | extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * |
161 | | NamedPropertyValuesContainer_get_implementation( |
162 | | css::uno::XComponentContext *, |
163 | | css::uno::Sequence<css::uno::Any> const &) |
164 | 5.17k | { |
165 | 5.17k | return cppu::acquire(new NamedPropertyValuesContainer()); |
166 | 5.17k | } |
167 | | |
168 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |