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 .
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
;
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
>
50 sal_uInt16
* mpWhichIds
;
54 NamespaceMap( sal_uInt16
* pWhichIds
, SfxItemPool
* pPool
);
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
;
62 virtual Type SAL_CALL
getElementType( ) override
;
63 virtual sal_Bool SAL_CALL
hasElements( ) override
;
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()
81 Sequence
<OUString
> aSupportedServiceNames
{ u
"com.sun.star.xml.NamespaceMap"_ustr
};
82 return aSupportedServiceNames
;
85 static OUString
NamespaceMap_getImplementationName()
88 return u
"com.sun.star.comp.Svx.NamespaceMap"_ustr
;
93 class NamespaceIteratorImpl
98 sal_uInt16
* mpWhichId
;
100 std::vector
<const SvXMLAttrContainerItem
*> mvItems
;
103 const SvXMLAttrContainerItem
* mpCurrentAttr
;
104 sal_uInt16 mnCurrentAttr
;
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
)
122 mpCurrentAttr
= nullptr;
125 mpWhichId
= pWhichIds
;
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
);
150 // we need the next namespace item
151 mpCurrentAttr
= nullptr;
154 // are we finished with the current whichid?
155 if( mnItem
== static_cast<sal_Int32
>(mvItems
.size()) )
159 // are we finished with the current pool?
160 if( 0 == *mpWhichId
)
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
)
193 Any SAL_CALL
NamespaceMap::getByName( const OUString
& aName
)
195 NamespaceIteratorImpl
aIter( mpWhichIds
, mpPool
);
204 bFound
= aIter
.next( aPrefix
, aURL
);
206 while( bFound
&& (aPrefix
!= aName
) );
209 throw NoSuchElementException();
214 Sequence
< OUString
> SAL_CALL
NamespaceMap::getElementNames()
216 NamespaceIteratorImpl
aIter( mpWhichIds
, mpPool
);
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
);
240 bFound
= aIter
.next( aPrefix
, aURL
);
242 while( bFound
&& (aPrefix
!= aName
) );
248 Type SAL_CALL
NamespaceMap::getElementType()
250 return ::cppu::UnoType
<OUString
>::get();
253 sal_Bool SAL_CALL
NamespaceMap::hasElements()
255 NamespaceIteratorImpl
aIter( mpWhichIds
, mpPool
);
260 return aIter
.next( aPrefix
, aURL
);
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: */