tdf#161411 - UI: Add Better wording for ASCII-only characters
[LibreOffice.git] / svx / source / unodraw / UnoNamespaceMap.cxx
blob85e92438039e3dc36a2b4c0768849f9c6268cdae
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 getXWeak(new NamespaceMap( pWhichIds, pPool ));
78 static Sequence< OUString > NamespaceMap_getSupportedServiceNames()
79 noexcept
81 Sequence<OUString> aSupportedServiceNames { u"com.sun.star.xml.NamespaceMap"_ustr };
82 return aSupportedServiceNames;
85 static OUString NamespaceMap_getImplementationName()
86 noexcept
88 return u"com.sun.star.comp.Svx.NamespaceMap"_ustr;
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 ItemSurrogates aSurrogates;
131 mpPool->GetItemSurrogates(aSurrogates, *mpWhichId);
132 mvItems.reserve(aSurrogates.size());
133 for (const SfxPoolItem* pItem : aSurrogates)
134 mvItems.push_back(static_cast<const SvXMLAttrContainerItem*>(pItem));
138 bool NamespaceIteratorImpl::next( OUString& rPrefix, OUString& rURL )
140 // we still need to process the current attribute
141 if( mpCurrentAttr && (mnCurrentAttr != USHRT_MAX) )
143 rPrefix = mpCurrentAttr->GetPrefix( mnCurrentAttr );
144 rURL = mpCurrentAttr->GetNamespace( mnCurrentAttr );
146 mnCurrentAttr = mpCurrentAttr->GetNextNamespaceIndex( mnCurrentAttr );
147 return true;
150 // we need the next namespace item
151 mpCurrentAttr = nullptr;
152 mnItem++;
154 // are we finished with the current whichid?
155 if( mnItem == static_cast<sal_Int32>(mvItems.size()) )
157 mpWhichId++;
159 // are we finished with the current pool?
160 if( 0 == *mpWhichId )
161 return false;
163 mnItem = -1;
164 mvItems.clear();
165 if (mpPool)
167 ItemSurrogates aSurrogates;
168 mpPool->GetItemSurrogates(aSurrogates, *mpWhichId);
169 mvItems.reserve(aSurrogates.size());
170 for (const SfxPoolItem* pItem2 : aSurrogates)
171 mvItems.push_back(static_cast<const SvXMLAttrContainerItem*>(pItem2));
173 return next( rPrefix, rURL );
176 auto pItem = mvItems[mnItem];
177 // get that item and see if there namespaces inside
178 if( pItem->GetAttrCount() > 0 )
180 mpCurrentAttr = pItem;
181 mnCurrentAttr = pItem->GetFirstNamespaceIndex();
183 return next( rPrefix, rURL );
187 NamespaceMap::NamespaceMap( sal_uInt16* pWhichIds, SfxItemPool* pPool )
188 : mpWhichIds( pWhichIds ), mpPool( pPool )
192 // XNameAccess
193 Any SAL_CALL NamespaceMap::getByName( const OUString& aName )
195 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
197 OUString aPrefix;
198 OUString aURL;
200 bool bFound;
204 bFound = aIter.next( aPrefix, aURL );
206 while( bFound && (aPrefix != aName ) );
208 if( !bFound )
209 throw NoSuchElementException();
211 return Any( aURL );
214 Sequence< OUString > SAL_CALL NamespaceMap::getElementNames()
216 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
218 OUString aPrefix;
219 OUString aURL;
221 std::set< OUString > aPrefixSet;
223 while( aIter.next( aPrefix, aURL ) )
224 aPrefixSet.insert( aPrefix );
226 return comphelper::containerToSequence(aPrefixSet);
229 sal_Bool SAL_CALL NamespaceMap::hasByName( const OUString& aName )
231 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
233 OUString aPrefix;
234 OUString aURL;
236 bool bFound;
240 bFound = aIter.next( aPrefix, aURL );
242 while( bFound && (aPrefix != aName ) );
244 return bFound;
247 // XElementAccess
248 Type SAL_CALL NamespaceMap::getElementType()
250 return ::cppu::UnoType<OUString>::get();
253 sal_Bool SAL_CALL NamespaceMap::hasElements()
255 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
257 OUString aPrefix;
258 OUString aURL;
260 return aIter.next( aPrefix, aURL );
263 // XServiceInfo
264 OUString SAL_CALL NamespaceMap::getImplementationName( )
266 return NamespaceMap_getImplementationName();
269 sal_Bool SAL_CALL NamespaceMap::supportsService( const OUString& serviceName )
271 return cppu::supportsService( this, serviceName );
274 Sequence< OUString > SAL_CALL NamespaceMap::getSupportedServiceNames( )
276 return NamespaceMap_getSupportedServiceNames();
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */