Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / unodraw / UnoNamespaceMap.cxx
blobcd49cc0a6134751b30e911eb509d80540cb8fff7
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 <climits>
22 #include <set>
24 #include <svx/UnoNamespaceMap.hxx>
25 #include <com/sun/star/container/XNameAccess.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <comphelper/sequence.hxx>
29 #include <cppuhelper/implbase.hxx>
30 #include <cppuhelper/supportsservice.hxx>
31 #include <svl/itempool.hxx>
32 #include <editeng/xmlcnitm.hxx>
34 using namespace ::cppu;
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::container;
38 using namespace ::com::sun::star::lang;
40 namespace svx
42 namespace {
44 /** implements a component to export namespaces of all SvXMLAttrContainerItem inside
45 one or two pools with a variable count of which ids.
47 class NamespaceMap : public WeakImplHelper< XNameAccess, XServiceInfo >
49 private:
50 sal_uInt16* mpWhichIds;
51 SfxItemPool* mpPool;
53 public:
54 NamespaceMap( sal_uInt16* pWhichIds, SfxItemPool* pPool );
56 // XNameAccess
57 virtual Any SAL_CALL getByName( const OUString& aName ) override;
58 virtual Sequence< OUString > SAL_CALL getElementNames( ) override;
59 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override;
61 // XElementAccess
62 virtual Type SAL_CALL getElementType( ) override;
63 virtual sal_Bool SAL_CALL hasElements( ) override;
65 // XServiceInfo
66 virtual OUString SAL_CALL getImplementationName( ) override;
67 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
68 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override;
73 Reference< XInterface > NamespaceMap_createInstance( sal_uInt16* pWhichIds, SfxItemPool* pPool )
75 return static_cast<XWeak*>(new NamespaceMap( pWhichIds, pPool ));
78 static Sequence< OUString > NamespaceMap_getSupportedServiceNames()
79 noexcept
81 Sequence<OUString> aSupportedServiceNames { "com.sun.star.xml.NamespaceMap" };
82 return aSupportedServiceNames;
85 static OUString NamespaceMap_getImplementationName()
86 noexcept
88 return "com.sun.star.comp.Svx.NamespaceMap";
91 namespace {
93 class NamespaceIteratorImpl
95 private:
96 SfxItemPool* mpPool;
98 sal_uInt16* mpWhichId;
100 std::vector<const SvXMLAttrContainerItem*> mvItems;
101 sal_Int32 mnItem;
103 const SvXMLAttrContainerItem* mpCurrentAttr;
104 sal_uInt16 mnCurrentAttr;
106 public:
108 NamespaceIteratorImpl( sal_uInt16* pWhichIds, SfxItemPool* pPool );
110 bool next( OUString& rPrefix, OUString& rURL );
116 using namespace ::svx;
119 NamespaceIteratorImpl::NamespaceIteratorImpl( sal_uInt16* pWhichIds, SfxItemPool* pPool )
121 mpPool = pPool;
122 mpCurrentAttr = nullptr;
123 mnCurrentAttr = 0;
125 mpWhichId = pWhichIds;
127 mnItem = -1;
128 if (mpWhichId && (0 != *mpWhichId) && mpPool)
130 mvItems.reserve(mpPool->GetItemCount2( *mpWhichId ));
131 for (const SfxPoolItem* pItem : mpPool->GetItemSurrogates( *mpWhichId ))
132 mvItems.push_back(static_cast<const SvXMLAttrContainerItem*>(pItem));
136 bool NamespaceIteratorImpl::next( OUString& rPrefix, OUString& rURL )
138 // we still need to process the current attribute
139 if( mpCurrentAttr && (mnCurrentAttr != USHRT_MAX) )
141 rPrefix = mpCurrentAttr->GetPrefix( mnCurrentAttr );
142 rURL = mpCurrentAttr->GetNamespace( mnCurrentAttr );
144 mnCurrentAttr = mpCurrentAttr->GetNextNamespaceIndex( mnCurrentAttr );
145 return true;
148 // we need the next namespace item
149 mpCurrentAttr = nullptr;
150 mnItem++;
152 // are we finished with the current whichid?
153 if( mnItem == static_cast<sal_Int32>(mvItems.size()) )
155 mpWhichId++;
157 // are we finished with the current pool?
158 if( 0 == *mpWhichId )
159 return false;
161 mnItem = -1;
162 mvItems.clear();
163 if (mpPool)
165 mvItems.reserve(mpPool->GetItemCount2( *mpWhichId ));
166 for (const SfxPoolItem* pItem2 : mpPool->GetItemSurrogates( *mpWhichId ))
167 mvItems.push_back(static_cast<const SvXMLAttrContainerItem*>(pItem2));
169 return next( rPrefix, rURL );
172 auto pItem = mvItems[mnItem];
173 // get that item and see if there namespaces inside
174 if( pItem->GetAttrCount() > 0 )
176 mpCurrentAttr = pItem;
177 mnCurrentAttr = pItem->GetFirstNamespaceIndex();
179 return next( rPrefix, rURL );
183 NamespaceMap::NamespaceMap( sal_uInt16* pWhichIds, SfxItemPool* pPool )
184 : mpWhichIds( pWhichIds ), mpPool( pPool )
188 // XNameAccess
189 Any SAL_CALL NamespaceMap::getByName( const OUString& aName )
191 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
193 OUString aPrefix;
194 OUString aURL;
196 bool bFound;
200 bFound = aIter.next( aPrefix, aURL );
202 while( bFound && (aPrefix != aName ) );
204 if( !bFound )
205 throw NoSuchElementException();
207 return Any( aURL );
210 Sequence< OUString > SAL_CALL NamespaceMap::getElementNames()
212 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
214 OUString aPrefix;
215 OUString aURL;
217 std::set< OUString > aPrefixSet;
219 while( aIter.next( aPrefix, aURL ) )
220 aPrefixSet.insert( aPrefix );
222 return comphelper::containerToSequence(aPrefixSet);
225 sal_Bool SAL_CALL NamespaceMap::hasByName( const OUString& aName )
227 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
229 OUString aPrefix;
230 OUString aURL;
232 bool bFound;
236 bFound = aIter.next( aPrefix, aURL );
238 while( bFound && (aPrefix != aName ) );
240 return bFound;
243 // XElementAccess
244 Type SAL_CALL NamespaceMap::getElementType()
246 return ::cppu::UnoType<OUString>::get();
249 sal_Bool SAL_CALL NamespaceMap::hasElements()
251 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
253 OUString aPrefix;
254 OUString aURL;
256 return aIter.next( aPrefix, aURL );
259 // XServiceInfo
260 OUString SAL_CALL NamespaceMap::getImplementationName( )
262 return NamespaceMap_getImplementationName();
265 sal_Bool SAL_CALL NamespaceMap::supportsService( const OUString& serviceName )
267 return cppu::supportsService( this, serviceName );
270 Sequence< OUString > SAL_CALL NamespaceMap::getSupportedServiceNames( )
272 return NamespaceMap_getSupportedServiceNames();
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */