Line data Source code
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 :
21 : #include "comphelper_module.hxx"
22 : #include "comphelper_services.hxx"
23 :
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>
31 :
32 :
33 : using namespace com::sun::star;
34 :
35 : typedef std::map< OUString, uno::Sequence<beans::PropertyValue> > NamedPropertyValues;
36 :
37 : class NamedPropertyValuesContainer : public cppu::WeakImplHelper2< container::XNameContainer, lang::XServiceInfo >
38 : {
39 : public:
40 : NamedPropertyValuesContainer() throw();
41 : virtual ~NamedPropertyValuesContainer() throw();
42 :
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;
50 :
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;
55 :
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;
64 :
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;
70 :
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;
75 :
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 >& );
80 :
81 : private:
82 : NamedPropertyValues maProperties;
83 : };
84 :
85 0 : NamedPropertyValuesContainer::NamedPropertyValuesContainer() throw()
86 : {
87 0 : }
88 :
89 0 : NamedPropertyValuesContainer::~NamedPropertyValuesContainer() throw()
90 : {
91 0 : }
92 :
93 : // XNameContainer
94 0 : 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)
97 : {
98 0 : if( maProperties.find( aName ) != maProperties.end() )
99 0 : throw container::ElementExistException();
100 :
101 0 : uno::Sequence<beans::PropertyValue> aProps;
102 0 : if( !(aElement >>= aProps ) )
103 0 : throw lang::IllegalArgumentException();
104 :
105 0 : maProperties.insert( NamedPropertyValues::value_type(aName ,aProps) );
106 0 : }
107 :
108 0 : 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)
111 : {
112 0 : NamedPropertyValues::iterator aIter = maProperties.find( Name );
113 0 : if( aIter == maProperties.end() )
114 0 : throw container::NoSuchElementException();
115 :
116 0 : maProperties.erase( aIter );
117 0 : }
118 :
119 : // XNameReplace
120 0 : 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)
123 : {
124 0 : NamedPropertyValues::iterator aIter = maProperties.find( aName );
125 0 : if( aIter == maProperties.end() )
126 0 : throw container::NoSuchElementException();
127 :
128 0 : uno::Sequence<beans::PropertyValue> aProps;
129 0 : if( !(aElement >>= aProps) )
130 0 : throw lang::IllegalArgumentException();
131 :
132 0 : (*aIter).second = aProps;
133 0 : }
134 :
135 : // XNameAccess
136 0 : ::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)
139 : {
140 0 : NamedPropertyValues::iterator aIter = maProperties.find( aName );
141 0 : if( aIter == maProperties.end() )
142 0 : throw container::NoSuchElementException();
143 :
144 0 : uno::Any aElement;
145 :
146 0 : aElement <<= (*aIter).second;
147 :
148 0 : return aElement;
149 : }
150 :
151 0 : ::com::sun::star::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getElementNames( )
152 : throw(::com::sun::star::uno::RuntimeException, std::exception)
153 : {
154 0 : NamedPropertyValues::iterator aIter = maProperties.begin();
155 0 : const NamedPropertyValues::iterator aEnd = maProperties.end();
156 :
157 0 : uno::Sequence< OUString > aNames( maProperties.size() );
158 0 : OUString* pNames = aNames.getArray();
159 :
160 0 : while( aIter != aEnd )
161 : {
162 0 : *pNames++ = (*aIter++).first;
163 : }
164 :
165 0 : return aNames;
166 : }
167 :
168 0 : sal_Bool SAL_CALL NamedPropertyValuesContainer::hasByName( const OUString& aName )
169 : throw(::com::sun::star::uno::RuntimeException, std::exception)
170 : {
171 0 : NamedPropertyValues::iterator aIter = maProperties.find( aName );
172 0 : return aIter != maProperties.end();
173 : }
174 :
175 : // XElementAccess
176 0 : ::com::sun::star::uno::Type SAL_CALL NamedPropertyValuesContainer::getElementType( )
177 : throw(::com::sun::star::uno::RuntimeException, std::exception)
178 : {
179 0 : return ::getCppuType((uno::Sequence<beans::PropertyValue> *)0);
180 : }
181 :
182 0 : sal_Bool SAL_CALL NamedPropertyValuesContainer::hasElements( )
183 : throw(::com::sun::star::uno::RuntimeException, std::exception)
184 : {
185 0 : return !maProperties.empty();
186 : }
187 :
188 : //XServiceInfo
189 0 : OUString SAL_CALL NamedPropertyValuesContainer::getImplementationName( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
190 : {
191 0 : return getImplementationName_static();
192 : }
193 :
194 0 : OUString SAL_CALL NamedPropertyValuesContainer::getImplementationName_static( )
195 : {
196 0 : return OUString( "NamedPropertyValuesContainer" );
197 : }
198 :
199 0 : sal_Bool SAL_CALL NamedPropertyValuesContainer::supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException, std::exception)
200 : {
201 0 : return cppu::supportsService(this, ServiceName);
202 : }
203 :
204 0 : ::com::sun::star::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
205 : {
206 0 : return getSupportedServiceNames_static();
207 : }
208 :
209 0 : ::com::sun::star::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames_static( )
210 : {
211 0 : const OUString aServiceName( "com.sun.star.document.NamedPropertyValues" );
212 0 : const uno::Sequence< OUString > aSeq( &aServiceName, 1 );
213 0 : return aSeq;
214 : }
215 :
216 0 : uno::Reference< uno::XInterface > SAL_CALL NamedPropertyValuesContainer::Create(
217 : SAL_UNUSED_PARAMETER const uno::Reference< uno::XComponentContext >&)
218 : {
219 0 : return (cppu::OWeakObject*)new NamedPropertyValuesContainer();
220 : }
221 :
222 0 : void createRegistryInfo_NamedPropertyValuesContainer()
223 : {
224 0 : static ::comphelper::module::OAutoRegistration< NamedPropertyValuesContainer > aAutoRegistration;
225 0 : }
226 :
227 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|